Skip to contentSkip to conversation

Megasas HBA emulation and SCSI update v.3

PATCH In review Hannes Reinecke 8 participants 38 messages Nov 24, 2010 – Dec 21, 2010 Kbuild

lore
Share & tools
lore
#4 lore raw

PATCH 3/15 scsi: INQUIRY VPD fixes

In reply to Hannes Reinecke

We should announce and support the block device characterics page only on block devices, not on CDROMs. And the VPD page 0x83 has an off-by-one error.

---
 hw/scsi-disk.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)
hw/scsi-disk.c +14 -4
index dc71957..a76e91c 100644
@@ -398,15 +398,20 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
 
         switch (page_code) {
         case 0x00: /* Supported page codes, mandatory */
+        {
+            int pages;
             DPRINTF("Inquiry EVPD[Supported pages] "
                     "buffer size %zd\n", req->cmd.xfer);
-            outbuf[buflen++] = 4;    // number of pages
+            pages = buflen++;
             outbuf[buflen++] = 0x00; // list of supported pages (this page)
             outbuf[buflen++] = 0x80; // unit serial number
             outbuf[buflen++] = 0x83; // device identification
-            outbuf[buflen++] = 0xb0; // block device characteristics
+            if (bdrv_get_type_hint(s->bs) != BDRV_TYPE_CDROM) {
+                outbuf[buflen++] = 0xb0; // block device characteristics
+            }
+            outbuf[pages] = buflen - pages - 1; // number of pages
             break;
-
+        }
         case 0x80: /* Device serial number, optional */
         {
             int l = strlen(s->serial);
@@ -434,7 +439,7 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
             DPRINTF("Inquiry EVPD[Device identification] "
                     "buffer size %zd\n", req->cmd.xfer);
 
-            outbuf[buflen++] = 3 + id_len;
+            outbuf[buflen++] = 4 + id_len;
             outbuf[buflen++] = 0x2; // ASCII
             outbuf[buflen++] = 0;   // not officially assigned
             outbuf[buflen++] = 0;   // reserved
@@ -451,6 +456,11 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
             unsigned int opt_io_size =
                     s->qdev.conf.opt_io_size / s->qdev.blocksize;
 
+            if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
+                DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
+                        page_code);
+                return -1;
+            }
             /* required VPD size with unmap support */
             outbuf[3] = buflen = 0x3c;
 
Signature · 1 line
1.6.0.2
#6 lore raw

PATCH 5/15 scsi-disk: Remove duplicate cdb parsing

In reply to Hannes Reinecke

We parse the CDB twice, which is completely unnecessary.

---
 hw/scsi-disk.c |   74 ++++++++++++++++----------------------------------------
 1 files changed, 21 insertions(+), 53 deletions(-)
hw/scsi-disk.c +21 -53
index da6c3f0..58e5f5b 100644
@@ -1004,9 +1004,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                                  uint8_t *buf, int lun)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
-    uint64_t lba;
     uint32_t len;
-    int cmdlen;
     int is_write;
     uint8_t command;
     uint8_t *outbuf;
@@ -1025,55 +1023,21 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     outbuf = (uint8_t *)r->iov.iov_base;
     is_write = 0;
     DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
-    switch (command >> 5) {
-    case 0:
-        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
-              (((uint64_t) buf[1] & 0x1f) << 16);
-        len = buf[4];
-        cmdlen = 6;
-        break;
-    case 1:
-    case 2:
-        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
-              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
-        len = buf[8] | (buf[7] << 8);
-        cmdlen = 10;
-        break;
-    case 4:
-        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
-              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
-              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
-              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
-        len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
-        cmdlen = 16;
-        break;
-    case 5:
-        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
-              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
-        len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
-        cmdlen = 12;
-        break;
-    default:
+
+    if (scsi_req_parse(&r->req, buf) != 0) {
         BADF("Unsupported command length, command %x\n", command);
         goto fail;
     }
 #ifdef DEBUG_SCSI
     {
         int i;
-        for (i = 1; i < cmdlen; i++) {
+        for (i = 1; i < r->req.cmd.len; i++) {
             printf(" 0x%02x", buf[i]);
         }
         printf("\n");
     }
 #endif
 
-    if (scsi_req_parse(&r->req, buf) != 0) {
-        BADF("Unsupported command length, command %x\n", command);
-        goto fail;
-    }
-    assert(r->req.cmd.len == cmdlen);
-    assert(r->req.cmd.lba == lba);
-
     if (lun || buf[1] >> 5) {
         /* Only LUN 0 supported.  */
         DPRINTF("Unimplemented LUN %d\n", lun ? lun : buf[1] >> 5);
@@ -1111,10 +1075,11 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     case READ_10:
     case READ_12:
     case READ_16:
-        DPRINTF("Read (sector %" PRId64 ", count %d)\n", lba, len);
-        if (lba > s->max_lba)
+        len = r->req.cmd.xfer / d->blocksize;
+        DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
+        if (r->req.cmd.lba > s->max_lba)
             goto illegal_lba;
-        r->sector = lba * s->cluster_size;
+        r->sector = r->req.cmd.lba * s->cluster_size;
         r->sector_count = len * s->cluster_size;
         break;
     case WRITE_6:
@@ -1124,42 +1089,45 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     case WRITE_VERIFY:
     case WRITE_VERIFY_12:
     case WRITE_VERIFY_16:
+        len = r->req.cmd.xfer / d->blocksize;
         DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
-                (command & 0xe) == 0xe ? "And Verify " : "", lba, len);
-        if (lba > s->max_lba)
+                (command & 0xe) == 0xe ? "And Verify " : "",
+                r->req.cmd.lba, len);
+        if (r->req.cmd.lba > s->max_lba)
             goto illegal_lba;
-        r->sector = lba * s->cluster_size;
+        r->sector = r->req.cmd.lba * s->cluster_size;
         r->sector_count = len * s->cluster_size;
         is_write = 1;
         break;
     case MODE_SELECT:
-        DPRINTF("Mode Select(6) (len %d)\n", len);
+        DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
         /* We don't support mode parameter changes.
            Allow the mode parameter header + block descriptors only. */
-        if (len > 12) {
+        if (r->req.cmd.xfer > 12) {
             goto fail;
         }
         break;
     case MODE_SELECT_10:
-        DPRINTF("Mode Select(10) (len %d)\n", len);
+        DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
         /* We don't support mode parameter changes.
            Allow the mode parameter header + block descriptors only. */
-        if (len > 16) {
+        if (r->req.cmd.xfer > 16) {
             goto fail;
         }
         break;
     case SEEK_6:
     case SEEK_10:
-        DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10, lba);
-        if (lba > s->max_lba) {
+        DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
+                r->req.cmd.lba);
+        if (r->req.cmd.lba > s->max_lba) {
             goto illegal_lba;
         }
         break;
     default:
-	DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
+        DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
     fail:
         scsi_command_complete(r, CHECK_CONDITION, ILLEGAL_REQUEST);
-	return 0;
+        return 0;
     illegal_lba:
         scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR);
         return 0;
Signature · 1 line
1.6.0.2
#10 lore raw

PATCH 7/15 lsi53c895a: Rename 'sense' to 'status'

In reply to Hannes Reinecke

The 'sense' field in the HBA status structure is misnamed, as it actually carries the SCSI status. Rename it.

---
 hw/lsi53c895a.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
hw/lsi53c895a.c +9 -9
index f97335e..8246ee8 100644
@@ -189,7 +189,7 @@ typedef struct {
     uint32_t script_ram_base;
 
     int carry; /* ??? Should this be an a visible register somewhere?  */
-    int sense;
+    int status;
     /* Action to take at the end of a MSG IN phase.
        0 = COMMAND, 1 = disconnect, 2 = DATA OUT, 3 = DATA IN.  */
     int msg_action;
@@ -695,8 +695,8 @@ static void lsi_command_complete(SCSIBus *bus, int reason, uint32_t tag,
 
     out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
     if (reason == SCSI_REASON_DONE) {
-        DPRINTF("Command complete sense=%d\n", (int)arg);
-        s->sense = arg;
+        DPRINTF("Command complete status=%d\n", (int)arg);
+        s->status = arg;
         s->command_complete = 2;
         if (s->waiting && s->dbc != 0) {
             /* Raise phase mismatch for short transfers.  */
@@ -783,14 +783,14 @@ static void lsi_do_command(LSIState *s)
 
 static void lsi_do_status(LSIState *s)
 {
-    uint8_t sense;
-    DPRINTF("Get status len=%d sense=%d\n", s->dbc, s->sense);
+    uint8_t status;
+    DPRINTF("Get status len=%d status=%d\n", s->dbc, s->status);
     if (s->dbc != 1)
         BADF("Bad Status move\n");
     s->dbc = 1;
-    sense = s->sense;
-    s->sfbr = sense;
-    cpu_physical_memory_write(s->dnad, &sense, 1);
+    status = s->status;
+    s->sfbr = status;
+    cpu_physical_memory_write(s->dnad, &status, 1);
     lsi_set_phase(s, PHASE_MI);
     s->msg_action = 1;
     lsi_add_msg_byte(s, 0); /* COMMAND COMPLETE */
@@ -2067,7 +2067,7 @@ static const VMStateDescription vmstate_lsi_scsi = {
         VMSTATE_PCI_DEVICE(dev, LSIState),
 
         VMSTATE_INT32(carry, LSIState),
-        VMSTATE_INT32(sense, LSIState),
+        VMSTATE_INT32(status, LSIState),
         VMSTATE_INT32(msg_action, LSIState),
         VMSTATE_INT32(msg_len, LSIState),
         VMSTATE_BUFFER(msg, LSIState),
Signature · 1 line
1.6.0.2
#0 lore raw

cover Megasas HBA emulation and SCSI update v.3

The is the third version of the megasas patchset; changes are:
v2 -> v3:
- Include review by Stefan Hajnoczi
- Return SCSI error status on invalid data direction
- Initialize sense code in scsi-generic.c

v1 -> v2:
- Include suggestions (and acked-by) from Christoph Hellwig
- Updated patch description as requested by Gerd Hoffman
- Use generic iov_size() implementation
- Drop old patches
- Make SCSI HBA configurable

This patchset can be found at

git://repo.or.cz/qemu/megasas.git
branch megasas.v3

The original description was:
Due to popular demand here is now the LSI Megaraid HBA emulation. And an update to the SCSI stack to bring it more in line with modern standards.

For the SCSI update there are three key points:
- Improved sense code handling: the SCSI standard specifies several sense codes to be returned on common errors; we should be following that.
- Reworked SCSI layer: Instead of indirectly addressing a SCSI request by an abstract 'tag' we're now using the request directly, thus saving us a lookup on every invocation.
- Add iovec passing: I've added an interface to pass an iovec to a SCSI request, so that we don't need to use bounce buffers for data transfer. The old interface is left in place.

And, of course, the megasas driver itself.

A quick 'dd' comparison against virtio on a tmpfs backed sparse file shows:

# dd if=/dev/sdb of=/dev/null bs=16M count=1000 iflag=direct
1000+0 records in
1000+0 records out
16777216000 bytes (17 GB) copied, 9.40123 s, 1.8 GB/s
# dd if=/dev/vda of=/dev/null bs=16M count=1000 iflag=direct
1000+0 records in
1000+0 records out
16777216000 bytes (17 GB) copied, 7.44681 s, 2.3 GB/s

So we're having about 80% virtio speed.
Unoptimized :-)

Hannes Reinecke (15):
scsi: Increase the number of possible devices
scsi: Return SAM status codes
scsi: INQUIRY VPD fixes
scsi: Move sense handling into the driver
scsi-disk: Remove duplicate cdb parsing
scsi: Update sense code handling
lsi53c895a: Rename 'sense' to 'status'
scsi-disk: Allocate iovec dynamically
scsi: Use 'SCSIRequest' directly
scsi-disk: add data direction checking
Remove 'bus' argument from SCSI command completion callbacks
scsi: Implement 'get_sense' callback
scsi: Implement alloc_req_iov callback
megasas: LSI Megaraid SAS emulation
Make SCSI HBA configurable

 Makefile.objs                        |    5 +-
 blockdev.h                           |    2 +-
 default-configs/i386-softmmu.mak     |    2 +
 default-configs/mips-softmmu.mak     |    2 +-
 default-configs/mips64-softmmu.mak   |    2 +-
 default-configs/mips64el-softmmu.mak |    2 +-
 default-configs/mipsel-softmmu.mak   |    2 +-
 default-configs/ppc-softmmu.mak      |    2 +
 default-configs/ppc64-softmmu.mak    |    2 +
 default-configs/ppcemb-softmmu.mak   |    2 +
 default-configs/sparc-softmmu.mak    |    2 +-
 default-configs/sparc64-softmmu.mak  |    2 +
 default-configs/x86_64-softmmu.mak   |    2 +
 hw/esp.c                             |   23 +-
 hw/lsi53c895a.c                      |   47 +-
 hw/megasas.c                         | 1828 ++++++++++++++++++++++++++++++++++
 hw/mfi.h                             | 1197 ++++++++++++++++++++++
 hw/pci_ids.h                         |    2 +
 hw/scsi-bus.c                        |  120 ++-
 hw/scsi-defs.h                       |   20 +-
 hw/scsi-disk.c                       |  459 +++++-----
 hw/scsi-generic.c                    |  239 +++--
 hw/scsi.h                            |   65 +-
 hw/usb-msd.c                         |   26 +-
 24 files changed, 3618 insertions(+), 437 deletions(-)
 create mode 100644 hw/megasas.c
 create mode 100644 hw/mfi.h
#1 lore raw

PATCH 1/15 scsi: Increase the number of possible devices

The SCSI parallel interface has a limit of 8 devices, but not the SCSI stack in general. So we should be removing the hard-coded limit and use MAX_SCSI_DEVS instead. And we only need to scan those devices which are allocated by the bus.

---
 blockdev.h    |    2 +-
 hw/scsi-bus.c |    2 +-
 hw/scsi.h     |    3 ++-
 3 files changed, 4 insertions(+), 3 deletions(-)
blockdev.h +1 -1
index 653affc..451dc06 100644
@@ -32,7 +32,7 @@ struct DriveInfo {
 };
 
 #define MAX_IDE_DEVS	2
-#define MAX_SCSI_DEVS	7
+#define MAX_SCSI_DEVS	255
 
 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 int drive_get_max_bus(BlockInterfaceType type);
hw/scsi-bus.c +1 -1
index 5a3fd4b..74a08b7 100644
@@ -108,7 +108,7 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
     int res = 0, unit;
 
     loc_push_none(&loc);
-    for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
+    for (unit = 0; unit < bus->ndev; unit++) {
         dinfo = drive_get(IF_SCSI, bus->busnr, unit);
         if (dinfo == NULL) {
             continue;
hw/scsi.h +2 -1
index cb06d6d..9c798ae 100644
@@ -3,6 +3,7 @@
 
 #include "qdev.h"
 #include "block.h"
+#include "blockdev.h"
 #include "block_int.h"
 
 #define SCSI_CMD_BUF_SIZE     16
@@ -86,7 +87,7 @@ struct SCSIBus {
     int tcq, ndev;
     scsi_completionfn complete;
 
-    SCSIDevice *devs[8];
+    SCSIDevice *devs[MAX_SCSI_DEVS];
 };
 
 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH 2/15 scsi: Return SAM status codes

In reply to Hannes Reinecke

Traditionally, the linux stack is using SCSI status codes which are shifted by one as compared to those defined in SAM. A SCSI emulation should naturally return the SAM defined codes, not the linux ones.
So to avoid any confusion this patch modifies the existing definitions to match those found in SAM and removes any (now obsolete) byte-shift from the returned status codes.

---
 hw/scsi-defs.h    |   20 +++++++++++---------
 hw/scsi-generic.c |   10 +++++-----
 2 files changed, 16 insertions(+), 14 deletions(-)
hw/scsi-defs.h +11 -9
index a4a3518..1473ecb 100644
@@ -111,18 +111,20 @@
 #define BLANK 0xa1
 
 /*
- *  Status codes
+ *  SAM Status codes
  */
 
 #define GOOD                 0x00
-#define CHECK_CONDITION      0x01
-#define CONDITION_GOOD       0x02
-#define BUSY                 0x04
-#define INTERMEDIATE_GOOD    0x08
-#define INTERMEDIATE_C_GOOD  0x0a
-#define RESERVATION_CONFLICT 0x0c
-#define COMMAND_TERMINATED   0x11
-#define QUEUE_FULL           0x14
+#define CHECK_CONDITION      0x02
+#define CONDITION_GOOD       0x04
+#define BUSY                 0x08
+#define INTERMEDIATE_GOOD    0x10
+#define INTERMEDIATE_C_GOOD  0x14
+#define RESERVATION_CONFLICT 0x18
+#define COMMAND_TERMINATED   0x22
+#define TASK_SET_FULL        0x28
+#define ACA_ACTIVE           0x30
+#define TASK_ABORTED         0x40
 
 #define STATUS_MASK          0x3e
 
hw/scsi-generic.c +5 -5
index 7212091..9be1cca 100644
@@ -96,17 +96,17 @@ static void scsi_command_complete(void *opaque, int ret)
         s->senselen = r->io_header.sb_len_wr;
 
     if (ret != 0)
-        r->req.status = BUSY << 1;
+        r->req.status = BUSY;
     else {
         if (s->driver_status & SG_ERR_DRIVER_TIMEOUT) {
-            r->req.status = BUSY << 1;
+            r->req.status = BUSY;
             BADF("Driver Timeout\n");
         } else if (r->io_header.status)
             r->req.status = r->io_header.status;
         else if (s->driver_status & SG_ERR_DRIVER_SENSE)
-            r->req.status = CHECK_CONDITION << 1;
+            r->req.status = CHECK_CONDITION;
         else
-            r->req.status = GOOD << 1;
+            r->req.status = GOOD;
     }
     DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
             r, r->req.tag, r->req.status);
@@ -333,7 +333,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         s->senselen = 7;
         s->driver_status = SG_ERR_DRIVER_SENSE;
         bus = scsi_bus_from_device(d);
-        bus->complete(bus, SCSI_REASON_DONE, tag, CHECK_CONDITION << 1);
+        bus->complete(bus, SCSI_REASON_DONE, tag, CHECK_CONDITION);
         return 0;
     }
 
Signature · 1 line
1.6.0.2
#11 lore raw

PATCH 8/15 scsi-disk: Allocate iovec dynamically

In reply to Hannes Reinecke

Rather than have the iovec part of the structure with a fixed size of '1' we should be allocating it dynamically. This will allow us to pass in SGLs directly.

---
 hw/scsi-disk.c |  102 +++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 60 insertions(+), 42 deletions(-)
hw/scsi-disk.c +60 -42
index a71607e..deec825 100644
@@ -37,6 +37,7 @@ do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 #include "scsi-defs.h"
 #include "sysemu.h"
 #include "blockdev.h"
+#include "iov.h"
 
 #define SCSI_DMA_BUF_SIZE    131072
 #define SCSI_MAX_INQUIRY_LEN 256
@@ -56,7 +57,10 @@ typedef struct SCSIDiskReq {
     /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
     uint64_t sector;
     uint32_t sector_count;
-    struct iovec iov;
+    uint8_t *iov_buf;
+    uint64_t iov_len;
+    struct iovec *iov;
+    int iov_num;
     QEMUIOVector qiov;
     uint32_t status;
 } SCSIDiskReq;
@@ -86,13 +90,19 @@ static SCSIDiskReq *scsi_new_request(SCSIDiskState *s, uint32_t tag,
 
     req = scsi_req_alloc(sizeof(SCSIDiskReq), &s->qdev, tag, lun);
     r = DO_UPCAST(SCSIDiskReq, req, req);
-    r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
+    r->iov_buf = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
+    r->iov = qemu_mallocz(sizeof(struct iovec));
+    r->iov[0].iov_base = r->iov_buf;
+    r->iov_num = 1;
     return r;
 }
 
 static void scsi_remove_request(SCSIDiskReq *r)
 {
-    qemu_vfree(r->iov.iov_base);
+    qemu_free(r->iov);
+    r->iov = NULL;
+    qemu_vfree(r->iov_buf);
+    r->iov_buf = NULL;
     scsi_req_free(&r->req);
 }
 
@@ -117,7 +127,7 @@ static void scsi_req_set_status(SCSIDiskReq *r, int status, SCSISense sense)
 /* Helper function for command completion.  */
 static void scsi_command_complete(SCSIDiskReq *r, int status, SCSISense sense)
 {
-    DPRINTF("Command complete tag=0x%x status=%d sense=%d/%d/%d\n",
+    DPRINTF("Command complete tag=0x%x status=%d sense=%02x/%02x/%02x\n",
             r->req.tag, status, sense.key, sense.asc, sense.ascq);
     scsi_req_set_status(r, status, sense);
     scsi_req_complete(&r->req);
@@ -142,7 +152,7 @@ static void scsi_cancel_io(SCSIDevice *d, uint32_t tag)
 static void scsi_read_complete(void * opaque, int ret)
 {
     SCSIDiskReq *r = (SCSIDiskReq *)opaque;
-    int n;
+    size_t iov_len = 0;
 
     r->req.aiocb = NULL;
 
@@ -151,13 +161,11 @@ static void scsi_read_complete(void * opaque, int ret)
             return;
         }
     }
+    iov_len = iov_size(r->iov, r->iov_num);
 
-    DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
+    DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, iov_len);
 
-    n = r->iov.iov_len / 512;
-    r->sector += n;
-    r->sector_count -= n;
-    r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len);
+    r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, iov_len);
 }
 
 
@@ -167,9 +175,10 @@ static void scsi_read_request(SCSIDiskReq *r)
     uint32_t n;
 
     if (r->sector_count == (uint32_t)-1) {
-        DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
+        DPRINTF("Read buf_len=%zd\n", r->iov[0].iov_len);
         r->sector_count = 0;
-        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len);
+        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag,
+                             r->iov[0].iov_len);
         return;
     }
     DPRINTF("Read sector_count=%d\n", r->sector_count);
@@ -179,15 +188,21 @@ static void scsi_read_request(SCSIDiskReq *r)
     }
 
     n = r->sector_count;
-    if (n > SCSI_DMA_BUF_SIZE / 512)
-        n = SCSI_DMA_BUF_SIZE / 512;
+    if (r->iov_buf) {
+        /* Reset iovec */
+        if (n > SCSI_DMA_BUF_SIZE / 512)
+            n = SCSI_DMA_BUF_SIZE / 512;
+        r->iov[0].iov_len = n * 512;
+    }
 
-    r->iov.iov_len = n * 512;
-    qemu_iovec_init_external(&r->qiov, &r->iov, 1);
+    qemu_iovec_init_external(&r->qiov, r->iov, r->iov_num);
     r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
                               scsi_read_complete, r);
     if (r->req.aiocb == NULL) {
         scsi_read_complete(r, -EIO);
+    } else {
+        r->sector += n;
+        r->sector_count -= n;
     }
 }
 
@@ -264,17 +279,20 @@ static void scsi_write_complete(void * opaque, int ret)
         }
     }
 
-    n = r->iov.iov_len / 512;
+    n = iov_size(r->iov, r->iov_num) / 512;
     r->sector += n;
     r->sector_count -= n;
     if (r->sector_count == 0) {
         scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
     } else {
         len = r->sector_count * 512;
-        if (len > SCSI_DMA_BUF_SIZE) {
-            len = SCSI_DMA_BUF_SIZE;
+        if (r->iov_buf) {
+            /* Reset iovec */
+            if (len > SCSI_DMA_BUF_SIZE) {
+                len = SCSI_DMA_BUF_SIZE;
+            }
+            r->iov[0].iov_len = len;
         }
-        r->iov.iov_len = len;
         DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
         r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, len);
     }
@@ -285,9 +303,9 @@ static void scsi_write_request(SCSIDiskReq *r)
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
     uint32_t n;
 
-    n = r->iov.iov_len / 512;
+    n = iov_size(r->iov, r->iov_num) / 512;
     if (n) {
-        qemu_iovec_init_external(&r->qiov, &r->iov, 1);
+        qemu_iovec_init_external(&r->qiov, r->iov, r->iov_num);
         r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
                                    scsi_write_complete, r);
         if (r->req.aiocb == NULL) {
@@ -352,7 +370,7 @@ static void scsi_dma_restart_bh(void *opaque)
                 scsi_write_request(r);
                 break;
             case SCSI_REQ_STATUS_RETRY_FLUSH:
-                ret = scsi_disk_emulate_command(r, r->iov.iov_base);
+                ret = scsi_disk_emulate_command(r, r->iov[0].iov_base);
                 if (ret == 0) {
                     scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
                 }
@@ -385,7 +403,7 @@ static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
         BADF("Bad buffer tag 0x%x\n", tag);
         return NULL;
     }
-    return (uint8_t *)r->iov.iov_base;
+    return r->iov_buf;
 }
 
 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
@@ -1001,12 +1019,10 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                                  uint8_t *buf, int lun)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
-    uint32_t len;
+    ssize_t len = 0;
     int is_write;
     uint8_t command;
-    uint8_t *outbuf;
     SCSIDiskReq *r;
-    int rc;
 
     command = buf[0];
     r = scsi_find_request(s, tag);
@@ -1017,7 +1033,6 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     /* ??? Tags are not unique for different luns.  We only implement a
        single lun, so this should not matter.  */
     r = scsi_new_request(s, tag, lun);
-    outbuf = (uint8_t *)r->iov.iov_base;
     is_write = 0;
     DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
 
@@ -1065,23 +1080,25 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     case REPORT_LUNS:
     case VERIFY:
     case REZERO_UNIT:
-        rc = scsi_disk_emulate_command(r, outbuf);
-        if (rc < 0) {
+        len = scsi_disk_emulate_command(r, r->iov[0].iov_base);
+        if (len < 0) {
             return 0;
         }
 
-        r->iov.iov_len = rc;
+        r->iov[0].iov_len = len;
         break;
     case READ_6:
     case READ_10:
     case READ_12:
     case READ_16:
-        len = r->req.cmd.xfer / d->blocksize;
-        DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
-        if (r->req.cmd.lba > s->max_lba)
+        r->sector_count = r->req.cmd.xfer / d->blocksize * s->cluster_size;
+        DPRINTF("Read (sector %" PRId64 ", blocks %d)\n", r->req.cmd.lba,
+                r->sector_count);
+        if (r->req.cmd.lba > s->max_lba) {
+            r->sector_count = 0;
             goto illegal_lba;
+        }
         r->sector = r->req.cmd.lba * s->cluster_size;
-        r->sector_count = len * s->cluster_size;
         break;
     case WRITE_6:
     case WRITE_10:
@@ -1090,14 +1107,15 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     case WRITE_VERIFY:
     case WRITE_VERIFY_12:
     case WRITE_VERIFY_16:
-        len = r->req.cmd.xfer / d->blocksize;
-        DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
+        r->sector_count = r->req.cmd.xfer / d->blocksize * s->cluster_size;
+        DPRINTF("Write %s(sector %" PRId64 ", blocks %d)\n",
                 (command & 0xe) == 0xe ? "And Verify " : "",
-                r->req.cmd.lba, len);
-        if (r->req.cmd.lba > s->max_lba)
+                r->req.cmd.lba, r->sector_count);
+        if (r->req.cmd.lba > s->max_lba) {
+            r->sector_count = 0;
             goto illegal_lba;
+        }
         r->sector = r->req.cmd.lba * s->cluster_size;
-        r->sector_count = len * s->cluster_size;
         is_write = 1;
         break;
     case MODE_SELECT:
@@ -1135,10 +1153,10 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LBA_OUT_OF_RANGE));
         return 0;
     }
-    if (r->sector_count == 0 && r->iov.iov_len == 0) {
+    if (r->sector_count == 0 && len == 0) {
         scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
     }
-    len = r->sector_count * 512 + r->iov.iov_len;
+    len += r->sector_count * 512;
     if (is_write) {
         return -len;
     } else {
Signature · 1 line
1.6.0.2
#15 lore raw

PATCH 12/15 scsi: Implement 'get_sense' callback

In reply to Hannes Reinecke

The get_sense callback copies existing sense information into the provided buffer. This is required if sense information should be transferred together with the command response.

---
 hw/scsi-disk.c    |    9 +++++++++
 hw/scsi-generic.c |   18 ++++++++++++++++++
 hw/scsi.h         |    1 +
 3 files changed, 28 insertions(+), 0 deletions(-)
hw/scsi-disk.c +9 -0
index a4f387d..68b8667 100644
@@ -373,6 +373,14 @@ static uint8_t *scsi_get_buf(SCSIRequest *req)
     return r->iov_buf;
 }
 
+/* Copy sense information into the provided buffer */
+static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
+{
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
+
+    return scsi_build_sense(s->sense, outbuf, len, len > 14);
+}
+
 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
@@ -1218,6 +1226,7 @@ static SCSIDeviceInfo scsi_disk_info = {
     .write_data   = scsi_write_data,
     .cancel_io    = scsi_cancel_io,
     .get_buf      = scsi_get_buf,
+    .get_sense    = scsi_get_sense,
     .qdev.props   = (Property[]) {
         DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),
         DEFINE_PROP_STRING("ver",  SCSIDiskState, version),
hw/scsi-generic.c +18 -0
index d4edc48..949b4cc 100644
@@ -83,6 +83,23 @@ static void scsi_clear_sense(SCSIGenericState *s)
     s->driver_status = 0;
 }
 
+static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
+{
+    SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, req->dev);
+    int size = SCSI_SENSE_BUF_SIZE;
+
+    if (!(s->driver_status & SG_ERR_DRIVER_SENSE)) {
+        size = scsi_build_sense(SENSE_CODE(NO_SENSE), s->sensebuf,
+                                SCSI_SENSE_BUF_SIZE, 0);
+    }
+    if (size > len) {
+        size = len;
+    }
+    memcpy(outbuf, s->sensebuf, size);
+
+    return size;
+}
+
 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun)
 {
     SCSIRequest *req;
@@ -550,6 +567,7 @@ static SCSIDeviceInfo scsi_generic_info = {
     .write_data   = scsi_write_data,
     .cancel_io    = scsi_cancel_io,
     .get_buf      = scsi_get_buf,
+    .get_sense    = scsi_get_sense,
     .qdev.props   = (Property[]) {
         DEFINE_BLOCK_PROPERTIES(SCSIGenericState, qdev.conf),
         DEFINE_PROP_END_OF_LIST(),
hw/scsi.h +1 -0
index c2d3300..cc96f85 100644
@@ -78,6 +78,7 @@ struct SCSIDeviceInfo {
     int (*write_data)(SCSIRequest *req);
     void (*cancel_io)(SCSIRequest *req);
     uint8_t *(*get_buf)(SCSIRequest *req);
+    int (*get_sense)(SCSIRequest *req, uint8_t *buf, int len);
 };
 
 typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv,
Signature · 1 line
1.6.0.2
#16 lore raw

PATCH 13/15 scsi: Implement alloc_req_iov callback

In reply to Hannes Reinecke

Add callback to create a request with a predefined iovec. This is required for drivers which can use the iovec of a command directly.

---
 hw/scsi-disk.c    |   25 +++++++++++++++++++++----
 hw/scsi-generic.c |   44 +++++++++++++++++++++++++++++++++-----------
 hw/scsi.h         |    2 ++
 3 files changed, 56 insertions(+), 15 deletions(-)
hw/scsi-disk.c +21 -4
index 68b8667..67f93a5 100644
@@ -96,14 +96,30 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
     return req;
 }
 
+static SCSIRequest *scsi_new_request_iovec(SCSIDevice *d, uint32_t tag,
+        uint32_t lun, struct iovec *iov, int iov_num)
+{
+    SCSIRequest *req;
+    SCSIDiskReq *r;
+
+    req = scsi_req_alloc(sizeof(SCSIDiskReq), d, tag, lun);
+    r = DO_UPCAST(SCSIDiskReq, req, req);
+    r->iov = iov;
+    r->iov_num = iov_num;
+    r->iov_buf = NULL;
+    return req;
+}
+
 static void scsi_remove_request(SCSIRequest *req)
 {
     SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
 
-    qemu_free(r->iov);
-    r->iov = NULL;
-    qemu_vfree(r->iov_buf);
-    r->iov_buf = NULL;
+    if (r->iov_buf) {
+        qemu_free(r->iov);
+        r->iov = NULL;
+        qemu_vfree(r->iov_buf);
+        r->iov_buf = NULL;
+    }
     scsi_req_free(&r->req);
 }
 
@@ -1220,6 +1236,7 @@ static SCSIDeviceInfo scsi_disk_info = {
     .init         = scsi_disk_initfn,
     .destroy      = scsi_destroy,
     .alloc_req    = scsi_new_request,
+    .alloc_req_iov = scsi_new_request_iovec,
     .free_req     = scsi_remove_request,
     .send_command = scsi_send_command,
     .read_data    = scsi_read_data,
hw/scsi-generic.c +33 -11

82 lines not on this page · raw message

hw/scsi.h +2 -0
index cc96f85..063154d 100644
@@ -72,6 +72,8 @@ struct SCSIDeviceInfo {
     scsi_qdev_initfn init;
     void (*destroy)(SCSIDevice *s);
     SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
+    SCSIRequest *(*alloc_req_iov)(SCSIDevice *s, uint32_t tag, uint32_t lun,
+                                  struct iovec *iov, int iov_num);
     void (*free_req)(SCSIRequest *req);
     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
     void (*read_data)(SCSIRequest *req);
Signature · 1 line
1.6.0.2
#5 lore raw

PATCH 4/15 scsi: Move sense handling into the driver

In reply to Hannes Reinecke

The current sense handling in scsi-bus is only used by the scsi-disk driver; the scsi-generic driver is using its own. So we should move the current sense handling into the scsi-disk driver.

---
 hw/scsi-bus.c  |   10 ----------
 hw/scsi-disk.c |   33 +++++++++++++++++++++++++--------
 hw/scsi.h      |    8 --------
 3 files changed, 25 insertions(+), 26 deletions(-)
hw/scsi-bus.c +0 -10
index 74a08b7..93f0e9a 100644
@@ -123,16 +123,6 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
     return res;
 }
 
-void scsi_dev_clear_sense(SCSIDevice *dev)
-{
-    memset(&dev->sense, 0, sizeof(dev->sense));
-}
-
-void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
-{
-    dev->sense.key = key;
-}
-
 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
 {
     SCSIRequest *req;
hw/scsi-disk.c +25 -8
index a76e91c..da6c3f0 100644
@@ -49,6 +49,10 @@ do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 
 typedef struct SCSIDiskState SCSIDiskState;
 
+typedef struct SCSISense {
+    uint8_t key;
+} SCSISense;
+
 typedef struct SCSIDiskReq {
     SCSIRequest req;
     /* ??? We should probably keep track of whether the data transfer is
@@ -72,6 +76,7 @@ struct SCSIDiskState
     QEMUBH *bh;
     char *version;
     char *serial;
+    SCSISense sense;
 };
 
 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
@@ -100,10 +105,22 @@ static SCSIDiskReq *scsi_find_request(SCSIDiskState *s, uint32_t tag)
     return DO_UPCAST(SCSIDiskReq, req, scsi_req_find(&s->qdev, tag));
 }
 
-static void scsi_req_set_status(SCSIRequest *req, int status, int sense_code)
+static void scsi_disk_clear_sense(SCSIDiskState *s)
 {
-    req->status = status;
-    scsi_dev_set_sense(req->dev, sense_code);
+    memset(&s->sense, 0, sizeof(s->sense));
+}
+
+static void scsi_disk_set_sense(SCSIDiskState *s, uint8_t key)
+{
+    s->sense.key = key;
+}
+
+static void scsi_req_set_status(SCSIDiskReq *r, int status, int sense_code)
+{
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
+
+    r->req.status = status;
+    scsi_disk_set_sense(s, sense_code);
 }
 
 /* Helper function for command completion.  */
@@ -111,7 +128,7 @@ static void scsi_command_complete(SCSIDiskReq *r, int status, int sense)
 {
     DPRINTF("Command complete tag=0x%x status=%d sense=%d\n",
             r->req.tag, status, sense);
-    scsi_req_set_status(&r->req, status, sense);
+    scsi_req_set_status(r, status, sense);
     scsi_req_complete(&r->req);
     scsi_remove_request(r);
 }
@@ -822,7 +839,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
             goto illegal_request;
         memset(outbuf, 0, 4);
         buflen = 4;
-        if (req->dev->sense.key == NOT_READY && req->cmd.xfer >= 18) {
+        if (s->sense.key == NOT_READY && req->cmd.xfer >= 18) {
             memset(outbuf, 0, 18);
             buflen = 18;
             outbuf[7] = 10;
@@ -832,8 +849,8 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         }
         outbuf[0] = 0xf0;
         outbuf[1] = 0;
-        outbuf[2] = req->dev->sense.key;
-        scsi_dev_clear_sense(req->dev);
+        outbuf[2] = s->sense.key;
+        scsi_disk_clear_sense(s);
         break;
     case INQUIRY:
         buflen = scsi_disk_emulate_inquiry(req, outbuf);
@@ -966,7 +983,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
     default:
         goto illegal_request;
     }
-    scsi_req_set_status(req, GOOD, NO_SENSE);
+    scsi_req_set_status(r, GOOD, NO_SENSE);
     return buflen;
 
 not_ready:
hw/scsi.h +0 -8
index 9c798ae..bf02adf 100644
@@ -26,10 +26,6 @@ enum SCSIXferMode {
     SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
 };
 
-typedef struct SCSISense {
-    uint8_t key;
-} SCSISense;
-
 typedef struct SCSIRequest {
     SCSIBus           *bus;
     SCSIDevice        *dev;
@@ -57,7 +53,6 @@ struct SCSIDevice
     QTAILQ_HEAD(, SCSIRequest) requests;
     int blocksize;
     int type;
-    struct SCSISense sense;
 };
 
 /* cdrom.c */
@@ -102,9 +97,6 @@ static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, int unit);
 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
 
-void scsi_dev_clear_sense(SCSIDevice *dev);
-void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key);
-
 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag);
 void scsi_req_free(SCSIRequest *req);
Signature · 1 line
1.6.0.2
#13 lore raw

PATCH 10/15 scsi-disk: add data direction checking

In reply to Hannes Reinecke

scsi_req_parse() already provides for a data direction setting, so we should be using it to check for correct direction. And we should return the sense code 'INVALID FIELD IN CDB' in these cases.

---
 hw/scsi-disk.c |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)
hw/scsi-disk.c +22 -8
index 9a5cd8e..0ccb627 100644
@@ -52,8 +52,6 @@ typedef struct SCSIDiskState SCSIDiskState;
 
 typedef struct SCSIDiskReq {
     SCSIRequest req;
-    /* ??? We should probably keep track of whether the data transfer is
-       a read or a write.  Currently we rely on the host getting it right.  */
     /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
     uint64_t sector;
     uint32_t sector_count;
@@ -172,6 +170,12 @@ static void scsi_read_data(SCSIRequest *req)
     /* No data transfer may already be in progress */
     assert(r->req.aiocb == NULL);
 
+    if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
+        DPRINTF("Data transfer direction invalid\n");
+        scsi_read_complete(r, -EINVAL);
+        return;
+    }
+
     if (r->sector_count == (uint32_t)-1) {
         DPRINTF("Read buf_len=%zd\n", r->iov[0].iov_len);
         r->sector_count = 0;
@@ -227,12 +231,19 @@ static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
         if (type == SCSI_REQ_STATUS_RETRY_READ) {
             r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, 0);
         }
-        if (error == EBADR) {
+        switch (error) {
+            case EBADR:
                 scsi_command_complete(r, CHECK_CONDITION,
                                       SENSE_CODE(TARGET_FAILURE));
-        } else {
+                break;
+            case EINVAL:
+                scsi_command_complete(r, CHECK_CONDITION,
+                                      SENSE_CODE(INVALID_FIELD));
+                break;
+            default:
                 scsi_command_complete(r, CHECK_CONDITION,
                                       SENSE_CODE(IO_ERROR));
+                break;
         }
         bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
     }
@@ -284,6 +295,12 @@ static int scsi_write_data(SCSIRequest *req)
     /* No data transfer may already be in progress */
     assert(r->req.aiocb == NULL);
 
+    if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
+        DPRINTF("Data transfer direction invalid\n");
+        scsi_write_complete(r, -EINVAL);
+        return 0;
+    }
+
     n = iov_size(r->iov, r->iov_num) / 512;
     if (n) {
         qemu_iovec_init_external(&r->qiov, r->iov, r->iov_num);
@@ -970,11 +987,9 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     ssize_t len = 0;
-    int is_write;
     uint8_t command;
 
     command = buf[0];
-    is_write = 0;
     DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
 
     if (scsi_req_parse(&r->req, buf) != 0) {
@@ -1057,7 +1072,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * s->cluster_size;
-        is_write = 1;
         break;
     case MODE_SELECT:
         DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
@@ -1098,7 +1112,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
     }
     len += r->sector_count * 512;
-    if (is_write) {
+    if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
         return -len;
     } else {
         if (!r->sector_count)
Signature · 1 line
1.6.0.2
#28 lore raw

PATCH 15/15 Make SCSI HBA configurable

In reply to Hannes Reinecke

This patch introduces configuration variables CONFIG_SCSI_LSI
CONFIG_SCSI_MEGASAS
and renames the existing CONFIG_ESP to CONFIG_SCSI_ESP. With this the available SCSI HBAs can be configured for each target configuration instead of compiling it in for everyone.

---
 Makefile.objs                        |    5 +++--
 default-configs/i386-softmmu.mak     |    2 ++
 default-configs/mips-softmmu.mak     |    2 +-
 default-configs/mips64-softmmu.mak   |    2 +-
 default-configs/mips64el-softmmu.mak |    2 +-
 default-configs/mipsel-softmmu.mak   |    2 +-
 default-configs/ppc-softmmu.mak      |    2 ++
 default-configs/ppc64-softmmu.mak    |    2 ++
 default-configs/ppcemb-softmmu.mak   |    2 ++
 default-configs/sparc-softmmu.mak    |    2 +-
 default-configs/sparc64-softmmu.mak  |    2 ++
 default-configs/x86_64-softmmu.mak   |    2 ++
 12 files changed, 20 insertions(+), 7 deletions(-)
Makefile.objs +3 -2
index 89c8661..3b040cd 100644
@@ -231,8 +231,9 @@ hw-obj-$(CONFIG_IDE_MACIO) += ide/macio.o
 hw-obj-$(CONFIG_IDE_VIA) += ide/via.o
 
 # SCSI layer
-hw-obj-y += lsi53c895a.o megasas.o
-hw-obj-$(CONFIG_ESP) += esp.o
+hw-obj-$(CONFIG_SCSI_LSI) += lsi53c895a.o
+hw-obj-$(CONFIG_SCSI_MEGASAS) += megasas.o
+hw-obj-$(CONFIG_SCSI_ESP) += esp.o
 
 hw-obj-y += dma-helpers.o sysbus.o isa-bus.o
 hw-obj-y += qdev-addr.o
default-configs/i386-softmmu.mak +2 -0
index ed00471..130c157 100644
@@ -19,6 +19,8 @@ CONFIG_IDE_QDEV=y
 CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_PIIX=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_NE2000_ISA=y
 CONFIG_PIIX_PCI=y
 CONFIG_SOUND=y
default-configs/mips-softmmu.mak +1 -1
index 3d0af83..7e840a4 100644
@@ -1,7 +1,7 @@
 # Default configuration for mips-softmmu
 
 CONFIG_ISA_MMIO=y
-CONFIG_ESP=y
+CONFIG_SCSI_ESP=y
 CONFIG_VGA_PCI=y
 CONFIG_VGA_ISA=y
 CONFIG_VGA_ISA_MM=y
default-configs/mips64-softmmu.mak +1 -1
index 0030de4..3816be3 100644
@@ -1,7 +1,7 @@
 # Default configuration for mips64-softmmu
 
 CONFIG_ISA_MMIO=y
-CONFIG_ESP=y
+CONFIG_SCSI_ESP=y
 CONFIG_VGA_PCI=y
 CONFIG_VGA_ISA=y
 CONFIG_VGA_ISA_MM=y
default-configs/mips64el-softmmu.mak +1 -1
index fa2a3ff..21c59e0 100644
@@ -1,7 +1,7 @@
 # Default configuration for mips64el-softmmu
 
 CONFIG_ISA_MMIO=y
-CONFIG_ESP=y
+CONFIG_SCSI_ESP=y
 CONFIG_VGA_PCI=y
 CONFIG_VGA_ISA=y
 CONFIG_VGA_ISA_MM=y
default-configs/mipsel-softmmu.mak +1 -1
index 238b73a..3cf853a 100644
@@ -1,7 +1,7 @@
 # Default configuration for mipsel-softmmu
 
 CONFIG_ISA_MMIO=y
-CONFIG_ESP=y
+CONFIG_SCSI_ESP=y
 CONFIG_VGA_PCI=y
 CONFIG_VGA_ISA=y
 CONFIG_VGA_ISA_MM=y
default-configs/ppc-softmmu.mak +2 -0
index 940f4bf..0cc5914 100644
@@ -29,6 +29,8 @@ CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_CMD646=y
 CONFIG_IDE_MACIO=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_NE2000_ISA=y
 CONFIG_SOUND=y
 CONFIG_VIRTIO_PCI=y
default-configs/ppc64-softmmu.mak +2 -0
index e1bc6b8..ce83254 100644
@@ -29,6 +29,8 @@ CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_CMD646=y
 CONFIG_IDE_MACIO=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_NE2000_ISA=y
 CONFIG_SOUND=y
 CONFIG_VIRTIO_PCI=y
default-configs/ppcemb-softmmu.mak +2 -0
index 8f1cc09..44111c3 100644
@@ -29,6 +29,8 @@ CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_CMD646=y
 CONFIG_IDE_MACIO=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_NE2000_ISA=y
 CONFIG_SOUND=y
 CONFIG_VIRTIO_PCI=y
default-configs/sparc-softmmu.mak +1 -1
index becf880..872929d 100644
@@ -1,7 +1,7 @@
 # Default configuration for sparc-softmmu
 
 CONFIG_ECC=y
-CONFIG_ESP=y
+CONFIG_SCSI_ESP=y
 CONFIG_ESCC=y
 CONFIG_M48T59=y
 CONFIG_PTIMER=y
default-configs/sparc64-softmmu.mak +2 -0
index 1cc3f13..b4a3c27 100644
@@ -13,4 +13,6 @@ CONFIG_IDE_QDEV=y
 CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_CMD646=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_VIRTIO_PCI=y
default-configs/x86_64-softmmu.mak +2 -0
index 5183203..f28b66d 100644
@@ -19,6 +19,8 @@ CONFIG_IDE_QDEV=y
 CONFIG_IDE_PCI=y
 CONFIG_IDE_ISA=y
 CONFIG_IDE_PIIX=y
+CONFIG_SCSI_LSI=y
+CONFIG_SCSI_MEGASAS=y
 CONFIG_NE2000_ISA=y
 CONFIG_PIIX_PCI=y
 CONFIG_SOUND=y
Signature · 1 line
1.6.0.2
#7 lore raw

PATCH 6/15 scsi: Update sense code handling

In reply to Hannes Reinecke

The SCSI spec has a quite detailed list of sense codes available. It even mandates the use of specific ones for some failure cases. The current implementation just has one type of 'generic' error which is actually a violation of the spec in certain cases. This patch introduces various predefined sense codes to have the sense code reporting more in line with the spec.

---
 hw/scsi-bus.c     |   92 ++++++++++++++++++++++++++++++++++++++++++++
 hw/scsi-disk.c    |  109 +++++++++++++++++++++++++++--------------------------
 hw/scsi-generic.c |   76 ++++++++++++++++++++++++++-----------
 hw/scsi.h         |   38 ++++++++++++++++++
 4 files changed, 239 insertions(+), 76 deletions(-)
hw/scsi-bus.c +92 -0
index 93f0e9a..afdf0ad 100644
@@ -388,6 +388,98 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
     return 0;
 }
 
+/*
+ * Predefined sense codes
+ */
+
+/* No sense data available */
+const struct SCSISense sense_code_NO_SENSE = {
+    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
+};
+
+/* LUN not ready, Manual intervention required */
+const struct SCSISense sense_code_LUN_NOT_READY = {
+    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
+};
+
+/* LUN not ready, Medium not present */
+const struct SCSISense sense_code_NO_MEDIUM = {
+    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
+};
+
+/* Hardware error, internal target failure */
+const struct SCSISense sense_code_TARGET_FAILURE = {
+    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
+};
+
+/* Illegal request, invalid command operation code */
+const struct SCSISense sense_code_INVALID_OPCODE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
+};
+
+/* Illegal request, LBA out of range */
+const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
+};
+
+/* Illegal request, Invalid field in CDB */
+const struct SCSISense sense_code_INVALID_FIELD = {
+    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
+};
+
+/* Illegal request, LUN not supported */
+const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
+    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
+};
+
+/* Command aborted, I/O process terminated */
+const struct SCSISense sense_code_IO_ERROR = {
+    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
+};
+
+/* Command aborted, I_T Nexus loss occurred */
+const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
+    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
+};
+
+/* Command aborted, Logical Unit failure */
+const struct SCSISense sense_code_LUN_FAILURE = {
+    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
+};
+
+/*
+ * scsi_build_sense
+ *
+ * Build a sense buffer
+ */
+int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
+{
+    if (len < 8)
+        return 0;
+    if (fixed && len < 14)
+        return 0;
+
+    memset(buf, 0, len);
+    if (fixed) {
+        /* Return fixed format sense buffer */
+        buf[0] = 0xf0;
+        buf[2] = sense.key;
+        buf[7] = 7;
+        buf[12] = sense.asc;
+        buf[13] = sense.ascq;
+        len = 14;
+    } else {
+        /* Return descriptor format sense buffer */
+        buf[0] = 0x72;
+        buf[1] = sense.key;
+        buf[2] = sense.asc;
+        buf[3] = sense.ascq;
+        len = 8;
+    }
+
+    return len;
+}
+
 static const char *scsi_command_name(uint8_t cmd)
 {
     static const char *names[] = {
hw/scsi-disk.c +56 -53

241 lines not on this page · raw message

hw/scsi-generic.c +53 -23

143 lines not on this page · raw message

hw/scsi.h +38 -0
index bf02adf..1196122 100644
@@ -26,6 +26,12 @@ enum SCSIXferMode {
     SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
 };
 
+typedef struct SCSISense {
+    uint8_t key;
+    uint8_t asc;
+    uint8_t ascq;
+} SCSISense;
+
 typedef struct SCSIRequest {
     SCSIBus           *bus;
     SCSIDevice        *dev;
@@ -97,6 +103,38 @@ static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, int unit);
 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
 
+/*
+ * Predefined sense codes
+ */
+
+/* No sense data available */
+extern const struct SCSISense sense_code_NO_SENSE;
+/* LUN not ready, Manual intervention required */
+extern const struct SCSISense sense_code_LUN_NOT_READY;
+/* LUN not ready, Medium not present */
+extern const struct SCSISense sense_code_NO_MEDIUM;
+/* Hardware error, internal target failure */
+extern const struct SCSISense sense_code_TARGET_FAILURE;
+/* Illegal request, invalid command operation code */
+extern const struct SCSISense sense_code_INVALID_OPCODE;
+/* Illegal request, LBA out of range */
+extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
+/* Illegal request, Invalid field in CDB */
+extern const struct SCSISense sense_code_INVALID_FIELD;
+/* Illegal request, LUN not supported */
+extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
+/* Command aborted, I/O process terminated */
+extern const struct SCSISense sense_code_IO_ERROR;
+/* Command aborted, I_T Nexus loss occurred */
+extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
+/* Command aborted, Logical Unit failure */
+extern const struct SCSISense sense_code_LUN_FAILURE;
+
+#define SENSE_CODE(x) sense_code_ ## x
+
+int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
+int scsi_sense_valid(SCSISense sense);
+
 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag);
 void scsi_req_free(SCSIRequest *req);
Signature · 1 line
1.6.0.2
#12 lore raw

PATCH 9/15 scsi: Use 'SCSIRequest' directly

In reply to Hannes Reinecke

Currently the SCSIRequest structure is abstracted away and cannot accessed directly from the driver. This requires the handler to do a lookup on an abstract 'tag' which identifies the SCSIRequest structure.
With this patch the SCSIRequest structure is exposed to the driver. This allows use to use it directly as an argument to the SCSIDeviceInfo callback functions and remove the lookup.
Two new callback functions 'alloc_req' and 'free_req' are introduced, which serve to allocate a new request and to free up resources after use.

---
 hw/esp.c          |   20 ++++---
 hw/lsi53c895a.c   |   26 +++++----
 hw/scsi-bus.c     |   15 +-----
 hw/scsi-disk.c    |  147 ++++++++++++++++------------------------------------
 hw/scsi-generic.c |  119 ++++++++++++++-----------------------------
 hw/scsi.h         |   21 ++++----
 hw/usb-msd.c      |   23 +++++----
 7 files changed, 136 insertions(+), 235 deletions(-)
hw/esp.c +12 -8
index 910fd31..2784bec 100644
@@ -65,6 +65,7 @@ struct ESPState {
     uint32_t dma;
     SCSIBus bus;
     SCSIDevice *current_dev;
+    SCSIRequest *current_req;
     uint8_t cmdbuf[TI_BUFSZ];
     uint32_t cmdlen;
     uint32_t do_cmd;
@@ -209,7 +210,7 @@ static uint32_t get_cmd(ESPState *s, uint8_t *buf)
 
     if (s->current_dev) {
         /* Started a new command before the old one finished.  Cancel it.  */
-        s->current_dev->info->cancel_io(s->current_dev, 0);
+        s->current_dev->info->cancel_io(s->current_req);
         s->async_len = 0;
     }
 
@@ -232,7 +233,8 @@ static void do_busid_cmd(ESPState *s, uint8_t *buf, uint8_t busid)
 
     DPRINTF("do_busid_cmd: busid 0x%x\n", busid);
     lun = busid & 7;
-    datalen = s->current_dev->info->send_command(s->current_dev, 0, buf, lun);
+    s->current_req = s->current_dev->info->alloc_req(s->current_dev, 0, lun);
+    datalen = s->current_dev->info->send_command(s->current_req, buf);
     s->ti_size = datalen;
     if (datalen != 0) {
         s->rregs[ESP_RSTAT] = STAT_TC;
@@ -240,10 +242,10 @@ static void do_busid_cmd(ESPState *s, uint8_t *buf, uint8_t busid)
         s->dma_counter = 0;
         if (datalen > 0) {
             s->rregs[ESP_RSTAT] |= STAT_DI;
-            s->current_dev->info->read_data(s->current_dev, 0);
+            s->current_dev->info->read_data(s->current_req);
         } else {
             s->rregs[ESP_RSTAT] |= STAT_DO;
-            s->current_dev->info->write_data(s->current_dev, 0);
+            s->current_dev->info->write_data(s->current_req);
         }
     }
     s->rregs[ESP_RINTR] = INTR_BS | INTR_FC;
@@ -372,9 +374,9 @@ static void esp_do_dma(ESPState *s)
     if (s->async_len == 0) {
         if (to_device) {
             // ti_size is negative
-            s->current_dev->info->write_data(s->current_dev, 0);
+            s->current_dev->info->write_data(s->current_req);
         } else {
-            s->current_dev->info->read_data(s->current_dev, 0);
+            s->current_dev->info->read_data(s->current_req);
             /* If there is still data to be read from the device then
                complete the DMA operation immediately.  Otherwise defer
                until the scsi layer has completed.  */
@@ -388,7 +390,7 @@ static void esp_do_dma(ESPState *s)
     }
 }
 
-static void esp_command_complete(SCSIBus *bus, int reason, uint32_t tag,
+static void esp_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
                                  uint32_t arg)
 {
     ESPState *s = DO_UPCAST(ESPState, busdev.qdev, bus->qbus.parent);
@@ -405,11 +407,13 @@ static void esp_command_complete(SCSIBus *bus, int reason, uint32_t tag,
         s->sense = arg;
         s->rregs[ESP_RSTAT] = STAT_ST;
         esp_dma_done(s);
+        req->dev->info->free_req(req);
+        s->current_req = NULL;
         s->current_dev = NULL;
     } else {
         DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
         s->async_len = arg;
-        s->async_buf = s->current_dev->info->get_buf(s->current_dev, 0);
+        s->async_buf = s->current_dev->info->get_buf(req);
         if (s->dma_left) {
             esp_do_dma(s);
         } else if (s->dma_counter != 0 && s->ti_size <= 0) {
hw/lsi53c895a.c +15 -11
index 8246ee8..82a5d39 100644
@@ -174,6 +174,7 @@ do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__);} while (0)
 #define LSI_TAG_VALID     (1 << 16)
 
 typedef struct lsi_request {
+    SCSIRequest *req;
     uint32_t tag;
     uint32_t dma_len;
     uint8_t *dma_buf;
@@ -569,7 +570,7 @@ static void lsi_do_dma(LSIState *s, int out)
     s->dbc -= count;
 
     if (s->current->dma_buf == NULL) {
-        s->current->dma_buf = dev->info->get_buf(dev, s->current->tag);
+        s->current->dma_buf = dev->info->get_buf(s->current->req);
     }
 
     /* ??? Set SFBR to first data byte.  */
@@ -583,10 +584,10 @@ static void lsi_do_dma(LSIState *s, int out)
         s->current->dma_buf = NULL;
         if (out) {
             /* Write the data.  */
-            dev->info->write_data(dev, s->current->tag);
+            dev->info->write_data(s->current->req);
         } else {
             /* Request any remaining data.  */
-            dev->info->read_data(dev, s->current->tag);
+            dev->info->read_data(s->current->req);
         }
     } else {
         s->current->dma_buf += count;
@@ -687,7 +688,7 @@ static int lsi_queue_tag(LSIState *s, uint32_t tag, uint32_t arg)
 }
 
 /* Callback to indicate that the SCSI layer has completed a transfer.  */
-static void lsi_command_complete(SCSIBus *bus, int reason, uint32_t tag,
+static void lsi_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
                                  uint32_t arg)
 {
     LSIState *s = DO_UPCAST(LSIState, dev.qdev, bus->qbus.parent);
@@ -704,7 +705,8 @@ static void lsi_command_complete(SCSIBus *bus, int reason, uint32_t tag,
         } else {
             lsi_set_phase(s, PHASE_ST);
         }
-
+        req->dev->info->free_req(req);
+        s->current->req = NULL;
         qemu_free(s->current);
         s->current = NULL;
 
@@ -712,14 +714,14 @@ static void lsi_command_complete(SCSIBus *bus, int reason, uint32_t tag,
         return;
     }
 
-    if (s->waiting == 1 || !s->current || tag != s->current->tag ||
+    if (s->waiting == 1 || !s->current || req->tag != s->current->tag ||
         (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON))) {
-        if (lsi_queue_tag(s, tag, arg))
+        if (lsi_queue_tag(s, req->tag, arg))
             return;
     }
 
     /* host adapter (re)connected */
-    DPRINTF("Data ready tag=0x%x len=%d\n", tag, arg);
+    DPRINTF("Data ready tag=0x%x len=%d\n", req->tag, arg);
     s->current->dma_len = arg;
     s->command_complete = 1;
     if (!s->waiting)
@@ -755,14 +757,16 @@ static void lsi_do_command(LSIState *s)
     assert(s->current == NULL);
     s->current = qemu_mallocz(sizeof(lsi_request));
     s->current->tag = s->select_tag;
+    s->current->req = dev->info->alloc_req(dev, s->current->tag,
+                                           s->current_lun);
 
-    n = dev->info->send_command(dev, s->current->tag, buf, s->current_lun);
+    n = dev->info->send_command(s->current->req, buf);
     if (n > 0) {
         lsi_set_phase(s, PHASE_DI);
-        dev->info->read_data(dev, s->current->tag);
+        dev->info->read_data(s->current->req);
     } else if (n < 0) {
         lsi_set_phase(s, PHASE_DO);
-        dev->info->write_data(dev, s->current->tag);
+        dev->info->write_data(s->current->req);
     }
 
     if (!s->command_complete) {
hw/scsi-bus.c +1 -14
index afdf0ad..bb88a56 100644
@@ -138,18 +138,6 @@ SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t l
     return req;
 }
 
-SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
-{
-    SCSIRequest *req;
-
-    QTAILQ_FOREACH(req, &d->requests, next) {
-        if (req->tag == tag) {
-            return req;
-        }
-    }
-    return NULL;
-}
-
 static void scsi_req_dequeue(SCSIRequest *req)
 {
     if (req->enqueued) {
@@ -607,6 +595,5 @@ void scsi_req_complete(SCSIRequest *req)
     assert(req->status != -1);
     scsi_req_dequeue(req);
     req->bus->complete(req->bus, SCSI_REASON_DONE,
-                       req->tag,
-                       req->status);
+                       req, req->status);
 }
hw/scsi-disk.c +45 -102

288 lines not on this page · raw message

hw/scsi-generic.c +39 -80

218 lines not on this page · raw message

hw/scsi.h +11 -10
index 1196122..202d680 100644
@@ -17,7 +17,8 @@ enum scsi_reason {
 typedef struct SCSIBus SCSIBus;
 typedef struct SCSIDevice SCSIDevice;
 typedef struct SCSIDeviceInfo SCSIDeviceInfo;
-typedef void (*scsi_completionfn)(SCSIBus *bus, int reason, uint32_t tag,
+typedef struct SCSIRequest SCSIRequest;
+typedef void (*scsi_completionfn)(SCSIBus *bus, int reason, SCSIRequest *req,
                                   uint32_t arg);
 
 enum SCSIXferMode {
@@ -32,7 +33,7 @@ typedef struct SCSISense {
     uint8_t ascq;
 } SCSISense;
 
-typedef struct SCSIRequest {
+struct SCSIRequest {
     SCSIBus           *bus;
     SCSIDevice        *dev;
     uint32_t          tag;
@@ -48,7 +49,7 @@ typedef struct SCSIRequest {
     BlockDriverAIOCB  *aiocb;
     bool enqueued;
     QTAILQ_ENTRY(SCSIRequest) next;
-} SCSIRequest;
+};
 
 struct SCSIDevice
 {
@@ -71,12 +72,13 @@ struct SCSIDeviceInfo {
     DeviceInfo qdev;
     scsi_qdev_initfn init;
     void (*destroy)(SCSIDevice *s);
-    int32_t (*send_command)(SCSIDevice *s, uint32_t tag, uint8_t *buf,
-                            int lun);
-    void (*read_data)(SCSIDevice *s, uint32_t tag);
-    int (*write_data)(SCSIDevice *s, uint32_t tag);
-    void (*cancel_io)(SCSIDevice *s, uint32_t tag);
-    uint8_t *(*get_buf)(SCSIDevice *s, uint32_t tag);
+    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
+    void (*free_req)(SCSIRequest *req);
+    int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
+    void (*read_data)(SCSIRequest *req);
+    int (*write_data)(SCSIRequest *req);
+    void (*cancel_io)(SCSIRequest *req);
+    uint8_t *(*get_buf)(SCSIRequest *req);
 };
 
 typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv,
@@ -136,7 +138,6 @@ int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
 int scsi_sense_valid(SCSISense sense);
 
 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
-SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag);
 void scsi_req_free(SCSIRequest *req);
 
 int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
hw/usb-msd.c +13 -10
index 0a95d8d..b4540c4 100644
@@ -47,6 +47,7 @@ typedef struct {
     uint32_t data_len;
     uint32_t residue;
     uint32_t tag;
+    SCSIRequest *req;
     SCSIBus bus;
     BlockConf conf;
     SCSIDevice *scsi_dev;
@@ -155,9 +156,9 @@ static void usb_msd_copy_data(MSDState *s)
     s->data_len -= len;
     if (s->scsi_len == 0) {
         if (s->mode == USB_MSDM_DATAIN) {
-            s->scsi_dev->info->read_data(s->scsi_dev, s->tag);
+            s->scsi_dev->info->read_data(s->req);
         } else if (s->mode == USB_MSDM_DATAOUT) {
-            s->scsi_dev->info->write_data(s->scsi_dev, s->tag);
+            s->scsi_dev->info->write_data(s->req);
         }
     }
 }
@@ -173,14 +174,14 @@ static void usb_msd_send_status(MSDState *s)
     memcpy(s->usb_buf, &csw, 13);
 }
 
-static void usb_msd_command_complete(SCSIBus *bus, int reason, uint32_t tag,
+static void usb_msd_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
                                      uint32_t arg)
 {
     MSDState *s = DO_UPCAST(MSDState, dev.qdev, bus->qbus.parent);
     USBPacket *p = s->packet;
 
-    if (tag != s->tag) {
-        fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag);
+    if (req->tag != s->tag) {
+        fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
     }
     if (reason == SCSI_REASON_DONE) {
         DPRINTF("Command complete %d\n", arg);
@@ -207,10 +208,11 @@ static void usb_msd_command_complete(SCSIBus *bus, int reason, uint32_t tag,
         } else if (s->data_len == 0) {
             s->mode = USB_MSDM_CSW;
         }
+        s->scsi_dev->info->free_req(req);
         return;
     }
     s->scsi_len = arg;
-    s->scsi_buf = s->scsi_dev->info->get_buf(s->scsi_dev, tag);
+    s->scsi_buf = s->scsi_dev->info->get_buf(req);
     if (p) {
         usb_msd_copy_data(s);
         if (s->usb_len == 0) {
@@ -348,7 +350,7 @@ static int usb_msd_handle_control(USBDevice *dev, int request, int value,
 static void usb_msd_cancel_io(USBPacket *p, void *opaque)
 {
     MSDState *s = opaque;
-    s->scsi_dev->info->cancel_io(s->scsi_dev, s->tag);
+    s->scsi_dev->info->cancel_io(s->req);
     s->packet = NULL;
     s->scsi_len = 0;
 }
@@ -396,14 +398,15 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
             DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
                     s->tag, cbw.flags, cbw.cmd_len, s->data_len);
             s->residue = 0;
-            s->scsi_dev->info->send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
+            s->req = s->scsi_dev->info->alloc_req(s->scsi_dev, s->tag, 0);
+            s->scsi_dev->info->send_command(s->req, cbw.cmd);
             /* ??? Should check that USB and SCSI data transfer
                directions match.  */
             if (s->residue == 0) {
                 if (s->mode == USB_MSDM_DATAIN) {
-                    s->scsi_dev->info->read_data(s->scsi_dev, s->tag);
+                    s->scsi_dev->info->read_data(s->req);
                 } else if (s->mode == USB_MSDM_DATAOUT) {
-                    s->scsi_dev->info->write_data(s->scsi_dev, s->tag);
+                    s->scsi_dev->info->write_data(s->req);
                 }
             }
             ret = len;
Signature · 1 line
1.6.0.2
#14 lore raw

PATCH 11/15 Remove 'bus' argument from SCSI command completion callbacks

In reply to Hannes Reinecke

The 'bus' argument is now pointless, as we already pass the request itself and the bus can be derived from it.

---
 hw/esp.c          |    5 ++---
 hw/lsi53c895a.c   |    5 ++---
 hw/scsi-bus.c     |    3 +--
 hw/scsi-disk.c    |    8 ++++----
 hw/scsi-generic.c |    6 +++---
 hw/scsi.h         |    3 +--
 hw/usb-msd.c      |    5 ++---
 7 files changed, 15 insertions(+), 20 deletions(-)
hw/esp.c +2 -3
index 2784bec..d55bb6d 100644
@@ -390,10 +390,9 @@ static void esp_do_dma(ESPState *s)
     }
 }
 
-static void esp_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
-                                 uint32_t arg)
+static void esp_command_complete(SCSIRequest *req, int reason, uint32_t arg)
 {
-    ESPState *s = DO_UPCAST(ESPState, busdev.qdev, bus->qbus.parent);
+    ESPState *s = DO_UPCAST(ESPState, busdev.qdev, req->bus->qbus.parent);
 
     if (reason == SCSI_REASON_DONE) {
         DPRINTF("SCSI Command complete\n");
hw/lsi53c895a.c +2 -3
index 82a5d39..858c8f4 100644
@@ -688,10 +688,9 @@ static int lsi_queue_tag(LSIState *s, uint32_t tag, uint32_t arg)
 }
 
 /* Callback to indicate that the SCSI layer has completed a transfer.  */
-static void lsi_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
-                                 uint32_t arg)
+static void lsi_command_complete(SCSIRequest *req, int reason, uint32_t arg)
 {
-    LSIState *s = DO_UPCAST(LSIState, dev.qdev, bus->qbus.parent);
+    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
     int out;
 
     out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
hw/scsi-bus.c +1 -2
index bb88a56..0f8fd57 100644
@@ -594,6 +594,5 @@ void scsi_req_complete(SCSIRequest *req)
 {
     assert(req->status != -1);
     scsi_req_dequeue(req);
-    req->bus->complete(req->bus, SCSI_REASON_DONE,
-                       req, req->status);
+    req->bus->complete(req, SCSI_REASON_DONE, req->status);
 }
hw/scsi-disk.c +4 -4
index 0ccb627..a4f387d 100644
@@ -156,7 +156,7 @@ static void scsi_read_complete(void * opaque, int ret)
 
     DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, iov_len);
 
-    r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, iov_len);
+    r->req.bus->complete(&r->req, SCSI_REASON_DATA, iov_len);
 }
 
 
@@ -179,7 +179,7 @@ static void scsi_read_data(SCSIRequest *req)
     if (r->sector_count == (uint32_t)-1) {
         DPRINTF("Read buf_len=%zd\n", r->iov[0].iov_len);
         r->sector_count = 0;
-        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req,
+        r->req.bus->complete(&r->req, SCSI_REASON_DATA,
                              r->iov[0].iov_len);
         return;
     }
@@ -229,7 +229,7 @@ static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
         vm_stop(0);
     } else {
         if (type == SCSI_REQ_STATUS_RETRY_READ) {
-            r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, 0);
+            r->req.bus->complete(&r->req, SCSI_REASON_DATA, 0);
         }
         switch (error) {
             case EBADR:
@@ -280,7 +280,7 @@ static void scsi_write_complete(void * opaque, int ret)
             r->iov[0].iov_len = len;
         }
         DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
-        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, len);
+        r->req.bus->complete(&r->req, SCSI_REASON_DATA, len);
     }
 }
 
hw/scsi-generic.c +3 -3
index ed085a1..d4edc48 100644
@@ -196,7 +196,7 @@ static void scsi_read_complete(void * opaque, int ret)
     DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, len);
 
     r->len = -1;
-    r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, len);
+    r->req.bus->complete(&r->req, SCSI_REASON_DATA, len);
     if (len == 0)
         scsi_command_complete(r, 0);
 }
@@ -226,7 +226,7 @@ static void scsi_read_data(SCSIRequest *req)
         DPRINTF("Sense: %d %d %d %d %d %d %d %d\n",
                 r->buf[0], r->buf[1], r->buf[2], r->buf[3],
                 r->buf[4], r->buf[5], r->buf[6], r->buf[7]);
-        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, s->senselen);
+        r->req.bus->complete(&r->req, SCSI_REASON_DATA, s->senselen);
         /* Clear sensebuf after REQUEST_SENSE */
         scsi_clear_sense(s);
         return;
@@ -272,7 +272,7 @@ static int scsi_write_data(SCSIRequest *req)
 
     if (r->len == 0) {
         r->len = r->buflen;
-        r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, &r->req, r->len);
+        r->req.bus->complete(&r->req, SCSI_REASON_DATA, r->len);
         return 0;
     }
 
hw/scsi.h +1 -2
index 202d680..c2d3300 100644
@@ -18,8 +18,7 @@ typedef struct SCSIBus SCSIBus;
 typedef struct SCSIDevice SCSIDevice;
 typedef struct SCSIDeviceInfo SCSIDeviceInfo;
 typedef struct SCSIRequest SCSIRequest;
-typedef void (*scsi_completionfn)(SCSIBus *bus, int reason, SCSIRequest *req,
-                                  uint32_t arg);
+typedef void (*scsi_completionfn)(SCSIRequest *req, int reason, uint32_t arg);
 
 enum SCSIXferMode {
     SCSI_XFER_NONE,      /*  TEST_UNIT_READY, ...            */
hw/usb-msd.c +2 -3
index b4540c4..ae4d2d4 100644
@@ -174,10 +174,9 @@ static void usb_msd_send_status(MSDState *s)
     memcpy(s->usb_buf, &csw, 13);
 }
 
-static void usb_msd_command_complete(SCSIBus *bus, int reason, SCSIRequest *req,
-                                     uint32_t arg)
+static void usb_msd_command_complete(SCSIRequest *req, int reason, uint32_t arg)
 {
-    MSDState *s = DO_UPCAST(MSDState, dev.qdev, bus->qbus.parent);
+    MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
     USBPacket *p = s->packet;
 
     if (req->tag != s->tag) {
Signature · 1 line
1.6.0.2
#22 lore raw

PATCH 14/15 megasas: LSI Megaraid SAS emulation

In reply to Hannes Reinecke

This patch adds an emulation for the LSI Megaraid SAS HBA.

---
 Makefile.objs |    2 +-
 hw/megasas.c  | 1828 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/mfi.h      | 1197 +++++++++++++++++++++++++++++++++++++
 hw/pci_ids.h  |    2 +
 hw/scsi.h     |    1 +
 5 files changed, 3029 insertions(+), 1 deletions(-)
 create mode 100644 hw/megasas.c
 create mode 100644 hw/mfi.h
Makefile.objs +1 -1
index 23b17ce..89c8661 100644
@@ -231,7 +231,7 @@ hw-obj-$(CONFIG_IDE_MACIO) += ide/macio.o
 hw-obj-$(CONFIG_IDE_VIA) += ide/via.o
 
 # SCSI layer
-hw-obj-y += lsi53c895a.o
+hw-obj-y += lsi53c895a.o megasas.o
 hw-obj-$(CONFIG_ESP) += esp.o
 
 hw-obj-y += dma-helpers.o sysbus.o isa-bus.o
hw/megasas.c new file +1828 -0

1,828 lines not on this page · raw message

hw/mfi.h new file +1197 -0

1,164 lines not on this page · raw message

hw/pci_ids.h +2 -0
index 82cba7e..6b9b54e 100644

Lines of this file are not shown. View the raw message.

hw/scsi.h +1 -0
index 063154d..d970256 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.6.0.2

Part of the diff is not shown here. View the raw message.

#29 lore raw
In reply to Hannes Reinecke

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

#17 lore raw

Re: [Qemu-devel] [PATCH 13/15] scsi: Implement alloc_req_iov callback

In reply to Hannes Reinecke
Quoted 3 lines · Hannes Reinecke

Add callback to create a request with a predefined iovec. This is required for drivers which can use the iovec of a command directly.

What happend to my comment that the iov and non-iov case should share code? Also what happened to the other comment about not naming the method implementation different from the method name.

#3 lore raw

Re: [Qemu-devel] [PATCH 02/15] scsi: Return SAM status codes

In reply to Hannes Reinecke
Quoted 7 lines · Hannes Reinecke

Traditionally, the linux stack is using SCSI status codes which are shifted by one as compared to those defined in SAM. A SCSI emulation should naturally return the SAM defined codes, not the linux ones.
So to avoid any confusion this patch modifies the existing definitions to match those found in SAM and removes any (now obsolete) byte-shift from the returned status codes.

Looks good.

#18 lore raw

Re: [Qemu-devel] [PATCH 13/15] scsi: Implement alloc_req_iov callback

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Hannes Reinecke

Add callback to create a request with a predefined iovec. This is required for drivers which can use the iovec of a command directly.

What happend to my comment that the iov and non-iov case should share code? Also what happened to the other comment about not naming the method implementation different from the method name.

Looked into it.
Sure I could be doing it for scsi-disk; for scsi-generic it won't work, though. And it's not much of a code-share to have from it; you'll end up with something like:

static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,

        uint32_t lun)
{
    struct iovec *iov;
    uint8_t *buf;
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
    SCSIRequest *req;
    SCSIDiskReq *r;

    buf = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
    iov = qemu_mallocz(sizeof(struct iovec));
    iov[0].iov_base = buf;
    req = scsi_new_request_iovec(d, tag, lun, iov, 1);
    r = DO_UPCAST(SCSIDiskReq, req, req);
    r->iov_buf = buf;
    return req;
}

which doesn't look better than the original:

static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,

        uint32_t lun)
{
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
    SCSIRequest *req;
    SCSIDiskReq *r;

    req = scsi_req_alloc(sizeof(SCSIDiskReq), d, tag, lun);
    r = DO_UPCAST(SCSIDiskReq, req, req);
    r->iov_buf = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
    r->iov = qemu_mallocz(sizeof(struct iovec));
    r->iov[0].iov_base = r->iov_buf;
    r->iov_num = 1;
    return req;
}

But I'm open to suggestions.

And for the naming:
The SCSI stack is using 'req' for every function accepting SCSIRequest as an argument:

hw/scsi.h:
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d,
uint32_t tag, uint32_t lun);
void scsi_req_free(SCSIRequest *req);

int scsi_req_parse(SCSIRequest *req, uint8_t *buf); void scsi_req_print(SCSIRequest *req);
void scsi_req_complete(SCSIRequest *req);

So using 'alloc_req' and 'free_req' is in line with this naming scheme. Using 'alloc_request' and 'free_request' really looked odd in the light of the general usage.

Hence I didn't do it.
But again, I'm open to suggestions here.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#23 lore raw

[Qemu-devel] Re: [PATCH 14/15] megasas: LSI Megaraid SAS emulation

In reply to Hannes Reinecke
Quoted 10 lines · Hannes Reinecke
hw/megasas.c new file +10 -0
+static int megasas_pd_get_info_submit(SCSIDevice * sdev, int lun,
+                                      struct megasas_cmd_t *cmd)
+{
+    struct mfi_pd_info * info = cmd->iov_buf;
+    uint8_t cmdbuf[6];
+    SCSIRequest *req;
+
+    if (info->inquiry_data[4] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0, info->inquiry_data,

hw/megasas.c line 799

Quoted 10 lines
hw/megasas.c new file +10 -0
+                              sizeof(info->inquiry_data));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);
+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit std inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;

Here...

Quoted 5 lines

+    } else if (info->vpd_page83[3] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0x83,(uint8_t *)info->vpd_page83,
+                              sizeof(info->vpd_page83));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);

Quoted 8 lines

+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit vpd inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;

...and here I can't tell for sure if an error status is returned intentionally or whether this is an if statement without {}-related bug.

On one hand it looks like it could be intentional. On the other hand we went through the trouble of sending an inquiry command that may have succeeded but we're still returning an error status.

Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH 06/15] scsi: Update sense code handling

In reply to Hannes Reinecke

Am 24.11.2010 12:16, schrieb Hannes Reinecke:

Quoted 94 lines

The SCSI spec has a quite detailed list of sense codes available. It even mandates the use of specific ones for some failure cases. The current implementation just has one type of 'generic' error which is actually a violation of the spec in certain cases. This patch introduces various predefined sense codes to have the sense code reporting more in line with the spec.

---
 hw/scsi-bus.c     |   92 ++++++++++++++++++++++++++++++++++++++++++++
 hw/scsi-disk.c    |  109 +++++++++++++++++++++++++++--------------------------
 hw/scsi-generic.c |   76 ++++++++++++++++++++++++++-----------
 hw/scsi.h         |   38 ++++++++++++++++++
 4 files changed, 239 insertions(+), 76 deletions(-)
hw/scsi-bus.c +70 -0
index 93f0e9a..afdf0ad 100644
@@ -388,6 +388,98 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
     return 0;
 }
 
+/*
+ * Predefined sense codes
+ */
+
+/* No sense data available */
+const struct SCSISense sense_code_NO_SENSE = {
+    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
+};
+
+/* LUN not ready, Manual intervention required */
+const struct SCSISense sense_code_LUN_NOT_READY = {
+    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
+};
+
+/* LUN not ready, Medium not present */
+const struct SCSISense sense_code_NO_MEDIUM = {
+    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
+};
+
+/* Hardware error, internal target failure */
+const struct SCSISense sense_code_TARGET_FAILURE = {
+    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
+};
+
+/* Illegal request, invalid command operation code */
+const struct SCSISense sense_code_INVALID_OPCODE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
+};
+
+/* Illegal request, LBA out of range */
+const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
+};
+
+/* Illegal request, Invalid field in CDB */
+const struct SCSISense sense_code_INVALID_FIELD = {
+    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
+};
+
+/* Illegal request, LUN not supported */
+const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
+    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
+};
+
+/* Command aborted, I/O process terminated */
+const struct SCSISense sense_code_IO_ERROR = {
+    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
+};
+
+/* Command aborted, I_T Nexus loss occurred */
+const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
+    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
+};
+
+/* Command aborted, Logical Unit failure */
+const struct SCSISense sense_code_LUN_FAILURE = {
+    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
+};
+
+/*
+ * scsi_build_sense
+ *
+ * Build a sense buffer
+ */
+int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
+{
+    if (len < 8)
+        return 0;
+    if (fixed && len < 14)
+        return 0;

hw/scsi-bus.c int scsi_req_parse(SCSIRequest *req, uint8_t *buf) · line 470

Quoted 10 lines
hw/scsi-bus.c +10 -0
+
+    memset(buf, 0, len);
+    if (fixed) {
+        /* Return fixed format sense buffer */
+        buf[0] = 0xf0;
+        buf[2] = sense.key;
+        buf[7] = 7;
+        buf[12] = sense.asc;
+        buf[13] = sense.ascq;
+        len = 14;

My spec says: "Device servers shall return at least 18 bytes of parameter data in response to a REQUEST SENSE command if the allocation length is 18 or greater and the DESC bit is set to zero."

So should this be MIN(len, 18) instead?

Kevin

#24 lore raw

[Qemu-devel] Re: [PATCH 14/15] megasas: LSI Megaraid SAS emulation

In reply to Stefan Hajnoczi
Quoted 38 lines · Stefan Hajnoczi
Quoted 10 lines · Hannes Reinecke
hw/megasas.c new file +10 -0
+static int megasas_pd_get_info_submit(SCSIDevice * sdev, int lun,
+                                      struct megasas_cmd_t *cmd)
+{
+    struct mfi_pd_info * info = cmd->iov_buf;
+    uint8_t cmdbuf[6];
+    SCSIRequest *req;
+
+    if (info->inquiry_data[4] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0, info->inquiry_data,

hw/megasas.c line 799

Quoted 10 lines
hw/megasas.c new file +10 -0
+                              sizeof(info->inquiry_data));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);
+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit std inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;

Here...

hw/megasas.c line 812

Quoted 13 lines
hw/megasas.c new file +13 -0
+    } else if (info->vpd_page83[3] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0x83,(uint8_t *)info->vpd_page83,
+                              sizeof(info->vpd_page83));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);
+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit vpd inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;
Quoted 8 lines

...and here I can't tell for sure if an error status is returned intentionally or whether this is an if statement without {}-related bug.

On one hand it looks like it could be intentional. On the other hand we went through the trouble of sending an inquiry command that may have succeeded but we're still returning an error status.

MFI_STAT_INVALID_STATUS is _not_ an error status. It's used internally to signal that a command is still being processed. ->read_data() itself doesn't have a meaningful return code; we need to wait for the ->complete callback to get results. So we're returning MFI_STAT_INVALID_STATUS to signal this condition.

So that's ok.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#25 lore raw

[Qemu-devel] Re: [PATCH 14/15] megasas: LSI Megaraid SAS emulation

Quoted 48 lines · Hannes Reinecke
Quoted 38 lines · Stefan Hajnoczi
Quoted 10 lines · Hannes Reinecke
hw/megasas.c new file +10 -0
+static int megasas_pd_get_info_submit(SCSIDevice * sdev, int lun,
+                                      struct megasas_cmd_t *cmd)
+{
+    struct mfi_pd_info * info = cmd->iov_buf;
+    uint8_t cmdbuf[6];
+    SCSIRequest *req;
+
+    if (info->inquiry_data[4] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0, info->inquiry_data,

hw/megasas.c line 799

Quoted 10 lines
hw/megasas.c new file +10 -0
+                              sizeof(info->inquiry_data));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);
+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit std inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;

Here...

Quoted 5 lines

+    } else if (info->vpd_page83[3] == 0) {
+        /* Additional length is zero, resubmit */
+        megasas_setup_inquiry(cmdbuf, 0x83,(uint8_t *)info->vpd_page83,
+                              sizeof(info->vpd_page83));
+        req = sdev->info->alloc_req(sdev, (uint32_t) -1, lun);

Quoted 8 lines

+        if (!req) {
+            return MFI_STAT_FLASH_ALLOC_FAIL;
+        }
+        DPRINTF_DCMD("PD get info submit vpd inquiry to dev %d\n", lun);
+        req->hba_private = cmd;
+        if (cmd->sdev->info->send_command(req, cmdbuf) > 0)
+            cmd->sdev->info->read_data(req);
+        return MFI_STAT_INVALID_STATUS;

Quoted 8 lines

...and here I can't tell for sure if an error status is returned intentionally or whether this is an if statement without {}-related bug.

On one hand it looks like it could be intentional.  On the other hand we went through the trouble of sending an inquiry command that may have succeeded but we're still returning an error status.

Quoted 7 lines

MFI_STAT_INVALID_STATUS is _not_ an error status. It's used internally to signal that a command is still being processed. ->read_data() itself doesn't have a meaningful return code; we need to wait for the ->complete callback to get results. So we're returning MFI_STAT_INVALID_STATUS to signal this condition.

So that's ok.

Okay, thanks for explaining.

Stefan

#19 lore raw

Re: [Qemu-devel] [PATCH 13/15] scsi: Implement alloc_req_iov callback

In reply to Hannes Reinecke
Quoted 4 lines · Hannes Reinecke

Looked into it.
Sure I could be doing it for scsi-disk; for scsi-generic it won't work, though. And it's not much of a code-share to have from it; you'll end up with something like:

Yes, and that is a good start to completely get rid of the non-iovec version. Keeping two parallel APIs around that have slighly mismatching semantics is a bad idea. And for scsi-generic in the version in your the difference is even worse and more subtile.

I think the only way to get this interface future proof is to unify it. That is always pass an iovec from the HBA driver, and make the length chunking an explicitly flag passed to ->alloc_req instead of an implicit one when using the old interface. Then refactor the code currently resetting.

Talking about scsi-generic, how is the auto request-sense in scsi_read_data and the mode select snooping in scsi_write_complete supposed to for for the iovec interface?

Quoted 9 lines

And for the naming:
The SCSI stack is using 'req' for every function accepting SCSIRequest as an argument:

hw/scsi.h:
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d,
uint32_t tag, uint32_t lun);
void scsi_req_free(SCSIRequest *req);

Quoted 7 lines

int scsi_req_parse(SCSIRequest *req, uint8_t *buf); void scsi_req_print(SCSIRequest *req);
void scsi_req_complete(SCSIRequest *req);

So using 'alloc_req' and 'free_req' is in line with this naming scheme. Using 'alloc_request' and 'free_request' really looked odd in the light of the general usage.

Keeping the method names is fine, but please name the implementations matching it, e.g.

scsi_alloc_req and co.

And yes, using the scsi_ prefix is a bit confusing with the generic code also using it, eventually the disk driver should use scsi_disk and the generic driver scsi_generic prefixes.

And in general it would be nice if you could simplify answer to reviews. If something that the reviewer requests doesn't make sense state so in reply instead of silently ignoring it.

#20 lore raw

Re: [Qemu-devel] [PATCH 13/15] scsi: Implement alloc_req_iov callback

Quoted 11 lines · Christoph Hellwig
Quoted 4 lines · Hannes Reinecke

Looked into it.
Sure I could be doing it for scsi-disk; for scsi-generic it won't work, though. And it's not much of a code-share to have from it; you'll end up with something like:

Yes, and that is a good start to completely get rid of the non-iovec version. Keeping two parallel APIs around that have slighly mismatching semantics is a bad idea. And for scsi-generic in the version in your the difference is even worse and more subtile.

Quoted 6 lines

I think the only way to get this interface future proof is to unify it. That is always pass an iovec from the HBA driver, and make the length chunking an explicitly flag passed to ->alloc_req instead of an implicit one when using the old interface. Then refactor the code currently resetting.

I don't think that'll work. It's only in send_command() when the CDB is parsed, and only then we do know how much data we should be expecting.
If you have a iovec passed in this doesn't matter as you can't really enlarge it. But in the other case you really need the size if you want to allocate a buffer large enough to hold the data.

I must say I'd like to get rid of the chunking transfer in scsi-disk. To have it for scsi-disk only is really pointless, as you can potentially send exactly the same commands via scsi-generic. So for scsi-generic to work properly qemu need to be able to allocate the _entire_ data buffer. And hence qemu _must_ be able to allocate the same buffer for the scsi-disk emulation.
So any malloc space arguments don't really work out here.

By the same reasoning we could remove the chunking altogether; any HBA _must_ be capable of issuing the entire data (if issued via scsi-generic) even today. So I don't really buy the argument of chunking being required for large I/Os.

But then, I've been down that road already.
With no large success.

Quoted 4 lines

Talking about scsi-generic, how is the auto request-sense in scsi_read_data and the mode select snooping in scsi_write_complete supposed to for for the iovec interface?

The sense code is actually a property of the device, no the command. And a REQUEST_SENSE command will just return the status of the last command. So the sense buffer is always stored with the device in SCSIGenericState and can be retrieved at will.

But you are correct, the MODE SELECT snooping needs to be modified. No big deal, though.

Quoted 17 lines
Quoted 9 lines

And for the naming:
The SCSI stack is using 'req' for every function accepting SCSIRequest as an argument:

hw/scsi.h:
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d,
uint32_t tag, uint32_t lun);
void scsi_req_free(SCSIRequest *req);

Quoted 7 lines

int scsi_req_parse(SCSIRequest *req, uint8_t *buf); void scsi_req_print(SCSIRequest *req);
void scsi_req_complete(SCSIRequest *req);

So using 'alloc_req' and 'free_req' is in line with this naming scheme. Using 'alloc_request' and 'free_request' really looked odd in the light of the general usage.

Quoted 9 lines

Keeping the method names is fine, but please name the implementations matching it, e.g.

scsi_alloc_req and co.

And yes, using the scsi_ prefix is a bit confusing with the generic code also using it, eventually the disk driver should use scsi_disk and the generic driver scsi_generic prefixes.

Ah, now I think I see what you mean.
You were think along these lines:

static SCSIDeviceInfo scsi_generic_info = {

    .qdev.name    = "scsi-generic",
    .qdev.desc    = "pass through generic scsi device (/dev/sg*)",
    .qdev.size    = sizeof(SCSIGenericState),
    .qdev.reset   = scsi_generic_reset,
    .init         = scsi_generic_initfn,
    .destroy      = scsi_destroy,
    .alloc_req    = scsi_generic_alloc_req,

.alloc_req_iov = scsi_generic_alloc_req_iovec,
.free_req = scsi_generic_free_req,
.send_command = scsi_send_command,
.read_data = scsi_read_data,

correct?
Yeah, that's easy to do.

Quoted 3 lines

And in general it would be nice if you could simplify answer to reviews. If something that the reviewer requests doesn't make sense state so in reply instead of silently ignoring it.

Will do in the future.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#26 lore raw

[Qemu-devel] Re: [PATCH 14/15] megasas: LSI Megaraid SAS emulation

In reply to Hannes Reinecke
Quoted 4 lines · Hannes Reinecke
hw/megasas.c new file +4 -0
+static int megasas_scsi_init(PCIDevice *dev)
+{
+    MPTState *s = DO_UPCAST(MPTState, dev, dev);
+    uint8_t *pci_conf;

hw/megasas.c line 1749

Quoted 10 lines
hw/megasas.c new file +10 -0
+    int i;
+
+    pci_conf = s->dev.config;
+
+    /* PCI Vendor ID (word) */
+    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_LSI_LOGIC);
+    /* PCI device ID (word) */
+    pci_config_set_device_id(pci_conf,  PCI_DEVICE_ID_LSI_SAS1078);
+    /* PCI subsystem ID */
+    pci_set_word(&pci_conf[PCI_SUBSYSTEM_VENDOR_ID], 0x1000);

PCI_VENDOR_ID_LSI_LOGIC

Quoted 1 line

+ pci_set_word(&pci_conf[PCI_SUBSYSTEM_ID], 0x1013);

What is 0x1013?

hw/megasas.c line 1755

Quoted 5 lines
hw/megasas.c new file +5 -0
+    /* PCI base class code */
+    pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_RAID);
+
+    /* PCI latency timer = 0 */
+    pci_conf[0x0d] = 0;

PCI_LATENCY_TIMER

hw/megasas.c line 1757

Quoted 2 lines
hw/megasas.c new file +2 -0
+    /* Interrupt pin 1 */
+    pci_conf[0x3d] = 0x01;

pci_config_set_interrupt_pin()

Sebastian

#21 lore raw

Re: [Qemu-devel] [PATCH 13/15] scsi: Implement alloc_req_iov callback

In reply to Hannes Reinecke
Quoted 8 lines

I must say I'd like to get rid of the chunking transfer in scsi-disk. To have it for scsi-disk only is really pointless, as you can potentially send exactly the same commands via scsi-generic. So for scsi-generic to work properly qemu need to be able to allocate the _entire_ data buffer. And hence qemu _must_ be able to allocate the same buffer for the scsi-disk emulation.
So any malloc space arguments don't really work out here.

Quoted 4 lines

By the same reasoning we could remove the chunking altogether; any HBA _must_ be capable of issuing the entire data (if issued via scsi-generic) even today. So I don't really buy the argument of chunking being required for large I/Os.

We've been over this before. Your logic is fundamentally flawed.

In many cases The HBA simply doesn't know where the data is going to go until after the command has been issued. Issuing the command (which may fail) and setting up the buffer to receive the data are separate operations, and there may be guest visible state in between. Even if you assume the initial command is accepted successfully, the DMA buffer may be submitted in several parts. If the device response does not full all these parts then we should not be accessing the unused ones. And this is assuming your HBA is DMA capable to start with - a USB mass storage device almost certainly isn't.

Even if you do know the full DMA buffer ahead of time, there's no guarantee that you'll actually be able to map it all at once. You have to assume that bounce buffers are required, and only a small chunk of the ram can be mapped at any point.

Combine this with the fact that the guest may submit arbitrarily large requests and you need some form of chunking. IIRC the passthrough support ignores the linux interface already restricts you to relatively small requests.

Paul

#30 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Ping for patches 1 to 11?

Paolo

#31 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Christoph Hellwig

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Ping for patches 1 to 11?

Paolo

The first few already have been merged by Kevin Wolf; I'll see to prepare an updated patchset.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#32 lore raw
Quoted 8 lines · Hannes Reinecke
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Christoph Hellwig

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Quoted 6 lines
Quoted 3 lines

Ping for patches 1 to 11?

Paolo

The first few already have been merged by Kevin Wolf; I'll see to prepare an updated patchset.

Actually, I was about to ask as I'd like to base some new work of mine on top of these. I don't see any recent commit from Kevin in the qemu master branch (nor in any other branch on
http://git.savannah.gnu.org/cgit/qemu.git/log/).

Does Kevin maintain a separate staging tree ?

Cheers,
Ben.

#33 lore raw
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 8 lines · Hannes Reinecke
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Christoph Hellwig

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Quoted 6 lines
Quoted 3 lines

Ping for patches 1 to 11?

Paolo

The first few already have been merged by Kevin Wolf; I'll see to prepare an updated patchset.

Quoted 6 lines

Actually, I was about to ask as I'd like to base some new work of mine on top of these. I don't see any recent commit from Kevin in the qemu master branch (nor in any other branch on
http://git.savannah.gnu.org/cgit/qemu.git/log/).

Does Kevin maintain a separate staging tree ?

BTW. I could use patch 12 too (get_sense() callback :-) Would save me from manufacturing REQUEST_SENSE etc... I can carry it locally for the time being but it would be nice to have it upstream.

Cheers,
Ben.

#34 lore raw
In reply to Benjamin Herrenschmidt
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 8 lines · Hannes Reinecke
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Christoph Hellwig

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP.  The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Quoted 6 lines
Quoted 3 lines

Ping for patches 1 to 11?

Paolo

The first few already have been merged by Kevin Wolf; I'll see to prepare an updated patchset.

Quoted 6 lines

Actually, I was about to ask as I'd like to base some new work of mine on top of these. I don't see any recent commit from Kevin in the qemu master branch (nor in any other branch on
http://git.savannah.gnu.org/cgit/qemu.git/log/).

Does Kevin maintain a separate staging tree ?

http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/block

Stefan

#35 lore raw
In reply to Benjamin Herrenschmidt

Am 16.12.2010 02:45, schrieb Benjamin Herrenschmidt:

Quoted 13 lines · Hannes Reinecke
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines

Btw, it might make sense to split this series into two.

Patches 1 to 11 are genuine improvements to the SCSI code, which I'd like to see merged ASAP. The rest is the actual megasas driver, which I still want to see, but haven't even gotten to review yet.

Ping for patches 1 to 11?

Paolo

Quoted 7 lines
Quoted 2 lines

The first few already have been merged by Kevin Wolf; I'll see to prepare an updated patchset.

Actually, I was about to ask as I'd like to base some new work of mine on top of these. I don't see any recent commit from Kevin in the qemu master branch (nor in any other branch on
http://git.savannah.gnu.org/cgit/qemu.git/log/).

Patches 1 to 5 are already in master, for the rest I'm waiting for the next update. If you need them earlier for basing your own work on them I can take this version of the patches into my block branch, even though some of the need an update before they can be merged into master.

Quoted 1 line

Does Kevin maintain a separate staging tree ?

It's at git://repo.or.cz/qemu/kevin.git block

Kevin

#37 lore raw
Quoted 4 lines · Christoph Hellwig

Any chance you could resubmit the remaining patches after addressing the outstanding comments? We'll really need the scsi changes to move forward without introducing conflicts.

Yes, I'm currently rebasing my patchset relative to Kevin's git tree. Hopefully I'll be finished sometime this week.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#9 lore raw

Re: [Qemu-devel] [PATCH 06/15] scsi: Update sense code handling

In reply to Kevin Wolf
Quoted 110 lines · Kevin Wolf

Am 24.11.2010 12:16, schrieb Hannes Reinecke:

Quoted 94 lines

The SCSI spec has a quite detailed list of sense codes available. It even mandates the use of specific ones for some failure cases. The current implementation just has one type of 'generic' error which is actually a violation of the spec in certain cases. This patch introduces various predefined sense codes to have the sense code reporting more in line with the spec.

---
 hw/scsi-bus.c     |   92 ++++++++++++++++++++++++++++++++++++++++++++
 hw/scsi-disk.c    |  109 +++++++++++++++++++++++++++--------------------------
 hw/scsi-generic.c |   76 ++++++++++++++++++++++++++-----------
 hw/scsi.h         |   38 ++++++++++++++++++
 4 files changed, 239 insertions(+), 76 deletions(-)
hw/scsi-bus.c +70 -0
index 93f0e9a..afdf0ad 100644
@@ -388,6 +388,98 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
     return 0;
 }
 
+/*
+ * Predefined sense codes
+ */
+
+/* No sense data available */
+const struct SCSISense sense_code_NO_SENSE = {
+    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
+};
+
+/* LUN not ready, Manual intervention required */
+const struct SCSISense sense_code_LUN_NOT_READY = {
+    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
+};
+
+/* LUN not ready, Medium not present */
+const struct SCSISense sense_code_NO_MEDIUM = {
+    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
+};
+
+/* Hardware error, internal target failure */
+const struct SCSISense sense_code_TARGET_FAILURE = {
+    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
+};
+
+/* Illegal request, invalid command operation code */
+const struct SCSISense sense_code_INVALID_OPCODE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
+};
+
+/* Illegal request, LBA out of range */
+const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
+    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
+};
+
+/* Illegal request, Invalid field in CDB */
+const struct SCSISense sense_code_INVALID_FIELD = {
+    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
+};
+
+/* Illegal request, LUN not supported */
+const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
+    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
+};
+
+/* Command aborted, I/O process terminated */
+const struct SCSISense sense_code_IO_ERROR = {
+    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
+};
+
+/* Command aborted, I_T Nexus loss occurred */
+const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
+    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
+};
+
+/* Command aborted, Logical Unit failure */
+const struct SCSISense sense_code_LUN_FAILURE = {
+    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
+};
+
+/*
+ * scsi_build_sense
+ *
+ * Build a sense buffer
+ */
+int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
+{
+    if (len < 8)
+        return 0;
+    if (fixed && len < 14)
+        return 0;

hw/scsi-bus.c int scsi_req_parse(SCSIRequest *req, uint8_t *buf) · line 470

Quoted 10 lines
hw/scsi-bus.c +10 -0
+
+    memset(buf, 0, len);
+    if (fixed) {
+        /* Return fixed format sense buffer */
+        buf[0] = 0xf0;
+        buf[2] = sense.key;
+        buf[7] = 7;
+        buf[12] = sense.asc;
+        buf[13] = sense.ascq;
+        len = 14;

My spec says: "Device servers shall return at least 18 bytes of parameter data in response to a REQUEST SENSE command if the allocation length is 18 or greater and the DESC bit is set to zero."

Quoted 2 lines

So should this be MIN(len, 18) instead?

Yes, you are correct.
And we should actually always return sense data, even if the length is smaller than the minimum.

Fixed in my megasas git tree; will be included in the next round of patches.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#27 lore raw

Re: [Qemu-devel] Re: [PATCH 14/15] megasas: LSI Megaraid SAS emulation

In reply to Sebastian Herbszt
Quoted 18 lines · Sebastian Herbszt
Quoted 4 lines · Hannes Reinecke
hw/megasas.c new file +4 -0
+static int megasas_scsi_init(PCIDevice *dev)
+{
+    MPTState *s = DO_UPCAST(MPTState, dev, dev);
+    uint8_t *pci_conf;

hw/megasas.c line 1749

Quoted 10 lines
hw/megasas.c new file +10 -0
+    int i;
+
+    pci_conf = s->dev.config;
+
+    /* PCI Vendor ID (word) */
+    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_LSI_LOGIC);
+    /* PCI device ID (word) */
+    pci_config_set_device_id(pci_conf,  PCI_DEVICE_ID_LSI_SAS1078);
+    /* PCI subsystem ID */
+    pci_set_word(&pci_conf[PCI_SUBSYSTEM_VENDOR_ID], 0x1000);

PCI_VENDOR_ID_LSI_LOGIC

Ok.

Quoted 4 lines
Quoted 1 line

+ pci_set_word(&pci_conf[PCI_SUBSYSTEM_ID], 0x1013);

What is 0x1013?

That's the device ID for Megaraid SAS 8708EM2.
I'll be adding a #define for it.

Quoted 8 lines

hw/megasas.c line 1755

Quoted 5 lines
hw/megasas.c new file +5 -0
+    /* PCI base class code */
+    pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_RAID);
+
+    /* PCI latency timer = 0 */
+    pci_conf[0x0d] = 0;

PCI_LATENCY_TIMER

Ok.

Quoted 5 lines

hw/megasas.c line 1757

Quoted 2 lines
hw/megasas.c new file +2 -0
+    /* Interrupt pin 1 */
+    pci_conf[0x3d] = 0x01;

pci_config_set_interrupt_pin()

Ok.

Fixed in my megasas git tree. Will be included in the next round of patches.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)