2 * QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation
3 * Based on the linux driver code at drivers/scsi/megaraid
5 * Copyright (c) 2009-2012 Hannes Reinecke, SUSE Labs
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "hw/pci/pci.h"
24 #include "hw/qdev-properties.h"
25 #include "sysemu/dma.h"
26 #include "sysemu/block-backend.h"
27 #include "hw/pci/msi.h"
28 #include "hw/pci/msix.h"
30 #include "qemu/module.h"
31 #include "hw/scsi/scsi.h"
32 #include "scsi/constants.h"
34 #include "qapi/error.h"
36 #include "migration/vmstate.h"
38 #define MEGASAS_VERSION_GEN1 "1.70"
39 #define MEGASAS_VERSION_GEN2 "1.80"
40 #define MEGASAS_MAX_FRAMES 2048 /* Firmware limit at 65535 */
41 #define MEGASAS_DEFAULT_FRAMES 1000 /* Windows requires this */
42 #define MEGASAS_GEN2_DEFAULT_FRAMES 1008 /* Windows requires this */
43 #define MEGASAS_MAX_SGE 128 /* Firmware limit */
44 #define MEGASAS_DEFAULT_SGE 80
45 #define MEGASAS_MAX_SECTORS 0xFFFF /* No real limit */
46 #define MEGASAS_MAX_ARRAYS 128
48 #define MEGASAS_HBA_SERIAL "QEMU123456"
49 #define NAA_LOCALLY_ASSIGNED_ID 0x3ULL
50 #define IEEE_COMPANY_LOCALLY_ASSIGNED 0x525400
52 #define MEGASAS_FLAG_USE_JBOD 0
53 #define MEGASAS_MASK_USE_JBOD (1 << MEGASAS_FLAG_USE_JBOD)
54 #define MEGASAS_FLAG_USE_QUEUE64 1
55 #define MEGASAS_MASK_USE_QUEUE64 (1 << MEGASAS_FLAG_USE_QUEUE64)
57 static const char *mfi_frame_desc
[] = {
58 "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
59 "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
61 typedef struct MegasasCmd
{
70 union mfi_frame
*frame
;
76 struct MegasasState
*state
;
79 typedef struct MegasasState
{
86 MemoryRegion queue_io
;
102 MegasasCmd
*event_cmd
;
103 uint16_t event_locale
;
105 uint32_t event_count
;
106 uint32_t shutdown_event
;
112 uint64_t reply_queue_pa
;
114 uint16_t reply_queue_len
;
115 uint16_t reply_queue_head
;
116 uint16_t reply_queue_tail
;
117 uint64_t consumer_pa
;
118 uint64_t producer_pa
;
120 MegasasCmd frames
[MEGASAS_MAX_FRAMES
];
121 DECLARE_BITMAP(frame_map
, MEGASAS_MAX_FRAMES
);
125 typedef struct MegasasBaseClass
{
126 PCIDeviceClass parent_class
;
127 const char *product_name
;
128 const char *product_version
;
134 #define TYPE_MEGASAS_BASE "megasas-base"
135 #define TYPE_MEGASAS_GEN1 "megasas"
136 #define TYPE_MEGASAS_GEN2 "megasas-gen2"
138 #define MEGASAS(obj) \
139 OBJECT_CHECK(MegasasState, (obj), TYPE_MEGASAS_BASE)
141 #define MEGASAS_DEVICE_CLASS(oc) \
142 OBJECT_CLASS_CHECK(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
143 #define MEGASAS_DEVICE_GET_CLASS(oc) \
144 OBJECT_GET_CLASS(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
146 #define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
148 static bool megasas_intr_enabled(MegasasState
*s
)
150 if ((s
->intr_mask
& MEGASAS_INTR_DISABLED_MASK
) !=
151 MEGASAS_INTR_DISABLED_MASK
) {
157 static bool megasas_use_queue64(MegasasState
*s
)
159 return s
->flags
& MEGASAS_MASK_USE_QUEUE64
;
162 static bool megasas_use_msix(MegasasState
*s
)
164 return s
->msix
!= ON_OFF_AUTO_OFF
;
167 static bool megasas_is_jbod(MegasasState
*s
)
169 return s
->flags
& MEGASAS_MASK_USE_JBOD
;
172 static void megasas_frame_set_cmd_status(MegasasState
*s
,
173 unsigned long frame
, uint8_t v
)
175 PCIDevice
*pci
= &s
->parent_obj
;
176 stb_pci_dma(pci
, frame
+ offsetof(struct mfi_frame_header
, cmd_status
), v
);
179 static void megasas_frame_set_scsi_status(MegasasState
*s
,
180 unsigned long frame
, uint8_t v
)
182 PCIDevice
*pci
= &s
->parent_obj
;
183 stb_pci_dma(pci
, frame
+ offsetof(struct mfi_frame_header
, scsi_status
), v
);
187 * Context is considered opaque, but the HBA firmware is running
188 * in little endian mode. So convert it to little endian, too.
190 static uint64_t megasas_frame_get_context(MegasasState
*s
,
193 PCIDevice
*pci
= &s
->parent_obj
;
194 return ldq_le_pci_dma(pci
, frame
+ offsetof(struct mfi_frame_header
, context
));
197 static bool megasas_frame_is_ieee_sgl(MegasasCmd
*cmd
)
199 return cmd
->flags
& MFI_FRAME_IEEE_SGL
;
202 static bool megasas_frame_is_sgl64(MegasasCmd
*cmd
)
204 return cmd
->flags
& MFI_FRAME_SGL64
;
207 static bool megasas_frame_is_sense64(MegasasCmd
*cmd
)
209 return cmd
->flags
& MFI_FRAME_SENSE64
;
212 static uint64_t megasas_sgl_get_addr(MegasasCmd
*cmd
,
217 if (megasas_frame_is_ieee_sgl(cmd
)) {
218 addr
= le64_to_cpu(sgl
->sg_skinny
->addr
);
219 } else if (megasas_frame_is_sgl64(cmd
)) {
220 addr
= le64_to_cpu(sgl
->sg64
->addr
);
222 addr
= le32_to_cpu(sgl
->sg32
->addr
);
227 static uint32_t megasas_sgl_get_len(MegasasCmd
*cmd
,
232 if (megasas_frame_is_ieee_sgl(cmd
)) {
233 len
= le32_to_cpu(sgl
->sg_skinny
->len
);
234 } else if (megasas_frame_is_sgl64(cmd
)) {
235 len
= le32_to_cpu(sgl
->sg64
->len
);
237 len
= le32_to_cpu(sgl
->sg32
->len
);
242 static union mfi_sgl
*megasas_sgl_next(MegasasCmd
*cmd
,
245 uint8_t *next
= (uint8_t *)sgl
;
247 if (megasas_frame_is_ieee_sgl(cmd
)) {
248 next
+= sizeof(struct mfi_sg_skinny
);
249 } else if (megasas_frame_is_sgl64(cmd
)) {
250 next
+= sizeof(struct mfi_sg64
);
252 next
+= sizeof(struct mfi_sg32
);
255 if (next
>= (uint8_t *)cmd
->frame
+ cmd
->pa_size
) {
258 return (union mfi_sgl
*)next
;
261 static void megasas_soft_reset(MegasasState
*s
);
263 static int megasas_map_sgl(MegasasState
*s
, MegasasCmd
*cmd
, union mfi_sgl
*sgl
)
269 cmd
->flags
= le16_to_cpu(cmd
->frame
->header
.flags
);
270 iov_count
= cmd
->frame
->header
.sge_count
;
271 if (iov_count
> MEGASAS_MAX_SGE
) {
272 trace_megasas_iovec_sgl_overflow(cmd
->index
, iov_count
,
276 pci_dma_sglist_init(&cmd
->qsg
, PCI_DEVICE(s
), iov_count
);
277 for (i
= 0; i
< iov_count
; i
++) {
278 dma_addr_t iov_pa
, iov_size_p
;
281 trace_megasas_iovec_sgl_underflow(cmd
->index
, i
);
284 iov_pa
= megasas_sgl_get_addr(cmd
, sgl
);
285 iov_size_p
= megasas_sgl_get_len(cmd
, sgl
);
286 if (!iov_pa
|| !iov_size_p
) {
287 trace_megasas_iovec_sgl_invalid(cmd
->index
, i
,
291 qemu_sglist_add(&cmd
->qsg
, iov_pa
, iov_size_p
);
292 sgl
= megasas_sgl_next(cmd
, sgl
);
293 iov_size
+= (size_t)iov_size_p
;
295 if (cmd
->iov_size
> iov_size
) {
296 trace_megasas_iovec_overflow(cmd
->index
, iov_size
, cmd
->iov_size
);
297 } else if (cmd
->iov_size
< iov_size
) {
298 trace_megasas_iovec_underflow(cmd
->index
, iov_size
, cmd
->iov_size
);
303 qemu_sglist_destroy(&cmd
->qsg
);
304 return iov_count
- i
;
308 * passthrough sense and io sense are at the same offset
310 static int megasas_build_sense(MegasasCmd
*cmd
, uint8_t *sense_ptr
,
313 PCIDevice
*pcid
= PCI_DEVICE(cmd
->state
);
314 uint32_t pa_hi
= 0, pa_lo
;
318 frame_sense_len
= cmd
->frame
->header
.sense_len
;
319 if (sense_len
> frame_sense_len
) {
320 sense_len
= frame_sense_len
;
323 pa_lo
= le32_to_cpu(cmd
->frame
->pass
.sense_addr_lo
);
324 if (megasas_frame_is_sense64(cmd
)) {
325 pa_hi
= le32_to_cpu(cmd
->frame
->pass
.sense_addr_hi
);
327 pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
328 pci_dma_write(pcid
, pa
, sense_ptr
, sense_len
);
329 cmd
->frame
->header
.sense_len
= sense_len
;
334 static void megasas_write_sense(MegasasCmd
*cmd
, SCSISense sense
)
336 uint8_t sense_buf
[SCSI_SENSE_BUF_SIZE
];
337 uint8_t sense_len
= 18;
339 memset(sense_buf
, 0, sense_len
);
341 sense_buf
[2] = sense
.key
;
343 sense_buf
[12] = sense
.asc
;
344 sense_buf
[13] = sense
.ascq
;
345 megasas_build_sense(cmd
, sense_buf
, sense_len
);
348 static void megasas_copy_sense(MegasasCmd
*cmd
)
350 uint8_t sense_buf
[SCSI_SENSE_BUF_SIZE
];
353 sense_len
= scsi_req_get_sense(cmd
->req
, sense_buf
,
354 SCSI_SENSE_BUF_SIZE
);
355 megasas_build_sense(cmd
, sense_buf
, sense_len
);
359 * Format an INQUIRY CDB
361 static int megasas_setup_inquiry(uint8_t *cdb
, int pg
, int len
)
369 cdb
[3] = (len
>> 8) & 0xff;
370 cdb
[4] = (len
& 0xff);
375 * Encode lba and len into a READ_16/WRITE_16 CDB
377 static void megasas_encode_lba(uint8_t *cdb
, uint64_t lba
,
378 uint32_t len
, bool is_write
)
380 memset(cdb
, 0x0, 16);
386 cdb
[2] = (lba
>> 56) & 0xff;
387 cdb
[3] = (lba
>> 48) & 0xff;
388 cdb
[4] = (lba
>> 40) & 0xff;
389 cdb
[5] = (lba
>> 32) & 0xff;
390 cdb
[6] = (lba
>> 24) & 0xff;
391 cdb
[7] = (lba
>> 16) & 0xff;
392 cdb
[8] = (lba
>> 8) & 0xff;
393 cdb
[9] = (lba
) & 0xff;
394 cdb
[10] = (len
>> 24) & 0xff;
395 cdb
[11] = (len
>> 16) & 0xff;
396 cdb
[12] = (len
>> 8) & 0xff;
397 cdb
[13] = (len
) & 0xff;
403 static uint64_t megasas_fw_time(void)
407 qemu_get_timedate(&curtime
, 0);
408 return ((uint64_t)curtime
.tm_sec
& 0xff) << 48 |
409 ((uint64_t)curtime
.tm_min
& 0xff) << 40 |
410 ((uint64_t)curtime
.tm_hour
& 0xff) << 32 |
411 ((uint64_t)curtime
.tm_mday
& 0xff) << 24 |
412 ((uint64_t)curtime
.tm_mon
& 0xff) << 16 |
413 ((uint64_t)(curtime
.tm_year
+ 1900) & 0xffff);
417 * Default disk sata address
418 * 0x1221 is the magic number as
419 * present in real hardware,
420 * so use it here, too.
422 static uint64_t megasas_get_sata_addr(uint16_t id
)
424 uint64_t addr
= (0x1221ULL
<< 48);
425 return addr
| ((uint64_t)id
<< 24);
431 static int megasas_next_index(MegasasState
*s
, int index
, int limit
)
434 if (index
== limit
) {
440 static MegasasCmd
*megasas_lookup_frame(MegasasState
*s
,
443 MegasasCmd
*cmd
= NULL
;
446 index
= s
->reply_queue_head
;
448 while (num
< s
->fw_cmds
&& index
< MEGASAS_MAX_FRAMES
) {
449 if (s
->frames
[index
].pa
&& s
->frames
[index
].pa
== frame
) {
450 cmd
= &s
->frames
[index
];
453 index
= megasas_next_index(s
, index
, s
->fw_cmds
);
460 static void megasas_unmap_frame(MegasasState
*s
, MegasasCmd
*cmd
)
462 PCIDevice
*p
= PCI_DEVICE(s
);
465 pci_dma_unmap(p
, cmd
->frame
, cmd
->pa_size
, 0, 0);
470 qemu_sglist_destroy(&cmd
->qsg
);
471 clear_bit(cmd
->index
, s
->frame_map
);
475 * This absolutely needs to be locked if
476 * qemu ever goes multithreaded.
478 static MegasasCmd
*megasas_enqueue_frame(MegasasState
*s
,
479 hwaddr frame
, uint64_t context
, int count
)
481 PCIDevice
*pcid
= PCI_DEVICE(s
);
482 MegasasCmd
*cmd
= NULL
;
483 int frame_size
= MEGASAS_MAX_SGE
* sizeof(union mfi_sgl
);
484 hwaddr frame_size_p
= frame_size
;
488 while (index
< s
->fw_cmds
) {
489 index
= find_next_zero_bit(s
->frame_map
, s
->fw_cmds
, index
);
490 if (!s
->frames
[index
].pa
)
492 /* Busy frame found */
493 trace_megasas_qf_mapped(index
);
495 if (index
>= s
->fw_cmds
) {
496 /* All frames busy */
497 trace_megasas_qf_busy(frame
);
500 cmd
= &s
->frames
[index
];
501 set_bit(index
, s
->frame_map
);
502 trace_megasas_qf_new(index
, frame
);
505 /* Map all possible frames */
506 cmd
->frame
= pci_dma_map(pcid
, frame
, &frame_size_p
, 0);
507 if (!cmd
->frame
|| frame_size_p
!= frame_size
) {
508 trace_megasas_qf_map_failed(cmd
->index
, (unsigned long)frame
);
510 megasas_unmap_frame(s
, cmd
);
515 cmd
->pa_size
= frame_size_p
;
516 cmd
->context
= context
;
517 if (!megasas_use_queue64(s
)) {
518 cmd
->context
&= (uint64_t)0xFFFFFFFF;
521 cmd
->dcmd_opcode
= -1;
524 if (s
->consumer_pa
) {
525 s
->reply_queue_tail
= ldl_le_pci_dma(pcid
, s
->consumer_pa
);
527 trace_megasas_qf_enqueue(cmd
->index
, cmd
->count
, cmd
->context
,
528 s
->reply_queue_head
, s
->reply_queue_tail
, s
->busy
);
533 static void megasas_complete_frame(MegasasState
*s
, uint64_t context
)
535 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
536 int tail
, queue_offset
;
538 /* Decrement busy count */
540 if (s
->reply_queue_pa
) {
542 * Put command on the reply queue.
543 * Context is opaque, but emulation is running in
544 * little endian. So convert it.
546 if (megasas_use_queue64(s
)) {
547 queue_offset
= s
->reply_queue_head
* sizeof(uint64_t);
548 stq_le_pci_dma(pci_dev
, s
->reply_queue_pa
+ queue_offset
, context
);
550 queue_offset
= s
->reply_queue_head
* sizeof(uint32_t);
551 stl_le_pci_dma(pci_dev
, s
->reply_queue_pa
+ queue_offset
, context
);
553 s
->reply_queue_tail
= ldl_le_pci_dma(pci_dev
, s
->consumer_pa
);
554 trace_megasas_qf_complete(context
, s
->reply_queue_head
,
555 s
->reply_queue_tail
, s
->busy
);
558 if (megasas_intr_enabled(s
)) {
559 /* Update reply queue pointer */
560 s
->reply_queue_tail
= ldl_le_pci_dma(pci_dev
, s
->consumer_pa
);
561 tail
= s
->reply_queue_head
;
562 s
->reply_queue_head
= megasas_next_index(s
, tail
, s
->fw_cmds
);
563 trace_megasas_qf_update(s
->reply_queue_head
, s
->reply_queue_tail
,
565 stl_le_pci_dma(pci_dev
, s
->producer_pa
, s
->reply_queue_head
);
567 if (msix_enabled(pci_dev
)) {
568 trace_megasas_msix_raise(0);
569 msix_notify(pci_dev
, 0);
570 } else if (msi_enabled(pci_dev
)) {
571 trace_megasas_msi_raise(0);
572 msi_notify(pci_dev
, 0);
575 if (s
->doorbell
== 1) {
576 trace_megasas_irq_raise();
577 pci_irq_assert(pci_dev
);
581 trace_megasas_qf_complete_noirq(context
);
585 static void megasas_complete_command(MegasasCmd
*cmd
)
590 cmd
->req
->hba_private
= NULL
;
591 scsi_req_unref(cmd
->req
);
594 megasas_unmap_frame(cmd
->state
, cmd
);
595 megasas_complete_frame(cmd
->state
, cmd
->context
);
598 static void megasas_reset_frames(MegasasState
*s
)
603 for (i
= 0; i
< s
->fw_cmds
; i
++) {
606 megasas_unmap_frame(s
, cmd
);
609 bitmap_zero(s
->frame_map
, MEGASAS_MAX_FRAMES
);
612 static void megasas_abort_command(MegasasCmd
*cmd
)
614 /* Never abort internal commands. */
615 if (cmd
->dcmd_opcode
!= -1) {
618 if (cmd
->req
!= NULL
) {
619 scsi_req_cancel(cmd
->req
);
623 static int megasas_init_firmware(MegasasState
*s
, MegasasCmd
*cmd
)
625 PCIDevice
*pcid
= PCI_DEVICE(s
);
626 uint32_t pa_hi
, pa_lo
;
627 hwaddr iq_pa
, initq_size
= sizeof(struct mfi_init_qinfo
);
628 struct mfi_init_qinfo
*initq
= NULL
;
630 int ret
= MFI_STAT_OK
;
632 if (s
->reply_queue_pa
) {
633 trace_megasas_initq_mapped(s
->reply_queue_pa
);
636 pa_lo
= le32_to_cpu(cmd
->frame
->init
.qinfo_new_addr_lo
);
637 pa_hi
= le32_to_cpu(cmd
->frame
->init
.qinfo_new_addr_hi
);
638 iq_pa
= (((uint64_t) pa_hi
<< 32) | pa_lo
);
639 trace_megasas_init_firmware((uint64_t)iq_pa
);
640 initq
= pci_dma_map(pcid
, iq_pa
, &initq_size
, 0);
641 if (!initq
|| initq_size
!= sizeof(*initq
)) {
642 trace_megasas_initq_map_failed(cmd
->index
);
644 ret
= MFI_STAT_MEMORY_NOT_AVAILABLE
;
647 s
->reply_queue_len
= le32_to_cpu(initq
->rq_entries
) & 0xFFFF;
648 if (s
->reply_queue_len
> s
->fw_cmds
) {
649 trace_megasas_initq_mismatch(s
->reply_queue_len
, s
->fw_cmds
);
651 ret
= MFI_STAT_INVALID_PARAMETER
;
654 pa_lo
= le32_to_cpu(initq
->rq_addr_lo
);
655 pa_hi
= le32_to_cpu(initq
->rq_addr_hi
);
656 s
->reply_queue_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
657 pa_lo
= le32_to_cpu(initq
->ci_addr_lo
);
658 pa_hi
= le32_to_cpu(initq
->ci_addr_hi
);
659 s
->consumer_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
660 pa_lo
= le32_to_cpu(initq
->pi_addr_lo
);
661 pa_hi
= le32_to_cpu(initq
->pi_addr_hi
);
662 s
->producer_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
663 s
->reply_queue_head
= ldl_le_pci_dma(pcid
, s
->producer_pa
);
664 s
->reply_queue_head
%= MEGASAS_MAX_FRAMES
;
665 s
->reply_queue_tail
= ldl_le_pci_dma(pcid
, s
->consumer_pa
);
666 s
->reply_queue_tail
%= MEGASAS_MAX_FRAMES
;
667 flags
= le32_to_cpu(initq
->flags
);
668 if (flags
& MFI_QUEUE_FLAG_CONTEXT64
) {
669 s
->flags
|= MEGASAS_MASK_USE_QUEUE64
;
671 trace_megasas_init_queue((unsigned long)s
->reply_queue_pa
,
672 s
->reply_queue_len
, s
->reply_queue_head
,
673 s
->reply_queue_tail
, flags
);
674 megasas_reset_frames(s
);
675 s
->fw_state
= MFI_FWSTATE_OPERATIONAL
;
678 pci_dma_unmap(pcid
, initq
, initq_size
, 0, 0);
683 static int megasas_map_dcmd(MegasasState
*s
, MegasasCmd
*cmd
)
685 dma_addr_t iov_pa
, iov_size
;
688 cmd
->flags
= le16_to_cpu(cmd
->frame
->header
.flags
);
689 iov_count
= cmd
->frame
->header
.sge_count
;
691 trace_megasas_dcmd_zero_sge(cmd
->index
);
694 } else if (iov_count
> 1) {
695 trace_megasas_dcmd_invalid_sge(cmd
->index
, iov_count
);
699 iov_pa
= megasas_sgl_get_addr(cmd
, &cmd
->frame
->dcmd
.sgl
);
700 iov_size
= megasas_sgl_get_len(cmd
, &cmd
->frame
->dcmd
.sgl
);
701 pci_dma_sglist_init(&cmd
->qsg
, PCI_DEVICE(s
), 1);
702 qemu_sglist_add(&cmd
->qsg
, iov_pa
, iov_size
);
703 cmd
->iov_size
= iov_size
;
707 static void megasas_finish_dcmd(MegasasCmd
*cmd
, uint32_t iov_size
)
709 trace_megasas_finish_dcmd(cmd
->index
, iov_size
);
711 if (iov_size
> cmd
->iov_size
) {
712 if (megasas_frame_is_ieee_sgl(cmd
)) {
713 cmd
->frame
->dcmd
.sgl
.sg_skinny
->len
= cpu_to_le32(iov_size
);
714 } else if (megasas_frame_is_sgl64(cmd
)) {
715 cmd
->frame
->dcmd
.sgl
.sg64
->len
= cpu_to_le32(iov_size
);
717 cmd
->frame
->dcmd
.sgl
.sg32
->len
= cpu_to_le32(iov_size
);
722 static int megasas_ctrl_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
724 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
725 PCIDeviceClass
*pci_class
= PCI_DEVICE_GET_CLASS(pci_dev
);
726 MegasasBaseClass
*base_class
= MEGASAS_DEVICE_GET_CLASS(s
);
727 struct mfi_ctrl_info info
;
728 size_t dcmd_size
= sizeof(info
);
730 int num_pd_disks
= 0;
732 memset(&info
, 0x0, dcmd_size
);
733 if (cmd
->iov_size
< dcmd_size
) {
734 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
736 return MFI_STAT_INVALID_PARAMETER
;
739 info
.pci
.vendor
= cpu_to_le16(pci_class
->vendor_id
);
740 info
.pci
.device
= cpu_to_le16(pci_class
->device_id
);
741 info
.pci
.subvendor
= cpu_to_le16(pci_class
->subsystem_vendor_id
);
742 info
.pci
.subdevice
= cpu_to_le16(pci_class
->subsystem_id
);
745 * For some reason the firmware supports
746 * only up to 8 device ports.
747 * Despite supporting a far larger number
748 * of devices for the physical devices.
749 * So just display the first 8 devices
750 * in the device port list, independent
751 * of how many logical devices are actually
754 info
.host
.type
= MFI_INFO_HOST_PCIE
;
755 info
.device
.type
= MFI_INFO_DEV_SAS3G
;
756 info
.device
.port_count
= 8;
757 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
758 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
761 if (num_pd_disks
< 8) {
762 pd_id
= ((sdev
->id
& 0xFF) << 8) | (sdev
->lun
& 0xFF);
763 info
.device
.port_addr
[num_pd_disks
] =
764 cpu_to_le64(megasas_get_sata_addr(pd_id
));
769 memcpy(info
.product_name
, base_class
->product_name
, 24);
770 snprintf(info
.serial_number
, 32, "%s", s
->hba_serial
);
771 snprintf(info
.package_version
, 0x60, "%s-QEMU", qemu_hw_version());
772 memcpy(info
.image_component
[0].name
, "APP", 3);
773 snprintf(info
.image_component
[0].version
, 10, "%s-QEMU",
774 base_class
->product_version
);
775 memcpy(info
.image_component
[0].build_date
, "Apr 1 2014", 11);
776 memcpy(info
.image_component
[0].build_time
, "12:34:56", 8);
777 info
.image_component_count
= 1;
778 if (pci_dev
->has_rom
) {
782 ptr
= memory_region_get_ram_ptr(&pci_dev
->rom
);
783 memcpy(biosver
, ptr
+ 0x41, 31);
785 memcpy(info
.image_component
[1].name
, "BIOS", 4);
786 memcpy(info
.image_component
[1].version
, biosver
,
787 strlen((const char *)biosver
));
788 info
.image_component_count
++;
790 info
.current_fw_time
= cpu_to_le32(megasas_fw_time());
793 info
.max_arrays
= MEGASAS_MAX_ARRAYS
;
794 info
.max_lds
= MFI_MAX_LD
;
795 info
.max_cmds
= cpu_to_le16(s
->fw_cmds
);
796 info
.max_sg_elements
= cpu_to_le16(s
->fw_sge
);
797 info
.max_request_size
= cpu_to_le32(MEGASAS_MAX_SECTORS
);
798 if (!megasas_is_jbod(s
))
799 info
.lds_present
= cpu_to_le16(num_pd_disks
);
800 info
.pd_present
= cpu_to_le16(num_pd_disks
);
801 info
.pd_disks_present
= cpu_to_le16(num_pd_disks
);
802 info
.hw_present
= cpu_to_le32(MFI_INFO_HW_NVRAM
|
805 info
.memory_size
= cpu_to_le16(512);
806 info
.nvram_size
= cpu_to_le16(32);
807 info
.flash_size
= cpu_to_le16(16);
808 info
.raid_levels
= cpu_to_le32(MFI_INFO_RAID_0
);
809 info
.adapter_ops
= cpu_to_le32(MFI_INFO_AOPS_RBLD_RATE
|
810 MFI_INFO_AOPS_SELF_DIAGNOSTIC
|
811 MFI_INFO_AOPS_MIXED_ARRAY
);
812 info
.ld_ops
= cpu_to_le32(MFI_INFO_LDOPS_DISK_CACHE_POLICY
|
813 MFI_INFO_LDOPS_ACCESS_POLICY
|
814 MFI_INFO_LDOPS_IO_POLICY
|
815 MFI_INFO_LDOPS_WRITE_POLICY
|
816 MFI_INFO_LDOPS_READ_POLICY
);
817 info
.max_strips_per_io
= cpu_to_le16(s
->fw_sge
);
818 info
.stripe_sz_ops
.min
= 3;
819 info
.stripe_sz_ops
.max
= ctz32(MEGASAS_MAX_SECTORS
+ 1);
820 info
.properties
.pred_fail_poll_interval
= cpu_to_le16(300);
821 info
.properties
.intr_throttle_cnt
= cpu_to_le16(16);
822 info
.properties
.intr_throttle_timeout
= cpu_to_le16(50);
823 info
.properties
.rebuild_rate
= 30;
824 info
.properties
.patrol_read_rate
= 30;
825 info
.properties
.bgi_rate
= 30;
826 info
.properties
.cc_rate
= 30;
827 info
.properties
.recon_rate
= 30;
828 info
.properties
.cache_flush_interval
= 4;
829 info
.properties
.spinup_drv_cnt
= 2;
830 info
.properties
.spinup_delay
= 6;
831 info
.properties
.ecc_bucket_size
= 15;
832 info
.properties
.ecc_bucket_leak_rate
= cpu_to_le16(1440);
833 info
.properties
.expose_encl_devices
= 1;
834 info
.properties
.OnOffProperties
= cpu_to_le32(MFI_CTRL_PROP_EnableJBOD
);
835 info
.pd_ops
= cpu_to_le32(MFI_INFO_PDOPS_FORCE_ONLINE
|
836 MFI_INFO_PDOPS_FORCE_OFFLINE
);
837 info
.pd_mix_support
= cpu_to_le32(MFI_INFO_PDMIX_SAS
|
838 MFI_INFO_PDMIX_SATA
|
841 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
845 static int megasas_mfc_get_defaults(MegasasState
*s
, MegasasCmd
*cmd
)
847 struct mfi_defaults info
;
848 size_t dcmd_size
= sizeof(struct mfi_defaults
);
850 memset(&info
, 0x0, dcmd_size
);
851 if (cmd
->iov_size
< dcmd_size
) {
852 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
854 return MFI_STAT_INVALID_PARAMETER
;
857 info
.sas_addr
= cpu_to_le64(s
->sas_addr
);
858 info
.stripe_size
= 3;
860 info
.background_rate
= 30;
861 info
.allow_mix_in_enclosure
= 1;
862 info
.allow_mix_in_ld
= 1;
863 info
.direct_pd_mapping
= 1;
864 /* Enable for BIOS support */
865 info
.bios_enumerate_lds
= 1;
866 info
.disable_ctrl_r
= 1;
867 info
.expose_enclosure_devices
= 1;
868 info
.disable_preboot_cli
= 1;
869 info
.cluster_disable
= 1;
871 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
875 static int megasas_dcmd_get_bios_info(MegasasState
*s
, MegasasCmd
*cmd
)
877 struct mfi_bios_data info
;
878 size_t dcmd_size
= sizeof(info
);
880 memset(&info
, 0x0, dcmd_size
);
881 if (cmd
->iov_size
< dcmd_size
) {
882 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
884 return MFI_STAT_INVALID_PARAMETER
;
886 info
.continue_on_error
= 1;
888 if (megasas_is_jbod(s
)) {
889 info
.expose_all_drives
= 1;
892 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
896 static int megasas_dcmd_get_fw_time(MegasasState
*s
, MegasasCmd
*cmd
)
899 size_t dcmd_size
= sizeof(fw_time
);
901 fw_time
= cpu_to_le64(megasas_fw_time());
903 cmd
->iov_size
-= dma_buf_read((uint8_t *)&fw_time
, dcmd_size
, &cmd
->qsg
);
907 static int megasas_dcmd_set_fw_time(MegasasState
*s
, MegasasCmd
*cmd
)
911 /* This is a dummy; setting of firmware time is not allowed */
912 memcpy(&fw_time
, cmd
->frame
->dcmd
.mbox
, sizeof(fw_time
));
914 trace_megasas_dcmd_set_fw_time(cmd
->index
, fw_time
);
915 fw_time
= cpu_to_le64(megasas_fw_time());
919 static int megasas_event_info(MegasasState
*s
, MegasasCmd
*cmd
)
921 struct mfi_evt_log_state info
;
922 size_t dcmd_size
= sizeof(info
);
924 memset(&info
, 0, dcmd_size
);
926 info
.newest_seq_num
= cpu_to_le32(s
->event_count
);
927 info
.shutdown_seq_num
= cpu_to_le32(s
->shutdown_event
);
928 info
.boot_seq_num
= cpu_to_le32(s
->boot_event
);
930 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
934 static int megasas_event_wait(MegasasState
*s
, MegasasCmd
*cmd
)
938 if (cmd
->iov_size
< sizeof(struct mfi_evt_detail
)) {
939 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
940 sizeof(struct mfi_evt_detail
));
941 return MFI_STAT_INVALID_PARAMETER
;
943 s
->event_count
= cpu_to_le32(cmd
->frame
->dcmd
.mbox
[0]);
944 event
.word
= cpu_to_le32(cmd
->frame
->dcmd
.mbox
[4]);
945 s
->event_locale
= event
.members
.locale
;
946 s
->event_class
= event
.members
.class;
948 /* Decrease busy count; event frame doesn't count here */
950 cmd
->iov_size
= sizeof(struct mfi_evt_detail
);
951 return MFI_STAT_INVALID_STATUS
;
954 static int megasas_dcmd_pd_get_list(MegasasState
*s
, MegasasCmd
*cmd
)
956 struct mfi_pd_list info
;
957 size_t dcmd_size
= sizeof(info
);
959 uint32_t offset
, dcmd_limit
, num_pd_disks
= 0, max_pd_disks
;
961 memset(&info
, 0, dcmd_size
);
963 dcmd_limit
= offset
+ sizeof(struct mfi_pd_address
);
964 if (cmd
->iov_size
< dcmd_limit
) {
965 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
967 return MFI_STAT_INVALID_PARAMETER
;
970 max_pd_disks
= (cmd
->iov_size
- offset
) / sizeof(struct mfi_pd_address
);
971 if (max_pd_disks
> MFI_MAX_SYS_PDS
) {
972 max_pd_disks
= MFI_MAX_SYS_PDS
;
974 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
975 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
978 if (num_pd_disks
>= max_pd_disks
)
981 pd_id
= ((sdev
->id
& 0xFF) << 8) | (sdev
->lun
& 0xFF);
982 info
.addr
[num_pd_disks
].device_id
= cpu_to_le16(pd_id
);
983 info
.addr
[num_pd_disks
].encl_device_id
= 0xFFFF;
984 info
.addr
[num_pd_disks
].encl_index
= 0;
985 info
.addr
[num_pd_disks
].slot_number
= sdev
->id
& 0xFF;
986 info
.addr
[num_pd_disks
].scsi_dev_type
= sdev
->type
;
987 info
.addr
[num_pd_disks
].connect_port_bitmap
= 0x1;
988 info
.addr
[num_pd_disks
].sas_addr
[0] =
989 cpu_to_le64(megasas_get_sata_addr(pd_id
));
991 offset
+= sizeof(struct mfi_pd_address
);
993 trace_megasas_dcmd_pd_get_list(cmd
->index
, num_pd_disks
,
994 max_pd_disks
, offset
);
996 info
.size
= cpu_to_le32(offset
);
997 info
.count
= cpu_to_le32(num_pd_disks
);
999 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, offset
, &cmd
->qsg
);
1003 static int megasas_dcmd_pd_list_query(MegasasState
*s
, MegasasCmd
*cmd
)
1007 /* mbox0 contains flags */
1008 flags
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1009 trace_megasas_dcmd_pd_list_query(cmd
->index
, flags
);
1010 if (flags
== MR_PD_QUERY_TYPE_ALL
||
1011 megasas_is_jbod(s
)) {
1012 return megasas_dcmd_pd_get_list(s
, cmd
);
1018 static int megasas_pd_get_info_submit(SCSIDevice
*sdev
, int lun
,
1021 struct mfi_pd_info
*info
= cmd
->iov_buf
;
1022 size_t dcmd_size
= sizeof(struct mfi_pd_info
);
1024 uint16_t pd_id
= ((sdev
->id
& 0xFF) << 8) | (lun
& 0xFF);
1028 if (!cmd
->iov_buf
) {
1029 cmd
->iov_buf
= g_malloc0(dcmd_size
);
1030 info
= cmd
->iov_buf
;
1031 info
->inquiry_data
[0] = 0x7f; /* Force PQual 0x3, PType 0x1f */
1032 info
->vpd_page83
[0] = 0x7f;
1033 megasas_setup_inquiry(cmdbuf
, 0, sizeof(info
->inquiry_data
));
1034 cmd
->req
= scsi_req_new(sdev
, cmd
->index
, lun
, cmdbuf
, cmd
);
1036 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
1037 "PD get info std inquiry");
1038 g_free(cmd
->iov_buf
);
1039 cmd
->iov_buf
= NULL
;
1040 return MFI_STAT_FLASH_ALLOC_FAIL
;
1042 trace_megasas_dcmd_internal_submit(cmd
->index
,
1043 "PD get info std inquiry", lun
);
1044 len
= scsi_req_enqueue(cmd
->req
);
1046 cmd
->iov_size
= len
;
1047 scsi_req_continue(cmd
->req
);
1049 return MFI_STAT_INVALID_STATUS
;
1050 } else if (info
->inquiry_data
[0] != 0x7f && info
->vpd_page83
[0] == 0x7f) {
1051 megasas_setup_inquiry(cmdbuf
, 0x83, sizeof(info
->vpd_page83
));
1052 cmd
->req
= scsi_req_new(sdev
, cmd
->index
, lun
, cmdbuf
, cmd
);
1054 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
1055 "PD get info vpd inquiry");
1056 return MFI_STAT_FLASH_ALLOC_FAIL
;
1058 trace_megasas_dcmd_internal_submit(cmd
->index
,
1059 "PD get info vpd inquiry", lun
);
1060 len
= scsi_req_enqueue(cmd
->req
);
1062 cmd
->iov_size
= len
;
1063 scsi_req_continue(cmd
->req
);
1065 return MFI_STAT_INVALID_STATUS
;
1067 /* Finished, set FW state */
1068 if ((info
->inquiry_data
[0] >> 5) == 0) {
1069 if (megasas_is_jbod(cmd
->state
)) {
1070 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_SYSTEM
);
1072 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_ONLINE
);
1075 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_OFFLINE
);
1078 info
->ref
.v
.device_id
= cpu_to_le16(pd_id
);
1079 info
->state
.ddf
.pd_type
= cpu_to_le16(MFI_PD_DDF_TYPE_IN_VD
|
1080 MFI_PD_DDF_TYPE_INTF_SAS
);
1081 blk_get_geometry(sdev
->conf
.blk
, &pd_size
);
1082 info
->raw_size
= cpu_to_le64(pd_size
);
1083 info
->non_coerced_size
= cpu_to_le64(pd_size
);
1084 info
->coerced_size
= cpu_to_le64(pd_size
);
1085 info
->encl_device_id
= 0xFFFF;
1086 info
->slot_number
= (sdev
->id
& 0xFF);
1087 info
->path_info
.count
= 1;
1088 info
->path_info
.sas_addr
[0] =
1089 cpu_to_le64(megasas_get_sata_addr(pd_id
));
1090 info
->connected_port_bitmap
= 0x1;
1091 info
->device_speed
= 1;
1092 info
->link_speed
= 1;
1093 resid
= dma_buf_read(cmd
->iov_buf
, dcmd_size
, &cmd
->qsg
);
1094 g_free(cmd
->iov_buf
);
1095 cmd
->iov_size
= dcmd_size
- resid
;
1096 cmd
->iov_buf
= NULL
;
1100 static int megasas_dcmd_pd_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
1102 size_t dcmd_size
= sizeof(struct mfi_pd_info
);
1104 uint8_t target_id
, lun_id
;
1105 SCSIDevice
*sdev
= NULL
;
1106 int retval
= MFI_STAT_DEVICE_NOT_FOUND
;
1108 if (cmd
->iov_size
< dcmd_size
) {
1109 return MFI_STAT_INVALID_PARAMETER
;
1112 /* mbox0 has the ID */
1113 pd_id
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1114 target_id
= (pd_id
>> 8) & 0xFF;
1115 lun_id
= pd_id
& 0xFF;
1116 sdev
= scsi_device_find(&s
->bus
, 0, target_id
, lun_id
);
1117 trace_megasas_dcmd_pd_get_info(cmd
->index
, pd_id
);
1120 /* Submit inquiry */
1121 retval
= megasas_pd_get_info_submit(sdev
, pd_id
, cmd
);
1127 static int megasas_dcmd_ld_get_list(MegasasState
*s
, MegasasCmd
*cmd
)
1129 struct mfi_ld_list info
;
1130 size_t dcmd_size
= sizeof(info
), resid
;
1131 uint32_t num_ld_disks
= 0, max_ld_disks
;
1135 memset(&info
, 0, dcmd_size
);
1136 if (cmd
->iov_size
> dcmd_size
) {
1137 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1139 return MFI_STAT_INVALID_PARAMETER
;
1142 max_ld_disks
= (cmd
->iov_size
- 8) / 16;
1143 if (megasas_is_jbod(s
)) {
1146 if (max_ld_disks
> MFI_MAX_LD
) {
1147 max_ld_disks
= MFI_MAX_LD
;
1149 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1150 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
1152 if (num_ld_disks
>= max_ld_disks
) {
1155 /* Logical device size is in blocks */
1156 blk_get_geometry(sdev
->conf
.blk
, &ld_size
);
1157 info
.ld_list
[num_ld_disks
].ld
.v
.target_id
= sdev
->id
;
1158 info
.ld_list
[num_ld_disks
].state
= MFI_LD_STATE_OPTIMAL
;
1159 info
.ld_list
[num_ld_disks
].size
= cpu_to_le64(ld_size
);
1162 info
.ld_count
= cpu_to_le32(num_ld_disks
);
1163 trace_megasas_dcmd_ld_get_list(cmd
->index
, num_ld_disks
, max_ld_disks
);
1165 resid
= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1166 cmd
->iov_size
= dcmd_size
- resid
;
1170 static int megasas_dcmd_ld_list_query(MegasasState
*s
, MegasasCmd
*cmd
)
1173 struct mfi_ld_targetid_list info
;
1174 size_t dcmd_size
= sizeof(info
), resid
;
1175 uint32_t num_ld_disks
= 0, max_ld_disks
= s
->fw_luns
;
1178 /* mbox0 contains flags */
1179 flags
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1180 trace_megasas_dcmd_ld_list_query(cmd
->index
, flags
);
1181 if (flags
!= MR_LD_QUERY_TYPE_ALL
&&
1182 flags
!= MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
) {
1186 memset(&info
, 0, dcmd_size
);
1187 if (cmd
->iov_size
< 12) {
1188 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1190 return MFI_STAT_INVALID_PARAMETER
;
1192 dcmd_size
= sizeof(uint32_t) * 2 + 3;
1193 max_ld_disks
= cmd
->iov_size
- dcmd_size
;
1194 if (megasas_is_jbod(s
)) {
1197 if (max_ld_disks
> MFI_MAX_LD
) {
1198 max_ld_disks
= MFI_MAX_LD
;
1200 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1201 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
1203 if (num_ld_disks
>= max_ld_disks
) {
1206 info
.targetid
[num_ld_disks
] = sdev
->lun
;
1210 info
.ld_count
= cpu_to_le32(num_ld_disks
);
1211 info
.size
= dcmd_size
;
1212 trace_megasas_dcmd_ld_get_list(cmd
->index
, num_ld_disks
, max_ld_disks
);
1214 resid
= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1215 cmd
->iov_size
= dcmd_size
- resid
;
1219 static int megasas_ld_get_info_submit(SCSIDevice
*sdev
, int lun
,
1222 struct mfi_ld_info
*info
= cmd
->iov_buf
;
1223 size_t dcmd_size
= sizeof(struct mfi_ld_info
);
1226 uint16_t sdev_id
= ((sdev
->id
& 0xFF) << 8) | (lun
& 0xFF);
1229 if (!cmd
->iov_buf
) {
1230 cmd
->iov_buf
= g_malloc0(dcmd_size
);
1231 info
= cmd
->iov_buf
;
1232 megasas_setup_inquiry(cdb
, 0x83, sizeof(info
->vpd_page83
));
1233 cmd
->req
= scsi_req_new(sdev
, cmd
->index
, lun
, cdb
, cmd
);
1235 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
1236 "LD get info vpd inquiry");
1237 g_free(cmd
->iov_buf
);
1238 cmd
->iov_buf
= NULL
;
1239 return MFI_STAT_FLASH_ALLOC_FAIL
;
1241 trace_megasas_dcmd_internal_submit(cmd
->index
,
1242 "LD get info vpd inquiry", lun
);
1243 len
= scsi_req_enqueue(cmd
->req
);
1245 cmd
->iov_size
= len
;
1246 scsi_req_continue(cmd
->req
);
1248 return MFI_STAT_INVALID_STATUS
;
1251 info
->ld_config
.params
.state
= MFI_LD_STATE_OPTIMAL
;
1252 info
->ld_config
.properties
.ld
.v
.target_id
= lun
;
1253 info
->ld_config
.params
.stripe_size
= 3;
1254 info
->ld_config
.params
.num_drives
= 1;
1255 info
->ld_config
.params
.is_consistent
= 1;
1256 /* Logical device size is in blocks */
1257 blk_get_geometry(sdev
->conf
.blk
, &ld_size
);
1258 info
->size
= cpu_to_le64(ld_size
);
1259 memset(info
->ld_config
.span
, 0, sizeof(info
->ld_config
.span
));
1260 info
->ld_config
.span
[0].start_block
= 0;
1261 info
->ld_config
.span
[0].num_blocks
= info
->size
;
1262 info
->ld_config
.span
[0].array_ref
= cpu_to_le16(sdev_id
);
1264 resid
= dma_buf_read(cmd
->iov_buf
, dcmd_size
, &cmd
->qsg
);
1265 g_free(cmd
->iov_buf
);
1266 cmd
->iov_size
= dcmd_size
- resid
;
1267 cmd
->iov_buf
= NULL
;
1271 static int megasas_dcmd_ld_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
1273 struct mfi_ld_info info
;
1274 size_t dcmd_size
= sizeof(info
);
1276 uint32_t max_ld_disks
= s
->fw_luns
;
1277 SCSIDevice
*sdev
= NULL
;
1278 int retval
= MFI_STAT_DEVICE_NOT_FOUND
;
1280 if (cmd
->iov_size
< dcmd_size
) {
1281 return MFI_STAT_INVALID_PARAMETER
;
1284 /* mbox0 has the ID */
1285 ld_id
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1286 trace_megasas_dcmd_ld_get_info(cmd
->index
, ld_id
);
1288 if (megasas_is_jbod(s
)) {
1289 return MFI_STAT_DEVICE_NOT_FOUND
;
1292 if (ld_id
< max_ld_disks
) {
1293 sdev
= scsi_device_find(&s
->bus
, 0, ld_id
, 0);
1297 retval
= megasas_ld_get_info_submit(sdev
, ld_id
, cmd
);
1303 static int megasas_dcmd_cfg_read(MegasasState
*s
, MegasasCmd
*cmd
)
1305 uint8_t data
[4096] = { 0 };
1306 struct mfi_config_data
*info
;
1307 int num_pd_disks
= 0, array_offset
, ld_offset
;
1310 if (cmd
->iov_size
> 4096) {
1311 return MFI_STAT_INVALID_PARAMETER
;
1314 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1317 info
= (struct mfi_config_data
*)&data
;
1320 * - One array per SCSI device
1321 * - One logical drive per SCSI device
1322 * spanning the entire device
1324 info
->array_count
= num_pd_disks
;
1325 info
->array_size
= sizeof(struct mfi_array
) * num_pd_disks
;
1326 info
->log_drv_count
= num_pd_disks
;
1327 info
->log_drv_size
= sizeof(struct mfi_ld_config
) * num_pd_disks
;
1328 info
->spares_count
= 0;
1329 info
->spares_size
= sizeof(struct mfi_spare
);
1330 info
->size
= sizeof(struct mfi_config_data
) + info
->array_size
+
1332 if (info
->size
> 4096) {
1333 return MFI_STAT_INVALID_PARAMETER
;
1336 array_offset
= sizeof(struct mfi_config_data
);
1337 ld_offset
= array_offset
+ sizeof(struct mfi_array
) * num_pd_disks
;
1339 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1340 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
1341 uint16_t sdev_id
= ((sdev
->id
& 0xFF) << 8) | (sdev
->lun
& 0xFF);
1342 struct mfi_array
*array
;
1343 struct mfi_ld_config
*ld
;
1347 array
= (struct mfi_array
*)(data
+ array_offset
);
1348 blk_get_geometry(sdev
->conf
.blk
, &pd_size
);
1349 array
->size
= cpu_to_le64(pd_size
);
1350 array
->num_drives
= 1;
1351 array
->array_ref
= cpu_to_le16(sdev_id
);
1352 array
->pd
[0].ref
.v
.device_id
= cpu_to_le16(sdev_id
);
1353 array
->pd
[0].ref
.v
.seq_num
= 0;
1354 array
->pd
[0].fw_state
= MFI_PD_STATE_ONLINE
;
1355 array
->pd
[0].encl
.pd
= 0xFF;
1356 array
->pd
[0].encl
.slot
= (sdev
->id
& 0xFF);
1357 for (i
= 1; i
< MFI_MAX_ROW_SIZE
; i
++) {
1358 array
->pd
[i
].ref
.v
.device_id
= 0xFFFF;
1359 array
->pd
[i
].ref
.v
.seq_num
= 0;
1360 array
->pd
[i
].fw_state
= MFI_PD_STATE_UNCONFIGURED_GOOD
;
1361 array
->pd
[i
].encl
.pd
= 0xFF;
1362 array
->pd
[i
].encl
.slot
= 0xFF;
1364 array_offset
+= sizeof(struct mfi_array
);
1365 ld
= (struct mfi_ld_config
*)(data
+ ld_offset
);
1366 memset(ld
, 0, sizeof(struct mfi_ld_config
));
1367 ld
->properties
.ld
.v
.target_id
= sdev
->id
;
1368 ld
->properties
.default_cache_policy
= MR_LD_CACHE_READ_AHEAD
|
1369 MR_LD_CACHE_READ_ADAPTIVE
;
1370 ld
->properties
.current_cache_policy
= MR_LD_CACHE_READ_AHEAD
|
1371 MR_LD_CACHE_READ_ADAPTIVE
;
1372 ld
->params
.state
= MFI_LD_STATE_OPTIMAL
;
1373 ld
->params
.stripe_size
= 3;
1374 ld
->params
.num_drives
= 1;
1375 ld
->params
.span_depth
= 1;
1376 ld
->params
.is_consistent
= 1;
1377 ld
->span
[0].start_block
= 0;
1378 ld
->span
[0].num_blocks
= cpu_to_le64(pd_size
);
1379 ld
->span
[0].array_ref
= cpu_to_le16(sdev_id
);
1380 ld_offset
+= sizeof(struct mfi_ld_config
);
1383 cmd
->iov_size
-= dma_buf_read((uint8_t *)data
, info
->size
, &cmd
->qsg
);
1387 static int megasas_dcmd_get_properties(MegasasState
*s
, MegasasCmd
*cmd
)
1389 struct mfi_ctrl_props info
;
1390 size_t dcmd_size
= sizeof(info
);
1392 memset(&info
, 0x0, dcmd_size
);
1393 if (cmd
->iov_size
< dcmd_size
) {
1394 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1396 return MFI_STAT_INVALID_PARAMETER
;
1398 info
.pred_fail_poll_interval
= cpu_to_le16(300);
1399 info
.intr_throttle_cnt
= cpu_to_le16(16);
1400 info
.intr_throttle_timeout
= cpu_to_le16(50);
1401 info
.rebuild_rate
= 30;
1402 info
.patrol_read_rate
= 30;
1405 info
.recon_rate
= 30;
1406 info
.cache_flush_interval
= 4;
1407 info
.spinup_drv_cnt
= 2;
1408 info
.spinup_delay
= 6;
1409 info
.ecc_bucket_size
= 15;
1410 info
.ecc_bucket_leak_rate
= cpu_to_le16(1440);
1411 info
.expose_encl_devices
= 1;
1413 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1417 static int megasas_cache_flush(MegasasState
*s
, MegasasCmd
*cmd
)
1423 static int megasas_ctrl_shutdown(MegasasState
*s
, MegasasCmd
*cmd
)
1425 s
->fw_state
= MFI_FWSTATE_READY
;
1429 /* Some implementations use CLUSTER RESET LD to simulate a device reset */
1430 static int megasas_cluster_reset_ld(MegasasState
*s
, MegasasCmd
*cmd
)
1435 /* mbox0 contains the device index */
1436 target_id
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1437 trace_megasas_dcmd_reset_ld(cmd
->index
, target_id
);
1438 for (i
= 0; i
< s
->fw_cmds
; i
++) {
1439 MegasasCmd
*tmp_cmd
= &s
->frames
[i
];
1440 if (tmp_cmd
->req
&& tmp_cmd
->req
->dev
->id
== target_id
) {
1441 SCSIDevice
*d
= tmp_cmd
->req
->dev
;
1442 qdev_reset_all(&d
->qdev
);
1448 static int megasas_dcmd_set_properties(MegasasState
*s
, MegasasCmd
*cmd
)
1450 struct mfi_ctrl_props info
;
1451 size_t dcmd_size
= sizeof(info
);
1453 if (cmd
->iov_size
< dcmd_size
) {
1454 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1456 return MFI_STAT_INVALID_PARAMETER
;
1458 dma_buf_write((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1459 trace_megasas_dcmd_unsupported(cmd
->index
, cmd
->iov_size
);
1463 static int megasas_dcmd_dummy(MegasasState
*s
, MegasasCmd
*cmd
)
1465 trace_megasas_dcmd_dummy(cmd
->index
, cmd
->iov_size
);
1469 static const struct dcmd_cmd_tbl_t
{
1472 int (*func
)(MegasasState
*s
, MegasasCmd
*cmd
);
1473 } dcmd_cmd_tbl
[] = {
1474 { MFI_DCMD_CTRL_MFI_HOST_MEM_ALLOC
, "CTRL_HOST_MEM_ALLOC",
1475 megasas_dcmd_dummy
},
1476 { MFI_DCMD_CTRL_GET_INFO
, "CTRL_GET_INFO",
1477 megasas_ctrl_get_info
},
1478 { MFI_DCMD_CTRL_GET_PROPERTIES
, "CTRL_GET_PROPERTIES",
1479 megasas_dcmd_get_properties
},
1480 { MFI_DCMD_CTRL_SET_PROPERTIES
, "CTRL_SET_PROPERTIES",
1481 megasas_dcmd_set_properties
},
1482 { MFI_DCMD_CTRL_ALARM_GET
, "CTRL_ALARM_GET",
1483 megasas_dcmd_dummy
},
1484 { MFI_DCMD_CTRL_ALARM_ENABLE
, "CTRL_ALARM_ENABLE",
1485 megasas_dcmd_dummy
},
1486 { MFI_DCMD_CTRL_ALARM_DISABLE
, "CTRL_ALARM_DISABLE",
1487 megasas_dcmd_dummy
},
1488 { MFI_DCMD_CTRL_ALARM_SILENCE
, "CTRL_ALARM_SILENCE",
1489 megasas_dcmd_dummy
},
1490 { MFI_DCMD_CTRL_ALARM_TEST
, "CTRL_ALARM_TEST",
1491 megasas_dcmd_dummy
},
1492 { MFI_DCMD_CTRL_EVENT_GETINFO
, "CTRL_EVENT_GETINFO",
1493 megasas_event_info
},
1494 { MFI_DCMD_CTRL_EVENT_GET
, "CTRL_EVENT_GET",
1495 megasas_dcmd_dummy
},
1496 { MFI_DCMD_CTRL_EVENT_WAIT
, "CTRL_EVENT_WAIT",
1497 megasas_event_wait
},
1498 { MFI_DCMD_CTRL_SHUTDOWN
, "CTRL_SHUTDOWN",
1499 megasas_ctrl_shutdown
},
1500 { MFI_DCMD_HIBERNATE_STANDBY
, "CTRL_STANDBY",
1501 megasas_dcmd_dummy
},
1502 { MFI_DCMD_CTRL_GET_TIME
, "CTRL_GET_TIME",
1503 megasas_dcmd_get_fw_time
},
1504 { MFI_DCMD_CTRL_SET_TIME
, "CTRL_SET_TIME",
1505 megasas_dcmd_set_fw_time
},
1506 { MFI_DCMD_CTRL_BIOS_DATA_GET
, "CTRL_BIOS_DATA_GET",
1507 megasas_dcmd_get_bios_info
},
1508 { MFI_DCMD_CTRL_FACTORY_DEFAULTS
, "CTRL_FACTORY_DEFAULTS",
1509 megasas_dcmd_dummy
},
1510 { MFI_DCMD_CTRL_MFC_DEFAULTS_GET
, "CTRL_MFC_DEFAULTS_GET",
1511 megasas_mfc_get_defaults
},
1512 { MFI_DCMD_CTRL_MFC_DEFAULTS_SET
, "CTRL_MFC_DEFAULTS_SET",
1513 megasas_dcmd_dummy
},
1514 { MFI_DCMD_CTRL_CACHE_FLUSH
, "CTRL_CACHE_FLUSH",
1515 megasas_cache_flush
},
1516 { MFI_DCMD_PD_GET_LIST
, "PD_GET_LIST",
1517 megasas_dcmd_pd_get_list
},
1518 { MFI_DCMD_PD_LIST_QUERY
, "PD_LIST_QUERY",
1519 megasas_dcmd_pd_list_query
},
1520 { MFI_DCMD_PD_GET_INFO
, "PD_GET_INFO",
1521 megasas_dcmd_pd_get_info
},
1522 { MFI_DCMD_PD_STATE_SET
, "PD_STATE_SET",
1523 megasas_dcmd_dummy
},
1524 { MFI_DCMD_PD_REBUILD
, "PD_REBUILD",
1525 megasas_dcmd_dummy
},
1526 { MFI_DCMD_PD_BLINK
, "PD_BLINK",
1527 megasas_dcmd_dummy
},
1528 { MFI_DCMD_PD_UNBLINK
, "PD_UNBLINK",
1529 megasas_dcmd_dummy
},
1530 { MFI_DCMD_LD_GET_LIST
, "LD_GET_LIST",
1531 megasas_dcmd_ld_get_list
},
1532 { MFI_DCMD_LD_LIST_QUERY
, "LD_LIST_QUERY",
1533 megasas_dcmd_ld_list_query
},
1534 { MFI_DCMD_LD_GET_INFO
, "LD_GET_INFO",
1535 megasas_dcmd_ld_get_info
},
1536 { MFI_DCMD_LD_GET_PROP
, "LD_GET_PROP",
1537 megasas_dcmd_dummy
},
1538 { MFI_DCMD_LD_SET_PROP
, "LD_SET_PROP",
1539 megasas_dcmd_dummy
},
1540 { MFI_DCMD_LD_DELETE
, "LD_DELETE",
1541 megasas_dcmd_dummy
},
1542 { MFI_DCMD_CFG_READ
, "CFG_READ",
1543 megasas_dcmd_cfg_read
},
1544 { MFI_DCMD_CFG_ADD
, "CFG_ADD",
1545 megasas_dcmd_dummy
},
1546 { MFI_DCMD_CFG_CLEAR
, "CFG_CLEAR",
1547 megasas_dcmd_dummy
},
1548 { MFI_DCMD_CFG_FOREIGN_READ
, "CFG_FOREIGN_READ",
1549 megasas_dcmd_dummy
},
1550 { MFI_DCMD_CFG_FOREIGN_IMPORT
, "CFG_FOREIGN_IMPORT",
1551 megasas_dcmd_dummy
},
1552 { MFI_DCMD_BBU_STATUS
, "BBU_STATUS",
1553 megasas_dcmd_dummy
},
1554 { MFI_DCMD_BBU_CAPACITY_INFO
, "BBU_CAPACITY_INFO",
1555 megasas_dcmd_dummy
},
1556 { MFI_DCMD_BBU_DESIGN_INFO
, "BBU_DESIGN_INFO",
1557 megasas_dcmd_dummy
},
1558 { MFI_DCMD_BBU_PROP_GET
, "BBU_PROP_GET",
1559 megasas_dcmd_dummy
},
1560 { MFI_DCMD_CLUSTER
, "CLUSTER",
1561 megasas_dcmd_dummy
},
1562 { MFI_DCMD_CLUSTER_RESET_ALL
, "CLUSTER_RESET_ALL",
1563 megasas_dcmd_dummy
},
1564 { MFI_DCMD_CLUSTER_RESET_LD
, "CLUSTER_RESET_LD",
1565 megasas_cluster_reset_ld
},
1569 static int megasas_handle_dcmd(MegasasState
*s
, MegasasCmd
*cmd
)
1573 const struct dcmd_cmd_tbl_t
*cmdptr
= dcmd_cmd_tbl
;
1575 cmd
->dcmd_opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
1576 trace_megasas_handle_dcmd(cmd
->index
, cmd
->dcmd_opcode
);
1577 if (megasas_map_dcmd(s
, cmd
) < 0) {
1578 return MFI_STAT_MEMORY_NOT_AVAILABLE
;
1580 while (cmdptr
->opcode
!= -1 && cmdptr
->opcode
!= cmd
->dcmd_opcode
) {
1583 len
= cmd
->iov_size
;
1584 if (cmdptr
->opcode
== -1) {
1585 trace_megasas_dcmd_unhandled(cmd
->index
, cmd
->dcmd_opcode
, len
);
1586 retval
= megasas_dcmd_dummy(s
, cmd
);
1588 trace_megasas_dcmd_enter(cmd
->index
, cmdptr
->desc
, len
);
1589 retval
= cmdptr
->func(s
, cmd
);
1591 if (retval
!= MFI_STAT_INVALID_STATUS
) {
1592 megasas_finish_dcmd(cmd
, len
);
1597 static int megasas_finish_internal_dcmd(MegasasCmd
*cmd
,
1598 SCSIRequest
*req
, size_t resid
)
1600 int retval
= MFI_STAT_OK
;
1603 trace_megasas_dcmd_internal_finish(cmd
->index
, cmd
->dcmd_opcode
, lun
);
1604 cmd
->iov_size
-= resid
;
1605 switch (cmd
->dcmd_opcode
) {
1606 case MFI_DCMD_PD_GET_INFO
:
1607 retval
= megasas_pd_get_info_submit(req
->dev
, lun
, cmd
);
1609 case MFI_DCMD_LD_GET_INFO
:
1610 retval
= megasas_ld_get_info_submit(req
->dev
, lun
, cmd
);
1613 trace_megasas_dcmd_internal_invalid(cmd
->index
, cmd
->dcmd_opcode
);
1614 retval
= MFI_STAT_INVALID_DCMD
;
1617 if (retval
!= MFI_STAT_INVALID_STATUS
) {
1618 megasas_finish_dcmd(cmd
, cmd
->iov_size
);
1623 static int megasas_enqueue_req(MegasasCmd
*cmd
, bool is_write
)
1627 len
= scsi_req_enqueue(cmd
->req
);
1632 if (len
> cmd
->iov_size
) {
1634 trace_megasas_iov_write_overflow(cmd
->index
, len
,
1637 trace_megasas_iov_read_overflow(cmd
->index
, len
,
1641 if (len
< cmd
->iov_size
) {
1643 trace_megasas_iov_write_underflow(cmd
->index
, len
,
1646 trace_megasas_iov_read_underflow(cmd
->index
, len
,
1649 cmd
->iov_size
= len
;
1651 scsi_req_continue(cmd
->req
);
1656 static int megasas_handle_scsi(MegasasState
*s
, MegasasCmd
*cmd
,
1660 int target_id
, lun_id
, cdb_len
;
1662 struct SCSIDevice
*sdev
= NULL
;
1663 bool is_logical
= (frame_cmd
== MFI_CMD_LD_SCSI_IO
);
1665 cdb
= cmd
->frame
->pass
.cdb
;
1666 target_id
= cmd
->frame
->header
.target_id
;
1667 lun_id
= cmd
->frame
->header
.lun_id
;
1668 cdb_len
= cmd
->frame
->header
.cdb_len
;
1671 if (target_id
>= MFI_MAX_LD
|| lun_id
!= 0) {
1672 trace_megasas_scsi_target_not_present(
1673 mfi_frame_desc
[frame_cmd
], is_logical
, target_id
, lun_id
);
1674 return MFI_STAT_DEVICE_NOT_FOUND
;
1677 sdev
= scsi_device_find(&s
->bus
, 0, target_id
, lun_id
);
1679 cmd
->iov_size
= le32_to_cpu(cmd
->frame
->header
.data_len
);
1680 trace_megasas_handle_scsi(mfi_frame_desc
[frame_cmd
], is_logical
,
1681 target_id
, lun_id
, sdev
, cmd
->iov_size
);
1683 if (!sdev
|| (megasas_is_jbod(s
) && is_logical
)) {
1684 trace_megasas_scsi_target_not_present(
1685 mfi_frame_desc
[frame_cmd
], is_logical
, target_id
, lun_id
);
1686 return MFI_STAT_DEVICE_NOT_FOUND
;
1690 trace_megasas_scsi_invalid_cdb_len(
1691 mfi_frame_desc
[frame_cmd
], is_logical
,
1692 target_id
, lun_id
, cdb_len
);
1693 megasas_write_sense(cmd
, SENSE_CODE(INVALID_OPCODE
));
1694 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1696 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1699 if (megasas_map_sgl(s
, cmd
, &cmd
->frame
->pass
.sgl
)) {
1700 megasas_write_sense(cmd
, SENSE_CODE(TARGET_FAILURE
));
1701 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1703 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1706 cmd
->req
= scsi_req_new(sdev
, cmd
->index
, lun_id
, cdb
, cmd
);
1708 trace_megasas_scsi_req_alloc_failed(
1709 mfi_frame_desc
[frame_cmd
], target_id
, lun_id
);
1710 megasas_write_sense(cmd
, SENSE_CODE(NO_SENSE
));
1711 cmd
->frame
->header
.scsi_status
= BUSY
;
1713 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1716 is_write
= (cmd
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
);
1717 if (cmd
->iov_size
) {
1719 trace_megasas_scsi_write_start(cmd
->index
, cmd
->iov_size
);
1721 trace_megasas_scsi_read_start(cmd
->index
, cmd
->iov_size
);
1724 trace_megasas_scsi_nodata(cmd
->index
);
1726 megasas_enqueue_req(cmd
, is_write
);
1727 return MFI_STAT_INVALID_STATUS
;
1730 static int megasas_handle_io(MegasasState
*s
, MegasasCmd
*cmd
, int frame_cmd
)
1732 uint32_t lba_count
, lba_start_hi
, lba_start_lo
;
1734 bool is_write
= (frame_cmd
== MFI_CMD_LD_WRITE
);
1737 struct SCSIDevice
*sdev
= NULL
;
1738 int target_id
, lun_id
, cdb_len
;
1740 lba_count
= le32_to_cpu(cmd
->frame
->io
.header
.data_len
);
1741 lba_start_lo
= le32_to_cpu(cmd
->frame
->io
.lba_lo
);
1742 lba_start_hi
= le32_to_cpu(cmd
->frame
->io
.lba_hi
);
1743 lba_start
= ((uint64_t)lba_start_hi
<< 32) | lba_start_lo
;
1745 target_id
= cmd
->frame
->header
.target_id
;
1746 lun_id
= cmd
->frame
->header
.lun_id
;
1747 cdb_len
= cmd
->frame
->header
.cdb_len
;
1749 if (target_id
< MFI_MAX_LD
&& lun_id
== 0) {
1750 sdev
= scsi_device_find(&s
->bus
, 0, target_id
, lun_id
);
1753 trace_megasas_handle_io(cmd
->index
,
1754 mfi_frame_desc
[frame_cmd
], target_id
, lun_id
,
1755 (unsigned long)lba_start
, (unsigned long)lba_count
);
1757 trace_megasas_io_target_not_present(cmd
->index
,
1758 mfi_frame_desc
[frame_cmd
], target_id
, lun_id
);
1759 return MFI_STAT_DEVICE_NOT_FOUND
;
1763 trace_megasas_scsi_invalid_cdb_len(
1764 mfi_frame_desc
[frame_cmd
], 1, target_id
, lun_id
, cdb_len
);
1765 megasas_write_sense(cmd
, SENSE_CODE(INVALID_OPCODE
));
1766 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1768 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1771 cmd
->iov_size
= lba_count
* sdev
->blocksize
;
1772 if (megasas_map_sgl(s
, cmd
, &cmd
->frame
->io
.sgl
)) {
1773 megasas_write_sense(cmd
, SENSE_CODE(TARGET_FAILURE
));
1774 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1776 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1779 megasas_encode_lba(cdb
, lba_start
, lba_count
, is_write
);
1780 cmd
->req
= scsi_req_new(sdev
, cmd
->index
,
1783 trace_megasas_scsi_req_alloc_failed(
1784 mfi_frame_desc
[frame_cmd
], target_id
, lun_id
);
1785 megasas_write_sense(cmd
, SENSE_CODE(NO_SENSE
));
1786 cmd
->frame
->header
.scsi_status
= BUSY
;
1788 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1790 len
= megasas_enqueue_req(cmd
, is_write
);
1793 trace_megasas_io_write_start(cmd
->index
, lba_start
, lba_count
, len
);
1795 trace_megasas_io_read_start(cmd
->index
, lba_start
, lba_count
, len
);
1798 return MFI_STAT_INVALID_STATUS
;
1801 static QEMUSGList
*megasas_get_sg_list(SCSIRequest
*req
)
1803 MegasasCmd
*cmd
= req
->hba_private
;
1805 if (cmd
->dcmd_opcode
!= -1) {
1812 static void megasas_xfer_complete(SCSIRequest
*req
, uint32_t len
)
1814 MegasasCmd
*cmd
= req
->hba_private
;
1817 trace_megasas_io_complete(cmd
->index
, len
);
1819 if (cmd
->dcmd_opcode
!= -1) {
1820 scsi_req_continue(req
);
1824 buf
= scsi_req_get_buf(req
);
1825 if (cmd
->dcmd_opcode
== MFI_DCMD_PD_GET_INFO
&& cmd
->iov_buf
) {
1826 struct mfi_pd_info
*info
= cmd
->iov_buf
;
1828 if (info
->inquiry_data
[0] == 0x7f) {
1829 memset(info
->inquiry_data
, 0, sizeof(info
->inquiry_data
));
1830 memcpy(info
->inquiry_data
, buf
, len
);
1831 } else if (info
->vpd_page83
[0] == 0x7f) {
1832 memset(info
->vpd_page83
, 0, sizeof(info
->vpd_page83
));
1833 memcpy(info
->vpd_page83
, buf
, len
);
1835 scsi_req_continue(req
);
1836 } else if (cmd
->dcmd_opcode
== MFI_DCMD_LD_GET_INFO
) {
1837 struct mfi_ld_info
*info
= cmd
->iov_buf
;
1840 memcpy(info
->vpd_page83
, buf
, sizeof(info
->vpd_page83
));
1841 scsi_req_continue(req
);
1846 static void megasas_command_complete(SCSIRequest
*req
, uint32_t status
,
1849 MegasasCmd
*cmd
= req
->hba_private
;
1850 uint8_t cmd_status
= MFI_STAT_OK
;
1852 trace_megasas_command_complete(cmd
->index
, status
, resid
);
1854 if (req
->io_canceled
) {
1858 if (cmd
->dcmd_opcode
!= -1) {
1860 * Internal command complete
1862 cmd_status
= megasas_finish_internal_dcmd(cmd
, req
, resid
);
1863 if (cmd_status
== MFI_STAT_INVALID_STATUS
) {
1867 req
->status
= status
;
1868 trace_megasas_scsi_complete(cmd
->index
, req
->status
,
1869 cmd
->iov_size
, req
->cmd
.xfer
);
1870 if (req
->status
!= GOOD
) {
1871 cmd_status
= MFI_STAT_SCSI_DONE_WITH_ERROR
;
1873 if (req
->status
== CHECK_CONDITION
) {
1874 megasas_copy_sense(cmd
);
1877 cmd
->frame
->header
.scsi_status
= req
->status
;
1879 cmd
->frame
->header
.cmd_status
= cmd_status
;
1880 megasas_complete_command(cmd
);
1883 static void megasas_command_cancelled(SCSIRequest
*req
)
1885 MegasasCmd
*cmd
= req
->hba_private
;
1890 cmd
->frame
->header
.cmd_status
= MFI_STAT_SCSI_IO_FAILED
;
1891 megasas_complete_command(cmd
);
1894 static int megasas_handle_abort(MegasasState
*s
, MegasasCmd
*cmd
)
1896 uint64_t abort_ctx
= le64_to_cpu(cmd
->frame
->abort
.abort_context
);
1897 hwaddr abort_addr
, addr_hi
, addr_lo
;
1898 MegasasCmd
*abort_cmd
;
1900 addr_hi
= le32_to_cpu(cmd
->frame
->abort
.abort_mfi_addr_hi
);
1901 addr_lo
= le32_to_cpu(cmd
->frame
->abort
.abort_mfi_addr_lo
);
1902 abort_addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1904 abort_cmd
= megasas_lookup_frame(s
, abort_addr
);
1906 trace_megasas_abort_no_cmd(cmd
->index
, abort_ctx
);
1910 if (!megasas_use_queue64(s
)) {
1911 abort_ctx
&= (uint64_t)0xFFFFFFFF;
1913 if (abort_cmd
->context
!= abort_ctx
) {
1914 trace_megasas_abort_invalid_context(cmd
->index
, abort_cmd
->context
,
1917 return MFI_STAT_ABORT_NOT_POSSIBLE
;
1919 trace_megasas_abort_frame(cmd
->index
, abort_cmd
->index
);
1920 megasas_abort_command(abort_cmd
);
1921 if (!s
->event_cmd
|| abort_cmd
!= s
->event_cmd
) {
1922 s
->event_cmd
= NULL
;
1928 static void megasas_handle_frame(MegasasState
*s
, uint64_t frame_addr
,
1929 uint32_t frame_count
)
1931 uint8_t frame_status
= MFI_STAT_INVALID_CMD
;
1932 uint64_t frame_context
;
1937 * Always read 64bit context, top bits will be
1938 * masked out if required in megasas_enqueue_frame()
1940 frame_context
= megasas_frame_get_context(s
, frame_addr
);
1942 cmd
= megasas_enqueue_frame(s
, frame_addr
, frame_context
, frame_count
);
1944 /* reply queue full */
1945 trace_megasas_frame_busy(frame_addr
);
1946 megasas_frame_set_scsi_status(s
, frame_addr
, BUSY
);
1947 megasas_frame_set_cmd_status(s
, frame_addr
, MFI_STAT_SCSI_DONE_WITH_ERROR
);
1948 megasas_complete_frame(s
, frame_context
);
1952 frame_cmd
= cmd
->frame
->header
.frame_cmd
;
1953 switch (frame_cmd
) {
1955 frame_status
= megasas_init_firmware(s
, cmd
);
1958 frame_status
= megasas_handle_dcmd(s
, cmd
);
1961 frame_status
= megasas_handle_abort(s
, cmd
);
1963 case MFI_CMD_PD_SCSI_IO
:
1964 case MFI_CMD_LD_SCSI_IO
:
1965 frame_status
= megasas_handle_scsi(s
, cmd
, frame_cmd
);
1967 case MFI_CMD_LD_READ
:
1968 case MFI_CMD_LD_WRITE
:
1969 frame_status
= megasas_handle_io(s
, cmd
, frame_cmd
);
1972 trace_megasas_unhandled_frame_cmd(cmd
->index
, frame_cmd
);
1976 if (frame_status
!= MFI_STAT_INVALID_STATUS
) {
1978 cmd
->frame
->header
.cmd_status
= frame_status
;
1980 megasas_frame_set_cmd_status(s
, frame_addr
, frame_status
);
1982 megasas_unmap_frame(s
, cmd
);
1983 megasas_complete_frame(s
, cmd
->context
);
1987 static uint64_t megasas_mmio_read(void *opaque
, hwaddr addr
,
1990 MegasasState
*s
= opaque
;
1991 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
1992 MegasasBaseClass
*base_class
= MEGASAS_DEVICE_GET_CLASS(s
);
1993 uint32_t retval
= 0;
1998 trace_megasas_mmio_readl("MFI_IDB", retval
);
2002 retval
= (msix_present(pci_dev
) ? MFI_FWSTATE_MSIX_SUPPORTED
: 0) |
2003 (s
->fw_state
& MFI_FWSTATE_MASK
) |
2004 ((s
->fw_sge
& 0xff) << 16) |
2005 (s
->fw_cmds
& 0xFFFF);
2006 trace_megasas_mmio_readl(addr
== MFI_OMSG0
? "MFI_OMSG0" : "MFI_OSP0",
2010 if (megasas_intr_enabled(s
) && s
->doorbell
) {
2011 retval
= base_class
->osts
;
2013 trace_megasas_mmio_readl("MFI_OSTS", retval
);
2016 retval
= s
->intr_mask
;
2017 trace_megasas_mmio_readl("MFI_OMSK", retval
);
2020 retval
= s
->doorbell
? 1 : 0;
2021 trace_megasas_mmio_readl("MFI_ODCR0", retval
);
2025 trace_megasas_mmio_readl("MFI_DIAG", retval
);
2029 trace_megasas_mmio_readl("MFI_OSP1", retval
);
2032 trace_megasas_mmio_invalid_readl(addr
);
2038 static int adp_reset_seq
[] = {0x00, 0x04, 0x0b, 0x02, 0x07, 0x0d};
2040 static void megasas_mmio_write(void *opaque
, hwaddr addr
,
2041 uint64_t val
, unsigned size
)
2043 MegasasState
*s
= opaque
;
2044 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
2045 uint64_t frame_addr
;
2046 uint32_t frame_count
;
2051 trace_megasas_mmio_writel("MFI_IDB", val
);
2052 if (val
& MFI_FWINIT_ABORT
) {
2053 /* Abort all pending cmds */
2054 for (i
= 0; i
< s
->fw_cmds
; i
++) {
2055 megasas_abort_command(&s
->frames
[i
]);
2058 if (val
& MFI_FWINIT_READY
) {
2059 /* move to FW READY */
2060 megasas_soft_reset(s
);
2062 if (val
& MFI_FWINIT_MFIMODE
) {
2065 if (val
& MFI_FWINIT_STOP_ADP
) {
2066 /* Terminal error, stop processing */
2067 s
->fw_state
= MFI_FWSTATE_FAULT
;
2071 trace_megasas_mmio_writel("MFI_OMSK", val
);
2073 if (!megasas_intr_enabled(s
) &&
2074 !msi_enabled(pci_dev
) &&
2075 !msix_enabled(pci_dev
)) {
2076 trace_megasas_irq_lower();
2077 pci_irq_deassert(pci_dev
);
2079 if (megasas_intr_enabled(s
)) {
2080 if (msix_enabled(pci_dev
)) {
2081 trace_megasas_msix_enabled(0);
2082 } else if (msi_enabled(pci_dev
)) {
2083 trace_megasas_msi_enabled(0);
2085 trace_megasas_intr_enabled();
2088 trace_megasas_intr_disabled();
2089 megasas_soft_reset(s
);
2093 trace_megasas_mmio_writel("MFI_ODCR0", val
);
2095 if (megasas_intr_enabled(s
)) {
2096 if (!msix_enabled(pci_dev
) && !msi_enabled(pci_dev
)) {
2097 trace_megasas_irq_lower();
2098 pci_irq_deassert(pci_dev
);
2103 trace_megasas_mmio_writel("MFI_IQPH", val
);
2104 /* Received high 32 bits of a 64 bit MFI frame address */
2108 trace_megasas_mmio_writel("MFI_IQPL", val
);
2109 /* Received low 32 bits of a 64 bit MFI frame address */
2112 if (addr
== MFI_IQP
) {
2113 trace_megasas_mmio_writel("MFI_IQP", val
);
2114 /* Received 64 bit MFI frame address */
2117 frame_addr
= (val
& ~0x1F);
2118 /* Add possible 64 bit offset */
2119 frame_addr
|= ((uint64_t)s
->frame_hi
<< 32);
2121 frame_count
= (val
>> 1) & 0xF;
2122 megasas_handle_frame(s
, frame_addr
, frame_count
);
2125 trace_megasas_mmio_writel("MFI_SEQ", val
);
2126 /* Magic sequence to start ADP reset */
2127 if (adp_reset_seq
[s
->adp_reset
++] == val
) {
2128 if (s
->adp_reset
== 6) {
2130 s
->diag
= MFI_DIAG_WRITE_ENABLE
;
2138 trace_megasas_mmio_writel("MFI_DIAG", val
);
2140 if ((s
->diag
& MFI_DIAG_WRITE_ENABLE
) &&
2141 (val
& MFI_DIAG_RESET_ADP
)) {
2142 s
->diag
|= MFI_DIAG_RESET_ADP
;
2143 megasas_soft_reset(s
);
2149 trace_megasas_mmio_invalid_writel(addr
, val
);
2154 static const MemoryRegionOps megasas_mmio_ops
= {
2155 .read
= megasas_mmio_read
,
2156 .write
= megasas_mmio_write
,
2157 .endianness
= DEVICE_LITTLE_ENDIAN
,
2159 .min_access_size
= 8,
2160 .max_access_size
= 8,
2164 static uint64_t megasas_port_read(void *opaque
, hwaddr addr
,
2167 return megasas_mmio_read(opaque
, addr
& 0xff, size
);
2170 static void megasas_port_write(void *opaque
, hwaddr addr
,
2171 uint64_t val
, unsigned size
)
2173 megasas_mmio_write(opaque
, addr
& 0xff, val
, size
);
2176 static const MemoryRegionOps megasas_port_ops
= {
2177 .read
= megasas_port_read
,
2178 .write
= megasas_port_write
,
2179 .endianness
= DEVICE_LITTLE_ENDIAN
,
2181 .min_access_size
= 4,
2182 .max_access_size
= 4,
2186 static uint64_t megasas_queue_read(void *opaque
, hwaddr addr
,
2192 static void megasas_queue_write(void *opaque
, hwaddr addr
,
2193 uint64_t val
, unsigned size
)
2198 static const MemoryRegionOps megasas_queue_ops
= {
2199 .read
= megasas_queue_read
,
2200 .write
= megasas_queue_write
,
2201 .endianness
= DEVICE_LITTLE_ENDIAN
,
2203 .min_access_size
= 8,
2204 .max_access_size
= 8,
2208 static void megasas_soft_reset(MegasasState
*s
)
2213 trace_megasas_reset(s
->fw_state
);
2214 for (i
= 0; i
< s
->fw_cmds
; i
++) {
2215 cmd
= &s
->frames
[i
];
2216 megasas_abort_command(cmd
);
2218 if (s
->fw_state
== MFI_FWSTATE_READY
) {
2222 * The EFI firmware doesn't handle UA,
2223 * so we need to clear the Power On/Reset UA
2224 * after the initial reset.
2226 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
2227 SCSIDevice
*sdev
= SCSI_DEVICE(kid
->child
);
2229 sdev
->unit_attention
= SENSE_CODE(NO_SENSE
);
2230 scsi_device_unit_attention_reported(sdev
);
2233 megasas_reset_frames(s
);
2234 s
->reply_queue_len
= s
->fw_cmds
;
2235 s
->reply_queue_pa
= 0;
2238 s
->fw_state
= MFI_FWSTATE_READY
;
2240 s
->intr_mask
= MEGASAS_INTR_DISABLED_MASK
;
2242 s
->flags
&= ~MEGASAS_MASK_USE_QUEUE64
;
2244 s
->boot_event
= s
->event_count
;
2247 static void megasas_scsi_reset(DeviceState
*dev
)
2249 MegasasState
*s
= MEGASAS(dev
);
2251 megasas_soft_reset(s
);
2254 static const VMStateDescription vmstate_megasas_gen1
= {
2257 .minimum_version_id
= 0,
2258 .fields
= (VMStateField
[]) {
2259 VMSTATE_PCI_DEVICE(parent_obj
, MegasasState
),
2260 VMSTATE_MSIX(parent_obj
, MegasasState
),
2262 VMSTATE_UINT32(fw_state
, MegasasState
),
2263 VMSTATE_UINT32(intr_mask
, MegasasState
),
2264 VMSTATE_UINT32(doorbell
, MegasasState
),
2265 VMSTATE_UINT64(reply_queue_pa
, MegasasState
),
2266 VMSTATE_UINT64(consumer_pa
, MegasasState
),
2267 VMSTATE_UINT64(producer_pa
, MegasasState
),
2268 VMSTATE_END_OF_LIST()
2272 static const VMStateDescription vmstate_megasas_gen2
= {
2273 .name
= "megasas-gen2",
2275 .minimum_version_id
= 0,
2276 .minimum_version_id_old
= 0,
2277 .fields
= (VMStateField
[]) {
2278 VMSTATE_PCI_DEVICE(parent_obj
, MegasasState
),
2279 VMSTATE_MSIX(parent_obj
, MegasasState
),
2281 VMSTATE_UINT32(fw_state
, MegasasState
),
2282 VMSTATE_UINT32(intr_mask
, MegasasState
),
2283 VMSTATE_UINT32(doorbell
, MegasasState
),
2284 VMSTATE_UINT64(reply_queue_pa
, MegasasState
),
2285 VMSTATE_UINT64(consumer_pa
, MegasasState
),
2286 VMSTATE_UINT64(producer_pa
, MegasasState
),
2287 VMSTATE_END_OF_LIST()
2291 static void megasas_scsi_uninit(PCIDevice
*d
)
2293 MegasasState
*s
= MEGASAS(d
);
2295 if (megasas_use_msix(s
)) {
2296 msix_uninit(d
, &s
->mmio_io
, &s
->mmio_io
);
2301 static const struct SCSIBusInfo megasas_scsi_info
= {
2303 .max_target
= MFI_MAX_LD
,
2306 .transfer_data
= megasas_xfer_complete
,
2307 .get_sg_list
= megasas_get_sg_list
,
2308 .complete
= megasas_command_complete
,
2309 .cancel
= megasas_command_cancelled
,
2312 static void megasas_scsi_realize(PCIDevice
*dev
, Error
**errp
)
2314 MegasasState
*s
= MEGASAS(dev
);
2315 MegasasBaseClass
*b
= MEGASAS_DEVICE_GET_CLASS(s
);
2321 pci_conf
= dev
->config
;
2323 /* PCI latency timer = 0 */
2324 pci_conf
[PCI_LATENCY_TIMER
] = 0;
2325 /* Interrupt pin 1 */
2326 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
2328 if (s
->msi
!= ON_OFF_AUTO_OFF
) {
2329 ret
= msi_init(dev
, 0x50, 1, true, false, &err
);
2330 /* Any error other than -ENOTSUP(board's MSI support is broken)
2331 * is a programming error */
2332 assert(!ret
|| ret
== -ENOTSUP
);
2333 if (ret
&& s
->msi
== ON_OFF_AUTO_ON
) {
2334 /* Can't satisfy user's explicit msi=on request, fail */
2335 error_append_hint(&err
, "You have to use msi=auto (default) or "
2336 "msi=off with this machine type.\n");
2337 error_propagate(errp
, err
);
2340 /* With msi=auto, we fall back to MSI off silently */
2341 s
->msi
= ON_OFF_AUTO_OFF
;
2346 memory_region_init_io(&s
->mmio_io
, OBJECT(s
), &megasas_mmio_ops
, s
,
2347 "megasas-mmio", 0x4000);
2348 memory_region_init_io(&s
->port_io
, OBJECT(s
), &megasas_port_ops
, s
,
2350 memory_region_init_io(&s
->queue_io
, OBJECT(s
), &megasas_queue_ops
, s
,
2351 "megasas-queue", 0x40000);
2353 if (megasas_use_msix(s
) &&
2354 msix_init(dev
, 15, &s
->mmio_io
, b
->mmio_bar
, 0x2000,
2355 &s
->mmio_io
, b
->mmio_bar
, 0x3800, 0x68, NULL
)) {
2356 /* TODO: check msix_init's error, and should fail on msix=on */
2357 s
->msix
= ON_OFF_AUTO_OFF
;
2360 if (pci_is_express(dev
)) {
2361 pcie_endpoint_cap_init(dev
, 0xa0);
2364 bar_type
= PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
;
2365 pci_register_bar(dev
, b
->ioport_bar
,
2366 PCI_BASE_ADDRESS_SPACE_IO
, &s
->port_io
);
2367 pci_register_bar(dev
, b
->mmio_bar
, bar_type
, &s
->mmio_io
);
2368 pci_register_bar(dev
, 3, bar_type
, &s
->queue_io
);
2370 if (megasas_use_msix(s
)) {
2371 msix_vector_use(dev
, 0);
2374 s
->fw_state
= MFI_FWSTATE_READY
;
2376 s
->sas_addr
= ((NAA_LOCALLY_ASSIGNED_ID
<< 24) |
2377 IEEE_COMPANY_LOCALLY_ASSIGNED
) << 36;
2378 s
->sas_addr
|= (pci_dev_bus_num(dev
) << 16);
2379 s
->sas_addr
|= (PCI_SLOT(dev
->devfn
) << 8);
2380 s
->sas_addr
|= PCI_FUNC(dev
->devfn
);
2382 if (!s
->hba_serial
) {
2383 s
->hba_serial
= g_strdup(MEGASAS_HBA_SERIAL
);
2385 if (s
->fw_sge
>= MEGASAS_MAX_SGE
- MFI_PASS_FRAME_SIZE
) {
2386 s
->fw_sge
= MEGASAS_MAX_SGE
- MFI_PASS_FRAME_SIZE
;
2387 } else if (s
->fw_sge
>= 128 - MFI_PASS_FRAME_SIZE
) {
2388 s
->fw_sge
= 128 - MFI_PASS_FRAME_SIZE
;
2390 s
->fw_sge
= 64 - MFI_PASS_FRAME_SIZE
;
2392 if (s
->fw_cmds
> MEGASAS_MAX_FRAMES
) {
2393 s
->fw_cmds
= MEGASAS_MAX_FRAMES
;
2395 trace_megasas_init(s
->fw_sge
, s
->fw_cmds
,
2396 megasas_is_jbod(s
) ? "jbod" : "raid");
2398 if (megasas_is_jbod(s
)) {
2399 s
->fw_luns
= MFI_MAX_SYS_PDS
;
2401 s
->fw_luns
= MFI_MAX_LD
;
2405 for (i
= 0; i
< s
->fw_cmds
; i
++) {
2406 s
->frames
[i
].index
= i
;
2407 s
->frames
[i
].context
= -1;
2408 s
->frames
[i
].pa
= 0;
2409 s
->frames
[i
].state
= s
;
2412 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
2413 &megasas_scsi_info
, NULL
);
2416 static Property megasas_properties_gen1
[] = {
2417 DEFINE_PROP_UINT32("max_sge", MegasasState
, fw_sge
,
2418 MEGASAS_DEFAULT_SGE
),
2419 DEFINE_PROP_UINT32("max_cmds", MegasasState
, fw_cmds
,
2420 MEGASAS_DEFAULT_FRAMES
),
2421 DEFINE_PROP_STRING("hba_serial", MegasasState
, hba_serial
),
2422 DEFINE_PROP_UINT64("sas_address", MegasasState
, sas_addr
, 0),
2423 DEFINE_PROP_ON_OFF_AUTO("msi", MegasasState
, msi
, ON_OFF_AUTO_AUTO
),
2424 DEFINE_PROP_ON_OFF_AUTO("msix", MegasasState
, msix
, ON_OFF_AUTO_AUTO
),
2425 DEFINE_PROP_BIT("use_jbod", MegasasState
, flags
,
2426 MEGASAS_FLAG_USE_JBOD
, false),
2427 DEFINE_PROP_END_OF_LIST(),
2430 static Property megasas_properties_gen2
[] = {
2431 DEFINE_PROP_UINT32("max_sge", MegasasState
, fw_sge
,
2432 MEGASAS_DEFAULT_SGE
),
2433 DEFINE_PROP_UINT32("max_cmds", MegasasState
, fw_cmds
,
2434 MEGASAS_GEN2_DEFAULT_FRAMES
),
2435 DEFINE_PROP_STRING("hba_serial", MegasasState
, hba_serial
),
2436 DEFINE_PROP_UINT64("sas_address", MegasasState
, sas_addr
, 0),
2437 DEFINE_PROP_ON_OFF_AUTO("msi", MegasasState
, msi
, ON_OFF_AUTO_AUTO
),
2438 DEFINE_PROP_ON_OFF_AUTO("msix", MegasasState
, msix
, ON_OFF_AUTO_AUTO
),
2439 DEFINE_PROP_BIT("use_jbod", MegasasState
, flags
,
2440 MEGASAS_FLAG_USE_JBOD
, false),
2441 DEFINE_PROP_END_OF_LIST(),
2444 typedef struct MegasasInfo
{
2447 const char *product_name
;
2448 const char *product_version
;
2450 uint16_t subsystem_id
;
2454 const VMStateDescription
*vmsd
;
2456 InterfaceInfo
*interfaces
;
2459 static struct MegasasInfo megasas_devices
[] = {
2461 .name
= TYPE_MEGASAS_GEN1
,
2462 .desc
= "LSI MegaRAID SAS 1078",
2463 .product_name
= "LSI MegaRAID SAS 8708EM2",
2464 .product_version
= MEGASAS_VERSION_GEN1
,
2465 .device_id
= PCI_DEVICE_ID_LSI_SAS1078
,
2466 .subsystem_id
= 0x1013,
2469 .osts
= MFI_1078_RM
| 1,
2470 .vmsd
= &vmstate_megasas_gen1
,
2471 .props
= megasas_properties_gen1
,
2472 .interfaces
= (InterfaceInfo
[]) {
2473 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2477 .name
= TYPE_MEGASAS_GEN2
,
2478 .desc
= "LSI MegaRAID SAS 2108",
2479 .product_name
= "LSI MegaRAID SAS 9260-8i",
2480 .product_version
= MEGASAS_VERSION_GEN2
,
2481 .device_id
= PCI_DEVICE_ID_LSI_SAS0079
,
2482 .subsystem_id
= 0x9261,
2485 .osts
= MFI_GEN2_RM
,
2486 .vmsd
= &vmstate_megasas_gen2
,
2487 .props
= megasas_properties_gen2
,
2488 .interfaces
= (InterfaceInfo
[]) {
2489 { INTERFACE_PCIE_DEVICE
},
2495 static void megasas_class_init(ObjectClass
*oc
, void *data
)
2497 DeviceClass
*dc
= DEVICE_CLASS(oc
);
2498 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
2499 MegasasBaseClass
*e
= MEGASAS_DEVICE_CLASS(oc
);
2500 const MegasasInfo
*info
= data
;
2502 pc
->realize
= megasas_scsi_realize
;
2503 pc
->exit
= megasas_scsi_uninit
;
2504 pc
->vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2505 pc
->device_id
= info
->device_id
;
2506 pc
->subsystem_vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2507 pc
->subsystem_id
= info
->subsystem_id
;
2508 pc
->class_id
= PCI_CLASS_STORAGE_RAID
;
2509 e
->mmio_bar
= info
->mmio_bar
;
2510 e
->ioport_bar
= info
->ioport_bar
;
2511 e
->osts
= info
->osts
;
2512 e
->product_name
= info
->product_name
;
2513 e
->product_version
= info
->product_version
;
2514 device_class_set_props(dc
, info
->props
);
2515 dc
->reset
= megasas_scsi_reset
;
2516 dc
->vmsd
= info
->vmsd
;
2517 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2518 dc
->desc
= info
->desc
;
2521 static const TypeInfo megasas_info
= {
2522 .name
= TYPE_MEGASAS_BASE
,
2523 .parent
= TYPE_PCI_DEVICE
,
2524 .instance_size
= sizeof(MegasasState
),
2525 .class_size
= sizeof(MegasasBaseClass
),
2529 static void megasas_register_types(void)
2533 type_register_static(&megasas_info
);
2534 for (i
= 0; i
< ARRAY_SIZE(megasas_devices
); i
++) {
2535 const MegasasInfo
*info
= &megasas_devices
[i
];
2536 TypeInfo type_info
= {};
2538 type_info
.name
= info
->name
;
2539 type_info
.parent
= TYPE_MEGASAS_BASE
;
2540 type_info
.class_data
= (void *)info
;
2541 type_info
.class_init
= megasas_class_init
;
2542 type_info
.interfaces
= info
->interfaces
;
2544 type_register(&type_info
);
2548 type_init(megasas_register_types
)