xref: /openbmc/qemu/hw/scsi/megasas.c (revision a8d25326)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation
349ab747fSPaolo Bonzini  * Based on the linux driver code at drivers/scsi/megaraid
449ab747fSPaolo Bonzini  *
549ab747fSPaolo Bonzini  * Copyright (c) 2009-2012 Hannes Reinecke, SUSE Labs
649ab747fSPaolo Bonzini  *
749ab747fSPaolo Bonzini  * This library is free software; you can redistribute it and/or
849ab747fSPaolo Bonzini  * modify it under the terms of the GNU Lesser General Public
949ab747fSPaolo Bonzini  * License as published by the Free Software Foundation; either
1049ab747fSPaolo Bonzini  * version 2 of the License, or (at your option) any later version.
1149ab747fSPaolo Bonzini  *
1249ab747fSPaolo Bonzini  * This library is distributed in the hope that it will be useful,
1349ab747fSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1449ab747fSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1549ab747fSPaolo Bonzini  * Lesser General Public License for more details.
1649ab747fSPaolo Bonzini  *
1749ab747fSPaolo Bonzini  * You should have received a copy of the GNU Lesser General Public
1849ab747fSPaolo Bonzini  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1949ab747fSPaolo Bonzini  */
2049ab747fSPaolo Bonzini 
21a4ab4792SPeter Maydell #include "qemu/osdep.h"
22*a8d25326SMarkus Armbruster #include "qemu-common.h"
2349ab747fSPaolo Bonzini #include "hw/hw.h"
2449ab747fSPaolo Bonzini #include "hw/pci/pci.h"
2549ab747fSPaolo Bonzini #include "sysemu/dma.h"
264be74634SMarkus Armbruster #include "sysemu/block-backend.h"
274522b69cSHannes Reinecke #include "hw/pci/msi.h"
2849ab747fSPaolo Bonzini #include "hw/pci/msix.h"
2949ab747fSPaolo Bonzini #include "qemu/iov.h"
300b8fa32fSMarkus Armbruster #include "qemu/module.h"
3149ab747fSPaolo Bonzini #include "hw/scsi/scsi.h"
3208e2c9f1SPaolo Bonzini #include "scsi/constants.h"
3349ab747fSPaolo Bonzini #include "trace.h"
341108b2f8SCao jin #include "qapi/error.h"
3547b43a1fSPaolo Bonzini #include "mfi.h"
3649ab747fSPaolo Bonzini 
37e23d0498SHannes Reinecke #define MEGASAS_VERSION_GEN1 "1.70"
38e23d0498SHannes Reinecke #define MEGASAS_VERSION_GEN2 "1.80"
3949ab747fSPaolo Bonzini #define MEGASAS_MAX_FRAMES 2048         /* Firmware limit at 65535 */
4049ab747fSPaolo Bonzini #define MEGASAS_DEFAULT_FRAMES 1000     /* Windows requires this */
41e23d0498SHannes Reinecke #define MEGASAS_GEN2_DEFAULT_FRAMES 1008     /* Windows requires this */
4249ab747fSPaolo Bonzini #define MEGASAS_MAX_SGE 128             /* Firmware limit */
4349ab747fSPaolo Bonzini #define MEGASAS_DEFAULT_SGE 80
4449ab747fSPaolo Bonzini #define MEGASAS_MAX_SECTORS 0xFFFF      /* No real limit */
4549ab747fSPaolo Bonzini #define MEGASAS_MAX_ARRAYS 128
4649ab747fSPaolo Bonzini 
4749ab747fSPaolo Bonzini #define MEGASAS_HBA_SERIAL "QEMU123456"
4849ab747fSPaolo Bonzini #define NAA_LOCALLY_ASSIGNED_ID 0x3ULL
4949ab747fSPaolo Bonzini #define IEEE_COMPANY_LOCALLY_ASSIGNED 0x525400
5049ab747fSPaolo Bonzini 
5149ab747fSPaolo Bonzini #define MEGASAS_FLAG_USE_JBOD      0
5249ab747fSPaolo Bonzini #define MEGASAS_MASK_USE_JBOD      (1 << MEGASAS_FLAG_USE_JBOD)
53b4b4a57fSCao jin #define MEGASAS_FLAG_USE_QUEUE64   1
5449ab747fSPaolo Bonzini #define MEGASAS_MASK_USE_QUEUE64   (1 << MEGASAS_FLAG_USE_QUEUE64)
5549ab747fSPaolo Bonzini 
5649ab747fSPaolo Bonzini static const char *mfi_frame_desc[] = {
5749ab747fSPaolo Bonzini     "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
5849ab747fSPaolo Bonzini     "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
5949ab747fSPaolo Bonzini 
6049ab747fSPaolo Bonzini typedef struct MegasasCmd {
6149ab747fSPaolo Bonzini     uint32_t index;
6249ab747fSPaolo Bonzini     uint16_t flags;
6349ab747fSPaolo Bonzini     uint16_t count;
6449ab747fSPaolo Bonzini     uint64_t context;
6549ab747fSPaolo Bonzini 
6649ab747fSPaolo Bonzini     hwaddr pa;
6749ab747fSPaolo Bonzini     hwaddr pa_size;
685104fac8SPaolo Bonzini     uint32_t dcmd_opcode;
6949ab747fSPaolo Bonzini     union mfi_frame *frame;
7049ab747fSPaolo Bonzini     SCSIRequest *req;
7149ab747fSPaolo Bonzini     QEMUSGList qsg;
7249ab747fSPaolo Bonzini     void *iov_buf;
7349ab747fSPaolo Bonzini     size_t iov_size;
7449ab747fSPaolo Bonzini     size_t iov_offset;
7549ab747fSPaolo Bonzini     struct MegasasState *state;
7649ab747fSPaolo Bonzini } MegasasCmd;
7749ab747fSPaolo Bonzini 
7849ab747fSPaolo Bonzini typedef struct MegasasState {
7952190c1eSAndreas Färber     /*< private >*/
8052190c1eSAndreas Färber     PCIDevice parent_obj;
8152190c1eSAndreas Färber     /*< public >*/
8252190c1eSAndreas Färber 
8349ab747fSPaolo Bonzini     MemoryRegion mmio_io;
8449ab747fSPaolo Bonzini     MemoryRegion port_io;
8549ab747fSPaolo Bonzini     MemoryRegion queue_io;
8649ab747fSPaolo Bonzini     uint32_t frame_hi;
8749ab747fSPaolo Bonzini 
8849ab747fSPaolo Bonzini     int fw_state;
8949ab747fSPaolo Bonzini     uint32_t fw_sge;
9049ab747fSPaolo Bonzini     uint32_t fw_cmds;
9149ab747fSPaolo Bonzini     uint32_t flags;
9249ab747fSPaolo Bonzini     int fw_luns;
9349ab747fSPaolo Bonzini     int intr_mask;
9449ab747fSPaolo Bonzini     int doorbell;
9549ab747fSPaolo Bonzini     int busy;
96e23d0498SHannes Reinecke     int diag;
97e23d0498SHannes Reinecke     int adp_reset;
98b4b4a57fSCao jin     OnOffAuto msi;
99b4b4a57fSCao jin     OnOffAuto msix;
10049ab747fSPaolo Bonzini 
10149ab747fSPaolo Bonzini     MegasasCmd *event_cmd;
10249ab747fSPaolo Bonzini     int event_locale;
10349ab747fSPaolo Bonzini     int event_class;
10449ab747fSPaolo Bonzini     int event_count;
10549ab747fSPaolo Bonzini     int shutdown_event;
10649ab747fSPaolo Bonzini     int boot_event;
10749ab747fSPaolo Bonzini 
10849ab747fSPaolo Bonzini     uint64_t sas_addr;
10949ab747fSPaolo Bonzini     char *hba_serial;
11049ab747fSPaolo Bonzini 
11149ab747fSPaolo Bonzini     uint64_t reply_queue_pa;
11249ab747fSPaolo Bonzini     void *reply_queue;
11349ab747fSPaolo Bonzini     int reply_queue_len;
11449ab747fSPaolo Bonzini     int reply_queue_head;
11549ab747fSPaolo Bonzini     int reply_queue_tail;
11649ab747fSPaolo Bonzini     uint64_t consumer_pa;
11749ab747fSPaolo Bonzini     uint64_t producer_pa;
11849ab747fSPaolo Bonzini 
11949ab747fSPaolo Bonzini     MegasasCmd frames[MEGASAS_MAX_FRAMES];
1206df5718bSHannes Reinecke     DECLARE_BITMAP(frame_map, MEGASAS_MAX_FRAMES);
12149ab747fSPaolo Bonzini     SCSIBus bus;
12249ab747fSPaolo Bonzini } MegasasState;
12349ab747fSPaolo Bonzini 
124e23d0498SHannes Reinecke typedef struct MegasasBaseClass {
125e23d0498SHannes Reinecke     PCIDeviceClass parent_class;
126e23d0498SHannes Reinecke     const char *product_name;
127e23d0498SHannes Reinecke     const char *product_version;
128e23d0498SHannes Reinecke     int mmio_bar;
129e23d0498SHannes Reinecke     int ioport_bar;
130e23d0498SHannes Reinecke     int osts;
131e23d0498SHannes Reinecke } MegasasBaseClass;
132e23d0498SHannes Reinecke 
133e23d0498SHannes Reinecke #define TYPE_MEGASAS_BASE "megasas-base"
134e23d0498SHannes Reinecke #define TYPE_MEGASAS_GEN1 "megasas"
135e23d0498SHannes Reinecke #define TYPE_MEGASAS_GEN2 "megasas-gen2"
136c79e16aeSPeter Crosthwaite 
137c79e16aeSPeter Crosthwaite #define MEGASAS(obj) \
138e23d0498SHannes Reinecke     OBJECT_CHECK(MegasasState, (obj), TYPE_MEGASAS_BASE)
139e23d0498SHannes Reinecke 
140e23d0498SHannes Reinecke #define MEGASAS_DEVICE_CLASS(oc) \
141e23d0498SHannes Reinecke     OBJECT_CLASS_CHECK(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
142e23d0498SHannes Reinecke #define MEGASAS_DEVICE_GET_CLASS(oc) \
143e23d0498SHannes Reinecke     OBJECT_GET_CLASS(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
144c79e16aeSPeter Crosthwaite 
14549ab747fSPaolo Bonzini #define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
14649ab747fSPaolo Bonzini 
14749ab747fSPaolo Bonzini static bool megasas_intr_enabled(MegasasState *s)
14849ab747fSPaolo Bonzini {
14949ab747fSPaolo Bonzini     if ((s->intr_mask & MEGASAS_INTR_DISABLED_MASK) !=
15049ab747fSPaolo Bonzini         MEGASAS_INTR_DISABLED_MASK) {
15149ab747fSPaolo Bonzini         return true;
15249ab747fSPaolo Bonzini     }
15349ab747fSPaolo Bonzini     return false;
15449ab747fSPaolo Bonzini }
15549ab747fSPaolo Bonzini 
15649ab747fSPaolo Bonzini static bool megasas_use_queue64(MegasasState *s)
15749ab747fSPaolo Bonzini {
15849ab747fSPaolo Bonzini     return s->flags & MEGASAS_MASK_USE_QUEUE64;
15949ab747fSPaolo Bonzini }
16049ab747fSPaolo Bonzini 
16149ab747fSPaolo Bonzini static bool megasas_use_msix(MegasasState *s)
16249ab747fSPaolo Bonzini {
163b4b4a57fSCao jin     return s->msix != ON_OFF_AUTO_OFF;
16449ab747fSPaolo Bonzini }
16549ab747fSPaolo Bonzini 
16649ab747fSPaolo Bonzini static bool megasas_is_jbod(MegasasState *s)
16749ab747fSPaolo Bonzini {
16849ab747fSPaolo Bonzini     return s->flags & MEGASAS_MASK_USE_JBOD;
16949ab747fSPaolo Bonzini }
17049ab747fSPaolo Bonzini 
17116578c6fSPaolo Bonzini static void megasas_frame_set_cmd_status(MegasasState *s,
17216578c6fSPaolo Bonzini                                          unsigned long frame, uint8_t v)
17349ab747fSPaolo Bonzini {
17416578c6fSPaolo Bonzini     PCIDevice *pci = &s->parent_obj;
17516578c6fSPaolo Bonzini     stb_pci_dma(pci, frame + offsetof(struct mfi_frame_header, cmd_status), v);
17649ab747fSPaolo Bonzini }
17749ab747fSPaolo Bonzini 
17816578c6fSPaolo Bonzini static void megasas_frame_set_scsi_status(MegasasState *s,
17916578c6fSPaolo Bonzini                                           unsigned long frame, uint8_t v)
18049ab747fSPaolo Bonzini {
18116578c6fSPaolo Bonzini     PCIDevice *pci = &s->parent_obj;
18216578c6fSPaolo Bonzini     stb_pci_dma(pci, frame + offsetof(struct mfi_frame_header, scsi_status), v);
18349ab747fSPaolo Bonzini }
18449ab747fSPaolo Bonzini 
18549ab747fSPaolo Bonzini /*
18649ab747fSPaolo Bonzini  * Context is considered opaque, but the HBA firmware is running
18749ab747fSPaolo Bonzini  * in little endian mode. So convert it to little endian, too.
18849ab747fSPaolo Bonzini  */
18916578c6fSPaolo Bonzini static uint64_t megasas_frame_get_context(MegasasState *s,
19016578c6fSPaolo Bonzini                                           unsigned long frame)
19149ab747fSPaolo Bonzini {
19216578c6fSPaolo Bonzini     PCIDevice *pci = &s->parent_obj;
19316578c6fSPaolo Bonzini     return ldq_le_pci_dma(pci, frame + offsetof(struct mfi_frame_header, context));
19449ab747fSPaolo Bonzini }
19549ab747fSPaolo Bonzini 
19649ab747fSPaolo Bonzini static bool megasas_frame_is_ieee_sgl(MegasasCmd *cmd)
19749ab747fSPaolo Bonzini {
19849ab747fSPaolo Bonzini     return cmd->flags & MFI_FRAME_IEEE_SGL;
19949ab747fSPaolo Bonzini }
20049ab747fSPaolo Bonzini 
20149ab747fSPaolo Bonzini static bool megasas_frame_is_sgl64(MegasasCmd *cmd)
20249ab747fSPaolo Bonzini {
20349ab747fSPaolo Bonzini     return cmd->flags & MFI_FRAME_SGL64;
20449ab747fSPaolo Bonzini }
20549ab747fSPaolo Bonzini 
20649ab747fSPaolo Bonzini static bool megasas_frame_is_sense64(MegasasCmd *cmd)
20749ab747fSPaolo Bonzini {
20849ab747fSPaolo Bonzini     return cmd->flags & MFI_FRAME_SENSE64;
20949ab747fSPaolo Bonzini }
21049ab747fSPaolo Bonzini 
21149ab747fSPaolo Bonzini static uint64_t megasas_sgl_get_addr(MegasasCmd *cmd,
21249ab747fSPaolo Bonzini                                      union mfi_sgl *sgl)
21349ab747fSPaolo Bonzini {
21449ab747fSPaolo Bonzini     uint64_t addr;
21549ab747fSPaolo Bonzini 
21649ab747fSPaolo Bonzini     if (megasas_frame_is_ieee_sgl(cmd)) {
21749ab747fSPaolo Bonzini         addr = le64_to_cpu(sgl->sg_skinny->addr);
21849ab747fSPaolo Bonzini     } else if (megasas_frame_is_sgl64(cmd)) {
21949ab747fSPaolo Bonzini         addr = le64_to_cpu(sgl->sg64->addr);
22049ab747fSPaolo Bonzini     } else {
22149ab747fSPaolo Bonzini         addr = le32_to_cpu(sgl->sg32->addr);
22249ab747fSPaolo Bonzini     }
22349ab747fSPaolo Bonzini     return addr;
22449ab747fSPaolo Bonzini }
22549ab747fSPaolo Bonzini 
22649ab747fSPaolo Bonzini static uint32_t megasas_sgl_get_len(MegasasCmd *cmd,
22749ab747fSPaolo Bonzini                                     union mfi_sgl *sgl)
22849ab747fSPaolo Bonzini {
22949ab747fSPaolo Bonzini     uint32_t len;
23049ab747fSPaolo Bonzini 
23149ab747fSPaolo Bonzini     if (megasas_frame_is_ieee_sgl(cmd)) {
23249ab747fSPaolo Bonzini         len = le32_to_cpu(sgl->sg_skinny->len);
23349ab747fSPaolo Bonzini     } else if (megasas_frame_is_sgl64(cmd)) {
23449ab747fSPaolo Bonzini         len = le32_to_cpu(sgl->sg64->len);
23549ab747fSPaolo Bonzini     } else {
23649ab747fSPaolo Bonzini         len = le32_to_cpu(sgl->sg32->len);
23749ab747fSPaolo Bonzini     }
23849ab747fSPaolo Bonzini     return len;
23949ab747fSPaolo Bonzini }
24049ab747fSPaolo Bonzini 
24149ab747fSPaolo Bonzini static union mfi_sgl *megasas_sgl_next(MegasasCmd *cmd,
24249ab747fSPaolo Bonzini                                        union mfi_sgl *sgl)
24349ab747fSPaolo Bonzini {
24449ab747fSPaolo Bonzini     uint8_t *next = (uint8_t *)sgl;
24549ab747fSPaolo Bonzini 
24649ab747fSPaolo Bonzini     if (megasas_frame_is_ieee_sgl(cmd)) {
24749ab747fSPaolo Bonzini         next += sizeof(struct mfi_sg_skinny);
24849ab747fSPaolo Bonzini     } else if (megasas_frame_is_sgl64(cmd)) {
24949ab747fSPaolo Bonzini         next += sizeof(struct mfi_sg64);
25049ab747fSPaolo Bonzini     } else {
25149ab747fSPaolo Bonzini         next += sizeof(struct mfi_sg32);
25249ab747fSPaolo Bonzini     }
25349ab747fSPaolo Bonzini 
25449ab747fSPaolo Bonzini     if (next >= (uint8_t *)cmd->frame + cmd->pa_size) {
25549ab747fSPaolo Bonzini         return NULL;
25649ab747fSPaolo Bonzini     }
25749ab747fSPaolo Bonzini     return (union mfi_sgl *)next;
25849ab747fSPaolo Bonzini }
25949ab747fSPaolo Bonzini 
26049ab747fSPaolo Bonzini static void megasas_soft_reset(MegasasState *s);
26149ab747fSPaolo Bonzini 
26249ab747fSPaolo Bonzini static int megasas_map_sgl(MegasasState *s, MegasasCmd *cmd, union mfi_sgl *sgl)
26349ab747fSPaolo Bonzini {
26449ab747fSPaolo Bonzini     int i;
26549ab747fSPaolo Bonzini     int iov_count = 0;
26649ab747fSPaolo Bonzini     size_t iov_size = 0;
26749ab747fSPaolo Bonzini 
26849ab747fSPaolo Bonzini     cmd->flags = le16_to_cpu(cmd->frame->header.flags);
26949ab747fSPaolo Bonzini     iov_count = cmd->frame->header.sge_count;
27049ab747fSPaolo Bonzini     if (iov_count > MEGASAS_MAX_SGE) {
27149ab747fSPaolo Bonzini         trace_megasas_iovec_sgl_overflow(cmd->index, iov_count,
27249ab747fSPaolo Bonzini                                          MEGASAS_MAX_SGE);
27349ab747fSPaolo Bonzini         return iov_count;
27449ab747fSPaolo Bonzini     }
27552190c1eSAndreas Färber     pci_dma_sglist_init(&cmd->qsg, PCI_DEVICE(s), iov_count);
27649ab747fSPaolo Bonzini     for (i = 0; i < iov_count; i++) {
27749ab747fSPaolo Bonzini         dma_addr_t iov_pa, iov_size_p;
27849ab747fSPaolo Bonzini 
27949ab747fSPaolo Bonzini         if (!sgl) {
28049ab747fSPaolo Bonzini             trace_megasas_iovec_sgl_underflow(cmd->index, i);
28149ab747fSPaolo Bonzini             goto unmap;
28249ab747fSPaolo Bonzini         }
28349ab747fSPaolo Bonzini         iov_pa = megasas_sgl_get_addr(cmd, sgl);
28449ab747fSPaolo Bonzini         iov_size_p = megasas_sgl_get_len(cmd, sgl);
28549ab747fSPaolo Bonzini         if (!iov_pa || !iov_size_p) {
28649ab747fSPaolo Bonzini             trace_megasas_iovec_sgl_invalid(cmd->index, i,
28749ab747fSPaolo Bonzini                                             iov_pa, iov_size_p);
28849ab747fSPaolo Bonzini             goto unmap;
28949ab747fSPaolo Bonzini         }
29049ab747fSPaolo Bonzini         qemu_sglist_add(&cmd->qsg, iov_pa, iov_size_p);
29149ab747fSPaolo Bonzini         sgl = megasas_sgl_next(cmd, sgl);
29249ab747fSPaolo Bonzini         iov_size += (size_t)iov_size_p;
29349ab747fSPaolo Bonzini     }
29449ab747fSPaolo Bonzini     if (cmd->iov_size > iov_size) {
29549ab747fSPaolo Bonzini         trace_megasas_iovec_overflow(cmd->index, iov_size, cmd->iov_size);
29649ab747fSPaolo Bonzini     } else if (cmd->iov_size < iov_size) {
297d17e7448SEric Blake         trace_megasas_iovec_underflow(cmd->index, iov_size, cmd->iov_size);
29849ab747fSPaolo Bonzini     }
29949ab747fSPaolo Bonzini     cmd->iov_offset = 0;
30049ab747fSPaolo Bonzini     return 0;
30149ab747fSPaolo Bonzini unmap:
30249ab747fSPaolo Bonzini     qemu_sglist_destroy(&cmd->qsg);
30349ab747fSPaolo Bonzini     return iov_count - i;
30449ab747fSPaolo Bonzini }
30549ab747fSPaolo Bonzini 
30649ab747fSPaolo Bonzini /*
30749ab747fSPaolo Bonzini  * passthrough sense and io sense are at the same offset
30849ab747fSPaolo Bonzini  */
30949ab747fSPaolo Bonzini static int megasas_build_sense(MegasasCmd *cmd, uint8_t *sense_ptr,
31049ab747fSPaolo Bonzini     uint8_t sense_len)
31149ab747fSPaolo Bonzini {
3121016b239SPaolo Bonzini     PCIDevice *pcid = PCI_DEVICE(cmd->state);
31349ab747fSPaolo Bonzini     uint32_t pa_hi = 0, pa_lo;
31449ab747fSPaolo Bonzini     hwaddr pa;
315134550bfSPaolo Bonzini     int frame_sense_len;
31649ab747fSPaolo Bonzini 
317134550bfSPaolo Bonzini     frame_sense_len = cmd->frame->header.sense_len;
318134550bfSPaolo Bonzini     if (sense_len > frame_sense_len) {
319134550bfSPaolo Bonzini         sense_len = frame_sense_len;
32049ab747fSPaolo Bonzini     }
32149ab747fSPaolo Bonzini     if (sense_len) {
32249ab747fSPaolo Bonzini         pa_lo = le32_to_cpu(cmd->frame->pass.sense_addr_lo);
32349ab747fSPaolo Bonzini         if (megasas_frame_is_sense64(cmd)) {
32449ab747fSPaolo Bonzini             pa_hi = le32_to_cpu(cmd->frame->pass.sense_addr_hi);
32549ab747fSPaolo Bonzini         }
32649ab747fSPaolo Bonzini         pa = ((uint64_t) pa_hi << 32) | pa_lo;
3271016b239SPaolo Bonzini         pci_dma_write(pcid, pa, sense_ptr, sense_len);
32849ab747fSPaolo Bonzini         cmd->frame->header.sense_len = sense_len;
32949ab747fSPaolo Bonzini     }
33049ab747fSPaolo Bonzini     return sense_len;
33149ab747fSPaolo Bonzini }
33249ab747fSPaolo Bonzini 
33349ab747fSPaolo Bonzini static void megasas_write_sense(MegasasCmd *cmd, SCSISense sense)
33449ab747fSPaolo Bonzini {
33549ab747fSPaolo Bonzini     uint8_t sense_buf[SCSI_SENSE_BUF_SIZE];
33649ab747fSPaolo Bonzini     uint8_t sense_len = 18;
33749ab747fSPaolo Bonzini 
33849ab747fSPaolo Bonzini     memset(sense_buf, 0, sense_len);
33949ab747fSPaolo Bonzini     sense_buf[0] = 0xf0;
34049ab747fSPaolo Bonzini     sense_buf[2] = sense.key;
34149ab747fSPaolo Bonzini     sense_buf[7] = 10;
34249ab747fSPaolo Bonzini     sense_buf[12] = sense.asc;
34349ab747fSPaolo Bonzini     sense_buf[13] = sense.ascq;
34449ab747fSPaolo Bonzini     megasas_build_sense(cmd, sense_buf, sense_len);
34549ab747fSPaolo Bonzini }
34649ab747fSPaolo Bonzini 
34749ab747fSPaolo Bonzini static void megasas_copy_sense(MegasasCmd *cmd)
34849ab747fSPaolo Bonzini {
34949ab747fSPaolo Bonzini     uint8_t sense_buf[SCSI_SENSE_BUF_SIZE];
35049ab747fSPaolo Bonzini     uint8_t sense_len;
35149ab747fSPaolo Bonzini 
35249ab747fSPaolo Bonzini     sense_len = scsi_req_get_sense(cmd->req, sense_buf,
35349ab747fSPaolo Bonzini                                    SCSI_SENSE_BUF_SIZE);
35449ab747fSPaolo Bonzini     megasas_build_sense(cmd, sense_buf, sense_len);
35549ab747fSPaolo Bonzini }
35649ab747fSPaolo Bonzini 
35749ab747fSPaolo Bonzini /*
35849ab747fSPaolo Bonzini  * Format an INQUIRY CDB
35949ab747fSPaolo Bonzini  */
36049ab747fSPaolo Bonzini static int megasas_setup_inquiry(uint8_t *cdb, int pg, int len)
36149ab747fSPaolo Bonzini {
36249ab747fSPaolo Bonzini     memset(cdb, 0, 6);
36349ab747fSPaolo Bonzini     cdb[0] = INQUIRY;
36449ab747fSPaolo Bonzini     if (pg > 0) {
36549ab747fSPaolo Bonzini         cdb[1] = 0x1;
36649ab747fSPaolo Bonzini         cdb[2] = pg;
36749ab747fSPaolo Bonzini     }
36849ab747fSPaolo Bonzini     cdb[3] = (len >> 8) & 0xff;
36949ab747fSPaolo Bonzini     cdb[4] = (len & 0xff);
37049ab747fSPaolo Bonzini     return len;
37149ab747fSPaolo Bonzini }
37249ab747fSPaolo Bonzini 
37349ab747fSPaolo Bonzini /*
37449ab747fSPaolo Bonzini  * Encode lba and len into a READ_16/WRITE_16 CDB
37549ab747fSPaolo Bonzini  */
37649ab747fSPaolo Bonzini static void megasas_encode_lba(uint8_t *cdb, uint64_t lba,
37749ab747fSPaolo Bonzini                                uint32_t len, bool is_write)
37849ab747fSPaolo Bonzini {
37949ab747fSPaolo Bonzini     memset(cdb, 0x0, 16);
38049ab747fSPaolo Bonzini     if (is_write) {
38149ab747fSPaolo Bonzini         cdb[0] = WRITE_16;
38249ab747fSPaolo Bonzini     } else {
38349ab747fSPaolo Bonzini         cdb[0] = READ_16;
38449ab747fSPaolo Bonzini     }
38549ab747fSPaolo Bonzini     cdb[2] = (lba >> 56) & 0xff;
38649ab747fSPaolo Bonzini     cdb[3] = (lba >> 48) & 0xff;
38749ab747fSPaolo Bonzini     cdb[4] = (lba >> 40) & 0xff;
38849ab747fSPaolo Bonzini     cdb[5] = (lba >> 32) & 0xff;
38949ab747fSPaolo Bonzini     cdb[6] = (lba >> 24) & 0xff;
39049ab747fSPaolo Bonzini     cdb[7] = (lba >> 16) & 0xff;
39149ab747fSPaolo Bonzini     cdb[8] = (lba >> 8) & 0xff;
39249ab747fSPaolo Bonzini     cdb[9] = (lba) & 0xff;
39349ab747fSPaolo Bonzini     cdb[10] = (len >> 24) & 0xff;
39449ab747fSPaolo Bonzini     cdb[11] = (len >> 16) & 0xff;
39549ab747fSPaolo Bonzini     cdb[12] = (len >> 8) & 0xff;
39649ab747fSPaolo Bonzini     cdb[13] = (len) & 0xff;
39749ab747fSPaolo Bonzini }
39849ab747fSPaolo Bonzini 
39949ab747fSPaolo Bonzini /*
40049ab747fSPaolo Bonzini  * Utility functions
40149ab747fSPaolo Bonzini  */
40249ab747fSPaolo Bonzini static uint64_t megasas_fw_time(void)
40349ab747fSPaolo Bonzini {
40449ab747fSPaolo Bonzini     struct tm curtime;
40549ab747fSPaolo Bonzini 
40649ab747fSPaolo Bonzini     qemu_get_timedate(&curtime, 0);
4079be38598SEduardo Habkost     return ((uint64_t)curtime.tm_sec & 0xff) << 48 |
40849ab747fSPaolo Bonzini         ((uint64_t)curtime.tm_min & 0xff)  << 40 |
40949ab747fSPaolo Bonzini         ((uint64_t)curtime.tm_hour & 0xff) << 32 |
41049ab747fSPaolo Bonzini         ((uint64_t)curtime.tm_mday & 0xff) << 24 |
41149ab747fSPaolo Bonzini         ((uint64_t)curtime.tm_mon & 0xff)  << 16 |
41249ab747fSPaolo Bonzini         ((uint64_t)(curtime.tm_year + 1900) & 0xffff);
41349ab747fSPaolo Bonzini }
41449ab747fSPaolo Bonzini 
41549ab747fSPaolo Bonzini /*
41649ab747fSPaolo Bonzini  * Default disk sata address
41749ab747fSPaolo Bonzini  * 0x1221 is the magic number as
41849ab747fSPaolo Bonzini  * present in real hardware,
41949ab747fSPaolo Bonzini  * so use it here, too.
42049ab747fSPaolo Bonzini  */
42149ab747fSPaolo Bonzini static uint64_t megasas_get_sata_addr(uint16_t id)
42249ab747fSPaolo Bonzini {
42349ab747fSPaolo Bonzini     uint64_t addr = (0x1221ULL << 48);
4248ef2eb8dSPaolo Bonzini     return addr | ((uint64_t)id << 24);
42549ab747fSPaolo Bonzini }
42649ab747fSPaolo Bonzini 
42749ab747fSPaolo Bonzini /*
42849ab747fSPaolo Bonzini  * Frame handling
42949ab747fSPaolo Bonzini  */
43049ab747fSPaolo Bonzini static int megasas_next_index(MegasasState *s, int index, int limit)
43149ab747fSPaolo Bonzini {
43249ab747fSPaolo Bonzini     index++;
43349ab747fSPaolo Bonzini     if (index == limit) {
43449ab747fSPaolo Bonzini         index = 0;
43549ab747fSPaolo Bonzini     }
43649ab747fSPaolo Bonzini     return index;
43749ab747fSPaolo Bonzini }
43849ab747fSPaolo Bonzini 
43949ab747fSPaolo Bonzini static MegasasCmd *megasas_lookup_frame(MegasasState *s,
44049ab747fSPaolo Bonzini     hwaddr frame)
44149ab747fSPaolo Bonzini {
44249ab747fSPaolo Bonzini     MegasasCmd *cmd = NULL;
44349ab747fSPaolo Bonzini     int num = 0, index;
44449ab747fSPaolo Bonzini 
44549ab747fSPaolo Bonzini     index = s->reply_queue_head;
44649ab747fSPaolo Bonzini 
44749ab747fSPaolo Bonzini     while (num < s->fw_cmds) {
44849ab747fSPaolo Bonzini         if (s->frames[index].pa && s->frames[index].pa == frame) {
44949ab747fSPaolo Bonzini             cmd = &s->frames[index];
45049ab747fSPaolo Bonzini             break;
45149ab747fSPaolo Bonzini         }
45249ab747fSPaolo Bonzini         index = megasas_next_index(s, index, s->fw_cmds);
45349ab747fSPaolo Bonzini         num++;
45449ab747fSPaolo Bonzini     }
45549ab747fSPaolo Bonzini 
45649ab747fSPaolo Bonzini     return cmd;
45749ab747fSPaolo Bonzini }
45849ab747fSPaolo Bonzini 
4596df5718bSHannes Reinecke static void megasas_unmap_frame(MegasasState *s, MegasasCmd *cmd)
46049ab747fSPaolo Bonzini {
4616df5718bSHannes Reinecke     PCIDevice *p = PCI_DEVICE(s);
46249ab747fSPaolo Bonzini 
46375f19f8cSPaolo Bonzini     if (cmd->pa_size) {
4646df5718bSHannes Reinecke         pci_dma_unmap(p, cmd->frame, cmd->pa_size, 0, 0);
46575f19f8cSPaolo Bonzini     }
4666df5718bSHannes Reinecke     cmd->frame = NULL;
4676df5718bSHannes Reinecke     cmd->pa = 0;
46875f19f8cSPaolo Bonzini     cmd->pa_size = 0;
46948285ba2SMarc-André Lureau     qemu_sglist_destroy(&cmd->qsg);
4706df5718bSHannes Reinecke     clear_bit(cmd->index, s->frame_map);
47149ab747fSPaolo Bonzini }
47249ab747fSPaolo Bonzini 
4736df5718bSHannes Reinecke /*
4746df5718bSHannes Reinecke  * This absolutely needs to be locked if
4756df5718bSHannes Reinecke  * qemu ever goes multithreaded.
4766df5718bSHannes Reinecke  */
47749ab747fSPaolo Bonzini static MegasasCmd *megasas_enqueue_frame(MegasasState *s,
47849ab747fSPaolo Bonzini     hwaddr frame, uint64_t context, int count)
47949ab747fSPaolo Bonzini {
4801016b239SPaolo Bonzini     PCIDevice *pcid = PCI_DEVICE(s);
48149ab747fSPaolo Bonzini     MegasasCmd *cmd = NULL;
4822e56fbc8SPeter Lieven     int frame_size = MEGASAS_MAX_SGE * sizeof(union mfi_sgl);
48349ab747fSPaolo Bonzini     hwaddr frame_size_p = frame_size;
4846df5718bSHannes Reinecke     unsigned long index;
48549ab747fSPaolo Bonzini 
4866df5718bSHannes Reinecke     index = 0;
4876df5718bSHannes Reinecke     while (index < s->fw_cmds) {
4886df5718bSHannes Reinecke         index = find_next_zero_bit(s->frame_map, s->fw_cmds, index);
4896df5718bSHannes Reinecke         if (!s->frames[index].pa)
4906df5718bSHannes Reinecke             break;
4916df5718bSHannes Reinecke         /* Busy frame found */
4926df5718bSHannes Reinecke         trace_megasas_qf_mapped(index);
4936df5718bSHannes Reinecke     }
4946df5718bSHannes Reinecke     if (index >= s->fw_cmds) {
49549ab747fSPaolo Bonzini         /* All frames busy */
4966df5718bSHannes Reinecke         trace_megasas_qf_busy(frame);
49749ab747fSPaolo Bonzini         return NULL;
49849ab747fSPaolo Bonzini     }
4996df5718bSHannes Reinecke     cmd = &s->frames[index];
5006df5718bSHannes Reinecke     set_bit(index, s->frame_map);
5016df5718bSHannes Reinecke     trace_megasas_qf_new(index, frame);
5026df5718bSHannes Reinecke 
50349ab747fSPaolo Bonzini     cmd->pa = frame;
50449ab747fSPaolo Bonzini     /* Map all possible frames */
5051016b239SPaolo Bonzini     cmd->frame = pci_dma_map(pcid, frame, &frame_size_p, 0);
50649ab747fSPaolo Bonzini     if (frame_size_p != frame_size) {
50749ab747fSPaolo Bonzini         trace_megasas_qf_map_failed(cmd->index, (unsigned long)frame);
50849ab747fSPaolo Bonzini         if (cmd->frame) {
5096df5718bSHannes Reinecke             megasas_unmap_frame(s, cmd);
51049ab747fSPaolo Bonzini         }
51149ab747fSPaolo Bonzini         s->event_count++;
51249ab747fSPaolo Bonzini         return NULL;
51349ab747fSPaolo Bonzini     }
51449ab747fSPaolo Bonzini     cmd->pa_size = frame_size_p;
51549ab747fSPaolo Bonzini     cmd->context = context;
51649ab747fSPaolo Bonzini     if (!megasas_use_queue64(s)) {
51749ab747fSPaolo Bonzini         cmd->context &= (uint64_t)0xFFFFFFFF;
51849ab747fSPaolo Bonzini     }
51949ab747fSPaolo Bonzini     cmd->count = count;
5205104fac8SPaolo Bonzini     cmd->dcmd_opcode = -1;
52149ab747fSPaolo Bonzini     s->busy++;
52249ab747fSPaolo Bonzini 
523aaf2a859SHannes Reinecke     if (s->consumer_pa) {
52416578c6fSPaolo Bonzini         s->reply_queue_tail = ldl_le_pci_dma(pcid, s->consumer_pa);
525aaf2a859SHannes Reinecke     }
52649ab747fSPaolo Bonzini     trace_megasas_qf_enqueue(cmd->index, cmd->count, cmd->context,
527aaf2a859SHannes Reinecke                              s->reply_queue_head, s->reply_queue_tail, s->busy);
52849ab747fSPaolo Bonzini 
52949ab747fSPaolo Bonzini     return cmd;
53049ab747fSPaolo Bonzini }
53149ab747fSPaolo Bonzini 
53249ab747fSPaolo Bonzini static void megasas_complete_frame(MegasasState *s, uint64_t context)
53349ab747fSPaolo Bonzini {
53452190c1eSAndreas Färber     PCIDevice *pci_dev = PCI_DEVICE(s);
53549ab747fSPaolo Bonzini     int tail, queue_offset;
53649ab747fSPaolo Bonzini 
53749ab747fSPaolo Bonzini     /* Decrement busy count */
53849ab747fSPaolo Bonzini     s->busy--;
53949ab747fSPaolo Bonzini     if (s->reply_queue_pa) {
54049ab747fSPaolo Bonzini         /*
54149ab747fSPaolo Bonzini          * Put command on the reply queue.
54249ab747fSPaolo Bonzini          * Context is opaque, but emulation is running in
54349ab747fSPaolo Bonzini          * little endian. So convert it.
54449ab747fSPaolo Bonzini          */
54549ab747fSPaolo Bonzini         if (megasas_use_queue64(s)) {
5467957ee71SHannes Reinecke             queue_offset = s->reply_queue_head * sizeof(uint64_t);
54716578c6fSPaolo Bonzini             stq_le_pci_dma(pci_dev, s->reply_queue_pa + queue_offset, context);
54849ab747fSPaolo Bonzini         } else {
5497957ee71SHannes Reinecke             queue_offset = s->reply_queue_head * sizeof(uint32_t);
55016578c6fSPaolo Bonzini             stl_le_pci_dma(pci_dev, s->reply_queue_pa + queue_offset, context);
55149ab747fSPaolo Bonzini         }
55216578c6fSPaolo Bonzini         s->reply_queue_tail = ldl_le_pci_dma(pci_dev, s->consumer_pa);
553aaf2a859SHannes Reinecke         trace_megasas_qf_complete(context, s->reply_queue_head,
5547957ee71SHannes Reinecke                                   s->reply_queue_tail, s->busy);
55549ab747fSPaolo Bonzini     }
55649ab747fSPaolo Bonzini 
55749ab747fSPaolo Bonzini     if (megasas_intr_enabled(s)) {
5587957ee71SHannes Reinecke         /* Update reply queue pointer */
55916578c6fSPaolo Bonzini         s->reply_queue_tail = ldl_le_pci_dma(pci_dev, s->consumer_pa);
5607957ee71SHannes Reinecke         tail = s->reply_queue_head;
5617957ee71SHannes Reinecke         s->reply_queue_head = megasas_next_index(s, tail, s->fw_cmds);
5627957ee71SHannes Reinecke         trace_megasas_qf_update(s->reply_queue_head, s->reply_queue_tail,
5637957ee71SHannes Reinecke                                 s->busy);
56416578c6fSPaolo Bonzini         stl_le_pci_dma(pci_dev, s->producer_pa, s->reply_queue_head);
56549ab747fSPaolo Bonzini         /* Notify HBA */
56652190c1eSAndreas Färber         if (msix_enabled(pci_dev)) {
56749ab747fSPaolo Bonzini             trace_megasas_msix_raise(0);
56852190c1eSAndreas Färber             msix_notify(pci_dev, 0);
5694522b69cSHannes Reinecke         } else if (msi_enabled(pci_dev)) {
5704522b69cSHannes Reinecke             trace_megasas_msi_raise(0);
5714522b69cSHannes Reinecke             msi_notify(pci_dev, 0);
57249ab747fSPaolo Bonzini         } else {
5737957ee71SHannes Reinecke             s->doorbell++;
5747957ee71SHannes Reinecke             if (s->doorbell == 1) {
57549ab747fSPaolo Bonzini                 trace_megasas_irq_raise();
5769e64f8a3SMarcel Apfelbaum                 pci_irq_assert(pci_dev);
57749ab747fSPaolo Bonzini             }
57849ab747fSPaolo Bonzini         }
57949ab747fSPaolo Bonzini     } else {
58049ab747fSPaolo Bonzini         trace_megasas_qf_complete_noirq(context);
58149ab747fSPaolo Bonzini     }
58249ab747fSPaolo Bonzini }
58349ab747fSPaolo Bonzini 
5849e55d588SPaolo Bonzini static void megasas_complete_command(MegasasCmd *cmd)
5859e55d588SPaolo Bonzini {
5869e55d588SPaolo Bonzini     cmd->iov_size = 0;
5879e55d588SPaolo Bonzini     cmd->iov_offset = 0;
5889e55d588SPaolo Bonzini 
5899e55d588SPaolo Bonzini     cmd->req->hba_private = NULL;
5909e55d588SPaolo Bonzini     scsi_req_unref(cmd->req);
5919e55d588SPaolo Bonzini     cmd->req = NULL;
5929e55d588SPaolo Bonzini 
5939e55d588SPaolo Bonzini     megasas_unmap_frame(cmd->state, cmd);
5949e55d588SPaolo Bonzini     megasas_complete_frame(cmd->state, cmd->context);
5959e55d588SPaolo Bonzini }
5969e55d588SPaolo Bonzini 
59749ab747fSPaolo Bonzini static void megasas_reset_frames(MegasasState *s)
59849ab747fSPaolo Bonzini {
59949ab747fSPaolo Bonzini     int i;
60049ab747fSPaolo Bonzini     MegasasCmd *cmd;
60149ab747fSPaolo Bonzini 
60249ab747fSPaolo Bonzini     for (i = 0; i < s->fw_cmds; i++) {
60349ab747fSPaolo Bonzini         cmd = &s->frames[i];
60449ab747fSPaolo Bonzini         if (cmd->pa) {
6056df5718bSHannes Reinecke             megasas_unmap_frame(s, cmd);
60649ab747fSPaolo Bonzini         }
60749ab747fSPaolo Bonzini     }
6086df5718bSHannes Reinecke     bitmap_zero(s->frame_map, MEGASAS_MAX_FRAMES);
60949ab747fSPaolo Bonzini }
61049ab747fSPaolo Bonzini 
61149ab747fSPaolo Bonzini static void megasas_abort_command(MegasasCmd *cmd)
61249ab747fSPaolo Bonzini {
6139e55d588SPaolo Bonzini     /* Never abort internal commands.  */
61487e459a8SPaolo Bonzini     if (cmd->dcmd_opcode != -1) {
61587e459a8SPaolo Bonzini         return;
61687e459a8SPaolo Bonzini     }
6179e55d588SPaolo Bonzini     if (cmd->req != NULL) {
61849ab747fSPaolo Bonzini         scsi_req_cancel(cmd->req);
61949ab747fSPaolo Bonzini     }
62049ab747fSPaolo Bonzini }
62149ab747fSPaolo Bonzini 
62249ab747fSPaolo Bonzini static int megasas_init_firmware(MegasasState *s, MegasasCmd *cmd)
62349ab747fSPaolo Bonzini {
6241016b239SPaolo Bonzini     PCIDevice *pcid = PCI_DEVICE(s);
62549ab747fSPaolo Bonzini     uint32_t pa_hi, pa_lo;
62696f8f23aSHannes Reinecke     hwaddr iq_pa, initq_size = sizeof(struct mfi_init_qinfo);
62796f8f23aSHannes Reinecke     struct mfi_init_qinfo *initq = NULL;
62849ab747fSPaolo Bonzini     uint32_t flags;
62949ab747fSPaolo Bonzini     int ret = MFI_STAT_OK;
63049ab747fSPaolo Bonzini 
63196f8f23aSHannes Reinecke     if (s->reply_queue_pa) {
63296f8f23aSHannes Reinecke         trace_megasas_initq_mapped(s->reply_queue_pa);
63396f8f23aSHannes Reinecke         goto out;
63496f8f23aSHannes Reinecke     }
63549ab747fSPaolo Bonzini     pa_lo = le32_to_cpu(cmd->frame->init.qinfo_new_addr_lo);
63649ab747fSPaolo Bonzini     pa_hi = le32_to_cpu(cmd->frame->init.qinfo_new_addr_hi);
63749ab747fSPaolo Bonzini     iq_pa = (((uint64_t) pa_hi << 32) | pa_lo);
63849ab747fSPaolo Bonzini     trace_megasas_init_firmware((uint64_t)iq_pa);
6391016b239SPaolo Bonzini     initq = pci_dma_map(pcid, iq_pa, &initq_size, 0);
64049ab747fSPaolo Bonzini     if (!initq || initq_size != sizeof(*initq)) {
64149ab747fSPaolo Bonzini         trace_megasas_initq_map_failed(cmd->index);
64249ab747fSPaolo Bonzini         s->event_count++;
64349ab747fSPaolo Bonzini         ret = MFI_STAT_MEMORY_NOT_AVAILABLE;
64449ab747fSPaolo Bonzini         goto out;
64549ab747fSPaolo Bonzini     }
64649ab747fSPaolo Bonzini     s->reply_queue_len = le32_to_cpu(initq->rq_entries) & 0xFFFF;
64749ab747fSPaolo Bonzini     if (s->reply_queue_len > s->fw_cmds) {
64849ab747fSPaolo Bonzini         trace_megasas_initq_mismatch(s->reply_queue_len, s->fw_cmds);
64949ab747fSPaolo Bonzini         s->event_count++;
65049ab747fSPaolo Bonzini         ret = MFI_STAT_INVALID_PARAMETER;
65149ab747fSPaolo Bonzini         goto out;
65249ab747fSPaolo Bonzini     }
65349ab747fSPaolo Bonzini     pa_lo = le32_to_cpu(initq->rq_addr_lo);
65449ab747fSPaolo Bonzini     pa_hi = le32_to_cpu(initq->rq_addr_hi);
65549ab747fSPaolo Bonzini     s->reply_queue_pa = ((uint64_t) pa_hi << 32) | pa_lo;
65649ab747fSPaolo Bonzini     pa_lo = le32_to_cpu(initq->ci_addr_lo);
65749ab747fSPaolo Bonzini     pa_hi = le32_to_cpu(initq->ci_addr_hi);
65849ab747fSPaolo Bonzini     s->consumer_pa = ((uint64_t) pa_hi << 32) | pa_lo;
65949ab747fSPaolo Bonzini     pa_lo = le32_to_cpu(initq->pi_addr_lo);
66049ab747fSPaolo Bonzini     pa_hi = le32_to_cpu(initq->pi_addr_hi);
66149ab747fSPaolo Bonzini     s->producer_pa = ((uint64_t) pa_hi << 32) | pa_lo;
66216578c6fSPaolo Bonzini     s->reply_queue_head = ldl_le_pci_dma(pcid, s->producer_pa);
663b60bdd1fSPrasad J Pandit     s->reply_queue_head %= MEGASAS_MAX_FRAMES;
66416578c6fSPaolo Bonzini     s->reply_queue_tail = ldl_le_pci_dma(pcid, s->consumer_pa);
665b60bdd1fSPrasad J Pandit     s->reply_queue_tail %= MEGASAS_MAX_FRAMES;
66649ab747fSPaolo Bonzini     flags = le32_to_cpu(initq->flags);
66749ab747fSPaolo Bonzini     if (flags & MFI_QUEUE_FLAG_CONTEXT64) {
66849ab747fSPaolo Bonzini         s->flags |= MEGASAS_MASK_USE_QUEUE64;
66949ab747fSPaolo Bonzini     }
67049ab747fSPaolo Bonzini     trace_megasas_init_queue((unsigned long)s->reply_queue_pa,
67149ab747fSPaolo Bonzini                              s->reply_queue_len, s->reply_queue_head,
67249ab747fSPaolo Bonzini                              s->reply_queue_tail, flags);
67349ab747fSPaolo Bonzini     megasas_reset_frames(s);
67449ab747fSPaolo Bonzini     s->fw_state = MFI_FWSTATE_OPERATIONAL;
67549ab747fSPaolo Bonzini out:
67649ab747fSPaolo Bonzini     if (initq) {
6771016b239SPaolo Bonzini         pci_dma_unmap(pcid, initq, initq_size, 0, 0);
67849ab747fSPaolo Bonzini     }
67949ab747fSPaolo Bonzini     return ret;
68049ab747fSPaolo Bonzini }
68149ab747fSPaolo Bonzini 
68249ab747fSPaolo Bonzini static int megasas_map_dcmd(MegasasState *s, MegasasCmd *cmd)
68349ab747fSPaolo Bonzini {
68449ab747fSPaolo Bonzini     dma_addr_t iov_pa, iov_size;
68524c0c77aSPaolo Bonzini     int iov_count;
68649ab747fSPaolo Bonzini 
68749ab747fSPaolo Bonzini     cmd->flags = le16_to_cpu(cmd->frame->header.flags);
68824c0c77aSPaolo Bonzini     iov_count = cmd->frame->header.sge_count;
68924c0c77aSPaolo Bonzini     if (!iov_count) {
69049ab747fSPaolo Bonzini         trace_megasas_dcmd_zero_sge(cmd->index);
69149ab747fSPaolo Bonzini         cmd->iov_size = 0;
69249ab747fSPaolo Bonzini         return 0;
69324c0c77aSPaolo Bonzini     } else if (iov_count > 1) {
69424c0c77aSPaolo Bonzini         trace_megasas_dcmd_invalid_sge(cmd->index, iov_count);
69549ab747fSPaolo Bonzini         cmd->iov_size = 0;
696765a7070SPaolo Bonzini         return -EINVAL;
69749ab747fSPaolo Bonzini     }
69849ab747fSPaolo Bonzini     iov_pa = megasas_sgl_get_addr(cmd, &cmd->frame->dcmd.sgl);
69949ab747fSPaolo Bonzini     iov_size = megasas_sgl_get_len(cmd, &cmd->frame->dcmd.sgl);
70052190c1eSAndreas Färber     pci_dma_sglist_init(&cmd->qsg, PCI_DEVICE(s), 1);
70149ab747fSPaolo Bonzini     qemu_sglist_add(&cmd->qsg, iov_pa, iov_size);
70249ab747fSPaolo Bonzini     cmd->iov_size = iov_size;
703765a7070SPaolo Bonzini     return 0;
70449ab747fSPaolo Bonzini }
70549ab747fSPaolo Bonzini 
70649ab747fSPaolo Bonzini static void megasas_finish_dcmd(MegasasCmd *cmd, uint32_t iov_size)
70749ab747fSPaolo Bonzini {
70849ab747fSPaolo Bonzini     trace_megasas_finish_dcmd(cmd->index, iov_size);
70949ab747fSPaolo Bonzini 
71049ab747fSPaolo Bonzini     if (iov_size > cmd->iov_size) {
71149ab747fSPaolo Bonzini         if (megasas_frame_is_ieee_sgl(cmd)) {
71249ab747fSPaolo Bonzini             cmd->frame->dcmd.sgl.sg_skinny->len = cpu_to_le32(iov_size);
71349ab747fSPaolo Bonzini         } else if (megasas_frame_is_sgl64(cmd)) {
71449ab747fSPaolo Bonzini             cmd->frame->dcmd.sgl.sg64->len = cpu_to_le32(iov_size);
71549ab747fSPaolo Bonzini         } else {
71649ab747fSPaolo Bonzini             cmd->frame->dcmd.sgl.sg32->len = cpu_to_le32(iov_size);
71749ab747fSPaolo Bonzini         }
71849ab747fSPaolo Bonzini     }
71949ab747fSPaolo Bonzini }
72049ab747fSPaolo Bonzini 
72149ab747fSPaolo Bonzini static int megasas_ctrl_get_info(MegasasState *s, MegasasCmd *cmd)
72249ab747fSPaolo Bonzini {
72352190c1eSAndreas Färber     PCIDevice *pci_dev = PCI_DEVICE(s);
724e23d0498SHannes Reinecke     PCIDeviceClass *pci_class = PCI_DEVICE_GET_CLASS(pci_dev);
725e23d0498SHannes Reinecke     MegasasBaseClass *base_class = MEGASAS_DEVICE_GET_CLASS(s);
72649ab747fSPaolo Bonzini     struct mfi_ctrl_info info;
72749ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
72849ab747fSPaolo Bonzini     BusChild *kid;
7293f2cd4ddSHannes Reinecke     int num_pd_disks = 0;
73049ab747fSPaolo Bonzini 
73136fef36bSP J P     memset(&info, 0x0, dcmd_size);
73249ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
73349ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
73449ab747fSPaolo Bonzini                                             dcmd_size);
73549ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
73649ab747fSPaolo Bonzini     }
73749ab747fSPaolo Bonzini 
738e23d0498SHannes Reinecke     info.pci.vendor = cpu_to_le16(pci_class->vendor_id);
739e23d0498SHannes Reinecke     info.pci.device = cpu_to_le16(pci_class->device_id);
740e23d0498SHannes Reinecke     info.pci.subvendor = cpu_to_le16(pci_class->subsystem_vendor_id);
741e23d0498SHannes Reinecke     info.pci.subdevice = cpu_to_le16(pci_class->subsystem_id);
74249ab747fSPaolo Bonzini 
74349ab747fSPaolo Bonzini     /*
74449ab747fSPaolo Bonzini      * For some reason the firmware supports
74549ab747fSPaolo Bonzini      * only up to 8 device ports.
74649ab747fSPaolo Bonzini      * Despite supporting a far larger number
74749ab747fSPaolo Bonzini      * of devices for the physical devices.
74849ab747fSPaolo Bonzini      * So just display the first 8 devices
74949ab747fSPaolo Bonzini      * in the device port list, independent
75049ab747fSPaolo Bonzini      * of how many logical devices are actually
75149ab747fSPaolo Bonzini      * present.
75249ab747fSPaolo Bonzini      */
75349ab747fSPaolo Bonzini     info.host.type = MFI_INFO_HOST_PCIE;
75449ab747fSPaolo Bonzini     info.device.type = MFI_INFO_DEV_SAS3G;
75549ab747fSPaolo Bonzini     info.device.port_count = 8;
75649ab747fSPaolo Bonzini     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
757e1dc6815SCao jin         SCSIDevice *sdev = SCSI_DEVICE(kid->child);
7583f2cd4ddSHannes Reinecke         uint16_t pd_id;
75949ab747fSPaolo Bonzini 
7603f2cd4ddSHannes Reinecke         if (num_pd_disks < 8) {
7613f2cd4ddSHannes Reinecke             pd_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
7623f2cd4ddSHannes Reinecke             info.device.port_addr[num_pd_disks] =
7633f2cd4ddSHannes Reinecke                 cpu_to_le64(megasas_get_sata_addr(pd_id));
76449ab747fSPaolo Bonzini         }
7653f2cd4ddSHannes Reinecke         num_pd_disks++;
76649ab747fSPaolo Bonzini     }
76749ab747fSPaolo Bonzini 
768e23d0498SHannes Reinecke     memcpy(info.product_name, base_class->product_name, 24);
76949ab747fSPaolo Bonzini     snprintf(info.serial_number, 32, "%s", s->hba_serial);
77069fbd0eaSEduardo Habkost     snprintf(info.package_version, 0x60, "%s-QEMU", qemu_hw_version());
77149ab747fSPaolo Bonzini     memcpy(info.image_component[0].name, "APP", 3);
772e23d0498SHannes Reinecke     snprintf(info.image_component[0].version, 10, "%s-QEMU",
773e23d0498SHannes Reinecke              base_class->product_version);
7745a7733b0SOlaf Hering     memcpy(info.image_component[0].build_date, "Apr  1 2014", 11);
7755a7733b0SOlaf Hering     memcpy(info.image_component[0].build_time, "12:34:56", 8);
77649ab747fSPaolo Bonzini     info.image_component_count = 1;
77752190c1eSAndreas Färber     if (pci_dev->has_rom) {
77849ab747fSPaolo Bonzini         uint8_t biosver[32];
77949ab747fSPaolo Bonzini         uint8_t *ptr;
78049ab747fSPaolo Bonzini 
78152190c1eSAndreas Färber         ptr = memory_region_get_ram_ptr(&pci_dev->rom);
78249ab747fSPaolo Bonzini         memcpy(biosver, ptr + 0x41, 31);
783844864fbSPrasad J Pandit         biosver[31] = 0;
78449ab747fSPaolo Bonzini         memcpy(info.image_component[1].name, "BIOS", 4);
78549ab747fSPaolo Bonzini         memcpy(info.image_component[1].version, biosver,
78649ab747fSPaolo Bonzini                strlen((const char *)biosver));
78749ab747fSPaolo Bonzini         info.image_component_count++;
78849ab747fSPaolo Bonzini     }
78949ab747fSPaolo Bonzini     info.current_fw_time = cpu_to_le32(megasas_fw_time());
79049ab747fSPaolo Bonzini     info.max_arms = 32;
79149ab747fSPaolo Bonzini     info.max_spans = 8;
79249ab747fSPaolo Bonzini     info.max_arrays = MEGASAS_MAX_ARRAYS;
7933f2cd4ddSHannes Reinecke     info.max_lds = MFI_MAX_LD;
79449ab747fSPaolo Bonzini     info.max_cmds = cpu_to_le16(s->fw_cmds);
79549ab747fSPaolo Bonzini     info.max_sg_elements = cpu_to_le16(s->fw_sge);
79649ab747fSPaolo Bonzini     info.max_request_size = cpu_to_le32(MEGASAS_MAX_SECTORS);
7973f2cd4ddSHannes Reinecke     if (!megasas_is_jbod(s))
7983f2cd4ddSHannes Reinecke         info.lds_present = cpu_to_le16(num_pd_disks);
7993f2cd4ddSHannes Reinecke     info.pd_present = cpu_to_le16(num_pd_disks);
8003f2cd4ddSHannes Reinecke     info.pd_disks_present = cpu_to_le16(num_pd_disks);
80149ab747fSPaolo Bonzini     info.hw_present = cpu_to_le32(MFI_INFO_HW_NVRAM |
80249ab747fSPaolo Bonzini                                    MFI_INFO_HW_MEM |
80349ab747fSPaolo Bonzini                                    MFI_INFO_HW_FLASH);
80449ab747fSPaolo Bonzini     info.memory_size = cpu_to_le16(512);
80549ab747fSPaolo Bonzini     info.nvram_size = cpu_to_le16(32);
80649ab747fSPaolo Bonzini     info.flash_size = cpu_to_le16(16);
80749ab747fSPaolo Bonzini     info.raid_levels = cpu_to_le32(MFI_INFO_RAID_0);
80849ab747fSPaolo Bonzini     info.adapter_ops = cpu_to_le32(MFI_INFO_AOPS_RBLD_RATE |
80949ab747fSPaolo Bonzini                                     MFI_INFO_AOPS_SELF_DIAGNOSTIC |
81049ab747fSPaolo Bonzini                                     MFI_INFO_AOPS_MIXED_ARRAY);
81149ab747fSPaolo Bonzini     info.ld_ops = cpu_to_le32(MFI_INFO_LDOPS_DISK_CACHE_POLICY |
81249ab747fSPaolo Bonzini                                MFI_INFO_LDOPS_ACCESS_POLICY |
81349ab747fSPaolo Bonzini                                MFI_INFO_LDOPS_IO_POLICY |
81449ab747fSPaolo Bonzini                                MFI_INFO_LDOPS_WRITE_POLICY |
81549ab747fSPaolo Bonzini                                MFI_INFO_LDOPS_READ_POLICY);
81649ab747fSPaolo Bonzini     info.max_strips_per_io = cpu_to_le16(s->fw_sge);
81749ab747fSPaolo Bonzini     info.stripe_sz_ops.min = 3;
818786a4ea8SStefan Hajnoczi     info.stripe_sz_ops.max = ctz32(MEGASAS_MAX_SECTORS + 1);
81949ab747fSPaolo Bonzini     info.properties.pred_fail_poll_interval = cpu_to_le16(300);
82049ab747fSPaolo Bonzini     info.properties.intr_throttle_cnt = cpu_to_le16(16);
82149ab747fSPaolo Bonzini     info.properties.intr_throttle_timeout = cpu_to_le16(50);
82249ab747fSPaolo Bonzini     info.properties.rebuild_rate = 30;
82349ab747fSPaolo Bonzini     info.properties.patrol_read_rate = 30;
82449ab747fSPaolo Bonzini     info.properties.bgi_rate = 30;
82549ab747fSPaolo Bonzini     info.properties.cc_rate = 30;
82649ab747fSPaolo Bonzini     info.properties.recon_rate = 30;
82749ab747fSPaolo Bonzini     info.properties.cache_flush_interval = 4;
82849ab747fSPaolo Bonzini     info.properties.spinup_drv_cnt = 2;
82949ab747fSPaolo Bonzini     info.properties.spinup_delay = 6;
83049ab747fSPaolo Bonzini     info.properties.ecc_bucket_size = 15;
83149ab747fSPaolo Bonzini     info.properties.ecc_bucket_leak_rate = cpu_to_le16(1440);
83249ab747fSPaolo Bonzini     info.properties.expose_encl_devices = 1;
83349ab747fSPaolo Bonzini     info.properties.OnOffProperties = cpu_to_le32(MFI_CTRL_PROP_EnableJBOD);
83449ab747fSPaolo Bonzini     info.pd_ops = cpu_to_le32(MFI_INFO_PDOPS_FORCE_ONLINE |
83549ab747fSPaolo Bonzini                                MFI_INFO_PDOPS_FORCE_OFFLINE);
83649ab747fSPaolo Bonzini     info.pd_mix_support = cpu_to_le32(MFI_INFO_PDMIX_SAS |
83749ab747fSPaolo Bonzini                                        MFI_INFO_PDMIX_SATA |
83849ab747fSPaolo Bonzini                                        MFI_INFO_PDMIX_LD);
83949ab747fSPaolo Bonzini 
84049ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
84149ab747fSPaolo Bonzini     return MFI_STAT_OK;
84249ab747fSPaolo Bonzini }
84349ab747fSPaolo Bonzini 
84449ab747fSPaolo Bonzini static int megasas_mfc_get_defaults(MegasasState *s, MegasasCmd *cmd)
84549ab747fSPaolo Bonzini {
84649ab747fSPaolo Bonzini     struct mfi_defaults info;
84749ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(struct mfi_defaults);
84849ab747fSPaolo Bonzini 
84949ab747fSPaolo Bonzini     memset(&info, 0x0, dcmd_size);
85049ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
85149ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
85249ab747fSPaolo Bonzini                                             dcmd_size);
85349ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
85449ab747fSPaolo Bonzini     }
85549ab747fSPaolo Bonzini 
85649ab747fSPaolo Bonzini     info.sas_addr = cpu_to_le64(s->sas_addr);
85749ab747fSPaolo Bonzini     info.stripe_size = 3;
85849ab747fSPaolo Bonzini     info.flush_time = 4;
85949ab747fSPaolo Bonzini     info.background_rate = 30;
86049ab747fSPaolo Bonzini     info.allow_mix_in_enclosure = 1;
86149ab747fSPaolo Bonzini     info.allow_mix_in_ld = 1;
86249ab747fSPaolo Bonzini     info.direct_pd_mapping = 1;
86349ab747fSPaolo Bonzini     /* Enable for BIOS support */
86449ab747fSPaolo Bonzini     info.bios_enumerate_lds = 1;
86549ab747fSPaolo Bonzini     info.disable_ctrl_r = 1;
86649ab747fSPaolo Bonzini     info.expose_enclosure_devices = 1;
86749ab747fSPaolo Bonzini     info.disable_preboot_cli = 1;
86849ab747fSPaolo Bonzini     info.cluster_disable = 1;
86949ab747fSPaolo Bonzini 
87049ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
87149ab747fSPaolo Bonzini     return MFI_STAT_OK;
87249ab747fSPaolo Bonzini }
87349ab747fSPaolo Bonzini 
87449ab747fSPaolo Bonzini static int megasas_dcmd_get_bios_info(MegasasState *s, MegasasCmd *cmd)
87549ab747fSPaolo Bonzini {
87649ab747fSPaolo Bonzini     struct mfi_bios_data info;
87749ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
87849ab747fSPaolo Bonzini 
87949ab747fSPaolo Bonzini     memset(&info, 0x0, dcmd_size);
88049ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
88149ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
88249ab747fSPaolo Bonzini                                             dcmd_size);
88349ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
88449ab747fSPaolo Bonzini     }
88549ab747fSPaolo Bonzini     info.continue_on_error = 1;
88649ab747fSPaolo Bonzini     info.verbose = 1;
88749ab747fSPaolo Bonzini     if (megasas_is_jbod(s)) {
88849ab747fSPaolo Bonzini         info.expose_all_drives = 1;
88949ab747fSPaolo Bonzini     }
89049ab747fSPaolo Bonzini 
89149ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
89249ab747fSPaolo Bonzini     return MFI_STAT_OK;
89349ab747fSPaolo Bonzini }
89449ab747fSPaolo Bonzini 
89549ab747fSPaolo Bonzini static int megasas_dcmd_get_fw_time(MegasasState *s, MegasasCmd *cmd)
89649ab747fSPaolo Bonzini {
89749ab747fSPaolo Bonzini     uint64_t fw_time;
89849ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(fw_time);
89949ab747fSPaolo Bonzini 
90049ab747fSPaolo Bonzini     fw_time = cpu_to_le64(megasas_fw_time());
90149ab747fSPaolo Bonzini 
90249ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&fw_time, dcmd_size, &cmd->qsg);
90349ab747fSPaolo Bonzini     return MFI_STAT_OK;
90449ab747fSPaolo Bonzini }
90549ab747fSPaolo Bonzini 
90649ab747fSPaolo Bonzini static int megasas_dcmd_set_fw_time(MegasasState *s, MegasasCmd *cmd)
90749ab747fSPaolo Bonzini {
90849ab747fSPaolo Bonzini     uint64_t fw_time;
90949ab747fSPaolo Bonzini 
91049ab747fSPaolo Bonzini     /* This is a dummy; setting of firmware time is not allowed */
91149ab747fSPaolo Bonzini     memcpy(&fw_time, cmd->frame->dcmd.mbox, sizeof(fw_time));
91249ab747fSPaolo Bonzini 
91349ab747fSPaolo Bonzini     trace_megasas_dcmd_set_fw_time(cmd->index, fw_time);
91449ab747fSPaolo Bonzini     fw_time = cpu_to_le64(megasas_fw_time());
91549ab747fSPaolo Bonzini     return MFI_STAT_OK;
91649ab747fSPaolo Bonzini }
91749ab747fSPaolo Bonzini 
91849ab747fSPaolo Bonzini static int megasas_event_info(MegasasState *s, MegasasCmd *cmd)
91949ab747fSPaolo Bonzini {
92049ab747fSPaolo Bonzini     struct mfi_evt_log_state info;
92149ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
92249ab747fSPaolo Bonzini 
92349ab747fSPaolo Bonzini     memset(&info, 0, dcmd_size);
92449ab747fSPaolo Bonzini 
92549ab747fSPaolo Bonzini     info.newest_seq_num = cpu_to_le32(s->event_count);
92649ab747fSPaolo Bonzini     info.shutdown_seq_num = cpu_to_le32(s->shutdown_event);
92749ab747fSPaolo Bonzini     info.boot_seq_num = cpu_to_le32(s->boot_event);
92849ab747fSPaolo Bonzini 
92949ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
93049ab747fSPaolo Bonzini     return MFI_STAT_OK;
93149ab747fSPaolo Bonzini }
93249ab747fSPaolo Bonzini 
93349ab747fSPaolo Bonzini static int megasas_event_wait(MegasasState *s, MegasasCmd *cmd)
93449ab747fSPaolo Bonzini {
93549ab747fSPaolo Bonzini     union mfi_evt event;
93649ab747fSPaolo Bonzini 
93749ab747fSPaolo Bonzini     if (cmd->iov_size < sizeof(struct mfi_evt_detail)) {
93849ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
93949ab747fSPaolo Bonzini                                             sizeof(struct mfi_evt_detail));
94049ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
94149ab747fSPaolo Bonzini     }
94249ab747fSPaolo Bonzini     s->event_count = cpu_to_le32(cmd->frame->dcmd.mbox[0]);
94349ab747fSPaolo Bonzini     event.word = cpu_to_le32(cmd->frame->dcmd.mbox[4]);
94449ab747fSPaolo Bonzini     s->event_locale = event.members.locale;
94549ab747fSPaolo Bonzini     s->event_class = event.members.class;
94649ab747fSPaolo Bonzini     s->event_cmd = cmd;
94749ab747fSPaolo Bonzini     /* Decrease busy count; event frame doesn't count here */
94849ab747fSPaolo Bonzini     s->busy--;
94949ab747fSPaolo Bonzini     cmd->iov_size = sizeof(struct mfi_evt_detail);
95049ab747fSPaolo Bonzini     return MFI_STAT_INVALID_STATUS;
95149ab747fSPaolo Bonzini }
95249ab747fSPaolo Bonzini 
95349ab747fSPaolo Bonzini static int megasas_dcmd_pd_get_list(MegasasState *s, MegasasCmd *cmd)
95449ab747fSPaolo Bonzini {
95549ab747fSPaolo Bonzini     struct mfi_pd_list info;
95649ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
95749ab747fSPaolo Bonzini     BusChild *kid;
95849ab747fSPaolo Bonzini     uint32_t offset, dcmd_limit, num_pd_disks = 0, max_pd_disks;
95949ab747fSPaolo Bonzini 
96049ab747fSPaolo Bonzini     memset(&info, 0, dcmd_size);
96149ab747fSPaolo Bonzini     offset = 8;
96249ab747fSPaolo Bonzini     dcmd_limit = offset + sizeof(struct mfi_pd_address);
96349ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_limit) {
96449ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
96549ab747fSPaolo Bonzini                                             dcmd_limit);
96649ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
96749ab747fSPaolo Bonzini     }
96849ab747fSPaolo Bonzini 
96949ab747fSPaolo Bonzini     max_pd_disks = (cmd->iov_size - offset) / sizeof(struct mfi_pd_address);
9703f2cd4ddSHannes Reinecke     if (max_pd_disks > MFI_MAX_SYS_PDS) {
9713f2cd4ddSHannes Reinecke         max_pd_disks = MFI_MAX_SYS_PDS;
97249ab747fSPaolo Bonzini     }
97349ab747fSPaolo Bonzini     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
974e1dc6815SCao jin         SCSIDevice *sdev = SCSI_DEVICE(kid->child);
9753f2cd4ddSHannes Reinecke         uint16_t pd_id;
97649ab747fSPaolo Bonzini 
9773f2cd4ddSHannes Reinecke         if (num_pd_disks >= max_pd_disks)
9783f2cd4ddSHannes Reinecke             break;
9793f2cd4ddSHannes Reinecke 
9803f2cd4ddSHannes Reinecke         pd_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
9813f2cd4ddSHannes Reinecke         info.addr[num_pd_disks].device_id = cpu_to_le16(pd_id);
98249ab747fSPaolo Bonzini         info.addr[num_pd_disks].encl_device_id = 0xFFFF;
98349ab747fSPaolo Bonzini         info.addr[num_pd_disks].encl_index = 0;
9843f2cd4ddSHannes Reinecke         info.addr[num_pd_disks].slot_number = sdev->id & 0xFF;
98549ab747fSPaolo Bonzini         info.addr[num_pd_disks].scsi_dev_type = sdev->type;
98649ab747fSPaolo Bonzini         info.addr[num_pd_disks].connect_port_bitmap = 0x1;
98749ab747fSPaolo Bonzini         info.addr[num_pd_disks].sas_addr[0] =
9883f2cd4ddSHannes Reinecke             cpu_to_le64(megasas_get_sata_addr(pd_id));
98949ab747fSPaolo Bonzini         num_pd_disks++;
99049ab747fSPaolo Bonzini         offset += sizeof(struct mfi_pd_address);
99149ab747fSPaolo Bonzini     }
99249ab747fSPaolo Bonzini     trace_megasas_dcmd_pd_get_list(cmd->index, num_pd_disks,
99349ab747fSPaolo Bonzini                                    max_pd_disks, offset);
99449ab747fSPaolo Bonzini 
99549ab747fSPaolo Bonzini     info.size = cpu_to_le32(offset);
99649ab747fSPaolo Bonzini     info.count = cpu_to_le32(num_pd_disks);
99749ab747fSPaolo Bonzini 
99849ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, offset, &cmd->qsg);
99949ab747fSPaolo Bonzini     return MFI_STAT_OK;
100049ab747fSPaolo Bonzini }
100149ab747fSPaolo Bonzini 
100249ab747fSPaolo Bonzini static int megasas_dcmd_pd_list_query(MegasasState *s, MegasasCmd *cmd)
100349ab747fSPaolo Bonzini {
100449ab747fSPaolo Bonzini     uint16_t flags;
100549ab747fSPaolo Bonzini 
100649ab747fSPaolo Bonzini     /* mbox0 contains flags */
100749ab747fSPaolo Bonzini     flags = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
100849ab747fSPaolo Bonzini     trace_megasas_dcmd_pd_list_query(cmd->index, flags);
100949ab747fSPaolo Bonzini     if (flags == MR_PD_QUERY_TYPE_ALL ||
101049ab747fSPaolo Bonzini         megasas_is_jbod(s)) {
101149ab747fSPaolo Bonzini         return megasas_dcmd_pd_get_list(s, cmd);
101249ab747fSPaolo Bonzini     }
101349ab747fSPaolo Bonzini 
101449ab747fSPaolo Bonzini     return MFI_STAT_OK;
101549ab747fSPaolo Bonzini }
101649ab747fSPaolo Bonzini 
101749ab747fSPaolo Bonzini static int megasas_pd_get_info_submit(SCSIDevice *sdev, int lun,
101849ab747fSPaolo Bonzini                                       MegasasCmd *cmd)
101949ab747fSPaolo Bonzini {
102049ab747fSPaolo Bonzini     struct mfi_pd_info *info = cmd->iov_buf;
102149ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(struct mfi_pd_info);
102249ab747fSPaolo Bonzini     uint64_t pd_size;
10233f2cd4ddSHannes Reinecke     uint16_t pd_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
102449ab747fSPaolo Bonzini     uint8_t cmdbuf[6];
102549ab747fSPaolo Bonzini     size_t len, resid;
102649ab747fSPaolo Bonzini 
102749ab747fSPaolo Bonzini     if (!cmd->iov_buf) {
10280bd0adbeSMarkus Armbruster         cmd->iov_buf = g_malloc0(dcmd_size);
102949ab747fSPaolo Bonzini         info = cmd->iov_buf;
103049ab747fSPaolo Bonzini         info->inquiry_data[0] = 0x7f; /* Force PQual 0x3, PType 0x1f */
103149ab747fSPaolo Bonzini         info->vpd_page83[0] = 0x7f;
103249ab747fSPaolo Bonzini         megasas_setup_inquiry(cmdbuf, 0, sizeof(info->inquiry_data));
103387e459a8SPaolo Bonzini         cmd->req = scsi_req_new(sdev, cmd->index, lun, cmdbuf, cmd);
103487e459a8SPaolo Bonzini         if (!cmd->req) {
103549ab747fSPaolo Bonzini             trace_megasas_dcmd_req_alloc_failed(cmd->index,
103649ab747fSPaolo Bonzini                                                 "PD get info std inquiry");
103749ab747fSPaolo Bonzini             g_free(cmd->iov_buf);
103849ab747fSPaolo Bonzini             cmd->iov_buf = NULL;
103949ab747fSPaolo Bonzini             return MFI_STAT_FLASH_ALLOC_FAIL;
104049ab747fSPaolo Bonzini         }
104149ab747fSPaolo Bonzini         trace_megasas_dcmd_internal_submit(cmd->index,
104249ab747fSPaolo Bonzini                                            "PD get info std inquiry", lun);
104387e459a8SPaolo Bonzini         len = scsi_req_enqueue(cmd->req);
104449ab747fSPaolo Bonzini         if (len > 0) {
104549ab747fSPaolo Bonzini             cmd->iov_size = len;
104687e459a8SPaolo Bonzini             scsi_req_continue(cmd->req);
104749ab747fSPaolo Bonzini         }
104849ab747fSPaolo Bonzini         return MFI_STAT_INVALID_STATUS;
104949ab747fSPaolo Bonzini     } else if (info->inquiry_data[0] != 0x7f && info->vpd_page83[0] == 0x7f) {
105049ab747fSPaolo Bonzini         megasas_setup_inquiry(cmdbuf, 0x83, sizeof(info->vpd_page83));
105187e459a8SPaolo Bonzini         cmd->req = scsi_req_new(sdev, cmd->index, lun, cmdbuf, cmd);
105287e459a8SPaolo Bonzini         if (!cmd->req) {
105349ab747fSPaolo Bonzini             trace_megasas_dcmd_req_alloc_failed(cmd->index,
105449ab747fSPaolo Bonzini                                                 "PD get info vpd inquiry");
105549ab747fSPaolo Bonzini             return MFI_STAT_FLASH_ALLOC_FAIL;
105649ab747fSPaolo Bonzini         }
105749ab747fSPaolo Bonzini         trace_megasas_dcmd_internal_submit(cmd->index,
105849ab747fSPaolo Bonzini                                            "PD get info vpd inquiry", lun);
105987e459a8SPaolo Bonzini         len = scsi_req_enqueue(cmd->req);
106049ab747fSPaolo Bonzini         if (len > 0) {
106149ab747fSPaolo Bonzini             cmd->iov_size = len;
106287e459a8SPaolo Bonzini             scsi_req_continue(cmd->req);
106349ab747fSPaolo Bonzini         }
106449ab747fSPaolo Bonzini         return MFI_STAT_INVALID_STATUS;
106549ab747fSPaolo Bonzini     }
106649ab747fSPaolo Bonzini     /* Finished, set FW state */
106749ab747fSPaolo Bonzini     if ((info->inquiry_data[0] >> 5) == 0) {
106849ab747fSPaolo Bonzini         if (megasas_is_jbod(cmd->state)) {
106949ab747fSPaolo Bonzini             info->fw_state = cpu_to_le16(MFI_PD_STATE_SYSTEM);
107049ab747fSPaolo Bonzini         } else {
107149ab747fSPaolo Bonzini             info->fw_state = cpu_to_le16(MFI_PD_STATE_ONLINE);
107249ab747fSPaolo Bonzini         }
107349ab747fSPaolo Bonzini     } else {
107449ab747fSPaolo Bonzini         info->fw_state = cpu_to_le16(MFI_PD_STATE_OFFLINE);
107549ab747fSPaolo Bonzini     }
107649ab747fSPaolo Bonzini 
10773f2cd4ddSHannes Reinecke     info->ref.v.device_id = cpu_to_le16(pd_id);
107849ab747fSPaolo Bonzini     info->state.ddf.pd_type = cpu_to_le16(MFI_PD_DDF_TYPE_IN_VD|
107949ab747fSPaolo Bonzini                                           MFI_PD_DDF_TYPE_INTF_SAS);
10804be74634SMarkus Armbruster     blk_get_geometry(sdev->conf.blk, &pd_size);
108149ab747fSPaolo Bonzini     info->raw_size = cpu_to_le64(pd_size);
108249ab747fSPaolo Bonzini     info->non_coerced_size = cpu_to_le64(pd_size);
108349ab747fSPaolo Bonzini     info->coerced_size = cpu_to_le64(pd_size);
108449ab747fSPaolo Bonzini     info->encl_device_id = 0xFFFF;
108549ab747fSPaolo Bonzini     info->slot_number = (sdev->id & 0xFF);
108649ab747fSPaolo Bonzini     info->path_info.count = 1;
108749ab747fSPaolo Bonzini     info->path_info.sas_addr[0] =
10883f2cd4ddSHannes Reinecke         cpu_to_le64(megasas_get_sata_addr(pd_id));
108949ab747fSPaolo Bonzini     info->connected_port_bitmap = 0x1;
109049ab747fSPaolo Bonzini     info->device_speed = 1;
109149ab747fSPaolo Bonzini     info->link_speed = 1;
109249ab747fSPaolo Bonzini     resid = dma_buf_read(cmd->iov_buf, dcmd_size, &cmd->qsg);
109349ab747fSPaolo Bonzini     g_free(cmd->iov_buf);
109449ab747fSPaolo Bonzini     cmd->iov_size = dcmd_size - resid;
109549ab747fSPaolo Bonzini     cmd->iov_buf = NULL;
109649ab747fSPaolo Bonzini     return MFI_STAT_OK;
109749ab747fSPaolo Bonzini }
109849ab747fSPaolo Bonzini 
109949ab747fSPaolo Bonzini static int megasas_dcmd_pd_get_info(MegasasState *s, MegasasCmd *cmd)
110049ab747fSPaolo Bonzini {
110149ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(struct mfi_pd_info);
110249ab747fSPaolo Bonzini     uint16_t pd_id;
11033f2cd4ddSHannes Reinecke     uint8_t target_id, lun_id;
110449ab747fSPaolo Bonzini     SCSIDevice *sdev = NULL;
110549ab747fSPaolo Bonzini     int retval = MFI_STAT_DEVICE_NOT_FOUND;
110649ab747fSPaolo Bonzini 
110749ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
110849ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
110949ab747fSPaolo Bonzini     }
111049ab747fSPaolo Bonzini 
111149ab747fSPaolo Bonzini     /* mbox0 has the ID */
111249ab747fSPaolo Bonzini     pd_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
11133f2cd4ddSHannes Reinecke     target_id = (pd_id >> 8) & 0xFF;
11143f2cd4ddSHannes Reinecke     lun_id = pd_id & 0xFF;
11153f2cd4ddSHannes Reinecke     sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
111649ab747fSPaolo Bonzini     trace_megasas_dcmd_pd_get_info(cmd->index, pd_id);
111749ab747fSPaolo Bonzini 
111849ab747fSPaolo Bonzini     if (sdev) {
111949ab747fSPaolo Bonzini         /* Submit inquiry */
112049ab747fSPaolo Bonzini         retval = megasas_pd_get_info_submit(sdev, pd_id, cmd);
112149ab747fSPaolo Bonzini     }
112249ab747fSPaolo Bonzini 
112349ab747fSPaolo Bonzini     return retval;
112449ab747fSPaolo Bonzini }
112549ab747fSPaolo Bonzini 
112649ab747fSPaolo Bonzini static int megasas_dcmd_ld_get_list(MegasasState *s, MegasasCmd *cmd)
112749ab747fSPaolo Bonzini {
112849ab747fSPaolo Bonzini     struct mfi_ld_list info;
112949ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info), resid;
11303f2cd4ddSHannes Reinecke     uint32_t num_ld_disks = 0, max_ld_disks;
113149ab747fSPaolo Bonzini     uint64_t ld_size;
113249ab747fSPaolo Bonzini     BusChild *kid;
113349ab747fSPaolo Bonzini 
113449ab747fSPaolo Bonzini     memset(&info, 0, dcmd_size);
1135e74a4315SHannes Reinecke     if (cmd->iov_size > dcmd_size) {
113649ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
113749ab747fSPaolo Bonzini                                             dcmd_size);
113849ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
113949ab747fSPaolo Bonzini     }
114049ab747fSPaolo Bonzini 
11413f2cd4ddSHannes Reinecke     max_ld_disks = (cmd->iov_size - 8) / 16;
114249ab747fSPaolo Bonzini     if (megasas_is_jbod(s)) {
114349ab747fSPaolo Bonzini         max_ld_disks = 0;
114449ab747fSPaolo Bonzini     }
11453f2cd4ddSHannes Reinecke     if (max_ld_disks > MFI_MAX_LD) {
11463f2cd4ddSHannes Reinecke         max_ld_disks = MFI_MAX_LD;
11473f2cd4ddSHannes Reinecke     }
114849ab747fSPaolo Bonzini     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1149e1dc6815SCao jin         SCSIDevice *sdev = SCSI_DEVICE(kid->child);
115049ab747fSPaolo Bonzini 
115149ab747fSPaolo Bonzini         if (num_ld_disks >= max_ld_disks) {
115249ab747fSPaolo Bonzini             break;
115349ab747fSPaolo Bonzini         }
115449ab747fSPaolo Bonzini         /* Logical device size is in blocks */
11554be74634SMarkus Armbruster         blk_get_geometry(sdev->conf.blk, &ld_size);
115649ab747fSPaolo Bonzini         info.ld_list[num_ld_disks].ld.v.target_id = sdev->id;
115749ab747fSPaolo Bonzini         info.ld_list[num_ld_disks].state = MFI_LD_STATE_OPTIMAL;
115849ab747fSPaolo Bonzini         info.ld_list[num_ld_disks].size = cpu_to_le64(ld_size);
115949ab747fSPaolo Bonzini         num_ld_disks++;
116049ab747fSPaolo Bonzini     }
116149ab747fSPaolo Bonzini     info.ld_count = cpu_to_le32(num_ld_disks);
116249ab747fSPaolo Bonzini     trace_megasas_dcmd_ld_get_list(cmd->index, num_ld_disks, max_ld_disks);
116349ab747fSPaolo Bonzini 
116449ab747fSPaolo Bonzini     resid = dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
116549ab747fSPaolo Bonzini     cmd->iov_size = dcmd_size - resid;
116649ab747fSPaolo Bonzini     return MFI_STAT_OK;
116749ab747fSPaolo Bonzini }
116849ab747fSPaolo Bonzini 
116934bb4d02SHannes Reinecke static int megasas_dcmd_ld_list_query(MegasasState *s, MegasasCmd *cmd)
117034bb4d02SHannes Reinecke {
117134bb4d02SHannes Reinecke     uint16_t flags;
1172d97ae368SHannes Reinecke     struct mfi_ld_targetid_list info;
1173d97ae368SHannes Reinecke     size_t dcmd_size = sizeof(info), resid;
1174d97ae368SHannes Reinecke     uint32_t num_ld_disks = 0, max_ld_disks = s->fw_luns;
1175d97ae368SHannes Reinecke     BusChild *kid;
117634bb4d02SHannes Reinecke 
117734bb4d02SHannes Reinecke     /* mbox0 contains flags */
117834bb4d02SHannes Reinecke     flags = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
117934bb4d02SHannes Reinecke     trace_megasas_dcmd_ld_list_query(cmd->index, flags);
1180d97ae368SHannes Reinecke     if (flags != MR_LD_QUERY_TYPE_ALL &&
1181d97ae368SHannes Reinecke         flags != MR_LD_QUERY_TYPE_EXPOSED_TO_HOST) {
1182d97ae368SHannes Reinecke         max_ld_disks = 0;
118334bb4d02SHannes Reinecke     }
118434bb4d02SHannes Reinecke 
1185d97ae368SHannes Reinecke     memset(&info, 0, dcmd_size);
1186d97ae368SHannes Reinecke     if (cmd->iov_size < 12) {
1187d97ae368SHannes Reinecke         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1188d97ae368SHannes Reinecke                                             dcmd_size);
1189d97ae368SHannes Reinecke         return MFI_STAT_INVALID_PARAMETER;
1190d97ae368SHannes Reinecke     }
1191d97ae368SHannes Reinecke     dcmd_size = sizeof(uint32_t) * 2 + 3;
11923f2cd4ddSHannes Reinecke     max_ld_disks = cmd->iov_size - dcmd_size;
1193d97ae368SHannes Reinecke     if (megasas_is_jbod(s)) {
1194d97ae368SHannes Reinecke         max_ld_disks = 0;
1195d97ae368SHannes Reinecke     }
11963f2cd4ddSHannes Reinecke     if (max_ld_disks > MFI_MAX_LD) {
11973f2cd4ddSHannes Reinecke         max_ld_disks = MFI_MAX_LD;
11983f2cd4ddSHannes Reinecke     }
1199d97ae368SHannes Reinecke     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1200e1dc6815SCao jin         SCSIDevice *sdev = SCSI_DEVICE(kid->child);
1201d97ae368SHannes Reinecke 
1202d97ae368SHannes Reinecke         if (num_ld_disks >= max_ld_disks) {
1203d97ae368SHannes Reinecke             break;
1204d97ae368SHannes Reinecke         }
1205d97ae368SHannes Reinecke         info.targetid[num_ld_disks] = sdev->lun;
1206d97ae368SHannes Reinecke         num_ld_disks++;
1207d97ae368SHannes Reinecke         dcmd_size++;
1208d97ae368SHannes Reinecke     }
1209d97ae368SHannes Reinecke     info.ld_count = cpu_to_le32(num_ld_disks);
1210d97ae368SHannes Reinecke     info.size = dcmd_size;
1211d97ae368SHannes Reinecke     trace_megasas_dcmd_ld_get_list(cmd->index, num_ld_disks, max_ld_disks);
1212d97ae368SHannes Reinecke 
1213d97ae368SHannes Reinecke     resid = dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
1214d97ae368SHannes Reinecke     cmd->iov_size = dcmd_size - resid;
121534bb4d02SHannes Reinecke     return MFI_STAT_OK;
121634bb4d02SHannes Reinecke }
121734bb4d02SHannes Reinecke 
121849ab747fSPaolo Bonzini static int megasas_ld_get_info_submit(SCSIDevice *sdev, int lun,
121949ab747fSPaolo Bonzini                                       MegasasCmd *cmd)
122049ab747fSPaolo Bonzini {
122149ab747fSPaolo Bonzini     struct mfi_ld_info *info = cmd->iov_buf;
122249ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(struct mfi_ld_info);
122349ab747fSPaolo Bonzini     uint8_t cdb[6];
122449ab747fSPaolo Bonzini     ssize_t len, resid;
12253f2cd4ddSHannes Reinecke     uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
122649ab747fSPaolo Bonzini     uint64_t ld_size;
122749ab747fSPaolo Bonzini 
122849ab747fSPaolo Bonzini     if (!cmd->iov_buf) {
12290bd0adbeSMarkus Armbruster         cmd->iov_buf = g_malloc0(dcmd_size);
123049ab747fSPaolo Bonzini         info = cmd->iov_buf;
123149ab747fSPaolo Bonzini         megasas_setup_inquiry(cdb, 0x83, sizeof(info->vpd_page83));
123287e459a8SPaolo Bonzini         cmd->req = scsi_req_new(sdev, cmd->index, lun, cdb, cmd);
123387e459a8SPaolo Bonzini         if (!cmd->req) {
123449ab747fSPaolo Bonzini             trace_megasas_dcmd_req_alloc_failed(cmd->index,
123549ab747fSPaolo Bonzini                                                 "LD get info vpd inquiry");
123649ab747fSPaolo Bonzini             g_free(cmd->iov_buf);
123749ab747fSPaolo Bonzini             cmd->iov_buf = NULL;
123849ab747fSPaolo Bonzini             return MFI_STAT_FLASH_ALLOC_FAIL;
123949ab747fSPaolo Bonzini         }
124049ab747fSPaolo Bonzini         trace_megasas_dcmd_internal_submit(cmd->index,
124149ab747fSPaolo Bonzini                                            "LD get info vpd inquiry", lun);
124287e459a8SPaolo Bonzini         len = scsi_req_enqueue(cmd->req);
124349ab747fSPaolo Bonzini         if (len > 0) {
124449ab747fSPaolo Bonzini             cmd->iov_size = len;
124587e459a8SPaolo Bonzini             scsi_req_continue(cmd->req);
124649ab747fSPaolo Bonzini         }
124749ab747fSPaolo Bonzini         return MFI_STAT_INVALID_STATUS;
124849ab747fSPaolo Bonzini     }
124949ab747fSPaolo Bonzini 
125049ab747fSPaolo Bonzini     info->ld_config.params.state = MFI_LD_STATE_OPTIMAL;
125149ab747fSPaolo Bonzini     info->ld_config.properties.ld.v.target_id = lun;
125249ab747fSPaolo Bonzini     info->ld_config.params.stripe_size = 3;
125349ab747fSPaolo Bonzini     info->ld_config.params.num_drives = 1;
125449ab747fSPaolo Bonzini     info->ld_config.params.is_consistent = 1;
125549ab747fSPaolo Bonzini     /* Logical device size is in blocks */
12564be74634SMarkus Armbruster     blk_get_geometry(sdev->conf.blk, &ld_size);
125749ab747fSPaolo Bonzini     info->size = cpu_to_le64(ld_size);
125849ab747fSPaolo Bonzini     memset(info->ld_config.span, 0, sizeof(info->ld_config.span));
125949ab747fSPaolo Bonzini     info->ld_config.span[0].start_block = 0;
126049ab747fSPaolo Bonzini     info->ld_config.span[0].num_blocks = info->size;
126149ab747fSPaolo Bonzini     info->ld_config.span[0].array_ref = cpu_to_le16(sdev_id);
126249ab747fSPaolo Bonzini 
126349ab747fSPaolo Bonzini     resid = dma_buf_read(cmd->iov_buf, dcmd_size, &cmd->qsg);
126449ab747fSPaolo Bonzini     g_free(cmd->iov_buf);
126549ab747fSPaolo Bonzini     cmd->iov_size = dcmd_size - resid;
126649ab747fSPaolo Bonzini     cmd->iov_buf = NULL;
126749ab747fSPaolo Bonzini     return MFI_STAT_OK;
126849ab747fSPaolo Bonzini }
126949ab747fSPaolo Bonzini 
127049ab747fSPaolo Bonzini static int megasas_dcmd_ld_get_info(MegasasState *s, MegasasCmd *cmd)
127149ab747fSPaolo Bonzini {
127249ab747fSPaolo Bonzini     struct mfi_ld_info info;
127349ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
127449ab747fSPaolo Bonzini     uint16_t ld_id;
127549ab747fSPaolo Bonzini     uint32_t max_ld_disks = s->fw_luns;
127649ab747fSPaolo Bonzini     SCSIDevice *sdev = NULL;
127749ab747fSPaolo Bonzini     int retval = MFI_STAT_DEVICE_NOT_FOUND;
127849ab747fSPaolo Bonzini 
127949ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
128049ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
128149ab747fSPaolo Bonzini     }
128249ab747fSPaolo Bonzini 
128349ab747fSPaolo Bonzini     /* mbox0 has the ID */
128449ab747fSPaolo Bonzini     ld_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
128549ab747fSPaolo Bonzini     trace_megasas_dcmd_ld_get_info(cmd->index, ld_id);
128649ab747fSPaolo Bonzini 
128749ab747fSPaolo Bonzini     if (megasas_is_jbod(s)) {
128849ab747fSPaolo Bonzini         return MFI_STAT_DEVICE_NOT_FOUND;
128949ab747fSPaolo Bonzini     }
129049ab747fSPaolo Bonzini 
129149ab747fSPaolo Bonzini     if (ld_id < max_ld_disks) {
129249ab747fSPaolo Bonzini         sdev = scsi_device_find(&s->bus, 0, ld_id, 0);
129349ab747fSPaolo Bonzini     }
129449ab747fSPaolo Bonzini 
129549ab747fSPaolo Bonzini     if (sdev) {
129649ab747fSPaolo Bonzini         retval = megasas_ld_get_info_submit(sdev, ld_id, cmd);
129749ab747fSPaolo Bonzini     }
129849ab747fSPaolo Bonzini 
129949ab747fSPaolo Bonzini     return retval;
130049ab747fSPaolo Bonzini }
130149ab747fSPaolo Bonzini 
130249ab747fSPaolo Bonzini static int megasas_dcmd_cfg_read(MegasasState *s, MegasasCmd *cmd)
130349ab747fSPaolo Bonzini {
1304d37af740SPrasad J Pandit     uint8_t data[4096] = { 0 };
130549ab747fSPaolo Bonzini     struct mfi_config_data *info;
130649ab747fSPaolo Bonzini     int num_pd_disks = 0, array_offset, ld_offset;
130749ab747fSPaolo Bonzini     BusChild *kid;
130849ab747fSPaolo Bonzini 
130949ab747fSPaolo Bonzini     if (cmd->iov_size > 4096) {
131049ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
131149ab747fSPaolo Bonzini     }
131249ab747fSPaolo Bonzini 
131349ab747fSPaolo Bonzini     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
131449ab747fSPaolo Bonzini         num_pd_disks++;
131549ab747fSPaolo Bonzini     }
131649ab747fSPaolo Bonzini     info = (struct mfi_config_data *)&data;
131749ab747fSPaolo Bonzini     /*
131849ab747fSPaolo Bonzini      * Array mapping:
131949ab747fSPaolo Bonzini      * - One array per SCSI device
132049ab747fSPaolo Bonzini      * - One logical drive per SCSI device
132149ab747fSPaolo Bonzini      *   spanning the entire device
132249ab747fSPaolo Bonzini      */
132349ab747fSPaolo Bonzini     info->array_count = num_pd_disks;
132449ab747fSPaolo Bonzini     info->array_size = sizeof(struct mfi_array) * num_pd_disks;
132549ab747fSPaolo Bonzini     info->log_drv_count = num_pd_disks;
132649ab747fSPaolo Bonzini     info->log_drv_size = sizeof(struct mfi_ld_config) * num_pd_disks;
132749ab747fSPaolo Bonzini     info->spares_count = 0;
132849ab747fSPaolo Bonzini     info->spares_size = sizeof(struct mfi_spare);
132949ab747fSPaolo Bonzini     info->size = sizeof(struct mfi_config_data) + info->array_size +
133049ab747fSPaolo Bonzini         info->log_drv_size;
133149ab747fSPaolo Bonzini     if (info->size > 4096) {
133249ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
133349ab747fSPaolo Bonzini     }
133449ab747fSPaolo Bonzini 
133549ab747fSPaolo Bonzini     array_offset = sizeof(struct mfi_config_data);
133649ab747fSPaolo Bonzini     ld_offset = array_offset + sizeof(struct mfi_array) * num_pd_disks;
133749ab747fSPaolo Bonzini 
133849ab747fSPaolo Bonzini     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1339e1dc6815SCao jin         SCSIDevice *sdev = SCSI_DEVICE(kid->child);
13403f2cd4ddSHannes Reinecke         uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
134149ab747fSPaolo Bonzini         struct mfi_array *array;
134249ab747fSPaolo Bonzini         struct mfi_ld_config *ld;
134349ab747fSPaolo Bonzini         uint64_t pd_size;
134449ab747fSPaolo Bonzini         int i;
134549ab747fSPaolo Bonzini 
134649ab747fSPaolo Bonzini         array = (struct mfi_array *)(data + array_offset);
13474be74634SMarkus Armbruster         blk_get_geometry(sdev->conf.blk, &pd_size);
134849ab747fSPaolo Bonzini         array->size = cpu_to_le64(pd_size);
134949ab747fSPaolo Bonzini         array->num_drives = 1;
135049ab747fSPaolo Bonzini         array->array_ref = cpu_to_le16(sdev_id);
135149ab747fSPaolo Bonzini         array->pd[0].ref.v.device_id = cpu_to_le16(sdev_id);
135249ab747fSPaolo Bonzini         array->pd[0].ref.v.seq_num = 0;
135349ab747fSPaolo Bonzini         array->pd[0].fw_state = MFI_PD_STATE_ONLINE;
135449ab747fSPaolo Bonzini         array->pd[0].encl.pd = 0xFF;
135549ab747fSPaolo Bonzini         array->pd[0].encl.slot = (sdev->id & 0xFF);
135649ab747fSPaolo Bonzini         for (i = 1; i < MFI_MAX_ROW_SIZE; i++) {
135749ab747fSPaolo Bonzini             array->pd[i].ref.v.device_id = 0xFFFF;
135849ab747fSPaolo Bonzini             array->pd[i].ref.v.seq_num = 0;
135949ab747fSPaolo Bonzini             array->pd[i].fw_state = MFI_PD_STATE_UNCONFIGURED_GOOD;
136049ab747fSPaolo Bonzini             array->pd[i].encl.pd = 0xFF;
136149ab747fSPaolo Bonzini             array->pd[i].encl.slot = 0xFF;
136249ab747fSPaolo Bonzini         }
136349ab747fSPaolo Bonzini         array_offset += sizeof(struct mfi_array);
136449ab747fSPaolo Bonzini         ld = (struct mfi_ld_config *)(data + ld_offset);
136549ab747fSPaolo Bonzini         memset(ld, 0, sizeof(struct mfi_ld_config));
13663f2cd4ddSHannes Reinecke         ld->properties.ld.v.target_id = sdev->id;
136749ab747fSPaolo Bonzini         ld->properties.default_cache_policy = MR_LD_CACHE_READ_AHEAD |
136849ab747fSPaolo Bonzini             MR_LD_CACHE_READ_ADAPTIVE;
136949ab747fSPaolo Bonzini         ld->properties.current_cache_policy = MR_LD_CACHE_READ_AHEAD |
137049ab747fSPaolo Bonzini             MR_LD_CACHE_READ_ADAPTIVE;
137149ab747fSPaolo Bonzini         ld->params.state = MFI_LD_STATE_OPTIMAL;
137249ab747fSPaolo Bonzini         ld->params.stripe_size = 3;
137349ab747fSPaolo Bonzini         ld->params.num_drives = 1;
137449ab747fSPaolo Bonzini         ld->params.span_depth = 1;
137549ab747fSPaolo Bonzini         ld->params.is_consistent = 1;
137649ab747fSPaolo Bonzini         ld->span[0].start_block = 0;
137749ab747fSPaolo Bonzini         ld->span[0].num_blocks = cpu_to_le64(pd_size);
137849ab747fSPaolo Bonzini         ld->span[0].array_ref = cpu_to_le16(sdev_id);
137949ab747fSPaolo Bonzini         ld_offset += sizeof(struct mfi_ld_config);
138049ab747fSPaolo Bonzini     }
138149ab747fSPaolo Bonzini 
138249ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)data, info->size, &cmd->qsg);
138349ab747fSPaolo Bonzini     return MFI_STAT_OK;
138449ab747fSPaolo Bonzini }
138549ab747fSPaolo Bonzini 
138649ab747fSPaolo Bonzini static int megasas_dcmd_get_properties(MegasasState *s, MegasasCmd *cmd)
138749ab747fSPaolo Bonzini {
138849ab747fSPaolo Bonzini     struct mfi_ctrl_props info;
138949ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
139049ab747fSPaolo Bonzini 
139149ab747fSPaolo Bonzini     memset(&info, 0x0, dcmd_size);
139249ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
139349ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
139449ab747fSPaolo Bonzini                                             dcmd_size);
139549ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
139649ab747fSPaolo Bonzini     }
139749ab747fSPaolo Bonzini     info.pred_fail_poll_interval = cpu_to_le16(300);
139849ab747fSPaolo Bonzini     info.intr_throttle_cnt = cpu_to_le16(16);
139949ab747fSPaolo Bonzini     info.intr_throttle_timeout = cpu_to_le16(50);
140049ab747fSPaolo Bonzini     info.rebuild_rate = 30;
140149ab747fSPaolo Bonzini     info.patrol_read_rate = 30;
140249ab747fSPaolo Bonzini     info.bgi_rate = 30;
140349ab747fSPaolo Bonzini     info.cc_rate = 30;
140449ab747fSPaolo Bonzini     info.recon_rate = 30;
140549ab747fSPaolo Bonzini     info.cache_flush_interval = 4;
140649ab747fSPaolo Bonzini     info.spinup_drv_cnt = 2;
140749ab747fSPaolo Bonzini     info.spinup_delay = 6;
140849ab747fSPaolo Bonzini     info.ecc_bucket_size = 15;
140949ab747fSPaolo Bonzini     info.ecc_bucket_leak_rate = cpu_to_le16(1440);
141049ab747fSPaolo Bonzini     info.expose_encl_devices = 1;
141149ab747fSPaolo Bonzini 
141249ab747fSPaolo Bonzini     cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
141349ab747fSPaolo Bonzini     return MFI_STAT_OK;
141449ab747fSPaolo Bonzini }
141549ab747fSPaolo Bonzini 
141649ab747fSPaolo Bonzini static int megasas_cache_flush(MegasasState *s, MegasasCmd *cmd)
141749ab747fSPaolo Bonzini {
14184be74634SMarkus Armbruster     blk_drain_all();
141949ab747fSPaolo Bonzini     return MFI_STAT_OK;
142049ab747fSPaolo Bonzini }
142149ab747fSPaolo Bonzini 
142249ab747fSPaolo Bonzini static int megasas_ctrl_shutdown(MegasasState *s, MegasasCmd *cmd)
142349ab747fSPaolo Bonzini {
142449ab747fSPaolo Bonzini     s->fw_state = MFI_FWSTATE_READY;
142549ab747fSPaolo Bonzini     return MFI_STAT_OK;
142649ab747fSPaolo Bonzini }
142749ab747fSPaolo Bonzini 
1428200b6966SHannes Reinecke /* Some implementations use CLUSTER RESET LD to simulate a device reset */
142949ab747fSPaolo Bonzini static int megasas_cluster_reset_ld(MegasasState *s, MegasasCmd *cmd)
143049ab747fSPaolo Bonzini {
1431200b6966SHannes Reinecke     uint16_t target_id;
1432200b6966SHannes Reinecke     int i;
1433200b6966SHannes Reinecke 
1434200b6966SHannes Reinecke     /* mbox0 contains the device index */
1435200b6966SHannes Reinecke     target_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
1436200b6966SHannes Reinecke     trace_megasas_dcmd_reset_ld(cmd->index, target_id);
1437200b6966SHannes Reinecke     for (i = 0; i < s->fw_cmds; i++) {
1438200b6966SHannes Reinecke         MegasasCmd *tmp_cmd = &s->frames[i];
1439200b6966SHannes Reinecke         if (tmp_cmd->req && tmp_cmd->req->dev->id == target_id) {
1440200b6966SHannes Reinecke             SCSIDevice *d = tmp_cmd->req->dev;
1441200b6966SHannes Reinecke             qdev_reset_all(&d->qdev);
1442200b6966SHannes Reinecke         }
1443200b6966SHannes Reinecke     }
1444200b6966SHannes Reinecke     return MFI_STAT_OK;
144549ab747fSPaolo Bonzini }
144649ab747fSPaolo Bonzini 
144749ab747fSPaolo Bonzini static int megasas_dcmd_set_properties(MegasasState *s, MegasasCmd *cmd)
144849ab747fSPaolo Bonzini {
144949ab747fSPaolo Bonzini     struct mfi_ctrl_props info;
145049ab747fSPaolo Bonzini     size_t dcmd_size = sizeof(info);
145149ab747fSPaolo Bonzini 
145249ab747fSPaolo Bonzini     if (cmd->iov_size < dcmd_size) {
145349ab747fSPaolo Bonzini         trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
145449ab747fSPaolo Bonzini                                             dcmd_size);
145549ab747fSPaolo Bonzini         return MFI_STAT_INVALID_PARAMETER;
145649ab747fSPaolo Bonzini     }
14571b858980SPrasad J Pandit     dma_buf_write((uint8_t *)&info, dcmd_size, &cmd->qsg);
145849ab747fSPaolo Bonzini     trace_megasas_dcmd_unsupported(cmd->index, cmd->iov_size);
145949ab747fSPaolo Bonzini     return MFI_STAT_OK;
146049ab747fSPaolo Bonzini }
146149ab747fSPaolo Bonzini 
146249ab747fSPaolo Bonzini static int megasas_dcmd_dummy(MegasasState *s, MegasasCmd *cmd)
146349ab747fSPaolo Bonzini {
146449ab747fSPaolo Bonzini     trace_megasas_dcmd_dummy(cmd->index, cmd->iov_size);
146549ab747fSPaolo Bonzini     return MFI_STAT_OK;
146649ab747fSPaolo Bonzini }
146749ab747fSPaolo Bonzini 
146849ab747fSPaolo Bonzini static const struct dcmd_cmd_tbl_t {
146949ab747fSPaolo Bonzini     int opcode;
147049ab747fSPaolo Bonzini     const char *desc;
147149ab747fSPaolo Bonzini     int (*func)(MegasasState *s, MegasasCmd *cmd);
147249ab747fSPaolo Bonzini } dcmd_cmd_tbl[] = {
147349ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_MFI_HOST_MEM_ALLOC, "CTRL_HOST_MEM_ALLOC",
147449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
147549ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_GET_INFO, "CTRL_GET_INFO",
147649ab747fSPaolo Bonzini       megasas_ctrl_get_info },
147749ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_GET_PROPERTIES, "CTRL_GET_PROPERTIES",
147849ab747fSPaolo Bonzini       megasas_dcmd_get_properties },
147949ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_SET_PROPERTIES, "CTRL_SET_PROPERTIES",
148049ab747fSPaolo Bonzini       megasas_dcmd_set_properties },
148149ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_ALARM_GET, "CTRL_ALARM_GET",
148249ab747fSPaolo Bonzini       megasas_dcmd_dummy },
148349ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_ALARM_ENABLE, "CTRL_ALARM_ENABLE",
148449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
148549ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_ALARM_DISABLE, "CTRL_ALARM_DISABLE",
148649ab747fSPaolo Bonzini       megasas_dcmd_dummy },
148749ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_ALARM_SILENCE, "CTRL_ALARM_SILENCE",
148849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
148949ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_ALARM_TEST, "CTRL_ALARM_TEST",
149049ab747fSPaolo Bonzini       megasas_dcmd_dummy },
149149ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_EVENT_GETINFO, "CTRL_EVENT_GETINFO",
149249ab747fSPaolo Bonzini       megasas_event_info },
149349ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_EVENT_GET, "CTRL_EVENT_GET",
149449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
149549ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_EVENT_WAIT, "CTRL_EVENT_WAIT",
149649ab747fSPaolo Bonzini       megasas_event_wait },
149749ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_SHUTDOWN, "CTRL_SHUTDOWN",
149849ab747fSPaolo Bonzini       megasas_ctrl_shutdown },
149949ab747fSPaolo Bonzini     { MFI_DCMD_HIBERNATE_STANDBY, "CTRL_STANDBY",
150049ab747fSPaolo Bonzini       megasas_dcmd_dummy },
150149ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_GET_TIME, "CTRL_GET_TIME",
150249ab747fSPaolo Bonzini       megasas_dcmd_get_fw_time },
150349ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_SET_TIME, "CTRL_SET_TIME",
150449ab747fSPaolo Bonzini       megasas_dcmd_set_fw_time },
150549ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_BIOS_DATA_GET, "CTRL_BIOS_DATA_GET",
150649ab747fSPaolo Bonzini       megasas_dcmd_get_bios_info },
150749ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_FACTORY_DEFAULTS, "CTRL_FACTORY_DEFAULTS",
150849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
150949ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_MFC_DEFAULTS_GET, "CTRL_MFC_DEFAULTS_GET",
151049ab747fSPaolo Bonzini       megasas_mfc_get_defaults },
151149ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_MFC_DEFAULTS_SET, "CTRL_MFC_DEFAULTS_SET",
151249ab747fSPaolo Bonzini       megasas_dcmd_dummy },
151349ab747fSPaolo Bonzini     { MFI_DCMD_CTRL_CACHE_FLUSH, "CTRL_CACHE_FLUSH",
151449ab747fSPaolo Bonzini       megasas_cache_flush },
151549ab747fSPaolo Bonzini     { MFI_DCMD_PD_GET_LIST, "PD_GET_LIST",
151649ab747fSPaolo Bonzini       megasas_dcmd_pd_get_list },
151749ab747fSPaolo Bonzini     { MFI_DCMD_PD_LIST_QUERY, "PD_LIST_QUERY",
151849ab747fSPaolo Bonzini       megasas_dcmd_pd_list_query },
151949ab747fSPaolo Bonzini     { MFI_DCMD_PD_GET_INFO, "PD_GET_INFO",
152049ab747fSPaolo Bonzini       megasas_dcmd_pd_get_info },
152149ab747fSPaolo Bonzini     { MFI_DCMD_PD_STATE_SET, "PD_STATE_SET",
152249ab747fSPaolo Bonzini       megasas_dcmd_dummy },
152349ab747fSPaolo Bonzini     { MFI_DCMD_PD_REBUILD, "PD_REBUILD",
152449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
152549ab747fSPaolo Bonzini     { MFI_DCMD_PD_BLINK, "PD_BLINK",
152649ab747fSPaolo Bonzini       megasas_dcmd_dummy },
152749ab747fSPaolo Bonzini     { MFI_DCMD_PD_UNBLINK, "PD_UNBLINK",
152849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
152949ab747fSPaolo Bonzini     { MFI_DCMD_LD_GET_LIST, "LD_GET_LIST",
153049ab747fSPaolo Bonzini       megasas_dcmd_ld_get_list},
153134bb4d02SHannes Reinecke     { MFI_DCMD_LD_LIST_QUERY, "LD_LIST_QUERY",
153234bb4d02SHannes Reinecke       megasas_dcmd_ld_list_query },
153349ab747fSPaolo Bonzini     { MFI_DCMD_LD_GET_INFO, "LD_GET_INFO",
153449ab747fSPaolo Bonzini       megasas_dcmd_ld_get_info },
153549ab747fSPaolo Bonzini     { MFI_DCMD_LD_GET_PROP, "LD_GET_PROP",
153649ab747fSPaolo Bonzini       megasas_dcmd_dummy },
153749ab747fSPaolo Bonzini     { MFI_DCMD_LD_SET_PROP, "LD_SET_PROP",
153849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
153949ab747fSPaolo Bonzini     { MFI_DCMD_LD_DELETE, "LD_DELETE",
154049ab747fSPaolo Bonzini       megasas_dcmd_dummy },
154149ab747fSPaolo Bonzini     { MFI_DCMD_CFG_READ, "CFG_READ",
154249ab747fSPaolo Bonzini       megasas_dcmd_cfg_read },
154349ab747fSPaolo Bonzini     { MFI_DCMD_CFG_ADD, "CFG_ADD",
154449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
154549ab747fSPaolo Bonzini     { MFI_DCMD_CFG_CLEAR, "CFG_CLEAR",
154649ab747fSPaolo Bonzini       megasas_dcmd_dummy },
154749ab747fSPaolo Bonzini     { MFI_DCMD_CFG_FOREIGN_READ, "CFG_FOREIGN_READ",
154849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
154949ab747fSPaolo Bonzini     { MFI_DCMD_CFG_FOREIGN_IMPORT, "CFG_FOREIGN_IMPORT",
155049ab747fSPaolo Bonzini       megasas_dcmd_dummy },
155149ab747fSPaolo Bonzini     { MFI_DCMD_BBU_STATUS, "BBU_STATUS",
155249ab747fSPaolo Bonzini       megasas_dcmd_dummy },
155349ab747fSPaolo Bonzini     { MFI_DCMD_BBU_CAPACITY_INFO, "BBU_CAPACITY_INFO",
155449ab747fSPaolo Bonzini       megasas_dcmd_dummy },
155549ab747fSPaolo Bonzini     { MFI_DCMD_BBU_DESIGN_INFO, "BBU_DESIGN_INFO",
155649ab747fSPaolo Bonzini       megasas_dcmd_dummy },
155749ab747fSPaolo Bonzini     { MFI_DCMD_BBU_PROP_GET, "BBU_PROP_GET",
155849ab747fSPaolo Bonzini       megasas_dcmd_dummy },
155949ab747fSPaolo Bonzini     { MFI_DCMD_CLUSTER, "CLUSTER",
156049ab747fSPaolo Bonzini       megasas_dcmd_dummy },
156149ab747fSPaolo Bonzini     { MFI_DCMD_CLUSTER_RESET_ALL, "CLUSTER_RESET_ALL",
156249ab747fSPaolo Bonzini       megasas_dcmd_dummy },
156349ab747fSPaolo Bonzini     { MFI_DCMD_CLUSTER_RESET_LD, "CLUSTER_RESET_LD",
156449ab747fSPaolo Bonzini       megasas_cluster_reset_ld },
156549ab747fSPaolo Bonzini     { -1, NULL, NULL }
156649ab747fSPaolo Bonzini };
156749ab747fSPaolo Bonzini 
156849ab747fSPaolo Bonzini static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
156949ab747fSPaolo Bonzini {
157049ab747fSPaolo Bonzini     int retval = 0;
1571765a7070SPaolo Bonzini     size_t len;
157249ab747fSPaolo Bonzini     const struct dcmd_cmd_tbl_t *cmdptr = dcmd_cmd_tbl;
157349ab747fSPaolo Bonzini 
15745104fac8SPaolo Bonzini     cmd->dcmd_opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
15755104fac8SPaolo Bonzini     trace_megasas_handle_dcmd(cmd->index, cmd->dcmd_opcode);
1576765a7070SPaolo Bonzini     if (megasas_map_dcmd(s, cmd) < 0) {
157749ab747fSPaolo Bonzini         return MFI_STAT_MEMORY_NOT_AVAILABLE;
157849ab747fSPaolo Bonzini     }
15795104fac8SPaolo Bonzini     while (cmdptr->opcode != -1 && cmdptr->opcode != cmd->dcmd_opcode) {
158049ab747fSPaolo Bonzini         cmdptr++;
158149ab747fSPaolo Bonzini     }
1582765a7070SPaolo Bonzini     len = cmd->iov_size;
158349ab747fSPaolo Bonzini     if (cmdptr->opcode == -1) {
15845104fac8SPaolo Bonzini         trace_megasas_dcmd_unhandled(cmd->index, cmd->dcmd_opcode, len);
158549ab747fSPaolo Bonzini         retval = megasas_dcmd_dummy(s, cmd);
158649ab747fSPaolo Bonzini     } else {
158749ab747fSPaolo Bonzini         trace_megasas_dcmd_enter(cmd->index, cmdptr->desc, len);
158849ab747fSPaolo Bonzini         retval = cmdptr->func(s, cmd);
158949ab747fSPaolo Bonzini     }
159049ab747fSPaolo Bonzini     if (retval != MFI_STAT_INVALID_STATUS) {
159149ab747fSPaolo Bonzini         megasas_finish_dcmd(cmd, len);
159249ab747fSPaolo Bonzini     }
159349ab747fSPaolo Bonzini     return retval;
159449ab747fSPaolo Bonzini }
159549ab747fSPaolo Bonzini 
159649ab747fSPaolo Bonzini static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
159736c327a6SPaolo Bonzini                                         SCSIRequest *req, size_t resid)
159849ab747fSPaolo Bonzini {
159949ab747fSPaolo Bonzini     int retval = MFI_STAT_OK;
160049ab747fSPaolo Bonzini     int lun = req->lun;
160149ab747fSPaolo Bonzini 
16025104fac8SPaolo Bonzini     trace_megasas_dcmd_internal_finish(cmd->index, cmd->dcmd_opcode, lun);
160336c327a6SPaolo Bonzini     cmd->iov_size -= resid;
16045104fac8SPaolo Bonzini     switch (cmd->dcmd_opcode) {
160549ab747fSPaolo Bonzini     case MFI_DCMD_PD_GET_INFO:
160649ab747fSPaolo Bonzini         retval = megasas_pd_get_info_submit(req->dev, lun, cmd);
160749ab747fSPaolo Bonzini         break;
160849ab747fSPaolo Bonzini     case MFI_DCMD_LD_GET_INFO:
160949ab747fSPaolo Bonzini         retval = megasas_ld_get_info_submit(req->dev, lun, cmd);
161049ab747fSPaolo Bonzini         break;
161149ab747fSPaolo Bonzini     default:
16125104fac8SPaolo Bonzini         trace_megasas_dcmd_internal_invalid(cmd->index, cmd->dcmd_opcode);
161349ab747fSPaolo Bonzini         retval = MFI_STAT_INVALID_DCMD;
161449ab747fSPaolo Bonzini         break;
161549ab747fSPaolo Bonzini     }
161649ab747fSPaolo Bonzini     if (retval != MFI_STAT_INVALID_STATUS) {
161749ab747fSPaolo Bonzini         megasas_finish_dcmd(cmd, cmd->iov_size);
161849ab747fSPaolo Bonzini     }
161949ab747fSPaolo Bonzini     return retval;
162049ab747fSPaolo Bonzini }
162149ab747fSPaolo Bonzini 
162249ab747fSPaolo Bonzini static int megasas_enqueue_req(MegasasCmd *cmd, bool is_write)
162349ab747fSPaolo Bonzini {
162449ab747fSPaolo Bonzini     int len;
162549ab747fSPaolo Bonzini 
162649ab747fSPaolo Bonzini     len = scsi_req_enqueue(cmd->req);
162749ab747fSPaolo Bonzini     if (len < 0) {
162849ab747fSPaolo Bonzini         len = -len;
162949ab747fSPaolo Bonzini     }
163049ab747fSPaolo Bonzini     if (len > 0) {
163149ab747fSPaolo Bonzini         if (len > cmd->iov_size) {
163249ab747fSPaolo Bonzini             if (is_write) {
163349ab747fSPaolo Bonzini                 trace_megasas_iov_write_overflow(cmd->index, len,
163449ab747fSPaolo Bonzini                                                  cmd->iov_size);
163549ab747fSPaolo Bonzini             } else {
163649ab747fSPaolo Bonzini                 trace_megasas_iov_read_overflow(cmd->index, len,
163749ab747fSPaolo Bonzini                                                 cmd->iov_size);
163849ab747fSPaolo Bonzini             }
163949ab747fSPaolo Bonzini         }
164049ab747fSPaolo Bonzini         if (len < cmd->iov_size) {
164149ab747fSPaolo Bonzini             if (is_write) {
164249ab747fSPaolo Bonzini                 trace_megasas_iov_write_underflow(cmd->index, len,
164349ab747fSPaolo Bonzini                                                   cmd->iov_size);
164449ab747fSPaolo Bonzini             } else {
164549ab747fSPaolo Bonzini                 trace_megasas_iov_read_underflow(cmd->index, len,
164649ab747fSPaolo Bonzini                                                  cmd->iov_size);
164749ab747fSPaolo Bonzini             }
164849ab747fSPaolo Bonzini             cmd->iov_size = len;
164949ab747fSPaolo Bonzini         }
165049ab747fSPaolo Bonzini         scsi_req_continue(cmd->req);
165149ab747fSPaolo Bonzini     }
165249ab747fSPaolo Bonzini     return len;
165349ab747fSPaolo Bonzini }
165449ab747fSPaolo Bonzini 
165549ab747fSPaolo Bonzini static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
165636c327a6SPaolo Bonzini                                int frame_cmd)
165749ab747fSPaolo Bonzini {
165849ab747fSPaolo Bonzini     uint8_t *cdb;
1659b356807fSPaolo Bonzini     int target_id, lun_id, cdb_len;
166049ab747fSPaolo Bonzini     bool is_write;
166149ab747fSPaolo Bonzini     struct SCSIDevice *sdev = NULL;
166236c327a6SPaolo Bonzini     bool is_logical = (frame_cmd == MFI_CMD_LD_SCSI_IO);
166349ab747fSPaolo Bonzini 
166449ab747fSPaolo Bonzini     cdb = cmd->frame->pass.cdb;
1665b356807fSPaolo Bonzini     target_id = cmd->frame->header.target_id;
1666b356807fSPaolo Bonzini     lun_id = cmd->frame->header.lun_id;
1667b356807fSPaolo Bonzini     cdb_len = cmd->frame->header.cdb_len;
166849ab747fSPaolo Bonzini 
16693f2cd4ddSHannes Reinecke     if (is_logical) {
1670b356807fSPaolo Bonzini         if (target_id >= MFI_MAX_LD || lun_id != 0) {
16713f2cd4ddSHannes Reinecke             trace_megasas_scsi_target_not_present(
1672b356807fSPaolo Bonzini                 mfi_frame_desc[frame_cmd], is_logical, target_id, lun_id);
16733f2cd4ddSHannes Reinecke             return MFI_STAT_DEVICE_NOT_FOUND;
16743f2cd4ddSHannes Reinecke         }
16753f2cd4ddSHannes Reinecke     }
1676b356807fSPaolo Bonzini     sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
16773f2cd4ddSHannes Reinecke 
167849ab747fSPaolo Bonzini     cmd->iov_size = le32_to_cpu(cmd->frame->header.data_len);
167936c327a6SPaolo Bonzini     trace_megasas_handle_scsi(mfi_frame_desc[frame_cmd], is_logical,
1680b356807fSPaolo Bonzini                               target_id, lun_id, sdev, cmd->iov_size);
168149ab747fSPaolo Bonzini 
168249ab747fSPaolo Bonzini     if (!sdev || (megasas_is_jbod(s) && is_logical)) {
168349ab747fSPaolo Bonzini         trace_megasas_scsi_target_not_present(
1684b356807fSPaolo Bonzini             mfi_frame_desc[frame_cmd], is_logical, target_id, lun_id);
168549ab747fSPaolo Bonzini         return MFI_STAT_DEVICE_NOT_FOUND;
168649ab747fSPaolo Bonzini     }
168749ab747fSPaolo Bonzini 
1688b356807fSPaolo Bonzini     if (cdb_len > 16) {
168949ab747fSPaolo Bonzini         trace_megasas_scsi_invalid_cdb_len(
169036c327a6SPaolo Bonzini                 mfi_frame_desc[frame_cmd], is_logical,
1691b356807fSPaolo Bonzini                 target_id, lun_id, cdb_len);
169249ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
169349ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = CHECK_CONDITION;
169449ab747fSPaolo Bonzini         s->event_count++;
169549ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
169649ab747fSPaolo Bonzini     }
169749ab747fSPaolo Bonzini 
169849ab747fSPaolo Bonzini     if (megasas_map_sgl(s, cmd, &cmd->frame->pass.sgl)) {
169949ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
170049ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = CHECK_CONDITION;
170149ab747fSPaolo Bonzini         s->event_count++;
170249ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
170349ab747fSPaolo Bonzini     }
170449ab747fSPaolo Bonzini 
1705b356807fSPaolo Bonzini     cmd->req = scsi_req_new(sdev, cmd->index, lun_id, cdb, cmd);
170649ab747fSPaolo Bonzini     if (!cmd->req) {
170749ab747fSPaolo Bonzini         trace_megasas_scsi_req_alloc_failed(
1708b356807fSPaolo Bonzini                 mfi_frame_desc[frame_cmd], target_id, lun_id);
170949ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
171049ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = BUSY;
171149ab747fSPaolo Bonzini         s->event_count++;
171249ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
171349ab747fSPaolo Bonzini     }
171449ab747fSPaolo Bonzini 
171549ab747fSPaolo Bonzini     is_write = (cmd->req->cmd.mode == SCSI_XFER_TO_DEV);
1716aaf2a859SHannes Reinecke     if (cmd->iov_size) {
171749ab747fSPaolo Bonzini         if (is_write) {
1718aaf2a859SHannes Reinecke             trace_megasas_scsi_write_start(cmd->index, cmd->iov_size);
171949ab747fSPaolo Bonzini         } else {
1720aaf2a859SHannes Reinecke             trace_megasas_scsi_read_start(cmd->index, cmd->iov_size);
172149ab747fSPaolo Bonzini         }
172249ab747fSPaolo Bonzini     } else {
172349ab747fSPaolo Bonzini         trace_megasas_scsi_nodata(cmd->index);
172449ab747fSPaolo Bonzini     }
1725aaf2a859SHannes Reinecke     megasas_enqueue_req(cmd, is_write);
172649ab747fSPaolo Bonzini     return MFI_STAT_INVALID_STATUS;
172749ab747fSPaolo Bonzini }
172849ab747fSPaolo Bonzini 
172936c327a6SPaolo Bonzini static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
173049ab747fSPaolo Bonzini {
173149ab747fSPaolo Bonzini     uint32_t lba_count, lba_start_hi, lba_start_lo;
173249ab747fSPaolo Bonzini     uint64_t lba_start;
173336c327a6SPaolo Bonzini     bool is_write = (frame_cmd == MFI_CMD_LD_WRITE);
173449ab747fSPaolo Bonzini     uint8_t cdb[16];
173549ab747fSPaolo Bonzini     int len;
173649ab747fSPaolo Bonzini     struct SCSIDevice *sdev = NULL;
1737b356807fSPaolo Bonzini     int target_id, lun_id, cdb_len;
173849ab747fSPaolo Bonzini 
173949ab747fSPaolo Bonzini     lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
174049ab747fSPaolo Bonzini     lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
174149ab747fSPaolo Bonzini     lba_start_hi = le32_to_cpu(cmd->frame->io.lba_hi);
174249ab747fSPaolo Bonzini     lba_start = ((uint64_t)lba_start_hi << 32) | lba_start_lo;
174349ab747fSPaolo Bonzini 
1744b356807fSPaolo Bonzini     target_id = cmd->frame->header.target_id;
1745b356807fSPaolo Bonzini     lun_id = cmd->frame->header.lun_id;
1746b356807fSPaolo Bonzini     cdb_len = cmd->frame->header.cdb_len;
1747b356807fSPaolo Bonzini 
1748b356807fSPaolo Bonzini     if (target_id < MFI_MAX_LD && lun_id == 0) {
1749b356807fSPaolo Bonzini         sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
175049ab747fSPaolo Bonzini     }
175149ab747fSPaolo Bonzini 
175249ab747fSPaolo Bonzini     trace_megasas_handle_io(cmd->index,
1753b356807fSPaolo Bonzini                             mfi_frame_desc[frame_cmd], target_id, lun_id,
175449ab747fSPaolo Bonzini                             (unsigned long)lba_start, (unsigned long)lba_count);
175549ab747fSPaolo Bonzini     if (!sdev) {
175649ab747fSPaolo Bonzini         trace_megasas_io_target_not_present(cmd->index,
1757b356807fSPaolo Bonzini             mfi_frame_desc[frame_cmd], target_id, lun_id);
175849ab747fSPaolo Bonzini         return MFI_STAT_DEVICE_NOT_FOUND;
175949ab747fSPaolo Bonzini     }
176049ab747fSPaolo Bonzini 
1761b356807fSPaolo Bonzini     if (cdb_len > 16) {
176249ab747fSPaolo Bonzini         trace_megasas_scsi_invalid_cdb_len(
1763b356807fSPaolo Bonzini             mfi_frame_desc[frame_cmd], 1, target_id, lun_id, cdb_len);
176449ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
176549ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = CHECK_CONDITION;
176649ab747fSPaolo Bonzini         s->event_count++;
176749ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
176849ab747fSPaolo Bonzini     }
176949ab747fSPaolo Bonzini 
177049ab747fSPaolo Bonzini     cmd->iov_size = lba_count * sdev->blocksize;
177149ab747fSPaolo Bonzini     if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
177249ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
177349ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = CHECK_CONDITION;
177449ab747fSPaolo Bonzini         s->event_count++;
177549ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
177649ab747fSPaolo Bonzini     }
177749ab747fSPaolo Bonzini 
177849ab747fSPaolo Bonzini     megasas_encode_lba(cdb, lba_start, lba_count, is_write);
177949ab747fSPaolo Bonzini     cmd->req = scsi_req_new(sdev, cmd->index,
1780b356807fSPaolo Bonzini                             lun_id, cdb, cmd);
178149ab747fSPaolo Bonzini     if (!cmd->req) {
178249ab747fSPaolo Bonzini         trace_megasas_scsi_req_alloc_failed(
1783b356807fSPaolo Bonzini             mfi_frame_desc[frame_cmd], target_id, lun_id);
178449ab747fSPaolo Bonzini         megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
178549ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = BUSY;
178649ab747fSPaolo Bonzini         s->event_count++;
178749ab747fSPaolo Bonzini         return MFI_STAT_SCSI_DONE_WITH_ERROR;
178849ab747fSPaolo Bonzini     }
178949ab747fSPaolo Bonzini     len = megasas_enqueue_req(cmd, is_write);
179049ab747fSPaolo Bonzini     if (len > 0) {
179149ab747fSPaolo Bonzini         if (is_write) {
179249ab747fSPaolo Bonzini             trace_megasas_io_write_start(cmd->index, lba_start, lba_count, len);
179349ab747fSPaolo Bonzini         } else {
179449ab747fSPaolo Bonzini             trace_megasas_io_read_start(cmd->index, lba_start, lba_count, len);
179549ab747fSPaolo Bonzini         }
179649ab747fSPaolo Bonzini     }
179749ab747fSPaolo Bonzini     return MFI_STAT_INVALID_STATUS;
179849ab747fSPaolo Bonzini }
179949ab747fSPaolo Bonzini 
180049ab747fSPaolo Bonzini static QEMUSGList *megasas_get_sg_list(SCSIRequest *req)
180149ab747fSPaolo Bonzini {
180249ab747fSPaolo Bonzini     MegasasCmd *cmd = req->hba_private;
180349ab747fSPaolo Bonzini 
180436c327a6SPaolo Bonzini     if (cmd->dcmd_opcode != -1) {
180549ab747fSPaolo Bonzini         return NULL;
180649ab747fSPaolo Bonzini     } else {
180749ab747fSPaolo Bonzini         return &cmd->qsg;
180849ab747fSPaolo Bonzini     }
180949ab747fSPaolo Bonzini }
181049ab747fSPaolo Bonzini 
181149ab747fSPaolo Bonzini static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
181249ab747fSPaolo Bonzini {
181349ab747fSPaolo Bonzini     MegasasCmd *cmd = req->hba_private;
181449ab747fSPaolo Bonzini     uint8_t *buf;
181549ab747fSPaolo Bonzini 
181649ab747fSPaolo Bonzini     trace_megasas_io_complete(cmd->index, len);
181749ab747fSPaolo Bonzini 
181836c327a6SPaolo Bonzini     if (cmd->dcmd_opcode != -1) {
181949ab747fSPaolo Bonzini         scsi_req_continue(req);
182049ab747fSPaolo Bonzini         return;
182149ab747fSPaolo Bonzini     }
182249ab747fSPaolo Bonzini 
182349ab747fSPaolo Bonzini     buf = scsi_req_get_buf(req);
18245104fac8SPaolo Bonzini     if (cmd->dcmd_opcode == MFI_DCMD_PD_GET_INFO && cmd->iov_buf) {
182549ab747fSPaolo Bonzini         struct mfi_pd_info *info = cmd->iov_buf;
182649ab747fSPaolo Bonzini 
182749ab747fSPaolo Bonzini         if (info->inquiry_data[0] == 0x7f) {
182849ab747fSPaolo Bonzini             memset(info->inquiry_data, 0, sizeof(info->inquiry_data));
182949ab747fSPaolo Bonzini             memcpy(info->inquiry_data, buf, len);
183049ab747fSPaolo Bonzini         } else if (info->vpd_page83[0] == 0x7f) {
183149ab747fSPaolo Bonzini             memset(info->vpd_page83, 0, sizeof(info->vpd_page83));
183249ab747fSPaolo Bonzini             memcpy(info->vpd_page83, buf, len);
183349ab747fSPaolo Bonzini         }
183449ab747fSPaolo Bonzini         scsi_req_continue(req);
18355104fac8SPaolo Bonzini     } else if (cmd->dcmd_opcode == MFI_DCMD_LD_GET_INFO) {
183649ab747fSPaolo Bonzini         struct mfi_ld_info *info = cmd->iov_buf;
183749ab747fSPaolo Bonzini 
183849ab747fSPaolo Bonzini         if (cmd->iov_buf) {
183949ab747fSPaolo Bonzini             memcpy(info->vpd_page83, buf, sizeof(info->vpd_page83));
184049ab747fSPaolo Bonzini             scsi_req_continue(req);
184149ab747fSPaolo Bonzini         }
184249ab747fSPaolo Bonzini     }
184349ab747fSPaolo Bonzini }
184449ab747fSPaolo Bonzini 
184549ab747fSPaolo Bonzini static void megasas_command_complete(SCSIRequest *req, uint32_t status,
184649ab747fSPaolo Bonzini                                      size_t resid)
184749ab747fSPaolo Bonzini {
184849ab747fSPaolo Bonzini     MegasasCmd *cmd = req->hba_private;
184949ab747fSPaolo Bonzini     uint8_t cmd_status = MFI_STAT_OK;
185049ab747fSPaolo Bonzini 
185149ab747fSPaolo Bonzini     trace_megasas_command_complete(cmd->index, status, resid);
185249ab747fSPaolo Bonzini 
18539e55d588SPaolo Bonzini     if (req->io_canceled) {
18549e55d588SPaolo Bonzini         return;
18559e55d588SPaolo Bonzini     }
18569e55d588SPaolo Bonzini 
185787e459a8SPaolo Bonzini     if (cmd->dcmd_opcode != -1) {
185849ab747fSPaolo Bonzini         /*
185949ab747fSPaolo Bonzini          * Internal command complete
186049ab747fSPaolo Bonzini          */
186136c327a6SPaolo Bonzini         cmd_status = megasas_finish_internal_dcmd(cmd, req, resid);
186249ab747fSPaolo Bonzini         if (cmd_status == MFI_STAT_INVALID_STATUS) {
186349ab747fSPaolo Bonzini             return;
186449ab747fSPaolo Bonzini         }
186549ab747fSPaolo Bonzini     } else {
186649ab747fSPaolo Bonzini         req->status = status;
186749ab747fSPaolo Bonzini         trace_megasas_scsi_complete(cmd->index, req->status,
186849ab747fSPaolo Bonzini                                     cmd->iov_size, req->cmd.xfer);
186949ab747fSPaolo Bonzini         if (req->status != GOOD) {
187049ab747fSPaolo Bonzini             cmd_status = MFI_STAT_SCSI_DONE_WITH_ERROR;
187149ab747fSPaolo Bonzini         }
187249ab747fSPaolo Bonzini         if (req->status == CHECK_CONDITION) {
187349ab747fSPaolo Bonzini             megasas_copy_sense(cmd);
187449ab747fSPaolo Bonzini         }
187549ab747fSPaolo Bonzini 
187649ab747fSPaolo Bonzini         cmd->frame->header.scsi_status = req->status;
187749ab747fSPaolo Bonzini     }
187849ab747fSPaolo Bonzini     cmd->frame->header.cmd_status = cmd_status;
18799e55d588SPaolo Bonzini     megasas_complete_command(cmd);
188049ab747fSPaolo Bonzini }
188149ab747fSPaolo Bonzini 
18829e55d588SPaolo Bonzini static void megasas_command_cancelled(SCSIRequest *req)
188349ab747fSPaolo Bonzini {
188449ab747fSPaolo Bonzini     MegasasCmd *cmd = req->hba_private;
188549ab747fSPaolo Bonzini 
18869e55d588SPaolo Bonzini     if (!cmd) {
18879e55d588SPaolo Bonzini         return;
188849ab747fSPaolo Bonzini     }
18899e55d588SPaolo Bonzini     cmd->frame->header.cmd_status = MFI_STAT_SCSI_IO_FAILED;
18909e55d588SPaolo Bonzini     megasas_complete_command(cmd);
189149ab747fSPaolo Bonzini }
189249ab747fSPaolo Bonzini 
189349ab747fSPaolo Bonzini static int megasas_handle_abort(MegasasState *s, MegasasCmd *cmd)
189449ab747fSPaolo Bonzini {
189549ab747fSPaolo Bonzini     uint64_t abort_ctx = le64_to_cpu(cmd->frame->abort.abort_context);
189649ab747fSPaolo Bonzini     hwaddr abort_addr, addr_hi, addr_lo;
189749ab747fSPaolo Bonzini     MegasasCmd *abort_cmd;
189849ab747fSPaolo Bonzini 
189949ab747fSPaolo Bonzini     addr_hi = le32_to_cpu(cmd->frame->abort.abort_mfi_addr_hi);
190049ab747fSPaolo Bonzini     addr_lo = le32_to_cpu(cmd->frame->abort.abort_mfi_addr_lo);
190149ab747fSPaolo Bonzini     abort_addr = ((uint64_t)addr_hi << 32) | addr_lo;
190249ab747fSPaolo Bonzini 
190349ab747fSPaolo Bonzini     abort_cmd = megasas_lookup_frame(s, abort_addr);
190449ab747fSPaolo Bonzini     if (!abort_cmd) {
190549ab747fSPaolo Bonzini         trace_megasas_abort_no_cmd(cmd->index, abort_ctx);
190649ab747fSPaolo Bonzini         s->event_count++;
190749ab747fSPaolo Bonzini         return MFI_STAT_OK;
190849ab747fSPaolo Bonzini     }
190949ab747fSPaolo Bonzini     if (!megasas_use_queue64(s)) {
191049ab747fSPaolo Bonzini         abort_ctx &= (uint64_t)0xFFFFFFFF;
191149ab747fSPaolo Bonzini     }
191249ab747fSPaolo Bonzini     if (abort_cmd->context != abort_ctx) {
1913d17e7448SEric Blake         trace_megasas_abort_invalid_context(cmd->index, abort_cmd->context,
1914d17e7448SEric Blake                                             abort_cmd->index);
191549ab747fSPaolo Bonzini         s->event_count++;
191649ab747fSPaolo Bonzini         return MFI_STAT_ABORT_NOT_POSSIBLE;
191749ab747fSPaolo Bonzini     }
191849ab747fSPaolo Bonzini     trace_megasas_abort_frame(cmd->index, abort_cmd->index);
191949ab747fSPaolo Bonzini     megasas_abort_command(abort_cmd);
192049ab747fSPaolo Bonzini     if (!s->event_cmd || abort_cmd != s->event_cmd) {
192149ab747fSPaolo Bonzini         s->event_cmd = NULL;
192249ab747fSPaolo Bonzini     }
192349ab747fSPaolo Bonzini     s->event_count++;
192449ab747fSPaolo Bonzini     return MFI_STAT_OK;
192549ab747fSPaolo Bonzini }
192649ab747fSPaolo Bonzini 
192749ab747fSPaolo Bonzini static void megasas_handle_frame(MegasasState *s, uint64_t frame_addr,
192849ab747fSPaolo Bonzini                                  uint32_t frame_count)
192949ab747fSPaolo Bonzini {
193049ab747fSPaolo Bonzini     uint8_t frame_status = MFI_STAT_INVALID_CMD;
193149ab747fSPaolo Bonzini     uint64_t frame_context;
193236c327a6SPaolo Bonzini     int frame_cmd;
193349ab747fSPaolo Bonzini     MegasasCmd *cmd;
193449ab747fSPaolo Bonzini 
193549ab747fSPaolo Bonzini     /*
193649ab747fSPaolo Bonzini      * Always read 64bit context, top bits will be
193749ab747fSPaolo Bonzini      * masked out if required in megasas_enqueue_frame()
193849ab747fSPaolo Bonzini      */
193916578c6fSPaolo Bonzini     frame_context = megasas_frame_get_context(s, frame_addr);
194049ab747fSPaolo Bonzini 
194149ab747fSPaolo Bonzini     cmd = megasas_enqueue_frame(s, frame_addr, frame_context, frame_count);
194249ab747fSPaolo Bonzini     if (!cmd) {
194349ab747fSPaolo Bonzini         /* reply queue full */
194449ab747fSPaolo Bonzini         trace_megasas_frame_busy(frame_addr);
194516578c6fSPaolo Bonzini         megasas_frame_set_scsi_status(s, frame_addr, BUSY);
194616578c6fSPaolo Bonzini         megasas_frame_set_cmd_status(s, frame_addr, MFI_STAT_SCSI_DONE_WITH_ERROR);
194749ab747fSPaolo Bonzini         megasas_complete_frame(s, frame_context);
194849ab747fSPaolo Bonzini         s->event_count++;
194949ab747fSPaolo Bonzini         return;
195049ab747fSPaolo Bonzini     }
195136c327a6SPaolo Bonzini     frame_cmd = cmd->frame->header.frame_cmd;
195236c327a6SPaolo Bonzini     switch (frame_cmd) {
195349ab747fSPaolo Bonzini     case MFI_CMD_INIT:
195449ab747fSPaolo Bonzini         frame_status = megasas_init_firmware(s, cmd);
195549ab747fSPaolo Bonzini         break;
195649ab747fSPaolo Bonzini     case MFI_CMD_DCMD:
195749ab747fSPaolo Bonzini         frame_status = megasas_handle_dcmd(s, cmd);
195849ab747fSPaolo Bonzini         break;
195949ab747fSPaolo Bonzini     case MFI_CMD_ABORT:
196049ab747fSPaolo Bonzini         frame_status = megasas_handle_abort(s, cmd);
196149ab747fSPaolo Bonzini         break;
196249ab747fSPaolo Bonzini     case MFI_CMD_PD_SCSI_IO:
196349ab747fSPaolo Bonzini     case MFI_CMD_LD_SCSI_IO:
196436c327a6SPaolo Bonzini         frame_status = megasas_handle_scsi(s, cmd, frame_cmd);
196549ab747fSPaolo Bonzini         break;
196649ab747fSPaolo Bonzini     case MFI_CMD_LD_READ:
196749ab747fSPaolo Bonzini     case MFI_CMD_LD_WRITE:
196836c327a6SPaolo Bonzini         frame_status = megasas_handle_io(s, cmd, frame_cmd);
196949ab747fSPaolo Bonzini         break;
197049ab747fSPaolo Bonzini     default:
197136c327a6SPaolo Bonzini         trace_megasas_unhandled_frame_cmd(cmd->index, frame_cmd);
197249ab747fSPaolo Bonzini         s->event_count++;
197349ab747fSPaolo Bonzini         break;
197449ab747fSPaolo Bonzini     }
197549ab747fSPaolo Bonzini     if (frame_status != MFI_STAT_INVALID_STATUS) {
1976421cc3e7SPaolo Bonzini         if (cmd->frame) {
197749ab747fSPaolo Bonzini             cmd->frame->header.cmd_status = frame_status;
1978421cc3e7SPaolo Bonzini         } else {
1979421cc3e7SPaolo Bonzini             megasas_frame_set_cmd_status(s, frame_addr, frame_status);
1980421cc3e7SPaolo Bonzini         }
19816df5718bSHannes Reinecke         megasas_unmap_frame(s, cmd);
198249ab747fSPaolo Bonzini         megasas_complete_frame(s, cmd->context);
198349ab747fSPaolo Bonzini     }
198449ab747fSPaolo Bonzini }
198549ab747fSPaolo Bonzini 
198649ab747fSPaolo Bonzini static uint64_t megasas_mmio_read(void *opaque, hwaddr addr,
198749ab747fSPaolo Bonzini                                   unsigned size)
198849ab747fSPaolo Bonzini {
198949ab747fSPaolo Bonzini     MegasasState *s = opaque;
1990e23d0498SHannes Reinecke     PCIDevice *pci_dev = PCI_DEVICE(s);
1991e23d0498SHannes Reinecke     MegasasBaseClass *base_class = MEGASAS_DEVICE_GET_CLASS(s);
199249ab747fSPaolo Bonzini     uint32_t retval = 0;
199349ab747fSPaolo Bonzini 
199449ab747fSPaolo Bonzini     switch (addr) {
199549ab747fSPaolo Bonzini     case MFI_IDB:
199649ab747fSPaolo Bonzini         retval = 0;
199777bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_IDB", retval);
199849ab747fSPaolo Bonzini         break;
199949ab747fSPaolo Bonzini     case MFI_OMSG0:
200049ab747fSPaolo Bonzini     case MFI_OSP0:
2001e23d0498SHannes Reinecke         retval = (msix_present(pci_dev) ? MFI_FWSTATE_MSIX_SUPPORTED : 0) |
200249ab747fSPaolo Bonzini             (s->fw_state & MFI_FWSTATE_MASK) |
200349ab747fSPaolo Bonzini             ((s->fw_sge & 0xff) << 16) |
200449ab747fSPaolo Bonzini             (s->fw_cmds & 0xFFFF);
200577bb6b17SHannes Reinecke         trace_megasas_mmio_readl(addr == MFI_OMSG0 ? "MFI_OMSG0" : "MFI_OSP0",
200677bb6b17SHannes Reinecke                                  retval);
200749ab747fSPaolo Bonzini         break;
200849ab747fSPaolo Bonzini     case MFI_OSTS:
200949ab747fSPaolo Bonzini         if (megasas_intr_enabled(s) && s->doorbell) {
2010e23d0498SHannes Reinecke             retval = base_class->osts;
201149ab747fSPaolo Bonzini         }
201277bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_OSTS", retval);
201349ab747fSPaolo Bonzini         break;
201449ab747fSPaolo Bonzini     case MFI_OMSK:
201549ab747fSPaolo Bonzini         retval = s->intr_mask;
201677bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_OMSK", retval);
201749ab747fSPaolo Bonzini         break;
201849ab747fSPaolo Bonzini     case MFI_ODCR0:
20197957ee71SHannes Reinecke         retval = s->doorbell ? 1 : 0;
202077bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_ODCR0", retval);
202149ab747fSPaolo Bonzini         break;
2022e23d0498SHannes Reinecke     case MFI_DIAG:
2023e23d0498SHannes Reinecke         retval = s->diag;
202477bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_DIAG", retval);
2025e23d0498SHannes Reinecke         break;
2026e23d0498SHannes Reinecke     case MFI_OSP1:
2027e23d0498SHannes Reinecke         retval = 15;
202877bb6b17SHannes Reinecke         trace_megasas_mmio_readl("MFI_OSP1", retval);
2029e23d0498SHannes Reinecke         break;
203049ab747fSPaolo Bonzini     default:
203149ab747fSPaolo Bonzini         trace_megasas_mmio_invalid_readl(addr);
203249ab747fSPaolo Bonzini         break;
203349ab747fSPaolo Bonzini     }
203449ab747fSPaolo Bonzini     return retval;
203549ab747fSPaolo Bonzini }
203649ab747fSPaolo Bonzini 
2037e23d0498SHannes Reinecke static int adp_reset_seq[] = {0x00, 0x04, 0x0b, 0x02, 0x07, 0x0d};
2038e23d0498SHannes Reinecke 
203949ab747fSPaolo Bonzini static void megasas_mmio_write(void *opaque, hwaddr addr,
204049ab747fSPaolo Bonzini                                uint64_t val, unsigned size)
204149ab747fSPaolo Bonzini {
204249ab747fSPaolo Bonzini     MegasasState *s = opaque;
204352190c1eSAndreas Färber     PCIDevice *pci_dev = PCI_DEVICE(s);
204449ab747fSPaolo Bonzini     uint64_t frame_addr;
204549ab747fSPaolo Bonzini     uint32_t frame_count;
204649ab747fSPaolo Bonzini     int i;
204749ab747fSPaolo Bonzini 
204849ab747fSPaolo Bonzini     switch (addr) {
204949ab747fSPaolo Bonzini     case MFI_IDB:
205077bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_IDB", val);
205149ab747fSPaolo Bonzini         if (val & MFI_FWINIT_ABORT) {
205249ab747fSPaolo Bonzini             /* Abort all pending cmds */
205349ab747fSPaolo Bonzini             for (i = 0; i < s->fw_cmds; i++) {
205449ab747fSPaolo Bonzini                 megasas_abort_command(&s->frames[i]);
205549ab747fSPaolo Bonzini             }
205649ab747fSPaolo Bonzini         }
205749ab747fSPaolo Bonzini         if (val & MFI_FWINIT_READY) {
205849ab747fSPaolo Bonzini             /* move to FW READY */
205949ab747fSPaolo Bonzini             megasas_soft_reset(s);
206049ab747fSPaolo Bonzini         }
206149ab747fSPaolo Bonzini         if (val & MFI_FWINIT_MFIMODE) {
206249ab747fSPaolo Bonzini             /* discard MFIs */
206349ab747fSPaolo Bonzini         }
2064e23d0498SHannes Reinecke         if (val & MFI_FWINIT_STOP_ADP) {
2065e23d0498SHannes Reinecke             /* Terminal error, stop processing */
2066e23d0498SHannes Reinecke             s->fw_state = MFI_FWSTATE_FAULT;
2067e23d0498SHannes Reinecke         }
206849ab747fSPaolo Bonzini         break;
206949ab747fSPaolo Bonzini     case MFI_OMSK:
207077bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_OMSK", val);
207149ab747fSPaolo Bonzini         s->intr_mask = val;
20724522b69cSHannes Reinecke         if (!megasas_intr_enabled(s) &&
20734522b69cSHannes Reinecke             !msi_enabled(pci_dev) &&
20744522b69cSHannes Reinecke             !msix_enabled(pci_dev)) {
207549ab747fSPaolo Bonzini             trace_megasas_irq_lower();
20769e64f8a3SMarcel Apfelbaum             pci_irq_deassert(pci_dev);
207749ab747fSPaolo Bonzini         }
207849ab747fSPaolo Bonzini         if (megasas_intr_enabled(s)) {
20794522b69cSHannes Reinecke             if (msix_enabled(pci_dev)) {
20804522b69cSHannes Reinecke                 trace_megasas_msix_enabled(0);
20814522b69cSHannes Reinecke             } else if (msi_enabled(pci_dev)) {
20824522b69cSHannes Reinecke                 trace_megasas_msi_enabled(0);
20834522b69cSHannes Reinecke             } else {
208449ab747fSPaolo Bonzini                 trace_megasas_intr_enabled();
20854522b69cSHannes Reinecke             }
208649ab747fSPaolo Bonzini         } else {
208749ab747fSPaolo Bonzini             trace_megasas_intr_disabled();
2088e23d0498SHannes Reinecke             megasas_soft_reset(s);
208949ab747fSPaolo Bonzini         }
209049ab747fSPaolo Bonzini         break;
209149ab747fSPaolo Bonzini     case MFI_ODCR0:
209277bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_ODCR0", val);
209349ab747fSPaolo Bonzini         s->doorbell = 0;
20947957ee71SHannes Reinecke         if (megasas_intr_enabled(s)) {
20957957ee71SHannes Reinecke             if (!msix_enabled(pci_dev) && !msi_enabled(pci_dev)) {
209649ab747fSPaolo Bonzini                 trace_megasas_irq_lower();
20979e64f8a3SMarcel Apfelbaum                 pci_irq_deassert(pci_dev);
209849ab747fSPaolo Bonzini             }
209949ab747fSPaolo Bonzini         }
210049ab747fSPaolo Bonzini         break;
210149ab747fSPaolo Bonzini     case MFI_IQPH:
210277bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_IQPH", val);
210349ab747fSPaolo Bonzini         /* Received high 32 bits of a 64 bit MFI frame address */
210449ab747fSPaolo Bonzini         s->frame_hi = val;
210549ab747fSPaolo Bonzini         break;
210649ab747fSPaolo Bonzini     case MFI_IQPL:
210777bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_IQPL", val);
210849ab747fSPaolo Bonzini         /* Received low 32 bits of a 64 bit MFI frame address */
2109e23d0498SHannes Reinecke         /* Fallthrough */
211049ab747fSPaolo Bonzini     case MFI_IQP:
211177bb6b17SHannes Reinecke         if (addr == MFI_IQP) {
211277bb6b17SHannes Reinecke             trace_megasas_mmio_writel("MFI_IQP", val);
2113e23d0498SHannes Reinecke             /* Received 64 bit MFI frame address */
211477bb6b17SHannes Reinecke             s->frame_hi = 0;
211577bb6b17SHannes Reinecke         }
211649ab747fSPaolo Bonzini         frame_addr = (val & ~0x1F);
211749ab747fSPaolo Bonzini         /* Add possible 64 bit offset */
211849ab747fSPaolo Bonzini         frame_addr |= ((uint64_t)s->frame_hi << 32);
211949ab747fSPaolo Bonzini         s->frame_hi = 0;
212049ab747fSPaolo Bonzini         frame_count = (val >> 1) & 0xF;
212149ab747fSPaolo Bonzini         megasas_handle_frame(s, frame_addr, frame_count);
212249ab747fSPaolo Bonzini         break;
2123e23d0498SHannes Reinecke     case MFI_SEQ:
212477bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_SEQ", val);
2125e23d0498SHannes Reinecke         /* Magic sequence to start ADP reset */
212624dfa9faSPrasad J Pandit         if (adp_reset_seq[s->adp_reset++] == val) {
212724dfa9faSPrasad J Pandit             if (s->adp_reset == 6) {
212824dfa9faSPrasad J Pandit                 s->adp_reset = 0;
212924dfa9faSPrasad J Pandit                 s->diag = MFI_DIAG_WRITE_ENABLE;
213024dfa9faSPrasad J Pandit             }
2131e23d0498SHannes Reinecke         } else {
2132e23d0498SHannes Reinecke             s->adp_reset = 0;
2133e23d0498SHannes Reinecke             s->diag = 0;
2134e23d0498SHannes Reinecke         }
2135e23d0498SHannes Reinecke         break;
2136e23d0498SHannes Reinecke     case MFI_DIAG:
213777bb6b17SHannes Reinecke         trace_megasas_mmio_writel("MFI_DIAG", val);
2138e23d0498SHannes Reinecke         /* ADP reset */
2139e23d0498SHannes Reinecke         if ((s->diag & MFI_DIAG_WRITE_ENABLE) &&
2140e23d0498SHannes Reinecke             (val & MFI_DIAG_RESET_ADP)) {
2141e23d0498SHannes Reinecke             s->diag |= MFI_DIAG_RESET_ADP;
2142e23d0498SHannes Reinecke             megasas_soft_reset(s);
2143e23d0498SHannes Reinecke             s->adp_reset = 0;
2144e23d0498SHannes Reinecke             s->diag = 0;
2145e23d0498SHannes Reinecke         }
2146e23d0498SHannes Reinecke         break;
214749ab747fSPaolo Bonzini     default:
214849ab747fSPaolo Bonzini         trace_megasas_mmio_invalid_writel(addr, val);
214949ab747fSPaolo Bonzini         break;
215049ab747fSPaolo Bonzini     }
215149ab747fSPaolo Bonzini }
215249ab747fSPaolo Bonzini 
215349ab747fSPaolo Bonzini static const MemoryRegionOps megasas_mmio_ops = {
215449ab747fSPaolo Bonzini     .read = megasas_mmio_read,
215549ab747fSPaolo Bonzini     .write = megasas_mmio_write,
215649ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
215749ab747fSPaolo Bonzini     .impl = {
215849ab747fSPaolo Bonzini         .min_access_size = 8,
215949ab747fSPaolo Bonzini         .max_access_size = 8,
216049ab747fSPaolo Bonzini     }
216149ab747fSPaolo Bonzini };
216249ab747fSPaolo Bonzini 
216349ab747fSPaolo Bonzini static uint64_t megasas_port_read(void *opaque, hwaddr addr,
216449ab747fSPaolo Bonzini                                   unsigned size)
216549ab747fSPaolo Bonzini {
216649ab747fSPaolo Bonzini     return megasas_mmio_read(opaque, addr & 0xff, size);
216749ab747fSPaolo Bonzini }
216849ab747fSPaolo Bonzini 
216949ab747fSPaolo Bonzini static void megasas_port_write(void *opaque, hwaddr addr,
217049ab747fSPaolo Bonzini                                uint64_t val, unsigned size)
217149ab747fSPaolo Bonzini {
217249ab747fSPaolo Bonzini     megasas_mmio_write(opaque, addr & 0xff, val, size);
217349ab747fSPaolo Bonzini }
217449ab747fSPaolo Bonzini 
217549ab747fSPaolo Bonzini static const MemoryRegionOps megasas_port_ops = {
217649ab747fSPaolo Bonzini     .read = megasas_port_read,
217749ab747fSPaolo Bonzini     .write = megasas_port_write,
217849ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
217949ab747fSPaolo Bonzini     .impl = {
218049ab747fSPaolo Bonzini         .min_access_size = 4,
218149ab747fSPaolo Bonzini         .max_access_size = 4,
218249ab747fSPaolo Bonzini     }
218349ab747fSPaolo Bonzini };
218449ab747fSPaolo Bonzini 
218549ab747fSPaolo Bonzini static uint64_t megasas_queue_read(void *opaque, hwaddr addr,
218649ab747fSPaolo Bonzini                                    unsigned size)
218749ab747fSPaolo Bonzini {
218849ab747fSPaolo Bonzini     return 0;
218949ab747fSPaolo Bonzini }
219049ab747fSPaolo Bonzini 
219155875fc4SSalva Peiró static void megasas_queue_write(void *opaque, hwaddr addr,
219255875fc4SSalva Peiró                                uint64_t val, unsigned size)
219355875fc4SSalva Peiró {
219455875fc4SSalva Peiró     return;
219555875fc4SSalva Peiró }
219655875fc4SSalva Peiró 
219749ab747fSPaolo Bonzini static const MemoryRegionOps megasas_queue_ops = {
219849ab747fSPaolo Bonzini     .read = megasas_queue_read,
219955875fc4SSalva Peiró     .write = megasas_queue_write,
220049ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
220149ab747fSPaolo Bonzini     .impl = {
220249ab747fSPaolo Bonzini         .min_access_size = 8,
220349ab747fSPaolo Bonzini         .max_access_size = 8,
220449ab747fSPaolo Bonzini     }
220549ab747fSPaolo Bonzini };
220649ab747fSPaolo Bonzini 
220749ab747fSPaolo Bonzini static void megasas_soft_reset(MegasasState *s)
220849ab747fSPaolo Bonzini {
220949ab747fSPaolo Bonzini     int i;
221049ab747fSPaolo Bonzini     MegasasCmd *cmd;
221149ab747fSPaolo Bonzini 
22128d72db68SHannes Reinecke     trace_megasas_reset(s->fw_state);
221349ab747fSPaolo Bonzini     for (i = 0; i < s->fw_cmds; i++) {
221449ab747fSPaolo Bonzini         cmd = &s->frames[i];
221549ab747fSPaolo Bonzini         megasas_abort_command(cmd);
221649ab747fSPaolo Bonzini     }
22178d72db68SHannes Reinecke     if (s->fw_state == MFI_FWSTATE_READY) {
22188d72db68SHannes Reinecke         BusChild *kid;
22198d72db68SHannes Reinecke 
22208d72db68SHannes Reinecke         /*
22218d72db68SHannes Reinecke          * The EFI firmware doesn't handle UA,
22228d72db68SHannes Reinecke          * so we need to clear the Power On/Reset UA
22238d72db68SHannes Reinecke          * after the initial reset.
22248d72db68SHannes Reinecke          */
22258d72db68SHannes Reinecke         QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
2226e1dc6815SCao jin             SCSIDevice *sdev = SCSI_DEVICE(kid->child);
22278d72db68SHannes Reinecke 
22288d72db68SHannes Reinecke             sdev->unit_attention = SENSE_CODE(NO_SENSE);
22298d72db68SHannes Reinecke             scsi_device_unit_attention_reported(sdev);
22308d72db68SHannes Reinecke         }
22318d72db68SHannes Reinecke     }
223249ab747fSPaolo Bonzini     megasas_reset_frames(s);
223349ab747fSPaolo Bonzini     s->reply_queue_len = s->fw_cmds;
223449ab747fSPaolo Bonzini     s->reply_queue_pa = 0;
223549ab747fSPaolo Bonzini     s->consumer_pa = 0;
223649ab747fSPaolo Bonzini     s->producer_pa = 0;
223749ab747fSPaolo Bonzini     s->fw_state = MFI_FWSTATE_READY;
223849ab747fSPaolo Bonzini     s->doorbell = 0;
223949ab747fSPaolo Bonzini     s->intr_mask = MEGASAS_INTR_DISABLED_MASK;
224049ab747fSPaolo Bonzini     s->frame_hi = 0;
224149ab747fSPaolo Bonzini     s->flags &= ~MEGASAS_MASK_USE_QUEUE64;
224249ab747fSPaolo Bonzini     s->event_count++;
224349ab747fSPaolo Bonzini     s->boot_event = s->event_count;
224449ab747fSPaolo Bonzini }
224549ab747fSPaolo Bonzini 
224649ab747fSPaolo Bonzini static void megasas_scsi_reset(DeviceState *dev)
224749ab747fSPaolo Bonzini {
2248c79e16aeSPeter Crosthwaite     MegasasState *s = MEGASAS(dev);
224949ab747fSPaolo Bonzini 
225049ab747fSPaolo Bonzini     megasas_soft_reset(s);
225149ab747fSPaolo Bonzini }
225249ab747fSPaolo Bonzini 
2253e23d0498SHannes Reinecke static const VMStateDescription vmstate_megasas_gen1 = {
225449ab747fSPaolo Bonzini     .name = "megasas",
225549ab747fSPaolo Bonzini     .version_id = 0,
225649ab747fSPaolo Bonzini     .minimum_version_id = 0,
225749ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
225852190c1eSAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, MegasasState),
225923335f62SHannes Reinecke         VMSTATE_MSIX(parent_obj, MegasasState),
226049ab747fSPaolo Bonzini 
226149ab747fSPaolo Bonzini         VMSTATE_INT32(fw_state, MegasasState),
226249ab747fSPaolo Bonzini         VMSTATE_INT32(intr_mask, MegasasState),
226349ab747fSPaolo Bonzini         VMSTATE_INT32(doorbell, MegasasState),
226449ab747fSPaolo Bonzini         VMSTATE_UINT64(reply_queue_pa, MegasasState),
226549ab747fSPaolo Bonzini         VMSTATE_UINT64(consumer_pa, MegasasState),
226649ab747fSPaolo Bonzini         VMSTATE_UINT64(producer_pa, MegasasState),
226749ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
226849ab747fSPaolo Bonzini     }
226949ab747fSPaolo Bonzini };
227049ab747fSPaolo Bonzini 
2271e23d0498SHannes Reinecke static const VMStateDescription vmstate_megasas_gen2 = {
2272e23d0498SHannes Reinecke     .name = "megasas-gen2",
2273e23d0498SHannes Reinecke     .version_id = 0,
2274e23d0498SHannes Reinecke     .minimum_version_id = 0,
2275e23d0498SHannes Reinecke     .minimum_version_id_old = 0,
2276e23d0498SHannes Reinecke     .fields      = (VMStateField[]) {
227720daa90aSDr. David Alan Gilbert         VMSTATE_PCI_DEVICE(parent_obj, MegasasState),
2278e23d0498SHannes Reinecke         VMSTATE_MSIX(parent_obj, MegasasState),
2279e23d0498SHannes Reinecke 
2280e23d0498SHannes Reinecke         VMSTATE_INT32(fw_state, MegasasState),
2281e23d0498SHannes Reinecke         VMSTATE_INT32(intr_mask, MegasasState),
2282e23d0498SHannes Reinecke         VMSTATE_INT32(doorbell, MegasasState),
2283e23d0498SHannes Reinecke         VMSTATE_UINT64(reply_queue_pa, MegasasState),
2284e23d0498SHannes Reinecke         VMSTATE_UINT64(consumer_pa, MegasasState),
2285e23d0498SHannes Reinecke         VMSTATE_UINT64(producer_pa, MegasasState),
2286e23d0498SHannes Reinecke         VMSTATE_END_OF_LIST()
2287e23d0498SHannes Reinecke     }
2288e23d0498SHannes Reinecke };
2289e23d0498SHannes Reinecke 
229049ab747fSPaolo Bonzini static void megasas_scsi_uninit(PCIDevice *d)
229149ab747fSPaolo Bonzini {
2292c79e16aeSPeter Crosthwaite     MegasasState *s = MEGASAS(d);
229349ab747fSPaolo Bonzini 
22944522b69cSHannes Reinecke     if (megasas_use_msix(s)) {
229523335f62SHannes Reinecke         msix_uninit(d, &s->mmio_io, &s->mmio_io);
22964522b69cSHannes Reinecke     }
22974522b69cSHannes Reinecke     msi_uninit(d);
22984522b69cSHannes Reinecke }
229949ab747fSPaolo Bonzini 
230049ab747fSPaolo Bonzini static const struct SCSIBusInfo megasas_scsi_info = {
230149ab747fSPaolo Bonzini     .tcq = true,
230249ab747fSPaolo Bonzini     .max_target = MFI_MAX_LD,
230349ab747fSPaolo Bonzini     .max_lun = 255,
230449ab747fSPaolo Bonzini 
230549ab747fSPaolo Bonzini     .transfer_data = megasas_xfer_complete,
230649ab747fSPaolo Bonzini     .get_sg_list = megasas_get_sg_list,
230749ab747fSPaolo Bonzini     .complete = megasas_command_complete,
23089e55d588SPaolo Bonzini     .cancel = megasas_command_cancelled,
230949ab747fSPaolo Bonzini };
231049ab747fSPaolo Bonzini 
2311ae071cc8SMarkus Armbruster static void megasas_scsi_realize(PCIDevice *dev, Error **errp)
231249ab747fSPaolo Bonzini {
2313c79e16aeSPeter Crosthwaite     MegasasState *s = MEGASAS(dev);
2314e23d0498SHannes Reinecke     MegasasBaseClass *b = MEGASAS_DEVICE_GET_CLASS(s);
231549ab747fSPaolo Bonzini     uint8_t *pci_conf;
231649ab747fSPaolo Bonzini     int i, bar_type;
23171108b2f8SCao jin     Error *err = NULL;
23181108b2f8SCao jin     int ret;
231949ab747fSPaolo Bonzini 
232052190c1eSAndreas Färber     pci_conf = dev->config;
232149ab747fSPaolo Bonzini 
232249ab747fSPaolo Bonzini     /* PCI latency timer = 0 */
232349ab747fSPaolo Bonzini     pci_conf[PCI_LATENCY_TIMER] = 0;
232449ab747fSPaolo Bonzini     /* Interrupt pin 1 */
232549ab747fSPaolo Bonzini     pci_conf[PCI_INTERRUPT_PIN] = 0x01;
232649ab747fSPaolo Bonzini 
2327afea4e14SCao jin     if (s->msi != ON_OFF_AUTO_OFF) {
23281108b2f8SCao jin         ret = msi_init(dev, 0x50, 1, true, false, &err);
23291108b2f8SCao jin         /* Any error other than -ENOTSUP(board's MSI support is broken)
23301108b2f8SCao jin          * is a programming error */
23311108b2f8SCao jin         assert(!ret || ret == -ENOTSUP);
23321108b2f8SCao jin         if (ret && s->msi == ON_OFF_AUTO_ON) {
23331108b2f8SCao jin             /* Can't satisfy user's explicit msi=on request, fail */
23341108b2f8SCao jin             error_append_hint(&err, "You have to use msi=auto (default) or "
23351108b2f8SCao jin                     "msi=off with this machine type.\n");
23361108b2f8SCao jin             error_propagate(errp, err);
23371108b2f8SCao jin             return;
23381108b2f8SCao jin         } else if (ret) {
23391108b2f8SCao jin             /* With msi=auto, we fall back to MSI off silently */
23401108b2f8SCao jin             s->msi = ON_OFF_AUTO_OFF;
23411108b2f8SCao jin             error_free(err);
23421108b2f8SCao jin         }
23431108b2f8SCao jin     }
23441108b2f8SCao jin 
234529776739SPaolo Bonzini     memory_region_init_io(&s->mmio_io, OBJECT(s), &megasas_mmio_ops, s,
234649ab747fSPaolo Bonzini                           "megasas-mmio", 0x4000);
234729776739SPaolo Bonzini     memory_region_init_io(&s->port_io, OBJECT(s), &megasas_port_ops, s,
234849ab747fSPaolo Bonzini                           "megasas-io", 256);
234929776739SPaolo Bonzini     memory_region_init_io(&s->queue_io, OBJECT(s), &megasas_queue_ops, s,
235049ab747fSPaolo Bonzini                           "megasas-queue", 0x40000);
235149ab747fSPaolo Bonzini 
235249ab747fSPaolo Bonzini     if (megasas_use_msix(s) &&
2353e23d0498SHannes Reinecke         msix_init(dev, 15, &s->mmio_io, b->mmio_bar, 0x2000,
2354ee640c62SCao jin                   &s->mmio_io, b->mmio_bar, 0x3800, 0x68, NULL)) {
2355ee640c62SCao jin         /* TODO: check msix_init's error, and should fail on msix=on */
2356b4b4a57fSCao jin         s->msix = ON_OFF_AUTO_OFF;
235749ab747fSPaolo Bonzini     }
2358ee640c62SCao jin 
2359e23d0498SHannes Reinecke     if (pci_is_express(dev)) {
2360e23d0498SHannes Reinecke         pcie_endpoint_cap_init(dev, 0xa0);
2361e23d0498SHannes Reinecke     }
236249ab747fSPaolo Bonzini 
236349ab747fSPaolo Bonzini     bar_type = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64;
2364e23d0498SHannes Reinecke     pci_register_bar(dev, b->ioport_bar,
2365e23d0498SHannes Reinecke                      PCI_BASE_ADDRESS_SPACE_IO, &s->port_io);
2366e23d0498SHannes Reinecke     pci_register_bar(dev, b->mmio_bar, bar_type, &s->mmio_io);
236752190c1eSAndreas Färber     pci_register_bar(dev, 3, bar_type, &s->queue_io);
236849ab747fSPaolo Bonzini 
236949ab747fSPaolo Bonzini     if (megasas_use_msix(s)) {
237052190c1eSAndreas Färber         msix_vector_use(dev, 0);
237149ab747fSPaolo Bonzini     }
237249ab747fSPaolo Bonzini 
23738d72db68SHannes Reinecke     s->fw_state = MFI_FWSTATE_READY;
237449ab747fSPaolo Bonzini     if (!s->sas_addr) {
237549ab747fSPaolo Bonzini         s->sas_addr = ((NAA_LOCALLY_ASSIGNED_ID << 24) |
237649ab747fSPaolo Bonzini                        IEEE_COMPANY_LOCALLY_ASSIGNED) << 36;
2377cdc57472SDavid Gibson         s->sas_addr |= (pci_dev_bus_num(dev) << 16);
237849ab747fSPaolo Bonzini         s->sas_addr |= (PCI_SLOT(dev->devfn) << 8);
237949ab747fSPaolo Bonzini         s->sas_addr |= PCI_FUNC(dev->devfn);
238049ab747fSPaolo Bonzini     }
238149ab747fSPaolo Bonzini     if (!s->hba_serial) {
238249ab747fSPaolo Bonzini         s->hba_serial = g_strdup(MEGASAS_HBA_SERIAL);
238349ab747fSPaolo Bonzini     }
238449ab747fSPaolo Bonzini     if (s->fw_sge >= MEGASAS_MAX_SGE - MFI_PASS_FRAME_SIZE) {
238549ab747fSPaolo Bonzini         s->fw_sge = MEGASAS_MAX_SGE - MFI_PASS_FRAME_SIZE;
238649ab747fSPaolo Bonzini     } else if (s->fw_sge >= 128 - MFI_PASS_FRAME_SIZE) {
238749ab747fSPaolo Bonzini         s->fw_sge = 128 - MFI_PASS_FRAME_SIZE;
238849ab747fSPaolo Bonzini     } else {
238949ab747fSPaolo Bonzini         s->fw_sge = 64 - MFI_PASS_FRAME_SIZE;
239049ab747fSPaolo Bonzini     }
239149ab747fSPaolo Bonzini     if (s->fw_cmds > MEGASAS_MAX_FRAMES) {
239249ab747fSPaolo Bonzini         s->fw_cmds = MEGASAS_MAX_FRAMES;
239349ab747fSPaolo Bonzini     }
239449ab747fSPaolo Bonzini     trace_megasas_init(s->fw_sge, s->fw_cmds,
239549ab747fSPaolo Bonzini                        megasas_is_jbod(s) ? "jbod" : "raid");
23963f2cd4ddSHannes Reinecke 
23973f2cd4ddSHannes Reinecke     if (megasas_is_jbod(s)) {
23983f2cd4ddSHannes Reinecke         s->fw_luns = MFI_MAX_SYS_PDS;
23993f2cd4ddSHannes Reinecke     } else {
24003f2cd4ddSHannes Reinecke         s->fw_luns = MFI_MAX_LD;
24013f2cd4ddSHannes Reinecke     }
240249ab747fSPaolo Bonzini     s->producer_pa = 0;
240349ab747fSPaolo Bonzini     s->consumer_pa = 0;
240449ab747fSPaolo Bonzini     for (i = 0; i < s->fw_cmds; i++) {
240549ab747fSPaolo Bonzini         s->frames[i].index = i;
240649ab747fSPaolo Bonzini         s->frames[i].context = -1;
240749ab747fSPaolo Bonzini         s->frames[i].pa = 0;
240849ab747fSPaolo Bonzini         s->frames[i].state = s;
240949ab747fSPaolo Bonzini     }
241049ab747fSPaolo Bonzini 
2411b1187b51SAndreas Färber     scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
2412b1187b51SAndreas Färber                  &megasas_scsi_info, NULL);
241322d6aa03SAndreas Färber }
241449ab747fSPaolo Bonzini 
2415e23d0498SHannes Reinecke static Property megasas_properties_gen1[] = {
241649ab747fSPaolo Bonzini     DEFINE_PROP_UINT32("max_sge", MegasasState, fw_sge,
241749ab747fSPaolo Bonzini                        MEGASAS_DEFAULT_SGE),
241849ab747fSPaolo Bonzini     DEFINE_PROP_UINT32("max_cmds", MegasasState, fw_cmds,
241949ab747fSPaolo Bonzini                        MEGASAS_DEFAULT_FRAMES),
242049ab747fSPaolo Bonzini     DEFINE_PROP_STRING("hba_serial", MegasasState, hba_serial),
2421c7bcc85dSPaolo Bonzini     DEFINE_PROP_UINT64("sas_address", MegasasState, sas_addr, 0),
2422b4b4a57fSCao jin     DEFINE_PROP_ON_OFF_AUTO("msi", MegasasState, msi, ON_OFF_AUTO_AUTO),
2423b4b4a57fSCao jin     DEFINE_PROP_ON_OFF_AUTO("msix", MegasasState, msix, ON_OFF_AUTO_AUTO),
242449ab747fSPaolo Bonzini     DEFINE_PROP_BIT("use_jbod", MegasasState, flags,
242549ab747fSPaolo Bonzini                     MEGASAS_FLAG_USE_JBOD, false),
242649ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
242749ab747fSPaolo Bonzini };
242849ab747fSPaolo Bonzini 
2429e23d0498SHannes Reinecke static Property megasas_properties_gen2[] = {
2430e23d0498SHannes Reinecke     DEFINE_PROP_UINT32("max_sge", MegasasState, fw_sge,
2431e23d0498SHannes Reinecke                        MEGASAS_DEFAULT_SGE),
2432e23d0498SHannes Reinecke     DEFINE_PROP_UINT32("max_cmds", MegasasState, fw_cmds,
2433e23d0498SHannes Reinecke                        MEGASAS_GEN2_DEFAULT_FRAMES),
2434e23d0498SHannes Reinecke     DEFINE_PROP_STRING("hba_serial", MegasasState, hba_serial),
2435e23d0498SHannes Reinecke     DEFINE_PROP_UINT64("sas_address", MegasasState, sas_addr, 0),
2436b4b4a57fSCao jin     DEFINE_PROP_ON_OFF_AUTO("msi", MegasasState, msi, ON_OFF_AUTO_AUTO),
2437b4b4a57fSCao jin     DEFINE_PROP_ON_OFF_AUTO("msix", MegasasState, msix, ON_OFF_AUTO_AUTO),
2438e23d0498SHannes Reinecke     DEFINE_PROP_BIT("use_jbod", MegasasState, flags,
2439e23d0498SHannes Reinecke                     MEGASAS_FLAG_USE_JBOD, false),
2440e23d0498SHannes Reinecke     DEFINE_PROP_END_OF_LIST(),
2441e23d0498SHannes Reinecke };
2442e23d0498SHannes Reinecke 
2443e23d0498SHannes Reinecke typedef struct MegasasInfo {
2444e23d0498SHannes Reinecke     const char *name;
2445e23d0498SHannes Reinecke     const char *desc;
2446e23d0498SHannes Reinecke     const char *product_name;
2447e23d0498SHannes Reinecke     const char *product_version;
2448e23d0498SHannes Reinecke     uint16_t device_id;
2449e23d0498SHannes Reinecke     uint16_t subsystem_id;
2450e23d0498SHannes Reinecke     int ioport_bar;
2451e23d0498SHannes Reinecke     int mmio_bar;
2452e23d0498SHannes Reinecke     int osts;
2453e23d0498SHannes Reinecke     const VMStateDescription *vmsd;
2454e23d0498SHannes Reinecke     Property *props;
245571d78767SEduardo Habkost     InterfaceInfo *interfaces;
2456e23d0498SHannes Reinecke } MegasasInfo;
2457e23d0498SHannes Reinecke 
2458e23d0498SHannes Reinecke static struct MegasasInfo megasas_devices[] = {
2459e23d0498SHannes Reinecke     {
2460e23d0498SHannes Reinecke         .name = TYPE_MEGASAS_GEN1,
2461e23d0498SHannes Reinecke         .desc = "LSI MegaRAID SAS 1078",
2462e23d0498SHannes Reinecke         .product_name = "LSI MegaRAID SAS 8708EM2",
2463e23d0498SHannes Reinecke         .product_version = MEGASAS_VERSION_GEN1,
2464e23d0498SHannes Reinecke         .device_id = PCI_DEVICE_ID_LSI_SAS1078,
2465e23d0498SHannes Reinecke         .subsystem_id = 0x1013,
2466e23d0498SHannes Reinecke         .ioport_bar = 2,
2467e23d0498SHannes Reinecke         .mmio_bar = 0,
2468e23d0498SHannes Reinecke         .osts = MFI_1078_RM | 1,
2469e23d0498SHannes Reinecke         .vmsd = &vmstate_megasas_gen1,
2470e23d0498SHannes Reinecke         .props = megasas_properties_gen1,
2471fd3b02c8SEduardo Habkost         .interfaces = (InterfaceInfo[]) {
2472fd3b02c8SEduardo Habkost             { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2473fd3b02c8SEduardo Habkost             { },
2474fd3b02c8SEduardo Habkost         },
2475e23d0498SHannes Reinecke     },{
2476e23d0498SHannes Reinecke         .name = TYPE_MEGASAS_GEN2,
2477e23d0498SHannes Reinecke         .desc = "LSI MegaRAID SAS 2108",
2478e23d0498SHannes Reinecke         .product_name = "LSI MegaRAID SAS 9260-8i",
2479e23d0498SHannes Reinecke         .product_version = MEGASAS_VERSION_GEN2,
2480e23d0498SHannes Reinecke         .device_id = PCI_DEVICE_ID_LSI_SAS0079,
2481e23d0498SHannes Reinecke         .subsystem_id = 0x9261,
2482e23d0498SHannes Reinecke         .ioport_bar = 0,
2483e23d0498SHannes Reinecke         .mmio_bar = 1,
2484e23d0498SHannes Reinecke         .osts = MFI_GEN2_RM,
2485e23d0498SHannes Reinecke         .vmsd = &vmstate_megasas_gen2,
2486e23d0498SHannes Reinecke         .props = megasas_properties_gen2,
248771d78767SEduardo Habkost         .interfaces = (InterfaceInfo[]) {
248871d78767SEduardo Habkost             { INTERFACE_PCIE_DEVICE },
248971d78767SEduardo Habkost             { }
249071d78767SEduardo Habkost         },
2491e23d0498SHannes Reinecke     }
2492e23d0498SHannes Reinecke };
2493e23d0498SHannes Reinecke 
249449ab747fSPaolo Bonzini static void megasas_class_init(ObjectClass *oc, void *data)
249549ab747fSPaolo Bonzini {
249649ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(oc);
249749ab747fSPaolo Bonzini     PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
2498e23d0498SHannes Reinecke     MegasasBaseClass *e = MEGASAS_DEVICE_CLASS(oc);
2499e23d0498SHannes Reinecke     const MegasasInfo *info = data;
250049ab747fSPaolo Bonzini 
2501ae071cc8SMarkus Armbruster     pc->realize = megasas_scsi_realize;
250249ab747fSPaolo Bonzini     pc->exit = megasas_scsi_uninit;
250349ab747fSPaolo Bonzini     pc->vendor_id = PCI_VENDOR_ID_LSI_LOGIC;
2504e23d0498SHannes Reinecke     pc->device_id = info->device_id;
250549ab747fSPaolo Bonzini     pc->subsystem_vendor_id = PCI_VENDOR_ID_LSI_LOGIC;
2506e23d0498SHannes Reinecke     pc->subsystem_id = info->subsystem_id;
250749ab747fSPaolo Bonzini     pc->class_id = PCI_CLASS_STORAGE_RAID;
2508e23d0498SHannes Reinecke     e->mmio_bar = info->mmio_bar;
2509e23d0498SHannes Reinecke     e->ioport_bar = info->ioport_bar;
2510e23d0498SHannes Reinecke     e->osts = info->osts;
2511e23d0498SHannes Reinecke     e->product_name = info->product_name;
2512e23d0498SHannes Reinecke     e->product_version = info->product_version;
2513e23d0498SHannes Reinecke     dc->props = info->props;
251449ab747fSPaolo Bonzini     dc->reset = megasas_scsi_reset;
2515e23d0498SHannes Reinecke     dc->vmsd = info->vmsd;
2516125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2517e23d0498SHannes Reinecke     dc->desc = info->desc;
251849ab747fSPaolo Bonzini }
251949ab747fSPaolo Bonzini 
252049ab747fSPaolo Bonzini static const TypeInfo megasas_info = {
2521e23d0498SHannes Reinecke     .name  = TYPE_MEGASAS_BASE,
252249ab747fSPaolo Bonzini     .parent = TYPE_PCI_DEVICE,
252349ab747fSPaolo Bonzini     .instance_size = sizeof(MegasasState),
2524e23d0498SHannes Reinecke     .class_size = sizeof(MegasasBaseClass),
2525e23d0498SHannes Reinecke     .abstract = true,
252649ab747fSPaolo Bonzini };
252749ab747fSPaolo Bonzini 
252849ab747fSPaolo Bonzini static void megasas_register_types(void)
252949ab747fSPaolo Bonzini {
2530e23d0498SHannes Reinecke     int i;
2531e23d0498SHannes Reinecke 
253249ab747fSPaolo Bonzini     type_register_static(&megasas_info);
2533e23d0498SHannes Reinecke     for (i = 0; i < ARRAY_SIZE(megasas_devices); i++) {
2534e23d0498SHannes Reinecke         const MegasasInfo *info = &megasas_devices[i];
2535e23d0498SHannes Reinecke         TypeInfo type_info = {};
2536e23d0498SHannes Reinecke 
2537e23d0498SHannes Reinecke         type_info.name = info->name;
2538e23d0498SHannes Reinecke         type_info.parent = TYPE_MEGASAS_BASE;
2539e23d0498SHannes Reinecke         type_info.class_data = (void *)info;
2540e23d0498SHannes Reinecke         type_info.class_init = megasas_class_init;
254171d78767SEduardo Habkost         type_info.interfaces = info->interfaces;
2542e23d0498SHannes Reinecke 
2543e23d0498SHannes Reinecke         type_register(&type_info);
2544e23d0498SHannes Reinecke     }
254549ab747fSPaolo Bonzini }
254649ab747fSPaolo Bonzini 
254749ab747fSPaolo Bonzini type_init(megasas_register_types)
2548