Megasas HBA emulation
PATCH v6 5/5 megasas: LSI Megaraid SAS emulation
In reply to Hannes ReineckeThis patch adds an emulation for the LSI Megaraid SAS 8708EM2 HBA.
--- Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/megasas.c | 1950 +++++++++++++++++++++++++++++++++++++++++++++++ hw/mfi.h | 1197 +++++++++++++++++++++++++++++ hw/pci_ids.h | 3 +- trace-events | 71 ++ 6 files changed, 3222 insertions(+), 1 deletions(-) create mode 100644 hw/megasas.c create mode 100644 hw/mfi.h
Makefile.objs +1 -0
@@ -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
@@ -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 +1950 -0
1,950 lines not on this page · raw message
hw/mfi.h new file +1197 -0
1,036 lines not on this page · raw message
trace-events +71 -0
71 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.7.3.4
Part of the diff is not shown here. View the raw message.
v6 cover Megasas HBA emulation
Hi all,
as Alex Graf reminded me the driver needed some more bugfixing to be done. I've found some issues and also moved the megasas emulation over to the new trace infrastructure. Driver works for me now and a full installation of openSUSE-12.1 works perfectly.
I've also included the fixes suggested by Stefan Hajnoczi. And during debugging I've found two minor issues in scsi_disk.c
Changes since v5:
- scsi-disk: Fixup debugging statement
A debugging statement wasn't converted. Do so now.
- scsi-disk: Mask out serial number EVPD
The 'serial' parameter to scsi-disk is optional. So if it's not set we should mask it out in the list of supported EVPD pages and not return '0' here.
- megasas: Use tracing infrastructure instead of DPRINTF
- megasas: Use new PCI infrastructure
- megasas: Check for iovec mapping failure cpu_map_physical_memory() might fail, so we need to check for it when mapping iovecs.
- megasas: Trace scsi buffer overflow
The transfer length as specified in the SCSI command might disagree with the length of the iovec. We should be tracing these issues.
- megasas: Reset frames after init firmware
When receiving an INIT FIRMWARE command we need reset all frames, otherwise some frames might point to invalid memory.
Chances since v4:
- iov: Update parameter usage in iov_(to|from)_buf() Updated description for the first patch and clarified the usage Renamed arguments for io_XXX for clarification
- scsi: Add 'hba_private' to SCSIRequest
Kept 'tag' for tracing and just add 'hba_private' as an additional field as per request from Paolo
- megasas: checkpatch.pl fixes and update to work with the changed interface in scsi_req_new(). Also included the suggested fixes from Alex.
Hannes Reinecke (5):
iov: Update parameter usage in iov_(to|from)_buf()
scsi: Add 'hba_private' to SCSIRequest
scsi-disk: Fixup debugging statement
scsi-disk: Mask out serial number EVPD
megasas: LSI Megaraid SAS emulation
Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/esp.c | 2 +- hw/lsi53c895a.c | 22 +- hw/megasas.c | 1950 +++++++++++++++++++++++++++++++++++++++++++++++ hw/mfi.h | 1197 +++++++++++++++++++++++++++++ hw/pci_ids.h | 3 +- hw/scsi-bus.c | 9 +- hw/scsi-disk.c | 21 +- hw/scsi-generic.c | 5 +- hw/scsi.h | 10 +- hw/spapr_vscsi.c | 29 +- hw/usb-msd.c | 9 +- hw/virtio-net.c | 2 +- hw/virtio-serial-bus.c | 2 +- iov.c | 49 +- iov.h | 10 +- trace-events | 71 ++ 18 files changed, 3306 insertions(+), 87 deletions(-) create mode 100644 hw/megasas.c create mode 100644 hw/mfi.h
Signature · 1 line
1.7.3.4
PATCH v6 1/5 iov: Update parameter usage in iov_(to|from)_buf()
iov_to_buf() has an 'offset' parameter, iov_from_buf() hasn't. This patch adds the missing parameter to iov_from_buf(). It also renames the 'offset' parameter to 'iov_off' to emphasize it's the offset into the iovec and not the buffer.
--- hw/virtio-net.c | 2 +- hw/virtio-serial-bus.c | 2 +- iov.c | 49 ++++++++++++++++++++++++++--------------------- iov.h | 10 ++++---- 4 files changed, 34 insertions(+), 29 deletions(-)
hw/virtio-net.c +1 -1
@@ -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
@@ -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 +27 -22
83 lines not on this page · raw message
iov.h +5 -5
@@ -12,8 +12,8 @@#include "qemu-common.h" -size_t iov_from_buf (struct iovec *iov,unsigned int iovcnt, -const void *buf,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); +size_t iov_from_buf (struct iovec *iov,unsigned int iov_cnt, +const void *buf,size_t iov_off,size_t size); +size_t iov_to_buf (const struct iovec *iov,const unsigned int iov_cnt, +void *buf,size_t iov_off,size_t size); +size_t iov_size (const struct iovec *iov,const unsigned int iov_cnt);
Signature · 1 line
1.7.3.4
PATCH v6 2/5 scsi: Add 'hba_private' to SCSIRequest
'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.
'tag' is still being kept for tracing purposes.
--- hw/esp.c | 2 +- hw/lsi53c895a.c | 22 ++++++++-------------- hw/scsi-bus.c | 9 ++++++--- hw/scsi-disk.c | 4 ++-- hw/scsi-generic.c | 5 +++-- hw/scsi.h | 10 +++++++--- hw/spapr_vscsi.c | 29 +++++++++-------------------- hw/usb-msd.c | 9 +-------- 8 files changed, 37 insertions(+), 53 deletions(-)
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,0 , lun,NULL ); datalen =scsi_req_enqueue (s->current_req, buf); s->ti_size = datalen;if (datalen !=0 ) {
hw/lsi53c895a.c +8 -14
@@ -661,7 +661,7 @@ static lsi_request *lsi_find_by_tag(LSIState *s, uint32_t tag)static void lsi_request_cancelled (SCSIRequest *req) {LSIState *s =DO_UPCAST (LSIState, dev.qdev, req->bus->qbus.parent); -lsi_request *p; +lsi_request *p = req->hba_private;if (s->current && req == s->current->req) {scsi_req_unref (req);
@@ -670,7 +670,6 @@ static void lsi_request_cancelled(SCSIRequest *req)return ; } - p =lsi_find_by_tag (s, req->tag);if (p) {QTAILQ_REMOVE (&s->queue, p, next);scsi_req_unref (req);
@@ -680,18 +679,12 @@ 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; - - p =lsi_find_by_tag (s, tag); -if (!p) { -BADF ("IO with unknown tag %d\n" , tag); -return 1 ; - } +lsi_request *p = req->hba_private;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 +736,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 +782,8 @@ 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->tag, s->current_lun, + s->current); n =scsi_req_enqueue (s->current->req, buf);if (n) {
hw/scsi-bus.c +6 -3
@@ -131,7 +131,8 @@ 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 tag, +uint32_t lun,void *hba_private) {SCSIRequest *req;
@@ -141,14 +142,16 @@ SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t l 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);return req; } -SCSIRequest *scsi_req_new (SCSIDevice *d,uint32_t tag,uint32_t lun) +SCSIRequest *scsi_req_new (SCSIDevice *d,uint32_t tag,uint32_t lun, +void *hba_private) { -return d->info->alloc_req (d, tag, lun); +return d->info->alloc_req (d, tag, lun, hba_private); }uint8_t *scsi_req_get_buf (SCSIRequest *req)
hw/scsi-disk.c +2 -2
@@ -81,13 +81,13 @@ 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) +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, tag, 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 +3 -2
@@ -96,11 +96,12 @@ 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 tag,uint32_t lun, +void *hba_private) {SCSIRequest *req; - req =scsi_req_alloc (sizeof (SCSIGenericReq), d, tag, lun); + req =scsi_req_alloc (sizeof (SCSIGenericReq), d, tag, lun, hba_private);return req; }
hw/scsi.h +7 -3
@@ -43,6 +43,7 @@ struct SCSIRequest { } cmd;BlockDriverAIOCB *aiocb;bool enqueued; +void *hba_private;QTAILQ_ENTRY (SCSIRequest) next; };
@@ -67,7 +68,8 @@ 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 tag,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 +140,10 @@ 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 tag, +uint32_t lun,void *hba_private); +SCSIRequest *scsi_req_new (SCSIDevice *d,uint32_t tag,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 +9 -20
@@ -121,7 +121,7 @@ static struct vscsi_req *vscsi_get_req(VSCSIState *s)return NULL ; } -static void vscsi_put_req (VSCSIState *s,vscsi_req *req) +static void vscsi_put_req (vscsi_req *req) {if (req->sreq !=NULL ) {scsi_req_unref (req->sreq);
@@ -130,15 +130,6 @@ static void vscsi_put_req(VSCSIState *s, vscsi_req *req) req->active =0 ; } -static vscsi_req *vscsi_find_req (VSCSIState *s,SCSIRequest *req) -{ -uint32_t tag = req->tag; -if (tag >=VSCSI_REQ_LIMIT || !s->reqs[tag].active) { -return NULL ; - } -return &s->reqs[tag]; -} -static void vscsi_decode_id_lun (uint64_t srp_lun,int *id,int *lun) {/* XXX Figure that one out properly ! This is crackpot */
@@ -454,7 +445,7 @@ static void vscsi_send_request_sense(VSCSIState *s, vscsi_req *req)if (n) { req->senselen = n;vscsi_send_rsp (s, req,CHECK_CONDITION ,0 ,0 ); -vscsi_put_req (s, req); +vscsi_put_req (req);return ; }
@@ -483,7 +474,7 @@ static void vscsi_send_request_sense(VSCSIState *s, vscsi_req *req)static void vscsi_transfer_data (SCSIRequest *sreq,uint32_t len) {VSCSIState *s =DO_UPCAST (VSCSIState, vdev.qdev, sreq->bus->qbus.parent); -vscsi_req *req =vscsi_find_req (s, sreq); +vscsi_req *req = sreq->hba_private;uint8_t *buf;int rc =0 ;
@@ -530,8 +521,7 @@ static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)/* Callback to indicate that the SCSI layer has completed a transfer. */ static void vscsi_command_complete (SCSIRequest *sreq,uint32_t status) { -VSCSIState *s =DO_UPCAST (VSCSIState, vdev.qdev, sreq->bus->qbus.parent); -vscsi_req *req =vscsi_find_req (s, sreq); +vscsi_req *req = sreq->hba_private;int32_t res_in =0 , res_out =0 ; dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x status=0x%x, req=%p\n",
@@ -563,15 +553,14 @@ static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status) } }vscsi_send_rsp (s, req,0 , res_in, res_out); -vscsi_put_req (s, req); +vscsi_put_req (req); }static void vscsi_request_cancelled (SCSIRequest *sreq) { -VSCSIState *s =DO_UPCAST (VSCSIState, vdev.qdev, sreq->bus->qbus.parent); -vscsi_req *req =vscsi_find_req (s, sreq); +vscsi_req *req = sreq->hba_private; -vscsi_put_req (s, req); +vscsi_put_req (req); }static void vscsi_process_login (VSCSIState *s,vscsi_req *req)
@@ -659,7 +648,7 @@ static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req) } req->lun = lun; - req->sreq =scsi_req_new (sdev, req->qtag, lun); + req->sreq =scsi_req_new (sdev, req->qtag, lun, req); n =scsi_req_enqueue (req->sreq, srp->cmd.cdb); dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
@@ -858,7 +847,7 @@ static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq) }if (done) { -vscsi_put_req (s, req); +vscsi_put_req (req); } }
hw/usb-msd.c +1 -8
@@ -216,10 +216,6 @@ 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); - } -assert ((s->mode ==USB_MSDM_DATAOUT ) == (req->cmd.mode ==SCSI_XFER_TO_DEV )); s->scsi_len = len; s->scsi_buf =scsi_req_get_buf (req);
@@ -241,9 +237,6 @@ 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); - }DPRINTF ("Command complete %d\n" , status); s->residue = s->data_len; s->result = status !=0 ;
@@ -387,7 +380,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, s->tag,0 ,NULL );scsi_req_enqueue (s->req, cbw.cmd);/* ??? Should check that USB and SCSI data transfer directions match. */
Signature · 1 line
1.7.3.4
PATCH v6 3/5 scsi-disk: Fixup debugging statement
A debugging statement wasn't converted to the new interface.
--- hw/scsi-disk.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/scsi-disk.c +1 -1
@@ -1007,7 +1007,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) command = buf[0 ]; outbuf = (uint8_t *)r->iov.iov_base; -DPRINTF ("Command: lun=%d tag=0x%x data=0x%02x" , lun, tag, buf[0 ]); +DPRINTF ("Command: lun=%d tag=0x%x data=0x%02x" , req->lun, req->tag, buf[0 ]);if (scsi_req_parse (&r->req, buf) !=0 ) {BADF ("Unsupported command length, command %x\n" , command);
Signature · 1 line
1.7.3.4
PATCH v6 4/5 scsi-disk: Mask out serial number EVPD
If the serial number is not set we should mask it out in the list of supported VPD pages and mark it as not supported.
--- hw/scsi-disk.c | 15 ++++++++++++--- 1 files changed, 12 insertions(+), 3 deletions(-)
hw/scsi-disk.c +12 -3
@@ -398,7 +398,8 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) "buffer size %zd\n", req->cmd.xfer); pages = buflen++; outbuf[buflen++] =0x00 ;// list of supported pages (this page) - outbuf[buflen++] =0x80 ;// unit serial number +if (s->serial) + outbuf[buflen++] =0x80 ;// unit serial number outbuf[buflen++] =0x83 ;// device identification if (s->drive_kind ==SCSI_HD ) { outbuf[buflen++] =0xb0 ;// block limits
@@ -409,8 +410,14 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) }case 0x80 :/* Device serial number, optional */ { -int l =strlen (s->serial); +int l; +if (!s->serial) { +DPRINTF ("Inquiry (EVPD[Serial number] not supported\n" ); +return -1 ; + } + + l =strlen (s->serial);if (l > req->cmd.xfer) l = req->cmd.xfer;if (l >20 )
@@ -1203,7 +1210,9 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)if (!s->serial) {/* try to fall back to value set with legacy -drive serial=... */ dinfo =drive_get_by_blockdev (s->bs); - s->serial =qemu_strdup (*dinfo->serial ? dinfo->serial :"0" ); +if (*dinfo->serial) { + s->serial =qemu_strdup (dinfo->serial); + } }if (!s->version) {
Signature · 1 line
1.7.3.4
Re: [PATCH 3/5] scsi-disk: Fixup debugging statement
In reply to Hannes ReineckeQuoted 12 lines · Hannes Reinecke
A debugging statement wasn't converted to the new interface.
Signed-off-by Hannes Reinecke<[email protected]>--- hw/scsi-disk.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)hw/scsi-disk.c +0
-0
hw/scsi-disk.c static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) · line 1013
Quoted 8 lines
hw/scsi-disk.c +1
-1@@ -1007,7 +1007,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) command = buf[0 ]; outbuf = (uint8_t *)r->iov.iov_base; -DPRINTF ("Command: lun=%d tag=0x%x data=0x%02x" , lun, tag, buf[0 ]); +DPRINTF ("Command: lun=%d tag=0x%x data=0x%02x" , req->lun, req->tag, buf[0 ]);if (scsi_req_parse (&r->req, buf) !=0 ) {BADF ("Unsupported command length, command %x\n" , command);
Re: [PATCH 4/5] scsi-disk: Mask out serial number EVPD
In reply to Hannes ReineckeQuoted 39 lines · Hannes Reinecke
If the serial number is not set we should mask it out in the list of supported VPD pages and mark it as not supported.
Signed-off-by Hannes Reinecke<[email protected]>--- hw/scsi-disk.c | 15 ++++++++++++--- 1 files changed, 12 insertions(+), 3 deletions(-)hw/scsi-disk.c +9
-2@@ -398,7 +398,8 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) "buffer size %zd\n", req->cmd.xfer); pages = buflen++; outbuf[buflen++] =0x00 ;// list of supported pages (this page) - outbuf[buflen++] =0x80 ;// unit serial number +if (s->serial) + outbuf[buflen++] =0x80 ;// unit serial number outbuf[buflen++] =0x83 ;// device identification if (s->drive_kind ==SCSI_HD ) { outbuf[buflen++] =0xb0 ;// block limits @@ -409,8 +410,14 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) }case 0x80 :/* Device serial number, optional */ { -int l =strlen (s->serial); +int l; +if (!s->serial) { +DPRINTF ("Inquiry (EVPD[Serial number] not supported\n" ); +return -1 ; + } + + l =strlen (s->serial);if (l> req->cmd.xfer) l = req->cmd.xfer;if (l>20 )
hw/scsi-disk.c static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1218
Quoted 10 lines
hw/scsi-disk.c +3
-1@@ -1203,7 +1210,9 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)if (!s->serial) {/* try to fall back to value set with legacy -drive serial=... */ dinfo =drive_get_by_blockdev (s->bs); - s->serial =qemu_strdup (*dinfo->serial ? dinfo->serial :"0" ); +if (*dinfo->serial) { + s->serial =qemu_strdup (dinfo->serial); + } }if (!s->version) {
Paolo
Quoted 30 lines · Hannes Reinecke
Hi all,
as Alex Graf reminded me the driver needed some more bugfixing to be done. I've found some issues and also moved the megasas emulation over to the new trace infrastructure. Driver works for me now and a full installation of openSUSE-12.1 works perfectly.
I've also included the fixes suggested by Stefan Hajnoczi. And during debugging I've found two minor issues inscsi_disk.cChanges since v5:
- scsi-disk: Fixup debugging statement
A debugging statement wasn't converted. Do so now.
- scsi-disk: Mask out serial number EVPD
The 'serial' parameter to scsi-disk is optional. So if it's not set we should mask it out in the list of supported EVPD pages and not return '0' here.
- megasas: Use tracing infrastructure instead of DPRINTF
- megasas: Use new PCI infrastructure
- megasas: Check for iovec mapping failurecpu_map_physical_memory()might fail, so we need to check for it when mapping iovecs.
- megasas: Trace scsi buffer overflow
The transfer length as specified in the SCSI command might disagree with the length of the iovec. We should be tracing these issues.
- megasas: Reset frames after init firmware
When receiving an INIT FIRMWARE command we need reset all frames, otherwise some frames might point to invalid memory.
Quoted 10 lines
Chances since v4:
- iov: Update parameter usage in iov_(to|from)_buf() Updated description for the first patch and clarified the usage Renamed arguments forio_XXXfor clarification
- scsi: Add 'hba_private' to SCSIRequest
Kept 'tag' for tracing and just add 'hba_private' as an additional field as per request from Paolo
- megasas: checkpatch.pl fixes and update to work with the changed interface inscsi_req_new(). Also included the suggested fixes from Alex.
agraf@busu:~/patch-rest/hannes1> ~/git/qemu/scripts/checkpatch.pl *
total: 0 errors, 0 warnings, 112 lines checked
[PATCH 1_5] iov: Update parameter usage in iov_(to|from)_buf().eml has no obvious style problems and is ready for submission.
total: 0 errors, 0 warnings, 257 lines checked
[PATCH 2_5] scsi: Add 'hba_private' to SCSIRequest.eml has no obvious style problems and is ready for submission.
total: 0 errors, 0 warnings, 8 lines checked
[PATCH 3_5] scsi-disk: Fixup debugging statement.eml has no obvious style problems and is ready for submission.
WARNING: braces {} are necessary for all arms of this statement
#56: FILE: hw/scsi-disk.c:401:
+ if (s->serial)
[...]
ERROR: do not use C99 // comments
#57: FILE: hw/scsi-disk.c:402:
+ outbuf[buflen++] = 0x80; // unit serial number
total: 1 errors, 1 warnings, 34 lines checked
[PATCH 4_5] scsi-disk: Mask out serial number EVPD.eml has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
ERROR: space required after that ',' (ctx:VxV)
#482: FILE: hw/megasas.c:399:
+ trace_megasas_frame_map_failed(cmd->index,(unsigned long)frame);
^ERROR: space required after that ',' (ctx:VxV)
#673: FILE: hw/megasas.c:590:
+ trace_megasas_dcmd_enter(cmd->index,"MFI DCMD get controller info");
^ERROR: space required after that ',' (ctx:VxV)
#748: FILE: hw/megasas.c:665:
+ trace_megasas_dcmd_invalid_xfer_len(cmd->index,"MFC Get defaults",
^
ERROR: space required after that ',' (ctx:VxV)
#774: FILE: hw/megasas.c:691:
+ trace_megasas_dcmd_invalid_xfer_len(cmd->index,"Get BIOS info",
^
ERROR: space required after that ',' (ctx:VxV)
#827: FILE: hw/megasas.c:744:
+ trace_megasas_dcmd_invalid_xfer_len(cmd->index,"PD get list",
^
ERROR: trailing whitespace
#3284: FILE: trace-events:339:
+disable megasas_handle_scsi(const char *frame, const char *desc, int dev, int lun, void *sdev, unsigned long size) "%s %s dev %x/%x sdev %p xfer %lu" $
ERROR: trailing whitespace
#3294: FILE: trace-events:349:
+disable megasas_handle_io(int cmd, const char *frame, int dev, int lun, unsigned long lba, unsigned long count) "scmd %d: %s dev %x/%x lba %lx count %lu" $
total: 7 errors, 0 warnings, 3255 lines checked
[PATCH 5_5] megasas: LSI Megaraid SAS emulation.eml has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
Re: [PATCH 5/5] megasas: LSI Megaraid SAS emulation
In reply to Hannes ReineckeQuoted 1 line · Hannes Reinecke
This patch adds an emulation for the LSI Megaraid SAS 8708EM2 HBA.
Nice, so it does work for me this time :).
Alex
Re: [PATCH 5/5] megasas: LSI Megaraid SAS emulation
In reply to Hannes ReineckeQuoted 131 lines · Hannes Reinecke
This patch adds an emulation for the LSI Megaraid SAS 8708EM2 HBA.
Signed-off-by Hannes Reinecke<[email protected]>--- Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/megasas.c | 1950 +++++++++++++++++++++++++++++++++++++++++++++++ hw/mfi.h | 1197 +++++++++++++++++++++++++++++ hw/pci_ids.h | 3 +- trace-events | 71 ++ 6 files changed, 3222 insertions(+), 1 deletions(-)create mode 100644
hw/megasas.c
create mode 100644hw/mfi.hMakefile.objs +1
-0@@ -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.odefault-configs/pci.mak +1
-0@@ -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=yhw/megasas.c new file +87
-0@@ -0,0 +1,1950 @@ +/* +* QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation +* +* Copyright (c) 2009-2011 Hannes Reinecke, SUSE Labs +* +* This code is licensed under the LGPL. +*/ + +#include "hw.h" +#include "pci.h" +#include "dma.h" +#include "iov.h" +#include "scsi.h" +#include "scsi-defs.h" +#include "block_int.h" +#include "trace.h" + +#include "mfi.h" + +#ifdef DEBUG_MEGASAS +#define DPRINTF (fmt, ...) \ + do { \ + printf("megasas: " fmt , ## __VA_ARGS__); \ + } while (0) +#else +#define DPRINTF (fmt, ...) do {} while (0) +#endif + +/* Static definitions */ +#define MEGASAS_VERSION "1.30" +#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; + +target_phys_addr_t pa; +target_phys_addr_t pa_size; +union mfi_frame *frame; +SCSIRequest *req; +struct iovec *iov; +void *iov_buf; +size_t iov_cnt; +size_t iov_size; +size_t iov_offset; +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; +
hw/megasas.c line 97
Quoted 10 lines
hw/megasas.c new file +10
-0+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)
Please make this a static function
hw/megasas.c line 109
Quoted 12 lines
hw/megasas.c new file +12
-0+ +#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));
... these too :).
Quoted 4 lines
hw/megasas.c new file +4
-0+ +static void megasas_soft_reset (MPTState *s); + +static int megasas_map_sgl (struct megasas_cmd_t *cmd,int pa_offset)
hw/megasas.c line 123
Quoted 10 lines
hw/megasas.c new file +10
-0+{ +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 ; +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 ));
Is there a maximum number of sg elements? You might want to consider making the iov array static and save yourself the malloc (which can really be a performance bottleneck). That's only an idea for future optimization though.
hw/megasas.c line 132
Quoted 9 lines
hw/megasas.c new file +9
-0+ cmd->iov_cnt =0 ; +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); + }else { + iov_pa =ldl_phys (pa);
These load data from memory in target endianness. Are you sure that's what you want? I'd expect this to be defined as little endian (especially given that ldq and ldl on the same address work).
Quoted 143 lines
hw/megasas.c new file +143
-0+ } + cmd->iov[i].iov_len = iov_size =ldl_phys (pa + sgl_addr_size); + cmd->iov[i].iov_base =cpu_physical_memory_map (iov_pa,&iov_size, + is_write); +if (!iov_size || iov_size != cmd->iov[i].iov_len) { +trace_megasas_iovec_map_failed (cmd->index, i, iov_size); + goto unmap; + } + cmd->iov_cnt++; + pa_offset += sgl_addr_size +sizeof (uint32_t ); + iov_count += iov_size; + } + cmd->iov[i].iov_base =NULL ; + cmd->iov[i].iov_len =0 ; +if (pa_offset> (cmd->count +1 ) *MFI_FRAME_SIZE ) { +trace_megasas_io_sgl_overflow (cmd->context, cmd->count, + cmd->frame->header.sge_count, pa_offset); + } +if (iov_count> cmd->iov_size * cmd->sdev->blocksize) { +trace_megasas_io_list_overflow (cmd->context, iov_count, + cmd->iov_size * cmd->sdev->blocksize); + } + cmd->iov_offset =0 ; +return 0 ; +unmap: +while (i>=0 ) { +cpu_physical_memory_unmap (cmd->iov[i].iov_base, + cmd->iov[i].iov_len,0 ,0 ); + i--; + } +qemu_free (cmd->iov); + iov_count = cmd->frame->header.sge_count - cmd->iov_cnt; + cmd->iov_cnt =0 ; + cmd->iov =NULL ; +return iov_count; +} + +static void megasas_unmap_sgl (struct megasas_cmd_t *cmd) +{ +uint16_t flags =le16_to_cpu (cmd->frame->header.flags); +int i, is_write = (flags&MFI_FRAME_DIR_WRITE ) ?1 :0 ; + +for (i =0 ; i< cmd->frame->header.sge_count; i++) { +cpu_physical_memory_unmap (cmd->iov[i].iov_base, cmd->iov[i].iov_len, + is_write, cmd->iov[i].iov_len); + } +qemu_free (cmd->iov); + cmd->iov =NULL ; + cmd->iov_cnt =0 ; + cmd->iov_offset =0 ; +} + +/* +* passthrough sense and io sense are at the same offset +*/ +static int megasas_build_sense (struct megasas_cmd_t *cmd,uint8_t *sense_ptr, +uint8_t sense_len) +{ +target_phys_addr_t pa, pa_hi =0 , pa_lo; +uint16_t flags =le16_to_cpu (cmd->frame->header.flags); +int is_sense64 = (flags&MFI_FRAME_SENSE64 ) ?1 :0 ; + +if (sense_len> cmd->frame->header.sense_len) { + sense_len = cmd->frame->header.sense_len; + } + + pa_lo =le32_to_cpu (cmd->frame->pass.sense_addr_lo); +if (is_sense64) { + pa_hi =le32_to_cpu (cmd->frame->pass.sense_addr_hi); + } + pa = ((uint64_t ) pa_hi<<32 ) | pa_lo; +cpu_physical_memory_write (pa, sense_ptr, sense_len); + cmd->frame->header.sense_len = sense_len; +return sense_len; +} + +static void megasas_write_sense (struct megasas_cmd_t *cmd,SCSISense sense) +{ +uint8_t *sense_ptr; +uint8_t sense_len; + + sense_ptr =qemu_mallocz (cmd->frame->header.sense_len); + sense_len =scsi_build_sense (sense, sense_ptr, + cmd->frame->header.sense_len,0 ); +megasas_build_sense (cmd, sense_ptr, sense_len); +qemu_free (sense_ptr); +} + +static void megasas_copy_sense (struct megasas_cmd_t *cmd) +{ +uint8_t *sense_ptr; +uint8_t sense_len; + + sense_ptr =qemu_mallocz (cmd->frame->header.sense_len); + sense_len =scsi_req_get_sense (cmd->req, sense_ptr, + cmd->frame->header.sense_len); +megasas_build_sense (cmd, sense_ptr, sense_len); +qemu_free (sense_ptr); +} + +/* +* Format an INQUIRY CDB +*/ +static int megasas_setup_inquiry (uint8_t *cdb,int pg, +uint8_t *buf,int len) +{ +memset (cdb,0 ,6 ); + cdb[0 ] =INQUIRY ; +if (pg>0 ) { + cdb[1 ] =0x1 ; + cdb[2 ] = pg; + } + cdb[3 ] = (len>>8 )&0xff ; + cdb[4 ] = (len&0xff ); +return len; +} + +/* +* Encode lba and len into a READ_16/WRITE_16 CDB +*/ +static void megasas_encode_lba (uint8_t *cdb,uint64_t lba, +uint32_t len,int is_write) +{ +memset (cdb,0x0 ,16 ); +if (is_write) { + cdb[0 ] =WRITE_16 ; + }else { + cdb[0 ] =READ_16 ; + } + cdb[2 ] = (lba>>56 )&0xff ; + cdb[3 ] = (lba>>48 )&0xff ; + cdb[4 ] = (lba>>40 )&0xff ; + cdb[5 ] = (lba>>32 )&0xff ; + cdb[6 ] = (lba>>24 )&0xff ; + cdb[7 ] = (lba>>16 )&0xff ; + cdb[8 ] = (lba>>8 )&0xff ; + cdb[9 ] = (lba)&0xff ; + cdb[10 ] = (len>>24 )&0xff ; + cdb[11 ] = (len>>16 )&0xff ; + cdb[12 ] = (len>>8 )&0xff ; + cdb[13 ] = (len)&0xff ; +} +
hw/megasas.c line 285
Quoted 10 lines
hw/megasas.c new file +10
-0+/* +* Utility functions +*/ +static uint64_t megasas_fw_time (void ) +{ +const time_t now =time (NULL ); +struct tm *curtime; +uint64_t bcd_time; + + curtime =gmtime (&now);
Please use qemu_get_timedate().
Quoted 31 lines
hw/megasas.c new file +31
-0+ bcd_time = ((uint64_t )curtime->tm_sec&0xff )<<48 | + ((uint64_t )curtime->tm_min&0xff )<<40 | + ((uint64_t )curtime->tm_hour&0xff )<<32 | + ((uint64_t )curtime->tm_mday&0xff )<<24 | + ((uint64_t )curtime->tm_mon&0xff )<<16 | + ((uint64_t )(curtime->tm_year +1900 )&0xffff ); + +return bcd_time; +} + +static uint64_t megasas_gen_sas_addr (uint64_t id) +{ +uint64_t addr; + + addr =0x5001a4aULL <<36 ; + addr |= id&0xfffffffff ; + +return addr; +} + +/* +* Frame handling +*/ + +static void megasas_dump_frame (struct megasas_cmd_t *cmd) +{ + DPRINTF("Frame %x: count %d pa %p context %x\n" + "\t%016lx %016lx\n\t%016lx %016lx\n" + "\t%016lx %016lx\n\t%016lx %016lx\n", + cmd->index, cmd->count, cmd->frame, cmd->context, + be64_to_cpu(cmd->frame->raw[0]),
hw/megasas.c line 326
Quoted 10 lines
hw/megasas.c new file +10
-0+be64_to_cpu (cmd->frame->raw[1 ]), +be64_to_cpu (cmd->frame->raw[2 ]), +be64_to_cpu (cmd->frame->raw[3 ]), +be64_to_cpu (cmd->frame->raw[4 ]), +be64_to_cpu (cmd->frame->raw[5 ]), +be64_to_cpu (cmd->frame->raw[6 ]), + be64_to_cpu(cmd->frame->raw[7])); +} + +static inline int megasas_next_index (MPTState *s,int index)
Please let the compiler decide the inlining :). Just remove the "inline" from all functions in the .c file.
Quoted 167 lines
hw/megasas.c new file +167
-0+{ + index++; +if (index == s->fw_cmds) { + index =0 ; + } +return index; +} + +static inline struct megasas_cmd_t *megasas_lookup_frame (MPTState *s, +target_phys_addr_t frame) +{ +struct megasas_cmd_t *cmd =NULL ; +int num =0 , index; + + index = s->reply_queue_index; + +while (num< s->fw_cmds) { +if (s->frames[index].pa&& s->frames[index].pa == frame) { + cmd =&s->frames[index]; +break ; + } + index =megasas_next_index (s, index); + num++; + } + +return cmd; +} + +static inline struct megasas_cmd_t *megasas_next_frame (MPTState *s, +target_phys_addr_t frame) +{ +struct megasas_cmd_t *cmd =NULL ; +int num =0 , index; + + cmd =megasas_lookup_frame (s, frame); +if (cmd) { +trace_megasas_qf_found (cmd->index, cmd->pa); +return cmd; + } + index = s->reply_queue_index; + num =0 ; +while (num< s->fw_cmds) { +if (!s->frames[index].pa) { + cmd =&s->frames[index]; +break ; + } + index =megasas_next_index (s, index); + num++; + } +if (!cmd) { +trace_megasas_qf_failed (frame); + } +trace_megasas_qf_new (index, cmd); +return cmd; +} + +static struct megasas_cmd_t * +megasas_enqueue_frame (MPTState *s,target_phys_addr_t frame,int count) +{ +struct megasas_cmd_t *cmd =NULL ; +uint8_t frame_size =MFI_FRAME_SIZE ; +target_phys_addr_t frame_size_p = frame_size; + + cmd =megasas_next_frame (s, frame); +/* All frames busy */ +if (!cmd) { +return NULL ; + } +if (!cmd->pa) { + cmd->pa = frame; + cmd->frame =cpu_physical_memory_map (frame,&frame_size_p,0 ); +if (frame_size_p != frame_size) { +trace_megasas_frame_map_failed (cmd->index,(unsigned long )frame); +if (cmd->frame) { +cpu_physical_memory_unmap (cmd->frame, frame_size_p,0 ,0 ); + cmd->frame =NULL ; + cmd->pa =0 ; + } + s->event_count++; +return NULL ; + } + cmd->pa_size = frame_size_p; + cmd->context =le32_to_cpu (cmd->frame->header.context); + } + cmd->count = count; + s->busy++; + +trace_megasas_qf_enqueue (cmd->index, cmd->count, cmd->context, + s->reply_queue_index, s->busy); + +return cmd; +} + +static void megasas_complete_frame (MPTState *s,int context) +{ +int tail; + +/* Decrement busy count */ + s->busy--; + +if (!MEGASAS_INTR_ENABLED (s)) { +trace_megasas_qf_complete_noirq (context); +return ; + } + +/* Put command on the reply queue */ + tail = s->reply_queue_index; +stl_phys (s->reply_queue_pa + tail *sizeof (uint32_t ), context); + + s->reply_queue_index =megasas_next_index (s, tail); +trace_megasas_qf_complete (context, tail, s->busy, s->doorbell); + +/* Notify HBA */ + s->doorbell++; +if (s->doorbell ==1 ) { +qemu_irq_raise (s->dev.irq[0 ]); + } +} + +static void megasas_reset_frames (MPTState *s) +{ +int i; +struct megasas_cmd_t *cmd; + +for (i =0 ; i< s->fw_cmds; i++) { + cmd =&s->frames[i]; +if (cmd->pa) { +cpu_physical_memory_unmap (cmd->frame, cmd->pa_size,0 ,0 ); + cmd->frame =NULL ; + cmd->pa =0 ; + } + } +} + +static void megasas_abort_command (struct megasas_cmd_t *cmd) +{ +if (cmd->req) { +scsi_req_unref (cmd->req); + cmd->req =NULL ; + } +} + +static int megasas_init_firmware (MPTState *s,struct megasas_cmd_t *cmd) +{ +target_phys_addr_t iq_pa, pa_hi, pa_lo, iq_pl, initq_size; +struct mfi_init_qinfo *initq; + + iq_pl =le32_to_cpu (cmd->frame->init.header.data_len); + pa_lo =le32_to_cpu (cmd->frame->init.qinfo_new_addr_lo); + pa_hi =le32_to_cpu (cmd->frame->init.qinfo_new_addr_hi); + iq_pa = (((uint64_t ) pa_hi<<32 ) | pa_lo); +trace_megasas_init_firmware ((int )iq_pl, (unsigned long )iq_pa); + initq_size =sizeof (*initq); + initq =cpu_physical_memory_map (iq_pa,&initq_size,0 ); +if (initq_size !=sizeof (*initq)) { +trace_megasas_init_queue_map_failed (cmd->index); +if (initq) { +cpu_physical_memory_unmap (initq, initq_size,0 ,0 ); + } + s->fw_state =MFI_FWSTATE_FAULT ; + s->event_count++; + goto out; + } + s->reply_queue_len =le32_to_cpu (initq->rq_entries); + pa_lo =le32_to_cpu (initq->rq_addr_lo); + pa_hi =le32_to_cpu (initq->rq_addr_hi); + s->reply_queue_pa = ((uint64_t ) pa_hi<<32 ) | pa_lo;
hw/megasas.c line 503
Quoted 10 lines
hw/megasas.c new file +10
-0+ pa_lo =le32_to_cpu (initq->ci_addr_lo); + pa_hi =le32_to_cpu (initq->ci_addr_hi); + s->consumer_pa = ((uint64_t ) pa_hi<<32 ) | pa_lo; + pa_lo =le32_to_cpu (initq->pi_addr_lo); + pa_hi =le32_to_cpu (initq->pi_addr_hi); + s->producer_pa = ((uint64_t ) pa_hi<<32 ) | pa_lo; +trace_megasas_init_queue ((unsigned long )s->reply_queue_pa, + s->reply_queue_len, (unsigned long )s->producer_pa, + (unsigned long )s->consumer_pa); + s->reply_queue_index =ldl_phys (s->producer_pa);
Possible endianness problem?
Quoted 54 lines
hw/megasas.c new file +54
-0+megasas_reset_frames (s); + s->fw_state =MFI_FWSTATE_OPERATIONAL ; +out: +cpu_physical_memory_unmap (initq, initq_size,0 ,0 ); +return 0 ; +} + +static int megasas_map_dcmd (struct megasas_cmd_t *cmd) +{ +target_phys_addr_t iov_pa, iov_size_p; +uint32_t iov_size =0 ; +uint16_t flags =le16_to_cpu (cmd->frame->header.flags); +int is_sgl64 = (flags&MFI_FRAME_SGL64 ) ?1 :0 ; + +if (!cmd->frame->header.sge_count) { +trace_megasas_dcmd_zero_sge (cmd->index); + cmd->iov_size =0 ; + cmd->iov_buf =NULL ; +return 0 ; + }else if (cmd->frame->header.sge_count>1 ) { +trace_megasas_dcmd_invalid_sge (cmd->index, + cmd->frame->header.sge_count); +return -1 ; + } +if (is_sgl64) { + iov_pa =le64_to_cpu (cmd->frame->dcmd.sgl.sg64[0 ].addr); + iov_size =le32_to_cpu (cmd->frame->dcmd.sgl.sg64[0 ].len); + }else { + iov_pa =le32_to_cpu (cmd->frame->dcmd.sgl.sg32[0 ].addr); + iov_size =le32_to_cpu (cmd->frame->dcmd.sgl.sg32[0 ].len); + } + iov_size_p = iov_size; + cmd->iov_buf =cpu_physical_memory_map (iov_pa,&iov_size_p,1 ); +if (iov_size_p != iov_size) { +trace_megasas_dcmd_map_failed (cmd->index); +if (cmd->iov_buf) { +cpu_physical_memory_unmap (cmd->iov_buf, iov_size,1 ,0 ); + cmd->iov_buf =NULL ; + cmd->iov_size =0 ; + } +return -1 ; + } + cmd->iov_size = iov_size; +return iov_size; +} + +static int megasas_finish_dcmd (struct megasas_cmd_t *cmd,uint32_t size) +{ +uint16_t flags =le16_to_cpu (cmd->frame->header.flags); +int is_sgl64 = (flags&MFI_FRAME_SGL64 ) ?1 :0 ; +int sgl_addr_size = is_sgl64 ?sizeof (uint64_t ) :sizeof (uint32_t ); + +if (size> cmd->iov_size) { +trace_megasas_dcmd_buffer_overflow (cmd->index, size, cmd->iov_size);
hw/megasas.c line 567
Quoted 10 lines
hw/megasas.c new file +10
-0+ size = cmd->iov_size; + } +if (!cmd->iov_buf) { +return 0 ; + } + +cpu_physical_memory_unmap (cmd->iov_buf, cmd->iov_size,1 , size); +if (cmd->iov_size> size) { +stl_phys (cmd->pa + offsetof(struct mfi_dcmd_frame , sgl) + + sgl_addr_size, size);
Endianness?
Quoted 17 lines
hw/megasas.c new file +17
-0+ } +return size; +} + +static int megasas_ctrl_get_info (MPTState *s,struct megasas_cmd_t *cmd) +{ +struct mfi_ctrl_info *info = cmd->iov_buf; +int n, num_ld_disks =0 ; + +for (n =0 ; n< s->fw_luns; n++) { +if (s->bus.devs[n]) { + num_ld_disks++; + } + } + +memset (cmd->iov_buf,0x0 , cmd->iov_size); +if (cmd->iov_size !=sizeof (struct mfi_ctrl_info )) {
hw/megasas.c line 594
Quoted 10 lines
hw/megasas.c new file +10
-0+trace_megasas_dcmd_invalid_xfer_len (cmd->index,"Ctrl Get Info" , + cmd->iov_size); +return MFI_STAT_INVALID_PARAMETER ; + } + +trace_megasas_dcmd_enter (cmd->index,"MFI DCMD get controller info" ); + info->pci.vendor =PCI_VENDOR_ID_LSI_LOGIC ; + info->pci.device =PCI_DEVICE_ID_LSI_SAS1078 ; + info->pci.subvendor =PCI_VENDOR_ID_LSI_LOGIC ; + info->pci.subdevice =0x1013 ;
Endianness issues in this whole function :)
Quoted 66 lines
hw/megasas.c new file +66
-0+ + info->host.type =MFI_INFO_HOST_PCIX ; + info->device.type =MFI_INFO_DEV_SAS3G ; + info->device.port_count =2 ; + info->device.port_addr[0 ] =megasas_gen_sas_addr ((unsigned long )s); + +memcpy (info->product_name,"MegaRAID SAS 8708EM2" ,20 ); +snprintf (info->serial_number,32 ,"QEMU%08lx" , + (unsigned long )s&0xFFFFFFFF ); +snprintf (info->package_version,0x60 ,"%s-QEMU" ,QEMU_VERSION ); +memcpy (info->image_component[0 ].name,"APP" ,8 ); +memcpy (info->image_component[0 ].version,MEGASAS_VERSION "-QEMU" ,32 ); +memcpy (info->image_component[0 ].build_date, __DATE__,16 ); +memcpy (info->image_component[0 ].build_time, __TIME__,16 ); + info->image_component_count =1 ; + info->current_fw_time =megasas_fw_time (); + info->max_arms =32 ; + info->max_spans =8 ; + info->max_arrays =MEGASAS_MAX_ARRAYS ; + info->max_lds = s->fw_luns; + info->max_cmds = s->fw_cmds; + info->max_sg_elements = s->fw_sge; + info->max_request_size =MEGASAS_MAX_SECTORS ; + info->lds_present = num_ld_disks; + info->pd_present = num_ld_disks +1 ; + info->pd_disks_present = num_ld_disks; + info->hw_present =MFI_INFO_HW_NVRAM |MFI_INFO_HW_MEM |MFI_INFO_HW_FLASH ; + info->memory_size =512 ; + info->nvram_size =32 ; + info->flash_size =16 ; + info->raid_levels =MFI_INFO_RAID_0 ; + info->adapter_ops =MFI_INFO_AOPS_RBLD_RATE | +MFI_INFO_AOPS_SELF_DIAGNOSTIC | +MFI_INFO_AOPS_MIXED_ARRAY ; + info->ld_ops =MFI_INFO_LDOPS_DISK_CACHE_POLICY | +MFI_INFO_LDOPS_ACCESS_POLICY | +MFI_INFO_LDOPS_IO_POLICY | +MFI_INFO_LDOPS_WRITE_POLICY | +MFI_INFO_LDOPS_READ_POLICY ; + info->max_strips_per_io = s->fw_sge; + info->stripe_sz_ops.min =3 ; + info->stripe_sz_ops.max =ffs (MEGASAS_MAX_SECTORS +1 ) -1 ; + info->properties.pred_fail_poll_interval =300 ; + info->properties.intr_throttle_cnt =16 ; + info->properties.intr_throttle_timeout =50 ; + info->properties.rebuild_rate =30 ; + info->properties.patrol_read_rate =30 ; + info->properties.bgi_rate =30 ; + info->properties.cc_rate =30 ; + info->properties.recon_rate =30 ; + info->properties.cache_flush_interval =4 ; + info->properties.spinup_drv_cnt =2 ; + info->properties.spinup_delay =6 ; + info->properties.ecc_bucket_size =15 ; + info->properties.ecc_bucket_leak_rate =1440 ; + info->properties.expose_encl_devices =1 ; + info->pd_ops =MFI_INFO_PDOPS_FORCE_ONLINE |MFI_INFO_PDOPS_FORCE_OFFLINE ; + info->pd_mix_support =MFI_INFO_PDMIX_SAS | +MFI_INFO_PDMIX_SATA | +MFI_INFO_PDMIX_LD ; + +return MFI_STAT_OK ; +} + +static int megasas_mfc_get_defaults (MPTState *s,struct megasas_cmd_t *cmd) +{
hw/megasas.c line 670
Quoted 10 lines
hw/megasas.c new file +10
-0+struct mfi_defaults *info = cmd->iov_buf; + +memset (cmd->iov_buf,0x0 , cmd->iov_size); +if (cmd->iov_size !=sizeof (struct mfi_defaults )) { +trace_megasas_dcmd_invalid_xfer_len (cmd->index,"MFC Get defaults" , + cmd->iov_size); +return MFI_STAT_INVALID_PARAMETER ; + } + + info->stripe_size =3 ;
... and more endianness
Quoted 48 lines
hw/megasas.c new file +48
-0+ info->flush_time =4 ; + info->background_rate =30 ; + info->allow_mix_in_enclosure =1 ; + info->allow_mix_in_ld =1 ; + info->direct_pd_mapping =1 ; + info->bios_enumerate_lds =1 ; + info->disable_ctrl_r =1 ; + info->expose_enclosure_devices =1 ; + info->disable_preboot_cli =1 ; + info->cluster_disable =1 ; + +return MFI_STAT_OK ; +} + +static int megasas_dcmd_get_bios_info (MPTState *s,struct megasas_cmd_t *cmd) +{ +struct mfi_bios_data *info = cmd->iov_buf; + +memset (cmd->iov_buf,0x0 , cmd->iov_size); +if (cmd->iov_size !=sizeof (struct mfi_bios_data )) { +trace_megasas_dcmd_invalid_xfer_len (cmd->index,"Get BIOS info" , + cmd->iov_size); +return MFI_STAT_INVALID_PARAMETER ; + } + info->continue_on_error =1 ; + +return MFI_STAT_OK ; +} + +static int megasas_dcmd_get_fw_time (MPTState *s,struct megasas_cmd_t *cmd) +{ +uint64_t fw_time; + + fw_time =megasas_fw_time (); + +memcpy (cmd->iov_buf,&fw_time,sizeof (fw_time)); +return MFI_STAT_OK ; +} + +static int megasas_dcmd_set_fw_time (MPTState *s,struct megasas_cmd_t *cmd) +{ +uint64_t fw_time; + +memset (cmd->iov_buf,0x0 , cmd->iov_size); +memcpy (&fw_time, cmd->frame->dcmd.mbox,sizeof (fw_time)); + +trace_megasas_dcmd_set_fw_time (cmd->index, fw_time); + fw_time =megasas_fw_time ();
hw/megasas.c line 728
Quoted 10 lines
hw/megasas.c new file +10
-0+memcpy (cmd->iov_buf,&fw_time,sizeof (fw_time)); +return MFI_STAT_OK ; +} + +static int megasas_event_info (MPTState *s,struct megasas_cmd_t *cmd) +{ +struct mfi_evt_log_state *info = cmd->iov_buf; + +memset (info,0 , cmd->iov_size); + info->newest_seq_num = s->event_count;
Endianness
Quoted 22 lines
hw/megasas.c new file +22
-0+ info->shutdown_seq_num = s->shutdown_event; + info->boot_seq_num = s->boot_event; + +return MFI_STAT_OK ; +} + +static int megasas_dcmd_pd_get_list (MPTState *s,struct megasas_cmd_t *cmd) +{ +struct mfi_pd_list *info = cmd->iov_buf; +uint32_t offset, num_pd_disks =0 , max_luns; +uint16_t dev_id; + +memset (cmd->iov_buf,0 , cmd->iov_size); + offset =8 ; +if (cmd->iov_size< (offset +sizeof (struct mfi_pd_address ))) { +trace_megasas_dcmd_invalid_xfer_len (cmd->index,"PD get list" , + cmd->iov_size); +return MFI_STAT_INVALID_PARAMETER ; + } + + max_luns = (cmd->iov_size - offset) /sizeof (struct mfi_pd_address ); +if (max_luns> s->fw_luns) {
hw/megasas.c line 760
Quoted 10 lines
hw/megasas.c new file +10
-0+ max_luns = s->fw_luns; + } +trace_megasas_dcmd_enter (cmd->index,"PD get list" ); + +for (dev_id =0 ; dev_id< max_luns; dev_id++) { +SCSIDevice *sdev; + + sdev = s->bus.devs[dev_id]; +if (sdev) { + info->addr[num_pd_disks].device_id = dev_id;
Here too info is guest visible, so needs to be endian converted.
I'll stop reading here. Please check all functions for possible endianness problems :)
Alex
Paolo Bonzini
maintainerRe: [PATCH 5/5] megasas: LSI Megaraid SAS emulation
Quoted 9 lines · Alexander Graf
hw/megasas.c line 132
Quoted 5 lines
hw/megasas.c new file +4
-0+if (is_sgl64) { + iov_pa =ldq_phys (pa); + }else { + iov_pa =ldl_phys (pa);These load data from memory in target endianness. Are you sure that's what you want? I'd expect this to be defined as little endian (especially given that ldq and ldl on the same address work).
Seems to be target endianness from the corresponding Linux code:
if (sge_count) {
scsi_for_each_sg(scp, os_sgl, sge_count, i) {
mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
}
}...
if (sge_count) {
scsi_for_each_sg(scp, os_sgl, sge_count, i) {
mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
}
}Note that this is _either_ a ldq or a ldl depending on what the driver told the device. It is not accessing a 64-bit value as 32-bit.
Paolo
Re: [PATCH 5/5] megasas: LSI Megaraid SAS emulation
Quoted 26 lines · Paolo Bonzini
Quoted 7 lines · Alexander Graf
hw/megasas.c line 132
Quoted 4 lines
hw/megasas.c new file +4
-0+if (is_sgl64) { + iov_pa =ldq_phys (pa); + }else { + iov_pa =ldl_phys (pa);These load data from memory in target endianness. Are you sure that's what you want? I'd expect this to be defined as little endian (especially given that ldq and ldl on the same address work).
Seems to be target endianness from the corresponding Linux code:
if (sge_count) { scsi_for_each_sg(scp, os_sgl, sge_count, i) { mfi_sgl->sge32[i].length = sg_dma_len(os_sgl); mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl); } }...
if (sge_count) { scsi_for_each_sg(scp, os_sgl, sge_count, i) { mfi_sgl->sge64[i].length = sg_dma_len(os_sgl); mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl); } }
Quoted 2 lines
Note that this is _either_ a ldq or a ldl depending on what the driver told the device. It is not accessing a 64-bit value as 32-bit.
So how would the device know which endianness the target is then? This looks like broken Linux code to me then. Christoph, is the above correct for big endian systems?
Btw, reading through the Qemu sources, there is ldl_le_p() to read little endian values from memory. I haven't quite found one for _phys though. We can just add that however.
Alex
Re: [Qemu-devel] [PATCH 5/5] megasas: LSI Megaraid SAS emulation
In reply to Hannes Reineckehw/megasas.c line 178
Quoted 9 lines · Hannes Reinecke
hw/megasas.c new file +9
-0+static void megasas_unmap_sgl (struct megasas_cmd_t *cmd) +{ + uint16_t flags = le16_to_cpu(cmd->frame->header.flags); + int i, is_write = (flags & MFI_FRAME_DIR_WRITE) ? 1 : 0; + + for (i = 0; i < cmd->frame->header.sge_count; i++) { + cpu_physical_memory_unmap(cmd->iov[i].iov_base, cmd->iov[i].iov_len, + is_write, cmd->iov[i].iov_len); + }
We cannot map control structures from guest memory and treating them as valid request state later on.
A malicious guest can issue the request, then change the fields the control structure while QEMU is processing the I/O, and then this function will execute with is_write/sge_count no longer the same as when the request started.
Good practice would be to copy in any request state needed instead of reaching into guest memory at later points of the request lifecycle. This way a malicious guest can never cause QEMU to crash or do something due to inconsistent state.
The particular problem I see here is starting the request with sge_count=1 and then setting it to sge_count=255. We will perform invalid iov[] accesses.
Stefan
Re: [Qemu-devel] [PATCH 5/5] megasas: LSI Megaraid SAS emulation
Quoted 14 lines · Stefan Hajnoczi
hw/megasas.c line 178
Quoted 9 lines · Hannes Reinecke
hw/megasas.c new file +9
-0+static void megasas_unmap_sgl (struct megasas_cmd_t *cmd) +{ +uint16_t flags =le16_to_cpu (cmd->frame->header.flags); +int i, is_write = (flags&MFI_FRAME_DIR_WRITE ) ?1 :0 ; + +for (i =0 ; i< cmd->frame->header.sge_count; i++) { +cpu_physical_memory_unmap (cmd->iov[i].iov_base, cmd->iov[i].iov_len, + is_write, cmd->iov[i].iov_len); + }We cannot map control structures from guest memory and treating them as valid request state later on.
Yes, I've been working on that one already.
What I'll be doing is to read in the sge count during 'map_sgl' and store this value internally (in ->iov_cnt). And during unmap I'll be using this value instead of the frame-provided one.
That way we'll be checking the sge_count field only once when we slurp in the entire frame.
Quoted 10 lines
A malicious guest can issue the request, then change the fields the control structure while QEMU is processing the I/O, and then this function will execute with is_write/sge_count no longer the same as when the request started.
Good practice would be to copy in any request state needed instead of reaching into guest memory at later points of the request lifecycle. This way a malicious guest can never cause QEMU to crash or do something due to inconsistent state.
See above, that's what I'll be doing.
Quoted 4 lines
The particular problem I see here is starting the request with sge_count=1 and then setting it to sge_count=255. We will perform invalid iov[] accesses.
Thanks for the hint. Will be fixing it up.
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)
Paolo Bonzini
maintainerRe: [Qemu-devel] [PATCH 5/5] megasas: LSI Megaraid SAS emulation
Quoted 11 lines · Hannes Reinecke
Quoted 4 lines
We cannot map control structures from guest memory and treating them as valid request state later on.
Yes, I've been working on that one already.
What I'll be doing is to read in the sge count during 'map_sgl' and store this value internally (in ->iov_cnt). And during unmap I'll be using this value instead of the frame-provided one.That way we'll be checking the
sge_countfield only once when we slurp in the entire frame.
Note the flags too. Perhaps it's easier to simply copy the entire frame header...
Paolo