Skip to contentSkip to conversation

Megasas HBA emulation

PATCH Superseded Hannes Reinecke 4 participants 15 messages Jul 1, 2011 – Jul 3, 2011

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Megasas HBA emulation

Hi all,

thanks to Paolo and Stefan most of the SCSI patches are now in, so I've made the next attempt of submitting my Megaraid SAS HBA emulation.

To do so, I've done two additional patches, both should be valid cleanups.

- Replace 'tag' by 'hba_private'
The SCSIRequest structure has a 'tag', which is being used by the drivers to match the SCSIRequest to the internal request structure. The only driver actually to benefit from this is the lsi53c895a driver, everyone else either leaves it blank or uses some internal numberting here.
So this patch converts the 'tag' to a 'hba_private' pointer, which allows the driver to store a pointer to the internal structure directly within the SCSIRequest. This saves the lookup and an additional field in the driver internal request structure.
- Add an 'offset' parameter to iov_to_buf()
iov_from_buf() has it, but iov_to_buf() has it not. But we'll be needing it if the iovec is larger than the buffer. So there.

And, of course, the megasas driver itself. Which has been modified to work with the new interface; otherwise there have been no changes to the previous submission.

Hannes Reinecke (3):
iov: Add 'offset' parameter to iov_to_buf()
scsi: replace 'tag' with 'hba_private' pointer
megasas: LSI Megaraid SAS emulation

 Makefile.objs           |    1 +
 default-configs/pci.mak |    1 +
 hw/esp.c                |    2 +-
 hw/lsi53c895a.c         |   17 +-
 hw/megasas.c            | 1923 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/mfi.h                | 1197 +++++++++++++++++++++++++++++
 hw/pci_ids.h            |    3 +-
 hw/scsi-bus.c           |   22 +-
 hw/scsi-disk.c          |    5 +-
 hw/scsi-generic.c       |    4 +-
 hw/scsi.h               |    8 +-
 hw/spapr_vscsi.c        |   41 +-
 hw/usb-msd.c            |   10 +-
 hw/virtio-net.c         |    2 +-
 hw/virtio-serial-bus.c  |    2 +-
 iov.c                   |   23 +-
 iov.h                   |    2 +-
 trace-events            |   14 +-
 18 files changed, 3193 insertions(+), 84 deletions(-)
 create mode 100644 hw/megasas.c
 create mode 100644 hw/mfi.h
#1 lore raw

PATCH v4 1/3 iov: Add 'offset' parameter to iov_to_buf()

Occasionally, the buffer needs to be placed at a offset within the iovec when copying the buffer to the iovec.

---
 hw/virtio-net.c        |    2 +-
 hw/virtio-serial-bus.c |    2 +-
 iov.c                  |   23 ++++++++++++++---------
 iov.h                  |    2 +-
 4 files changed, 17 insertions(+), 12 deletions(-)
hw/virtio-net.c +1 -1
index 6997e02..a32cc01 100644
@@ -657,7 +657,7 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
 
         /* copy in packet.  ugh */
         len = iov_from_buf(sg, elem.in_num,
-                           buf + offset, size - offset);
+                           buf + offset, 0, size - offset);
         total += len;
         offset += len;
         /* If buffers can't be merged, at this point we
hw/virtio-serial-bus.c +1 -1
index 7f6db7b..53c58d0 100644
@@ -103,7 +103,7 @@ static size_t write_to_port(VirtIOSerialPort *port,
         }
 
         len = iov_from_buf(elem.in_sg, elem.in_num,
-                           buf + offset, size - offset);
+                           buf + offset, 0, size - offset);
         offset += len;
 
         virtqueue_push(vq, &elem, len);
iov.c +14 -9
index 588cd04..9ead6ee 100644
@@ -15,21 +15,26 @@
 #include "iov.h"
 
 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
-                    const void *buf, size_t size)
+                    const void *buf, size_t offset, size_t size)
 {
-    size_t offset;
+    size_t iov_off, buf_off;
     unsigned int i;
 
-    offset = 0;
-    for (i = 0; offset < size && i < iovcnt; i++) {
-        size_t len;
+    iov_off = 0;
+    buf_off = 0;
+    for (i = 0; i < iovcnt && size; i++) {
+        if (offset < (iov_off + iov[i].iov_len)) {
+            size_t len = MIN((iov_off + iov[i].iov_len) - offset, size);
 
-        len = MIN(iov[i].iov_len, size - offset);
+            memcpy(iov[i].iov_base + (offset - iov_off), buf + buf_off, len);
 
-        memcpy(iov[i].iov_base, buf + offset, len);
-        offset += len;
+            buf_off += len;
+            offset += len;
+            size -= len;
+        }
+        iov_off += iov[i].iov_len;
     }
-    return offset;
+    return buf_off;
 }
 
 size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
iov.h +1 -1
index 60a8547..2677527 100644
@@ -13,7 +13,7 @@
 #include "qemu-common.h"
 
 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
-                    const void *buf, size_t size);
+                    const void *buf, size_t offset, size_t size);
 size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
                   void *buf, size_t offset, size_t size);
 size_t iov_size(const struct iovec *iov, const unsigned int iovcnt);
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH v4 2/3 scsi: replace 'tag' with 'hba_private' pointer

'tag' is just an abstraction to identify the command from the driver. So we should make that explicit by replacing 'tag' with a driver-defined pointer 'hba_private'. This saves the lookup for driver handling several commands in parallel.

---
 hw/esp.c          |    2 +-
 hw/lsi53c895a.c   |   17 ++++++++---------
 hw/scsi-bus.c     |   22 +++++++++++-----------
 hw/scsi-disk.c    |    5 ++---
 hw/scsi-generic.c |    4 ++--
 hw/scsi.h         |    8 ++++----
 hw/spapr_vscsi.c  |   41 ++++++++++++-----------------------------
 hw/usb-msd.c      |   10 +++++-----
 trace-events      |   14 +++++++-------
 9 files changed, 52 insertions(+), 71 deletions(-)
hw/esp.c +1 -1
index 6d3f5d2..912ff89 100644
@@ -244,7 +244,7 @@ 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;
-    s->current_req = scsi_req_new(s->current_dev, 0, lun);
+    s->current_req = scsi_req_new(s->current_dev, lun, s);
     datalen = scsi_req_enqueue(s->current_req, buf);
     s->ti_size = datalen;
     if (datalen != 0) {
hw/lsi53c895a.c +8 -9
index 940b43a..272e919 100644
@@ -670,7 +670,7 @@ static void lsi_request_cancelled(SCSIRequest *req)
         return;
     }
 
-    p = lsi_find_by_tag(s, req->tag);
+    p = req->hba_private;
     if (p) {
         QTAILQ_REMOVE(&s->queue, p, next);
         scsi_req_unref(req);
@@ -680,18 +680,17 @@ static void lsi_request_cancelled(SCSIRequest *req)
 
 /* Record that data is available for a queued command.  Returns zero if
    the device was reselected, nonzero if the IO is deferred.  */
-static int lsi_queue_tag(LSIState *s, uint32_t tag, uint32_t len)
+static int lsi_queue_req(LSIState *s, SCSIRequest *req, uint32_t len)
 {
-    lsi_request *p;
+    lsi_request *p = req->hba_private;
 
-    p = lsi_find_by_tag(s, tag);
     if (!p) {
-        BADF("IO with unknown tag %d\n", tag);
+        BADF("IO with unknown reference %p\n", req->hba_private);
         return 1;
     }
 
     if (p->pending) {
-        BADF("Multiple IO pending for tag %d\n", tag);
+        BADF("Multiple IO pending for request %p\n", p);
     }
     p->pending = len;
     /* Reselect if waiting for it, or if reselection triggers an IRQ
@@ -743,9 +742,9 @@ static void lsi_transfer_data(SCSIRequest *req, uint32_t len)
     LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
     int out;
 
-    if (s->waiting == 1 || !s->current || req->tag != s->current->tag ||
+    if (s->waiting == 1 || !s->current || req->hba_private != s->current ||
         (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON))) {
-        if (lsi_queue_tag(s, req->tag, len)) {
+        if (lsi_queue_req(s, req, len)) {
             return;
         }
     }
@@ -789,7 +788,7 @@ 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 = scsi_req_new(dev, s->current->tag, s->current_lun);
+    s->current->req = scsi_req_new(dev, s->current_lun, s->current);
 
     n = scsi_req_enqueue(s->current->req, buf);
     if (n) {
hw/scsi-bus.c +11 -11
index ad6a730..d1fc481 100644
@@ -131,7 +131,7 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
     return res;
 }
 
-SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
+SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t lun, void *hba_private)
 {
     SCSIRequest *req;
 
@@ -139,16 +139,16 @@ SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t l
     req->refcount = 1;
     req->bus = scsi_bus_from_device(d);
     req->dev = d;
-    req->tag = tag;
     req->lun = lun;
+    req->hba_private = hba_private;
     req->status = -1;
-    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
+    trace_scsi_req_alloc(req->dev->id, req->lun, req->hba_private);
     return req;
 }
 
-SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun)
+SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t lun, void *hba_private)
 {
-    return d->info->alloc_req(d, tag, lun);
+    return d->info->alloc_req(d, lun, hba_private);
 }
 
 uint8_t *scsi_req_get_buf(SCSIRequest *req)
@@ -182,7 +182,7 @@ int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
 
 static void scsi_req_dequeue(SCSIRequest *req)
 {
-    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
+    trace_scsi_req_dequeue(req->dev->id, req->lun, req->hba_private);
     if (req->enqueued) {
         QTAILQ_REMOVE(&req->dev->requests, req, next);
         req->enqueued = false;
@@ -214,7 +214,7 @@ static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
         req->cmd.len = 12;
         break;
     default:
-        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
+        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->hba_private, cmd[0]);
         return -1;
     }
 
@@ -412,10 +412,10 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
     memcpy(req->cmd.buf, buf, req->cmd.len);
     scsi_req_xfer_mode(req);
     req->cmd.lba = scsi_req_lba(req);
-    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
+    trace_scsi_req_parsed(req->dev->id, req->lun, req->hba_private, buf[0],
                           req->cmd.mode, req->cmd.xfer);
     if (req->cmd.lba != -1) {
-        trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
+        trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->hba_private, buf[0],
                               req->cmd.lba);
     }
     return 0;
@@ -624,7 +624,7 @@ void scsi_req_unref(SCSIRequest *req)
    will start the next chunk or complete the command.  */
 void scsi_req_continue(SCSIRequest *req)
 {
-    trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
+    trace_scsi_req_continue(req->dev->id, req->lun, req->hba_private);
     if (req->cmd.mode == SCSI_XFER_TO_DEV) {
         req->dev->info->write_data(req);
     } else {
@@ -637,7 +637,7 @@ void scsi_req_continue(SCSIRequest *req)
    Once it completes, calling scsi_req_continue will restart I/O.  */
 void scsi_req_data(SCSIRequest *req, int len)
 {
-    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
+    trace_scsi_req_data(req->dev->id, req->lun, req->hba_private, len);
     req->bus->ops->transfer_data(req, len);
 }
 
hw/scsi-disk.c +2 -3
index a8c7372..3098b62 100644
@@ -80,14 +80,13 @@ struct SCSIDiskState
 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
 
-static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
-        uint32_t lun)
+static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t lun, void *hba_private)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
     SCSIRequest *req;
     SCSIDiskReq *r;
 
-    req = scsi_req_alloc(sizeof(SCSIDiskReq), &s->qdev, tag, lun);
+    req = scsi_req_alloc(sizeof(SCSIDiskReq), &s->qdev, lun, hba_private);
     r = DO_UPCAST(SCSIDiskReq, req, req);
     r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
     return req;
hw/scsi-generic.c +2 -2
index 8e59c7e..fa34ca3 100644
@@ -96,11 +96,11 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
     return size;
 }
 
-static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun)
+static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t lun, void *hba_private)
 {
     SCSIRequest *req;
 
-    req = scsi_req_alloc(sizeof(SCSIGenericReq), d, tag, lun);
+    req = scsi_req_alloc(sizeof(SCSIGenericReq), d, lun, hba_private);
     return req;
 }
 
hw/scsi.h +4 -4
index c1dca35..883309e 100644
@@ -31,7 +31,6 @@ struct SCSIRequest {
     SCSIBus           *bus;
     SCSIDevice        *dev;
     uint32_t          refcount;
-    uint32_t          tag;
     uint32_t          lun;
     uint32_t          status;
     struct {
@@ -43,6 +42,7 @@ struct SCSIRequest {
     } cmd;
     BlockDriverAIOCB  *aiocb;
     bool enqueued;
+    void *hba_private;
     QTAILQ_ENTRY(SCSIRequest) next;
 };
 
@@ -67,7 +67,7 @@ struct SCSIDeviceInfo {
     DeviceInfo qdev;
     scsi_qdev_initfn init;
     void (*destroy)(SCSIDevice *s);
-    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
+    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t lun, void *hba_private);
     void (*free_req)(SCSIRequest *req);
     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
     void (*read_data)(SCSIRequest *req);
@@ -138,8 +138,8 @@ extern const struct SCSISense sense_code_LUN_FAILURE;
 int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
 int scsi_sense_valid(SCSISense sense);
 
-SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
-SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun);
+SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t lun, void *hba_private);
+SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t lun, void *hba_private);
 int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
 void scsi_req_free(SCSIRequest *req);
 SCSIRequest *scsi_req_ref(SCSIRequest *req);
hw/spapr_vscsi.c +12 -29

121 lines not on this page · raw message

hw/usb-msd.c +5 -5
index 86582cc..4e2ea03 100644
@@ -216,8 +216,8 @@ static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
     MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
     USBPacket *p = s->packet;
 
-    if (req->tag != s->tag) {
-        fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
+    if (req->hba_private != s) {
+        fprintf(stderr, "usb-msd: Unexpected SCSI command 0x%p\n", req);
     }
 
     assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
@@ -241,8 +241,8 @@ static void usb_msd_command_complete(SCSIRequest *req, uint32_t status)
     MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
     USBPacket *p = s->packet;
 
-    if (req->tag != s->tag) {
-        fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
+    if (req->hba_private != s) {
+        fprintf(stderr, "usb-msd: Unexpected SCSI command 0x%p\n", req);
     }
     DPRINTF("Command complete %d\n", status);
     s->residue = s->data_len;
@@ -387,7 +387,7 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
                     s->tag, cbw.flags, cbw.cmd_len, s->data_len);
             s->residue = 0;
             s->scsi_len = 0;
-            s->req = scsi_req_new(s->scsi_dev, s->tag, 0);
+            s->req = scsi_req_new(s->scsi_dev, 0, s);
             scsi_req_enqueue(s->req, cbw.cmd);
             /* ??? Should check that USB and SCSI data transfer
                directions match.  */
trace-events +7 -7
index bebf612..8c52148 100644
@@ -226,13 +226,13 @@ disable usb_clear_device_feature(int addr, int feature, int ret) "dev %d, featur
 disable usb_set_device_feature(int addr, int feature, int ret) "dev %d, feature %d, ret %d"
 
 # hw/scsi-bus.c
-disable scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d"
-disable scsi_req_data(int target, int lun, int tag, int len) "target %d lun %d tag %d len %d"
-disable scsi_req_dequeue(int target, int lun, int tag) "target %d lun %d tag %d"
-disable scsi_req_continue(int target, int lun, int tag) "target %d lun %d tag %d"
-disable scsi_req_parsed(int target, int lun, int tag, int cmd, int mode, int xfer) "target %d lun %d tag %d command %d dir %d length %d"
-disable scsi_req_parsed_lba(int target, int lun, int tag, int cmd, uint64_t lba) "target %d lun %d tag %d command %d lba %"PRIu64""
-disable scsi_req_parse_bad(int target, int lun, int tag, int cmd) "target %d lun %d tag %d command %d"
+disable scsi_req_alloc(int target, int lun, void *private) "target %d lun %d private %p"
+disable scsi_req_data(int target, int lun, void *private, int len) "target %d lun %d private %p len %d"
+disable scsi_req_dequeue(int target, int lun, void *private) "target %d lun %d p %p"
+disable scsi_req_continue(int target, int lun, void *private) "target %d lun %d private %p"
+disable scsi_req_parsed(int target, int lun, void *private, int cmd, int mode, int xfer) "target %d lun %d private %p command %d dir %d length %d"
+disable scsi_req_parsed_lba(int target, int lun, void *private, int cmd, uint64_t lba) "target %d lun %d private %p command %d lba %"PRIu64""
+disable scsi_req_parse_bad(int target, int lun, void *private, int cmd) "target %d lun %d private %p command %d"
 
 # vl.c
 disable vm_state_notify(int running, int reason) "running %d reason %d"
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH v4 3/3 megasas: LSI Megaraid SAS emulation

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

---
 Makefile.objs           |    1 +
 default-configs/pci.mak |    1 +
 hw/megasas.c            | 1923 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/mfi.h                | 1197 +++++++++++++++++++++++++++++
 hw/pci_ids.h            |    3 +-
 5 files changed, 3124 insertions(+), 1 deletions(-)
 create mode 100644 hw/megasas.c
 create mode 100644 hw/mfi.h
Makefile.objs +1 -0
index cea15e4..6f5d113 100644
@@ -258,6 +258,7 @@ hw-obj-$(CONFIG_AHCI) += ide/ich.o
 
 # SCSI layer
 hw-obj-$(CONFIG_LSI_SCSI_PCI) += lsi53c895a.o
+hw-obj-$(CONFIG_MEGASAS_SCSI_PCI) += megasas.o
 hw-obj-$(CONFIG_ESP) += esp.o
 
 hw-obj-y += dma-helpers.o sysbus.o isa-bus.o
default-configs/pci.mak +1 -0
index 22bd350..fabb56c 100644
@@ -9,6 +9,7 @@ CONFIG_EEPRO100_PCI=y
 CONFIG_PCNET_PCI=y
 CONFIG_PCNET_COMMON=y
 CONFIG_LSI_SCSI_PCI=y
+CONFIG_MEGASAS_SCSI_PCI=y
 CONFIG_RTL8139_PCI=y
 CONFIG_E1000_PCI=y
 CONFIG_IDE_CORE=y
hw/megasas.c new file +1923 -0

1,923 lines not on this page · raw message

hw/mfi.h new file +1197 -0

1,063 lines not on this page · raw message

hw/pci_ids.h +2 -1
index d94578c..796aaf1 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.

#4 lore raw

Re: [PATCH 3/3] megasas: LSI Megaraid SAS emulation

Quoted 43 lines · Hannes Reinecke

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

---

Makefile.objs           |    1 +
default-configs/pci.mak |    1 +
hw/megasas.c            | 1923 +++++++++++++++++++++++++++++++++++++++++++++++
hw/mfi.h                | 1197 +++++++++++++++++++++++++++++
hw/pci_ids.h            |    3 +-
5 files changed, 3124 insertions(+), 1 deletions(-)

create mode 100644 hw/megasas.c
create mode 100644 hw/mfi.h

Makefile.objs +0 -0
index cea15e4..6f5d113 100644
@@ -258,6 +258,7 @@ hw-obj-$(CONFIG_AHCI) += ide/ich.o
 
 # SCSI layer

hw-obj-$(CONFIG_LSI_SCSI_PCI) += lsi53c895a.o +hw-obj-$(CONFIG_MEGASAS_SCSI_PCI) += megasas.o hw-obj-$(CONFIG_ESP) += esp.o

hw-obj-y += dma-helpers.o sysbus.o isa-bus.o

default-configs/pci.mak +0 -0
index 22bd350..fabb56c 100644
@@ -9,6 +9,7 @@ CONFIG_EEPRO100_PCI=y

CONFIG_PCNET_PCI=y
CONFIG_PCNET_COMMON=y
CONFIG_LSI_SCSI_PCI=y
+CONFIG_MEGASAS_SCSI_PCI=y
CONFIG_RTL8139_PCI=y
CONFIG_E1000_PCI=y
CONFIG_IDE_CORE=y

hw/megasas.c new file +0 -0
new file mode 100644
index 0000000..75f9be3

hw/megasas.c line 6

Quoted 6 lines
hw/megasas.c new file +6 -0
@@ -0,0 +1,1923 @@
+/*
+ * QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation
+ *
+ * Copyright (c) 2009-2011 Hannes Reinecke, SUSE Labs
+ *
+ * This code is licenced under the LGPL.

Please take a look at the license header of other LGPL code and just copy it :).

hw/megasas.c line 10

Quoted 4 lines
hw/megasas.c new file +4 -0
+ */
+
+#include <time.h>
+#include <assert.h>

Are you sure you need to manually include those?

hw/megasas.c line 20

Quoted 10 lines
hw/megasas.c new file +10 -0
+
+#include "hw.h"
+#include "pci.h"
+#include "dma.h"
+#include "iov.h"
+#include "scsi.h"
+#include "scsi-defs.h"
+#include "block_int.h"
+#ifdef __linux__
+# include <scsi/sg.h>

Is this really necessary? Device code shouldn't be host dependent IMHO. I also haven't found any user of this in the actual code, so it might be as easy as merely removing the include :).

Quoted 69 lines
hw/megasas.c new file +69 -0
+#endif
+
+#include "mfi.h"
+
+#define DEBUG_MEGASAS
+#undef DEBUG_MEGASAS_REG
+#undef DEBUG_MEGASAS_QUEUE
+#undef DEBUG_MEGASAS_MFI
+#undef DEBUG_MEGASAS_IO
+#undef DEBUG_MEGASAS_DCMD
+
+#ifdef DEBUG_MEGASAS
+#define DPRINTF(fmt, ...) \
+do { printf("megasas: " fmt , ## __VA_ARGS__); } while (0)
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "megasas: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
+#ifdef DEBUG_MEGASAS_REG
+#define DPRINTF_REG DPRINTF
+#else
+#define DPRINTF_REG(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_QUEUE
+#define DPRINTF_QUEUE DPRINTF
+#else
+#define DPRINTF_QUEUE(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_MFI
+#define DPRINTF_MFI DPRINTF
+#else
+#define DPRINTF_MFI(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_IO
+#define DPRINTF_IO DPRINTF
+#else
+#define DPRINTF_IO(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_DCMD
+#define DPRINTF_DCMD DPRINTF
+#else
+#define DPRINTF_DCMD(fmt, ...) do {} while(0)
+#endif
+#else
+#define DPRINTF(fmt, ...) do {} while(0)
+#define DPRINTF_REG DPRINTF
+#define DPRINTF_QUEUE DPRINTF
+#define DPRINTF_MFI DPRINTF
+#define DPRINTF_IO DPRINTF
+#define DPRINTF_DCMD DPRINTF
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "megasas: error: " fmt , ## __VA_ARGS__);} while (0)
+#endif
+
+/* Static definitions */
+#define MEGASAS_VERSION "1.20"
+#define MEGASAS_MAX_FRAMES 2048         /* Firmware limit at 65535 */
+#define MEGASAS_DEFAULT_FRAMES 1000     /* Windows requires this */
+#define MEGASAS_MAX_SGE 256             /* Firmware limit */
+#define MEGASAS_DEFAULT_SGE 80
+#define MEGASAS_MAX_SECTORS 0xFFFF      /* No real limit */
+#define MEGASAS_MAX_ARRAYS 128
+
+const char *mfi_frame_desc[] = {
+    "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
+    "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
+
+struct megasas_cmd_t {
+    int index;
+    int context;
+    int count;

hw/megasas.c line 99

Quoted 10 lines
hw/megasas.c new file +10 -0
+
+    target_phys_addr_t pa;
+    target_phys_addr_t pa_size;
+    union mfi_frame *frame;
+    SCSIRequest *req;
+    struct iovec *iov;
+    void *iov_buf;
+    long iov_cnt;
+    long iov_size;
+    long iov_offset;

Why would anything be a long? It's either target_ulong or uintXX_t for device code usually :).

Quoted 61 lines
hw/megasas.c new file +61 -0
+    SCSIDevice *sdev;
+    struct megasas_state_t *state;
+};
+
+typedef struct megasas_state_t {
+    PCIDevice dev;
+    int mmio_io_addr;
+    int io_addr;
+    int queue_addr;
+    uint32_t frame_hi;
+
+    int fw_state;
+    uint32_t fw_sge;
+    uint32_t fw_cmds;
+    int fw_luns;
+    int intr_mask;
+    int doorbell;
+    int busy;
+    char *raid_mode_str;
+    int is_jbod;
+
+    int event_count;
+    int shutdown_event;
+    int boot_event;
+
+    uint64_t reply_queue_pa;
+    void *reply_queue;
+    int reply_queue_len;
+    int reply_queue_index;
+    uint64_t consumer_pa;
+    uint64_t producer_pa;
+
+    struct megasas_cmd_t frames[MEGASAS_MAX_FRAMES];
+
+    SCSIBus bus;
+} MPTState;
+
+#define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
+
+#define MEGASAS_INTR_ENABLED(s) (((s)->intr_mask & MEGASAS_INTR_DISABLED_MASK ) != MEGASAS_INTR_DISABLED_MASK)
+
+#define megasas_frame_set_cmd_status(f,v)                               \
+    stb_phys((f) + offsetof(struct mfi_frame_header, cmd_status), v);
+
+#define megasas_frame_set_scsi_status(f,v)                              \
+    stb_phys((f) + offsetof(struct mfi_frame_header, scsi_status), v);
+
+#define megasas_frame_get_cmd(f)                                        \
+    ldub_phys((f) + offsetof(struct mfi_frame_header, frame_cmd))
+
+#define megasas_frame_get_context(f)                                    \
+    ldl_phys(frame_addr + offsetof(struct mfi_frame_header, context));
+
+static void megasas_soft_reset(MPTState *s);
+
+static int megasas_map_sgl(struct megasas_cmd_t *cmd, int pa_offset)
+{
+    int i;
+    uint16_t flags = le16_to_cpu(cmd->frame->header.flags);
+    int is_sgl64 = (flags & MFI_FRAME_SGL64) ? 1 : 0;
+    int is_write = (flags & MFI_FRAME_DIR_WRITE) ? 1 : 0;

hw/megasas.c line 170

Quoted 10 lines
hw/megasas.c new file +10 -0
+    int sgl_addr_size = is_sgl64 ? sizeof(uint64_t) : sizeof(uint32_t);
+    size_t iov_count = 0;
+
+    cmd->iov = qemu_malloc(sizeof(struct iovec) * (cmd->frame->header.sge_count + 1));
+    for (i = 0; i < cmd->frame->header.sge_count; i++) {
+        target_phys_addr_t pa, iov_pa, iov_size;
+
+        pa = cmd->pa + pa_offset;
+        if (is_sgl64)
+            iov_pa = ldq_phys(pa);

Could you please send your patch through scripts/checkpatch.pl, so that coding style issues don't hold up the commit process?

[...]

hw/megasas.c line 1337

Quoted 8 lines
hw/megasas.c new file +8 -0
+
+static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
+{
+    struct megasas_cmd_t *cmd;
+    uint8_t *buf;
+
+    cmd = req->hba_private;
+    if (!cmd) {

Is this still necessary with the new pointer infrastructure?

Quoted 42 lines
hw/megasas.c new file +42 -0
+        /*
+	 * Bad. A command has been completed but we couldn't find it.
+	 * Only safe way out of here is to terminate everything and
+	 * hope the HBA recovers.
+	 */
+        DPRINTF("SCSI request %p not found", req);
+        return;
+    }
+
+    DPRINTF_IO("%s req %p cmd %p lun %p xfer completed, len %u\n",
+               mfi_frame_desc[cmd->frame->header.frame_cmd], req, cmd,
+               cmd->sdev, len);
+
+    if (len) {
+        uint16_t flags = le16_to_cpu(cmd->frame->header.flags);
+        int is_write = (flags & MFI_FRAME_DIR_WRITE) ? 1 : 0;
+        size_t bytes;
+
+        buf = scsi_req_get_buf(req);
+        if (is_write) {
+            DPRINTF_IO("%s req %p cmd %p lun %p write finished, left %u\n",
+                       mfi_frame_desc[cmd->frame->header.frame_cmd], req,
+                       cmd, cmd->sdev, len);
+            bytes = iov_to_buf(cmd->iov, cmd->iov_cnt, buf,
+                               cmd->iov_offset, len);
+            if (bytes != len) {
+                len = bytes;
+            }
+            cmd->iov_offset += bytes;
+        } else {
+            DPRINTF_IO("%s req %p cmd %p lun %p read finished, len %u\n",
+                       mfi_frame_desc[cmd->frame->header.frame_cmd], req,
+                       cmd, cmd->sdev, len);
+            bytes = iov_from_buf(cmd->iov, cmd->iov_cnt, buf,
+                                 cmd->iov_offset, len);
+            if (bytes != len) {
+                len = bytes;
+            }
+            cmd->iov_offset += bytes;
+        }
+    }
+    cmd->iov_size -= len;

hw/megasas.c line 1389

Quoted 10 lines
hw/megasas.c new file +10 -0
+    scsi_req_continue(req);
+}
+
+static void megasas_command_complete(SCSIRequest *req, uint32_t status)
+{
+    struct megasas_cmd_t *cmd;
+    uint8_t cmd_status = MFI_STAT_OK;
+
+    cmd = req->hba_private;
+    if (!cmd) {

same here

[...]

hw/pci_ids.h line 15

Quoted 9 lines
hw/pci_ids.h +0 -1
index d94578c..796aaf1 100644
@@ -12,9 +12,9 @@
 
 #define PCI_BASE_CLASS_STORAGE           0x01
 #define PCI_BASE_CLASS_NETWORK           0x02
-

I don't think this is intentional, no?

hw/pci_ids.h line 50

Quoted 11 lines
hw/pci_ids.h +2 -0
 #define PCI_CLASS_STORAGE_SCSI           0x0100
 #define PCI_CLASS_STORAGE_IDE            0x0101
+#define PCI_CLASS_STORAGE_RAID           0x0104
 #define PCI_CLASS_STORAGE_SATA           0x0106
 #define PCI_CLASS_STORAGE_OTHER          0x0180
 
@@ -47,6 +47,7 @@
 
 #define PCI_VENDOR_ID_LSI_LOGIC          0x1000
 #define PCI_DEVICE_ID_LSI_53C895A        0x0012
+#define PCI_DEVICE_ID_LSI_SAS1078        0x0060

Michael, these go to your table :)

Alex

#5 lore raw

Re: [PATCH 3/3] megasas: LSI Megaraid SAS emulation

Quoted 308 lines · Alexander Graf
Quoted 43 lines · Hannes Reinecke

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

---

Makefile.objs           |    1 +
default-configs/pci.mak |    1 +
hw/megasas.c            | 1923 +++++++++++++++++++++++++++++++++++++++++++++++
hw/mfi.h                | 1197 +++++++++++++++++++++++++++++
hw/pci_ids.h            |    3 +-
5 files changed, 3124 insertions(+), 1 deletions(-)

create mode 100644 hw/megasas.c
create mode 100644 hw/mfi.h

Makefile.objs +0 -0
index cea15e4..6f5d113 100644
@@ -258,6 +258,7 @@ hw-obj-$(CONFIG_AHCI) += ide/ich.o
 
 # SCSI layer

hw-obj-$(CONFIG_LSI_SCSI_PCI) += lsi53c895a.o +hw-obj-$(CONFIG_MEGASAS_SCSI_PCI) += megasas.o hw-obj-$(CONFIG_ESP) += esp.o

hw-obj-y += dma-helpers.o sysbus.o isa-bus.o

default-configs/pci.mak +0 -0
index 22bd350..fabb56c 100644
@@ -9,6 +9,7 @@ CONFIG_EEPRO100_PCI=y

CONFIG_PCNET_PCI=y
CONFIG_PCNET_COMMON=y
CONFIG_LSI_SCSI_PCI=y
+CONFIG_MEGASAS_SCSI_PCI=y
CONFIG_RTL8139_PCI=y
CONFIG_E1000_PCI=y
CONFIG_IDE_CORE=y

hw/megasas.c new file +0 -0
new file mode 100644
index 0000000..75f9be3

hw/megasas.c line 6

Quoted 6 lines
hw/megasas.c new file +6 -0
@@ -0,0 +1,1923 @@
+/*
+ * QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation
+ *
+ * Copyright (c) 2009-2011 Hannes Reinecke, SUSE Labs
+ *
+ * This code is licenced under the LGPL.

Please take a look at the license header of other LGPL code and just copy it :).

hw/megasas.c line 10

Quoted 4 lines
hw/megasas.c new file +4 -0
+ */
+
+#include <time.h>
+#include <assert.h>

Are you sure you need to manually include those?

hw/megasas.c line 20

Quoted 10 lines
hw/megasas.c new file +10 -0
+
+#include "hw.h"
+#include "pci.h"
+#include "dma.h"
+#include "iov.h"
+#include "scsi.h"
+#include "scsi-defs.h"
+#include "block_int.h"
+#ifdef __linux__
+# include <scsi/sg.h>

Is this really necessary? Device code shouldn't be host dependent IMHO. I also haven't found any user of this in the actual code, so it might be as easy as merely removing the include :).

Quoted 69 lines
hw/megasas.c new file +69 -0
+#endif
+
+#include "mfi.h"
+
+#define DEBUG_MEGASAS
+#undef DEBUG_MEGASAS_REG
+#undef DEBUG_MEGASAS_QUEUE
+#undef DEBUG_MEGASAS_MFI
+#undef DEBUG_MEGASAS_IO
+#undef DEBUG_MEGASAS_DCMD
+
+#ifdef DEBUG_MEGASAS
+#define DPRINTF(fmt, ...) \
+do { printf("megasas: " fmt , ## __VA_ARGS__); } while (0)
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "megasas: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
+#ifdef DEBUG_MEGASAS_REG
+#define DPRINTF_REG DPRINTF
+#else
+#define DPRINTF_REG(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_QUEUE
+#define DPRINTF_QUEUE DPRINTF
+#else
+#define DPRINTF_QUEUE(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_MFI
+#define DPRINTF_MFI DPRINTF
+#else
+#define DPRINTF_MFI(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_IO
+#define DPRINTF_IO DPRINTF
+#else
+#define DPRINTF_IO(fmt, ...) do {} while(0)
+#endif
+#ifdef DEBUG_MEGASAS_DCMD
+#define DPRINTF_DCMD DPRINTF
+#else
+#define DPRINTF_DCMD(fmt, ...) do {} while(0)
+#endif
+#else
+#define DPRINTF(fmt, ...) do {} while(0)
+#define DPRINTF_REG DPRINTF
+#define DPRINTF_QUEUE DPRINTF
+#define DPRINTF_MFI DPRINTF
+#define DPRINTF_IO DPRINTF
+#define DPRINTF_DCMD DPRINTF
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "megasas: error: " fmt , ## __VA_ARGS__);} while (0)
+#endif
+
+/* Static definitions */
+#define MEGASAS_VERSION "1.20"
+#define MEGASAS_MAX_FRAMES 2048         /* Firmware limit at 65535 */
+#define MEGASAS_DEFAULT_FRAMES 1000     /* Windows requires this */
+#define MEGASAS_MAX_SGE 256             /* Firmware limit */
+#define MEGASAS_DEFAULT_SGE 80
+#define MEGASAS_MAX_SECTORS 0xFFFF      /* No real limit */
+#define MEGASAS_MAX_ARRAYS 128
+
+const char *mfi_frame_desc[] = {
+    "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
+    "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
+
+struct megasas_cmd_t {
+    int index;
+    int context;
+    int count;

hw/megasas.c line 99

Quoted 10 lines
hw/megasas.c new file +10 -0
+
+    target_phys_addr_t pa;
+    target_phys_addr_t pa_size;
+    union mfi_frame *frame;
+    SCSIRequest *req;
+    struct iovec *iov;
+    void *iov_buf;
+    long iov_cnt;
+    long iov_size;
+    long iov_offset;

Why would anything be a long? It's either target_ulong or uintXX_t for device code usually :).

Quoted 61 lines
hw/megasas.c new file +61 -0
+    SCSIDevice *sdev;
+    struct megasas_state_t *state;
+};
+
+typedef struct megasas_state_t {
+    PCIDevice dev;
+    int mmio_io_addr;
+    int io_addr;
+    int queue_addr;
+    uint32_t frame_hi;
+
+    int fw_state;
+    uint32_t fw_sge;
+    uint32_t fw_cmds;
+    int fw_luns;
+    int intr_mask;
+    int doorbell;
+    int busy;
+    char *raid_mode_str;
+    int is_jbod;
+
+    int event_count;
+    int shutdown_event;
+    int boot_event;
+
+    uint64_t reply_queue_pa;
+    void *reply_queue;
+    int reply_queue_len;
+    int reply_queue_index;
+    uint64_t consumer_pa;
+    uint64_t producer_pa;
+
+    struct megasas_cmd_t frames[MEGASAS_MAX_FRAMES];
+
+    SCSIBus bus;
+} MPTState;
+
+#define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
+
+#define MEGASAS_INTR_ENABLED(s) (((s)->intr_mask & MEGASAS_INTR_DISABLED_MASK ) != MEGASAS_INTR_DISABLED_MASK)
+
+#define megasas_frame_set_cmd_status(f,v)                               \
+    stb_phys((f) + offsetof(struct mfi_frame_header, cmd_status), v);
+
+#define megasas_frame_set_scsi_status(f,v)                              \
+    stb_phys((f) + offsetof(struct mfi_frame_header, scsi_status), v);
+
+#define megasas_frame_get_cmd(f)                                        \
+    ldub_phys((f) + offsetof(struct mfi_frame_header, frame_cmd))
+
+#define megasas_frame_get_context(f)                                    \
+    ldl_phys(frame_addr + offsetof(struct mfi_frame_header, context));
+
+static void megasas_soft_reset(MPTState *s);
+
+static int megasas_map_sgl(struct megasas_cmd_t *cmd, int pa_offset)
+{
+    int i;
+    uint16_t flags = le16_to_cpu(cmd->frame->header.flags);
+    int is_sgl64 = (flags & MFI_FRAME_SGL64) ? 1 : 0;
+    int is_write = (flags & MFI_FRAME_DIR_WRITE) ? 1 : 0;

hw/megasas.c line 170

Quoted 10 lines
hw/megasas.c new file +10 -0
+    int sgl_addr_size = is_sgl64 ? sizeof(uint64_t) : sizeof(uint32_t);
+    size_t iov_count = 0;
+
+    cmd->iov = qemu_malloc(sizeof(struct iovec) * (cmd->frame->header.sge_count + 1));
+    for (i = 0; i < cmd->frame->header.sge_count; i++) {
+        target_phys_addr_t pa, iov_pa, iov_size;
+
+        pa = cmd->pa + pa_offset;
+        if (is_sgl64)
+            iov_pa = ldq_phys(pa);

Could you please send your patch through scripts/checkpatch.pl, so that coding style issues don't hold up the commit process?

[...]

hw/megasas.c line 1337

Quoted 8 lines
hw/megasas.c new file +8 -0
+
+static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
+{
+    struct megasas_cmd_t *cmd;
+    uint8_t *buf;
+
+    cmd = req->hba_private;
+    if (!cmd) {

Is this still necessary with the new pointer infrastructure?

Quoted 42 lines
hw/megasas.c new file +42 -0
+        /*
+	 * Bad. A command has been completed but we couldn't find it.
+	 * Only safe way out of here is to terminate everything and
+	 * hope the HBA recovers.
+	 */
+        DPRINTF("SCSI request %p not found", req);
+        return;
+    }
+
+    DPRINTF_IO("%s req %p cmd %p lun %p xfer completed, len %u\n",
+               mfi_frame_desc[cmd->frame->header.frame_cmd], req, cmd,
+               cmd->sdev, len);
+
+    if (len) {
+        uint16_t flags = le16_to_cpu(cmd->frame->header.flags);
+        int is_write = (flags & MFI_FRAME_DIR_WRITE) ? 1 : 0;
+        size_t bytes;
+
+        buf = scsi_req_get_buf(req);
+        if (is_write) {
+            DPRINTF_IO("%s req %p cmd %p lun %p write finished, left %u\n",
+                       mfi_frame_desc[cmd->frame->header.frame_cmd], req,
+                       cmd, cmd->sdev, len);
+            bytes = iov_to_buf(cmd->iov, cmd->iov_cnt, buf,
+                               cmd->iov_offset, len);
+            if (bytes != len) {
+                len = bytes;
+            }
+            cmd->iov_offset += bytes;
+        } else {
+            DPRINTF_IO("%s req %p cmd %p lun %p read finished, len %u\n",
+                       mfi_frame_desc[cmd->frame->header.frame_cmd], req,
+                       cmd, cmd->sdev, len);
+            bytes = iov_from_buf(cmd->iov, cmd->iov_cnt, buf,
+                                 cmd->iov_offset, len);
+            if (bytes != len) {
+                len = bytes;
+            }
+            cmd->iov_offset += bytes;
+        }
+    }
+    cmd->iov_size -= len;

hw/megasas.c line 1389

Quoted 10 lines
hw/megasas.c new file +10 -0
+    scsi_req_continue(req);
+}
+
+static void megasas_command_complete(SCSIRequest *req, uint32_t status)
+{
+    struct megasas_cmd_t *cmd;
+    uint8_t cmd_status = MFI_STAT_OK;
+
+    cmd = req->hba_private;
+    if (!cmd) {

same here

[...]

Quoted 6 lines
hw/pci_ids.h +0 -0
index d94578c..796aaf1 100644
@@ -12,9 +12,9 @@
 
Quoted 4 lines

hw/pci_ids.h line 15

Quoted 3 lines
hw/pci_ids.h +0 -1
 #define PCI_BASE_CLASS_STORAGE           0x01
 #define PCI_BASE_CLASS_NETWORK           0x02
-

Yes this separates base class (1 byte) from class
word codes.

Quoted 18 lines

I don't think this is intentional, no?

hw/pci_ids.h line 50

Quoted 11 lines
hw/pci_ids.h +2 -0
 #define PCI_CLASS_STORAGE_SCSI           0x0100
 #define PCI_CLASS_STORAGE_IDE            0x0101
+#define PCI_CLASS_STORAGE_RAID           0x0104
 #define PCI_CLASS_STORAGE_SATA           0x0106
 #define PCI_CLASS_STORAGE_OTHER          0x0180
 
@@ -47,6 +47,7 @@
 
 #define PCI_VENDOR_ID_LSI_LOGIC          0x1000
 #define PCI_DEVICE_ID_LSI_53C895A        0x0012
+#define PCI_DEVICE_ID_LSI_SAS1078        0x0060

Michael, these go to your table :)

Alex

maintainer
#6 lore raw

Re: [PATCH 2/3] scsi: replace 'tag' with 'hba_private' pointer

In reply to Hannes Reinecke
Quoted 5 lines · Hannes Reinecke

'tag' is just an abstraction to identify the command from the driver. So we should make that explicit by replacing 'tag' with a driver-defined pointer 'hba_private'. This saves the lookup for driver handling several commands in parallel.

This makes tracing a bit harder to follow. Perhaps you can keep the transport tag (a uint64_t) in the SCSIRequest for debugging purposes?

Quoted 18 lines
---
  hw/esp.c          |    2 +-
  hw/lsi53c895a.c   |   17 ++++++++---------
  hw/scsi-bus.c     |   22 +++++++++++-----------
  hw/scsi-disk.c    |    5 ++---
  hw/scsi-generic.c |    4 ++--
  hw/scsi.h         |    8 ++++----
  hw/spapr_vscsi.c  |   41 ++++++++++++-----------------------------
  hw/usb-msd.c      |   10 +++++-----
  trace-events      |   14 +++++++-------
  9 files changed, 52 insertions(+), 71 deletions(-)
hw/esp.c +0 -0
index 6d3f5d2..912ff89 100644

hw/esp.c static void do_busid_cmd(ESPState *s, uint8_t *buf, uint8_t busid) · line 247

Quoted 5 lines
hw/esp.c +1 -1
@@ -244,7 +244,7 @@ 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;
-    s->current_req = scsi_req_new(s->current_dev, 0, lun);
+    s->current_req = scsi_req_new(s->current_dev, lun, s);

Might as well pass NULL here. The hba_private value is basically unnecessary when the adapter doesn't support tagged command queuing.

hw/usb-msd.c static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) · line 221

Quoted 13 lines
hw/usb-msd.c +2 -2
index 86582cc..4e2ea03 100644
@@ -216,8 +216,8 @@ static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
      MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
      USBPacket *p = s->packet;
 
-    if (req->tag != s->tag) {
-        fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
+    if (req->hba_private != s) {
+        fprintf(stderr, "usb-msd: Unexpected SCSI command 0x%p\n", req);
      }

Same here, just pass NULL and remove these ifs.

Otherwise looks like a very good idea.

Paolo

#7 lore raw

Re: [PATCH 2/3] scsi: replace 'tag' with 'hba_private' pointer

Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Hannes Reinecke

'tag' is just an abstraction to identify the command from the driver. So we should make that explicit by replacing 'tag' with a driver-defined pointer 'hba_private'. This saves the lookup for driver handling several commands in parallel.

This makes tracing a bit harder to follow. Perhaps you can keep the transport tag (a uint64_t) in the SCSIRequest for debugging purposes?

Sure. Anything to get the patches accepted :-)

Quoted 45 lines
Quoted 18 lines

---
hw/esp.c | 2 +-
hw/lsi53c895a.c | 17 ++++++++---------
hw/scsi-bus.c | 22 +++++++++++----------- hw/scsi-disk.c | 5 ++---
hw/scsi-generic.c | 4 ++--
hw/scsi.h | 8 ++++----
hw/spapr_vscsi.c | 41 ++++++++++++----------------------------- hw/usb-msd.c | 10 +++++-----
trace-events | 14 +++++++-------

9 files changed, 52 insertions(+), 71 deletions(-)
hw/esp.c +0 -0
index 6d3f5d2..912ff89 100644

hw/esp.c static void do_busid_cmd(ESPState *s, uint8_t · line 248

Quoted 6 lines
hw/esp.c +1 -1
@@ -244,7 +244,7 @@ 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;
- s->current_req = scsi_req_new(s->current_dev, 0, lun);
+ s->current_req = scsi_req_new(s->current_dev, lun, s);

Might as well pass NULL here. The hba_private value is basically unnecessary when the adapter doesn't support tagged command queuing.

Quoted 5 lines
hw/usb-msd.c +0 -0
index 86582cc..4e2ea03 100644

hw/usb-msd.c static void usb_msd_transfer_data(SCSIRequest · line 222

Quoted 9 lines
hw/usb-msd.c +2 -2
@@ -216,8 +216,8 @@ static void usb_msd_transfer_data(SCSIRequest
 *req, uint32_t len)
 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
 USBPacket *p = s->packet;
 
- if (req->tag != s->tag) {
- fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
+ if (req->hba_private != s) {
+ fprintf(stderr, "usb-msd: Unexpected SCSI command 0x%p\n", req);
 }

Same here, just pass NULL and remove these ifs.

Quoted 2 lines

Otherwise looks like a very good idea.

Ok, I'll be resending both.

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: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
#8 lore raw

Re: [PATCH 2/3] scsi: replace 'tag' with 'hba_private' pointer

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Hannes Reinecke

'tag' is just an abstraction to identify the command from the driver. So we should make that explicit by replacing 'tag' with a driver-defined pointer 'hba_private'. This saves the lookup for driver handling several commands in parallel.

This makes tracing a bit harder to follow. Perhaps you can keep the transport tag (a uint64_t) in the SCSIRequest for debugging purposes?

Hmm. The transport tag wouldn't have any meaning outside scsi-bus.c.
And it's a 64-bit value. So why can't we use the hba_private pointer directly here?
After some I/O has been ongoing the linear 'tag' number becomes unreadable very quickly, so there's not much difference 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: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
maintainer
#9 lore raw

Re: [PATCH 2/3] scsi: replace 'tag' with 'hba_private' pointer

Quoted 8 lines · Hannes Reinecke
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Hannes Reinecke

'tag' is just an abstraction to identify the command from the driver. So we should make that explicit by replacing 'tag' with a driver-defined pointer 'hba_private'. This saves the lookup for driver handling several commands in parallel.

Quoted 4 lines
Quoted 3 lines

This makes tracing a bit harder to follow. Perhaps you can keep the transport tag (a uint64_t) in the SCSIRequest for debugging purposes?

Hmm. The transport tag wouldn't have any meaning outside scsi-bus.c.

It depends, in vmw_pvscsi I take it from a field in the request block that is 0..255. So either you have a small tag that is recycled but stays nice, or a large tag that is unwieldy but should not be recycled ever. A pointer is unwieldy _and_ is recycled, so it gives the worse of both worlds.

But I'm not very attached to this, I may even do it myself if/when I find the need. Won't ack yet because of the nit with ESP/USB, but even if you do not bother I will ack the next respin.

Paolo

#10 lore raw

Re: [PATCH 1/3] iov: Add 'offset' parameter to iov_to_buf()

In reply to Hannes Reinecke
Quoted 2 lines · Hannes Reinecke

Occasionally, the buffer needs to be placed at a offset within the iovec when copying the buffer to the iovec.

So this is a buffer into the iovec, right? Wouldn't it make sense to also modify iov_to_buf respectively then, so the API stays similar? Also, it'd be nice to give the parameter a more obvious name, so potential users can easily recognize what it offsets.

Alex

Quoted 91 lines

---

hw/virtio-net.c        |    2 +-
hw/virtio-serial-bus.c |    2 +-
iov.c                  |   23 ++++++++++++++---------
iov.h                  |    2 +-
4 files changed, 17 insertions(+), 12 deletions(-)
hw/virtio-net.c +1 -1
index 6997e02..a32cc01 100644
@@ -657,7 +657,7 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
 
        /* copy in packet.  ugh */
        len = iov_from_buf(sg, elem.in_num,
-                           buf + offset, size - offset);
+                           buf + offset, 0, size - offset);
        total += len;
        offset += len;
        /* If buffers can't be merged, at this point we
hw/virtio-serial-bus.c +1 -1
index 7f6db7b..53c58d0 100644
@@ -103,7 +103,7 @@ static size_t write_to_port(VirtIOSerialPort *port,
        }
 
        len = iov_from_buf(elem.in_sg, elem.in_num,
-                           buf + offset, size - offset);
+                           buf + offset, 0, size - offset);
        offset += len;
 
        virtqueue_push(vq, &elem, len);
iov.c +14 -9
index 588cd04..9ead6ee 100644
@@ -15,21 +15,26 @@
 #include "iov.h"
 
 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
-                    const void *buf, size_t size)
+                    const void *buf, size_t offset, size_t size)
 {
-    size_t offset;
+    size_t iov_off, buf_off;
    unsigned int i;
 
-    offset = 0;
-    for (i = 0; offset < size && i < iovcnt; i++) {
-        size_t len;
+    iov_off = 0;
+    buf_off = 0;
+    for (i = 0; i < iovcnt && size; i++) {
+        if (offset < (iov_off + iov[i].iov_len)) {
+            size_t len = MIN((iov_off + iov[i].iov_len) - offset, size);
 
-        len = MIN(iov[i].iov_len, size - offset);
+            memcpy(iov[i].iov_base + (offset - iov_off), buf + buf_off, len);
 
-        memcpy(iov[i].iov_base, buf + offset, len);
-        offset += len;
+            buf_off += len;
+            offset += len;
+            size -= len;
+        }
+        iov_off += iov[i].iov_len;
    }
-    return offset;
+    return buf_off;
 }
 
 size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
iov.h +1 -1
index 60a8547..2677527 100644
@@ -13,7 +13,7 @@
 #include "qemu-common.h"
 
 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
-                    const void *buf, size_t size);
+                    const void *buf, size_t offset, size_t size);
 size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
                  void *buf, size_t offset, size_t size);
 size_t iov_size(const struct iovec *iov, const unsigned int iovcnt);
Signature · 1 line
1.6.0.2
#11 lore raw

Re: [PATCH 1/3] iov: Add 'offset' parameter to iov_to_buf()

Quoted 8 lines · Alexander Graf
Quoted 2 lines · Hannes Reinecke

Occasionally, the buffer needs to be placed at a offset within the iovec when copying the buffer to the iovec.

So this is a buffer into the iovec, right? Wouldn't it make sense to also modify iov_to_buf respectively then, so the API stays similar?

Ahem. That's exactly what the patch does. Except from the mixed-up subject.

iov_to_buff() has an offset parameter, iov_from_buf() has not. For no obvious reasons.

Quoted 3 lines

Also, it'd be nice to give the parameter a more obvious name, so potential users can easily recognize what it offsets.

Yes, that sounds reasonable.

What about 'iov_off' ?
(And possibly rename 'iovcnt' to 'iov_cnt' for consistency ?)

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: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
#12 lore raw

Re: [PATCH 1/3] iov: Add 'offset' parameter to iov_to_buf()

Quoted 7 lines · Hannes Reinecke
Quoted 6 lines · Alexander Graf
Quoted 2 lines · Hannes Reinecke

Occasionally, the buffer needs to be placed at a offset within the iovec when copying the buffer to the iovec.

Quoted 7 lines
Quoted 2 lines

So this is a buffer into the iovec, right? Wouldn't it make sense to also modify iov_to_buf respectively then, so the API stays similar?

Ahem. That's exactly what the patch does. Except from the mixed-up subject.

iov_to_buff() has an offset parameter, iov_from_buf() has not. For no obvious reasons.

Ah, I see. Please state this in your patch description :). Makes it a lot easier to understand the rationale that you're merely moving the "from" API towards the same parameters as to "to" one.

Quoted 8 lines
Quoted 4 lines

Also, it'd be nice to give the parameter a more obvious name, so potential users can easily recognize what it offsets.

Yes, that sounds reasonable.

What about 'iov_off' ?
(And possibly rename 'iovcnt' to 'iov_cnt' for consistency ?)

Yup, that'd be a lot more readable :)

Alex

maintainer
#13 lore raw

Re: [PATCH 1/3] iov: Add 'offset' parameter to iov_to_buf()

In reply to Hannes Reinecke

iov.c line 18

Quoted 3 lines · Hannes Reinecke

size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,

iov.c +1 -1
-                    const void *buf, size_t size)
+                    const void *buf, size_t offset, size_t size)

Wrong commit subject, it seems. :)

Paolo

#14 lore raw

Re: [PATCH 1/3] iov: Add 'offset' parameter to iov_to_buf()

Quoted 7 lines · Paolo Bonzini
Quoted 3 lines · Hannes Reinecke

size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
- const void *buf, size_t size)
+ const void *buf, size_t offset, size_t size)

Wrong commit subject, it seems. :)

Bummer.

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: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)