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/>.
22 #include "hw/pci/pci.h"
23 #include "sysemu/dma.h"
24 #include "hw/pci/msi.h"
25 #include "hw/pci/msix.h"
27 #include "hw/scsi/scsi.h"
28 #include "block/scsi.h"
33 #define MEGASAS_VERSION "1.70"
34 #define MEGASAS_MAX_FRAMES 2048 /* Firmware limit at 65535 */
35 #define MEGASAS_DEFAULT_FRAMES 1000 /* Windows requires this */
36 #define MEGASAS_MAX_SGE 128 /* Firmware limit */
37 #define MEGASAS_DEFAULT_SGE 80
38 #define MEGASAS_MAX_SECTORS 0xFFFF /* No real limit */
39 #define MEGASAS_MAX_ARRAYS 128
41 #define MEGASAS_HBA_SERIAL "QEMU123456"
42 #define NAA_LOCALLY_ASSIGNED_ID 0x3ULL
43 #define IEEE_COMPANY_LOCALLY_ASSIGNED 0x525400
45 #define MEGASAS_FLAG_USE_JBOD 0
46 #define MEGASAS_MASK_USE_JBOD (1 << MEGASAS_FLAG_USE_JBOD)
47 #define MEGASAS_FLAG_USE_MSI 1
48 #define MEGASAS_MASK_USE_MSI (1 << MEGASAS_FLAG_USE_MSI)
49 #define MEGASAS_FLAG_USE_MSIX 2
50 #define MEGASAS_MASK_USE_MSIX (1 << MEGASAS_FLAG_USE_MSIX)
51 #define MEGASAS_FLAG_USE_QUEUE64 3
52 #define MEGASAS_MASK_USE_QUEUE64 (1 << MEGASAS_FLAG_USE_QUEUE64)
54 static const char *mfi_frame_desc
[] = {
55 "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
56 "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
58 typedef struct MegasasCmd
{
66 union mfi_frame
*frame
;
72 struct MegasasState
*state
;
75 typedef struct MegasasState
{
82 MemoryRegion queue_io
;
94 MegasasCmd
*event_cmd
;
104 uint64_t reply_queue_pa
;
107 int reply_queue_head
;
108 int reply_queue_tail
;
109 uint64_t consumer_pa
;
110 uint64_t producer_pa
;
112 MegasasCmd frames
[MEGASAS_MAX_FRAMES
];
117 #define TYPE_MEGASAS "megasas"
119 #define MEGASAS(obj) \
120 OBJECT_CHECK(MegasasState, (obj), TYPE_MEGASAS)
122 #define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
124 static bool megasas_intr_enabled(MegasasState
*s
)
126 if ((s
->intr_mask
& MEGASAS_INTR_DISABLED_MASK
) !=
127 MEGASAS_INTR_DISABLED_MASK
) {
133 static bool megasas_use_queue64(MegasasState
*s
)
135 return s
->flags
& MEGASAS_MASK_USE_QUEUE64
;
138 static bool megasas_use_msi(MegasasState
*s
)
140 return s
->flags
& MEGASAS_MASK_USE_MSI
;
143 static bool megasas_use_msix(MegasasState
*s
)
145 return s
->flags
& MEGASAS_MASK_USE_MSIX
;
148 static bool megasas_is_jbod(MegasasState
*s
)
150 return s
->flags
& MEGASAS_MASK_USE_JBOD
;
153 static void megasas_frame_set_cmd_status(unsigned long frame
, uint8_t v
)
155 stb_phys(&address_space_memory
,
156 frame
+ offsetof(struct mfi_frame_header
, cmd_status
), v
);
159 static void megasas_frame_set_scsi_status(unsigned long frame
, uint8_t v
)
161 stb_phys(&address_space_memory
,
162 frame
+ offsetof(struct mfi_frame_header
, scsi_status
), v
);
166 * Context is considered opaque, but the HBA firmware is running
167 * in little endian mode. So convert it to little endian, too.
169 static uint64_t megasas_frame_get_context(unsigned long frame
)
171 return ldq_le_phys(&address_space_memory
,
172 frame
+ offsetof(struct mfi_frame_header
, context
));
175 static bool megasas_frame_is_ieee_sgl(MegasasCmd
*cmd
)
177 return cmd
->flags
& MFI_FRAME_IEEE_SGL
;
180 static bool megasas_frame_is_sgl64(MegasasCmd
*cmd
)
182 return cmd
->flags
& MFI_FRAME_SGL64
;
185 static bool megasas_frame_is_sense64(MegasasCmd
*cmd
)
187 return cmd
->flags
& MFI_FRAME_SENSE64
;
190 static uint64_t megasas_sgl_get_addr(MegasasCmd
*cmd
,
195 if (megasas_frame_is_ieee_sgl(cmd
)) {
196 addr
= le64_to_cpu(sgl
->sg_skinny
->addr
);
197 } else if (megasas_frame_is_sgl64(cmd
)) {
198 addr
= le64_to_cpu(sgl
->sg64
->addr
);
200 addr
= le32_to_cpu(sgl
->sg32
->addr
);
205 static uint32_t megasas_sgl_get_len(MegasasCmd
*cmd
,
210 if (megasas_frame_is_ieee_sgl(cmd
)) {
211 len
= le32_to_cpu(sgl
->sg_skinny
->len
);
212 } else if (megasas_frame_is_sgl64(cmd
)) {
213 len
= le32_to_cpu(sgl
->sg64
->len
);
215 len
= le32_to_cpu(sgl
->sg32
->len
);
220 static union mfi_sgl
*megasas_sgl_next(MegasasCmd
*cmd
,
223 uint8_t *next
= (uint8_t *)sgl
;
225 if (megasas_frame_is_ieee_sgl(cmd
)) {
226 next
+= sizeof(struct mfi_sg_skinny
);
227 } else if (megasas_frame_is_sgl64(cmd
)) {
228 next
+= sizeof(struct mfi_sg64
);
230 next
+= sizeof(struct mfi_sg32
);
233 if (next
>= (uint8_t *)cmd
->frame
+ cmd
->pa_size
) {
236 return (union mfi_sgl
*)next
;
239 static void megasas_soft_reset(MegasasState
*s
);
241 static int megasas_map_sgl(MegasasState
*s
, MegasasCmd
*cmd
, union mfi_sgl
*sgl
)
247 cmd
->flags
= le16_to_cpu(cmd
->frame
->header
.flags
);
248 iov_count
= cmd
->frame
->header
.sge_count
;
249 if (iov_count
> MEGASAS_MAX_SGE
) {
250 trace_megasas_iovec_sgl_overflow(cmd
->index
, iov_count
,
254 pci_dma_sglist_init(&cmd
->qsg
, PCI_DEVICE(s
), iov_count
);
255 for (i
= 0; i
< iov_count
; i
++) {
256 dma_addr_t iov_pa
, iov_size_p
;
259 trace_megasas_iovec_sgl_underflow(cmd
->index
, i
);
262 iov_pa
= megasas_sgl_get_addr(cmd
, sgl
);
263 iov_size_p
= megasas_sgl_get_len(cmd
, sgl
);
264 if (!iov_pa
|| !iov_size_p
) {
265 trace_megasas_iovec_sgl_invalid(cmd
->index
, i
,
269 qemu_sglist_add(&cmd
->qsg
, iov_pa
, iov_size_p
);
270 sgl
= megasas_sgl_next(cmd
, sgl
);
271 iov_size
+= (size_t)iov_size_p
;
273 if (cmd
->iov_size
> iov_size
) {
274 trace_megasas_iovec_overflow(cmd
->index
, iov_size
, cmd
->iov_size
);
275 } else if (cmd
->iov_size
< iov_size
) {
276 trace_megasas_iovec_underflow(cmd
->iov_size
, iov_size
, cmd
->iov_size
);
281 qemu_sglist_destroy(&cmd
->qsg
);
282 return iov_count
- i
;
285 static void megasas_unmap_sgl(MegasasCmd
*cmd
)
287 qemu_sglist_destroy(&cmd
->qsg
);
292 * passthrough sense and io sense are at the same offset
294 static int megasas_build_sense(MegasasCmd
*cmd
, uint8_t *sense_ptr
,
297 PCIDevice
*pcid
= PCI_DEVICE(cmd
->state
);
298 uint32_t pa_hi
= 0, pa_lo
;
301 if (sense_len
> cmd
->frame
->header
.sense_len
) {
302 sense_len
= cmd
->frame
->header
.sense_len
;
305 pa_lo
= le32_to_cpu(cmd
->frame
->pass
.sense_addr_lo
);
306 if (megasas_frame_is_sense64(cmd
)) {
307 pa_hi
= le32_to_cpu(cmd
->frame
->pass
.sense_addr_hi
);
309 pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
310 pci_dma_write(pcid
, pa
, sense_ptr
, sense_len
);
311 cmd
->frame
->header
.sense_len
= sense_len
;
316 static void megasas_write_sense(MegasasCmd
*cmd
, SCSISense sense
)
318 uint8_t sense_buf
[SCSI_SENSE_BUF_SIZE
];
319 uint8_t sense_len
= 18;
321 memset(sense_buf
, 0, sense_len
);
323 sense_buf
[2] = sense
.key
;
325 sense_buf
[12] = sense
.asc
;
326 sense_buf
[13] = sense
.ascq
;
327 megasas_build_sense(cmd
, sense_buf
, sense_len
);
330 static void megasas_copy_sense(MegasasCmd
*cmd
)
332 uint8_t sense_buf
[SCSI_SENSE_BUF_SIZE
];
335 sense_len
= scsi_req_get_sense(cmd
->req
, sense_buf
,
336 SCSI_SENSE_BUF_SIZE
);
337 megasas_build_sense(cmd
, sense_buf
, sense_len
);
341 * Format an INQUIRY CDB
343 static int megasas_setup_inquiry(uint8_t *cdb
, int pg
, int len
)
351 cdb
[3] = (len
>> 8) & 0xff;
352 cdb
[4] = (len
& 0xff);
357 * Encode lba and len into a READ_16/WRITE_16 CDB
359 static void megasas_encode_lba(uint8_t *cdb
, uint64_t lba
,
360 uint32_t len
, bool is_write
)
362 memset(cdb
, 0x0, 16);
368 cdb
[2] = (lba
>> 56) & 0xff;
369 cdb
[3] = (lba
>> 48) & 0xff;
370 cdb
[4] = (lba
>> 40) & 0xff;
371 cdb
[5] = (lba
>> 32) & 0xff;
372 cdb
[6] = (lba
>> 24) & 0xff;
373 cdb
[7] = (lba
>> 16) & 0xff;
374 cdb
[8] = (lba
>> 8) & 0xff;
375 cdb
[9] = (lba
) & 0xff;
376 cdb
[10] = (len
>> 24) & 0xff;
377 cdb
[11] = (len
>> 16) & 0xff;
378 cdb
[12] = (len
>> 8) & 0xff;
379 cdb
[13] = (len
) & 0xff;
385 static uint64_t megasas_fw_time(void)
390 qemu_get_timedate(&curtime
, 0);
391 bcd_time
= ((uint64_t)curtime
.tm_sec
& 0xff) << 48 |
392 ((uint64_t)curtime
.tm_min
& 0xff) << 40 |
393 ((uint64_t)curtime
.tm_hour
& 0xff) << 32 |
394 ((uint64_t)curtime
.tm_mday
& 0xff) << 24 |
395 ((uint64_t)curtime
.tm_mon
& 0xff) << 16 |
396 ((uint64_t)(curtime
.tm_year
+ 1900) & 0xffff);
402 * Default disk sata address
403 * 0x1221 is the magic number as
404 * present in real hardware,
405 * so use it here, too.
407 static uint64_t megasas_get_sata_addr(uint16_t id
)
409 uint64_t addr
= (0x1221ULL
<< 48);
410 return addr
& (id
<< 24);
416 static int megasas_next_index(MegasasState
*s
, int index
, int limit
)
419 if (index
== limit
) {
425 static MegasasCmd
*megasas_lookup_frame(MegasasState
*s
,
428 MegasasCmd
*cmd
= NULL
;
431 index
= s
->reply_queue_head
;
433 while (num
< s
->fw_cmds
) {
434 if (s
->frames
[index
].pa
&& s
->frames
[index
].pa
== frame
) {
435 cmd
= &s
->frames
[index
];
438 index
= megasas_next_index(s
, index
, s
->fw_cmds
);
445 static MegasasCmd
*megasas_next_frame(MegasasState
*s
,
448 MegasasCmd
*cmd
= NULL
;
451 cmd
= megasas_lookup_frame(s
, frame
);
453 trace_megasas_qf_found(cmd
->index
, cmd
->pa
);
456 index
= s
->reply_queue_head
;
458 while (num
< s
->fw_cmds
) {
459 if (!s
->frames
[index
].pa
) {
460 cmd
= &s
->frames
[index
];
463 index
= megasas_next_index(s
, index
, s
->fw_cmds
);
467 trace_megasas_qf_failed(frame
);
469 trace_megasas_qf_new(index
, cmd
);
473 static MegasasCmd
*megasas_enqueue_frame(MegasasState
*s
,
474 hwaddr frame
, uint64_t context
, int count
)
476 PCIDevice
*pcid
= PCI_DEVICE(s
);
477 MegasasCmd
*cmd
= NULL
;
478 int frame_size
= MFI_FRAME_SIZE
* 16;
479 hwaddr frame_size_p
= frame_size
;
481 cmd
= megasas_next_frame(s
, frame
);
482 /* All frames busy */
488 /* Map all possible frames */
489 cmd
->frame
= pci_dma_map(pcid
, frame
, &frame_size_p
, 0);
490 if (frame_size_p
!= frame_size
) {
491 trace_megasas_qf_map_failed(cmd
->index
, (unsigned long)frame
);
493 pci_dma_unmap(pcid
, cmd
->frame
, frame_size_p
, 0, 0);
500 cmd
->pa_size
= frame_size_p
;
501 cmd
->context
= context
;
502 if (!megasas_use_queue64(s
)) {
503 cmd
->context
&= (uint64_t)0xFFFFFFFF;
509 trace_megasas_qf_enqueue(cmd
->index
, cmd
->count
, cmd
->context
,
510 s
->reply_queue_head
, s
->busy
);
515 static void megasas_complete_frame(MegasasState
*s
, uint64_t context
)
517 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
518 int tail
, queue_offset
;
520 /* Decrement busy count */
523 if (s
->reply_queue_pa
) {
525 * Put command on the reply queue.
526 * Context is opaque, but emulation is running in
527 * little endian. So convert it.
529 tail
= s
->reply_queue_head
;
530 if (megasas_use_queue64(s
)) {
531 queue_offset
= tail
* sizeof(uint64_t);
532 stq_le_phys(&address_space_memory
,
533 s
->reply_queue_pa
+ queue_offset
, context
);
535 queue_offset
= tail
* sizeof(uint32_t);
536 stl_le_phys(&address_space_memory
,
537 s
->reply_queue_pa
+ queue_offset
, context
);
539 s
->reply_queue_head
= megasas_next_index(s
, tail
, s
->fw_cmds
);
540 trace_megasas_qf_complete(context
, tail
, queue_offset
,
541 s
->busy
, s
->doorbell
);
544 if (megasas_intr_enabled(s
)) {
547 if (s
->doorbell
== 1) {
548 if (msix_enabled(pci_dev
)) {
549 trace_megasas_msix_raise(0);
550 msix_notify(pci_dev
, 0);
551 } else if (msi_enabled(pci_dev
)) {
552 trace_megasas_msi_raise(0);
553 msi_notify(pci_dev
, 0);
555 trace_megasas_irq_raise();
556 pci_irq_assert(pci_dev
);
560 trace_megasas_qf_complete_noirq(context
);
564 static void megasas_reset_frames(MegasasState
*s
)
566 PCIDevice
*pcid
= PCI_DEVICE(s
);
570 for (i
= 0; i
< s
->fw_cmds
; i
++) {
573 pci_dma_unmap(pcid
, cmd
->frame
, cmd
->pa_size
, 0, 0);
580 static void megasas_abort_command(MegasasCmd
*cmd
)
583 scsi_req_cancel(cmd
->req
);
588 static int megasas_init_firmware(MegasasState
*s
, MegasasCmd
*cmd
)
590 PCIDevice
*pcid
= PCI_DEVICE(s
);
591 uint32_t pa_hi
, pa_lo
;
592 hwaddr iq_pa
, initq_size
;
593 struct mfi_init_qinfo
*initq
;
595 int ret
= MFI_STAT_OK
;
597 pa_lo
= le32_to_cpu(cmd
->frame
->init
.qinfo_new_addr_lo
);
598 pa_hi
= le32_to_cpu(cmd
->frame
->init
.qinfo_new_addr_hi
);
599 iq_pa
= (((uint64_t) pa_hi
<< 32) | pa_lo
);
600 trace_megasas_init_firmware((uint64_t)iq_pa
);
601 initq_size
= sizeof(*initq
);
602 initq
= pci_dma_map(pcid
, iq_pa
, &initq_size
, 0);
603 if (!initq
|| initq_size
!= sizeof(*initq
)) {
604 trace_megasas_initq_map_failed(cmd
->index
);
606 ret
= MFI_STAT_MEMORY_NOT_AVAILABLE
;
609 s
->reply_queue_len
= le32_to_cpu(initq
->rq_entries
) & 0xFFFF;
610 if (s
->reply_queue_len
> s
->fw_cmds
) {
611 trace_megasas_initq_mismatch(s
->reply_queue_len
, s
->fw_cmds
);
613 ret
= MFI_STAT_INVALID_PARAMETER
;
616 pa_lo
= le32_to_cpu(initq
->rq_addr_lo
);
617 pa_hi
= le32_to_cpu(initq
->rq_addr_hi
);
618 s
->reply_queue_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
619 pa_lo
= le32_to_cpu(initq
->ci_addr_lo
);
620 pa_hi
= le32_to_cpu(initq
->ci_addr_hi
);
621 s
->consumer_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
622 pa_lo
= le32_to_cpu(initq
->pi_addr_lo
);
623 pa_hi
= le32_to_cpu(initq
->pi_addr_hi
);
624 s
->producer_pa
= ((uint64_t) pa_hi
<< 32) | pa_lo
;
625 s
->reply_queue_head
= ldl_le_phys(&address_space_memory
, s
->producer_pa
);
626 s
->reply_queue_tail
= ldl_le_phys(&address_space_memory
, s
->consumer_pa
);
627 flags
= le32_to_cpu(initq
->flags
);
628 if (flags
& MFI_QUEUE_FLAG_CONTEXT64
) {
629 s
->flags
|= MEGASAS_MASK_USE_QUEUE64
;
631 trace_megasas_init_queue((unsigned long)s
->reply_queue_pa
,
632 s
->reply_queue_len
, s
->reply_queue_head
,
633 s
->reply_queue_tail
, flags
);
634 megasas_reset_frames(s
);
635 s
->fw_state
= MFI_FWSTATE_OPERATIONAL
;
638 pci_dma_unmap(pcid
, initq
, initq_size
, 0, 0);
643 static int megasas_map_dcmd(MegasasState
*s
, MegasasCmd
*cmd
)
645 dma_addr_t iov_pa
, iov_size
;
647 cmd
->flags
= le16_to_cpu(cmd
->frame
->header
.flags
);
648 if (!cmd
->frame
->header
.sge_count
) {
649 trace_megasas_dcmd_zero_sge(cmd
->index
);
652 } else if (cmd
->frame
->header
.sge_count
> 1) {
653 trace_megasas_dcmd_invalid_sge(cmd
->index
,
654 cmd
->frame
->header
.sge_count
);
658 iov_pa
= megasas_sgl_get_addr(cmd
, &cmd
->frame
->dcmd
.sgl
);
659 iov_size
= megasas_sgl_get_len(cmd
, &cmd
->frame
->dcmd
.sgl
);
660 pci_dma_sglist_init(&cmd
->qsg
, PCI_DEVICE(s
), 1);
661 qemu_sglist_add(&cmd
->qsg
, iov_pa
, iov_size
);
662 cmd
->iov_size
= iov_size
;
663 return cmd
->iov_size
;
666 static void megasas_finish_dcmd(MegasasCmd
*cmd
, uint32_t iov_size
)
668 trace_megasas_finish_dcmd(cmd
->index
, iov_size
);
670 if (cmd
->frame
->header
.sge_count
) {
671 qemu_sglist_destroy(&cmd
->qsg
);
673 if (iov_size
> cmd
->iov_size
) {
674 if (megasas_frame_is_ieee_sgl(cmd
)) {
675 cmd
->frame
->dcmd
.sgl
.sg_skinny
->len
= cpu_to_le32(iov_size
);
676 } else if (megasas_frame_is_sgl64(cmd
)) {
677 cmd
->frame
->dcmd
.sgl
.sg64
->len
= cpu_to_le32(iov_size
);
679 cmd
->frame
->dcmd
.sgl
.sg32
->len
= cpu_to_le32(iov_size
);
685 static int megasas_ctrl_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
687 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
688 struct mfi_ctrl_info info
;
689 size_t dcmd_size
= sizeof(info
);
691 int num_ld_disks
= 0;
694 memset(&info
, 0x0, sizeof(info
));
695 if (cmd
->iov_size
< dcmd_size
) {
696 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
698 return MFI_STAT_INVALID_PARAMETER
;
701 info
.pci
.vendor
= cpu_to_le16(PCI_VENDOR_ID_LSI_LOGIC
);
702 info
.pci
.device
= cpu_to_le16(PCI_DEVICE_ID_LSI_SAS1078
);
703 info
.pci
.subvendor
= cpu_to_le16(PCI_VENDOR_ID_LSI_LOGIC
);
704 info
.pci
.subdevice
= cpu_to_le16(0x1013);
707 * For some reason the firmware supports
708 * only up to 8 device ports.
709 * Despite supporting a far larger number
710 * of devices for the physical devices.
711 * So just display the first 8 devices
712 * in the device port list, independent
713 * of how many logical devices are actually
716 info
.host
.type
= MFI_INFO_HOST_PCIE
;
717 info
.device
.type
= MFI_INFO_DEV_SAS3G
;
718 info
.device
.port_count
= 8;
719 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
720 SCSIDevice
*sdev
= DO_UPCAST(SCSIDevice
, qdev
, kid
->child
);
722 if (num_ld_disks
< 8) {
723 sdev_id
= ((sdev
->id
& 0xFF) >> 8) | (sdev
->lun
& 0xFF);
724 info
.device
.port_addr
[num_ld_disks
] =
725 cpu_to_le64(megasas_get_sata_addr(sdev_id
));
730 memcpy(info
.product_name
, "MegaRAID SAS 8708EM2", 20);
731 snprintf(info
.serial_number
, 32, "%s", s
->hba_serial
);
732 snprintf(info
.package_version
, 0x60, "%s-QEMU", QEMU_VERSION
);
733 memcpy(info
.image_component
[0].name
, "APP", 3);
734 memcpy(info
.image_component
[0].version
, MEGASAS_VERSION
"-QEMU", 9);
735 memcpy(info
.image_component
[0].build_date
, "Apr 1 2014", 11);
736 memcpy(info
.image_component
[0].build_time
, "12:34:56", 8);
737 info
.image_component_count
= 1;
738 if (pci_dev
->has_rom
) {
742 ptr
= memory_region_get_ram_ptr(&pci_dev
->rom
);
743 memcpy(biosver
, ptr
+ 0x41, 31);
744 memcpy(info
.image_component
[1].name
, "BIOS", 4);
745 memcpy(info
.image_component
[1].version
, biosver
,
746 strlen((const char *)biosver
));
747 info
.image_component_count
++;
749 info
.current_fw_time
= cpu_to_le32(megasas_fw_time());
752 info
.max_arrays
= MEGASAS_MAX_ARRAYS
;
753 info
.max_lds
= s
->fw_luns
;
754 info
.max_cmds
= cpu_to_le16(s
->fw_cmds
);
755 info
.max_sg_elements
= cpu_to_le16(s
->fw_sge
);
756 info
.max_request_size
= cpu_to_le32(MEGASAS_MAX_SECTORS
);
757 info
.lds_present
= cpu_to_le16(num_ld_disks
);
758 info
.pd_present
= cpu_to_le16(num_ld_disks
);
759 info
.pd_disks_present
= cpu_to_le16(num_ld_disks
);
760 info
.hw_present
= cpu_to_le32(MFI_INFO_HW_NVRAM
|
763 info
.memory_size
= cpu_to_le16(512);
764 info
.nvram_size
= cpu_to_le16(32);
765 info
.flash_size
= cpu_to_le16(16);
766 info
.raid_levels
= cpu_to_le32(MFI_INFO_RAID_0
);
767 info
.adapter_ops
= cpu_to_le32(MFI_INFO_AOPS_RBLD_RATE
|
768 MFI_INFO_AOPS_SELF_DIAGNOSTIC
|
769 MFI_INFO_AOPS_MIXED_ARRAY
);
770 info
.ld_ops
= cpu_to_le32(MFI_INFO_LDOPS_DISK_CACHE_POLICY
|
771 MFI_INFO_LDOPS_ACCESS_POLICY
|
772 MFI_INFO_LDOPS_IO_POLICY
|
773 MFI_INFO_LDOPS_WRITE_POLICY
|
774 MFI_INFO_LDOPS_READ_POLICY
);
775 info
.max_strips_per_io
= cpu_to_le16(s
->fw_sge
);
776 info
.stripe_sz_ops
.min
= 3;
777 info
.stripe_sz_ops
.max
= ffs(MEGASAS_MAX_SECTORS
+ 1) - 1;
778 info
.properties
.pred_fail_poll_interval
= cpu_to_le16(300);
779 info
.properties
.intr_throttle_cnt
= cpu_to_le16(16);
780 info
.properties
.intr_throttle_timeout
= cpu_to_le16(50);
781 info
.properties
.rebuild_rate
= 30;
782 info
.properties
.patrol_read_rate
= 30;
783 info
.properties
.bgi_rate
= 30;
784 info
.properties
.cc_rate
= 30;
785 info
.properties
.recon_rate
= 30;
786 info
.properties
.cache_flush_interval
= 4;
787 info
.properties
.spinup_drv_cnt
= 2;
788 info
.properties
.spinup_delay
= 6;
789 info
.properties
.ecc_bucket_size
= 15;
790 info
.properties
.ecc_bucket_leak_rate
= cpu_to_le16(1440);
791 info
.properties
.expose_encl_devices
= 1;
792 info
.properties
.OnOffProperties
= cpu_to_le32(MFI_CTRL_PROP_EnableJBOD
);
793 info
.pd_ops
= cpu_to_le32(MFI_INFO_PDOPS_FORCE_ONLINE
|
794 MFI_INFO_PDOPS_FORCE_OFFLINE
);
795 info
.pd_mix_support
= cpu_to_le32(MFI_INFO_PDMIX_SAS
|
796 MFI_INFO_PDMIX_SATA
|
799 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
803 static int megasas_mfc_get_defaults(MegasasState
*s
, MegasasCmd
*cmd
)
805 struct mfi_defaults info
;
806 size_t dcmd_size
= sizeof(struct mfi_defaults
);
808 memset(&info
, 0x0, dcmd_size
);
809 if (cmd
->iov_size
< dcmd_size
) {
810 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
812 return MFI_STAT_INVALID_PARAMETER
;
815 info
.sas_addr
= cpu_to_le64(s
->sas_addr
);
816 info
.stripe_size
= 3;
818 info
.background_rate
= 30;
819 info
.allow_mix_in_enclosure
= 1;
820 info
.allow_mix_in_ld
= 1;
821 info
.direct_pd_mapping
= 1;
822 /* Enable for BIOS support */
823 info
.bios_enumerate_lds
= 1;
824 info
.disable_ctrl_r
= 1;
825 info
.expose_enclosure_devices
= 1;
826 info
.disable_preboot_cli
= 1;
827 info
.cluster_disable
= 1;
829 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
833 static int megasas_dcmd_get_bios_info(MegasasState
*s
, MegasasCmd
*cmd
)
835 struct mfi_bios_data info
;
836 size_t dcmd_size
= sizeof(info
);
838 memset(&info
, 0x0, dcmd_size
);
839 if (cmd
->iov_size
< dcmd_size
) {
840 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
842 return MFI_STAT_INVALID_PARAMETER
;
844 info
.continue_on_error
= 1;
846 if (megasas_is_jbod(s
)) {
847 info
.expose_all_drives
= 1;
850 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
854 static int megasas_dcmd_get_fw_time(MegasasState
*s
, MegasasCmd
*cmd
)
857 size_t dcmd_size
= sizeof(fw_time
);
859 fw_time
= cpu_to_le64(megasas_fw_time());
861 cmd
->iov_size
-= dma_buf_read((uint8_t *)&fw_time
, dcmd_size
, &cmd
->qsg
);
865 static int megasas_dcmd_set_fw_time(MegasasState
*s
, MegasasCmd
*cmd
)
869 /* This is a dummy; setting of firmware time is not allowed */
870 memcpy(&fw_time
, cmd
->frame
->dcmd
.mbox
, sizeof(fw_time
));
872 trace_megasas_dcmd_set_fw_time(cmd
->index
, fw_time
);
873 fw_time
= cpu_to_le64(megasas_fw_time());
877 static int megasas_event_info(MegasasState
*s
, MegasasCmd
*cmd
)
879 struct mfi_evt_log_state info
;
880 size_t dcmd_size
= sizeof(info
);
882 memset(&info
, 0, dcmd_size
);
884 info
.newest_seq_num
= cpu_to_le32(s
->event_count
);
885 info
.shutdown_seq_num
= cpu_to_le32(s
->shutdown_event
);
886 info
.boot_seq_num
= cpu_to_le32(s
->boot_event
);
888 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
892 static int megasas_event_wait(MegasasState
*s
, MegasasCmd
*cmd
)
896 if (cmd
->iov_size
< sizeof(struct mfi_evt_detail
)) {
897 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
898 sizeof(struct mfi_evt_detail
));
899 return MFI_STAT_INVALID_PARAMETER
;
901 s
->event_count
= cpu_to_le32(cmd
->frame
->dcmd
.mbox
[0]);
902 event
.word
= cpu_to_le32(cmd
->frame
->dcmd
.mbox
[4]);
903 s
->event_locale
= event
.members
.locale
;
904 s
->event_class
= event
.members
.class;
906 /* Decrease busy count; event frame doesn't count here */
908 cmd
->iov_size
= sizeof(struct mfi_evt_detail
);
909 return MFI_STAT_INVALID_STATUS
;
912 static int megasas_dcmd_pd_get_list(MegasasState
*s
, MegasasCmd
*cmd
)
914 struct mfi_pd_list info
;
915 size_t dcmd_size
= sizeof(info
);
917 uint32_t offset
, dcmd_limit
, num_pd_disks
= 0, max_pd_disks
;
920 memset(&info
, 0, dcmd_size
);
922 dcmd_limit
= offset
+ sizeof(struct mfi_pd_address
);
923 if (cmd
->iov_size
< dcmd_limit
) {
924 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
926 return MFI_STAT_INVALID_PARAMETER
;
929 max_pd_disks
= (cmd
->iov_size
- offset
) / sizeof(struct mfi_pd_address
);
930 if (max_pd_disks
> s
->fw_luns
) {
931 max_pd_disks
= s
->fw_luns
;
934 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
935 SCSIDevice
*sdev
= DO_UPCAST(SCSIDevice
, qdev
, kid
->child
);
937 sdev_id
= ((sdev
->id
& 0xFF) >> 8) | (sdev
->lun
& 0xFF);
938 info
.addr
[num_pd_disks
].device_id
= cpu_to_le16(sdev_id
);
939 info
.addr
[num_pd_disks
].encl_device_id
= 0xFFFF;
940 info
.addr
[num_pd_disks
].encl_index
= 0;
941 info
.addr
[num_pd_disks
].slot_number
= (sdev
->id
& 0xFF);
942 info
.addr
[num_pd_disks
].scsi_dev_type
= sdev
->type
;
943 info
.addr
[num_pd_disks
].connect_port_bitmap
= 0x1;
944 info
.addr
[num_pd_disks
].sas_addr
[0] =
945 cpu_to_le64(megasas_get_sata_addr(sdev_id
));
947 offset
+= sizeof(struct mfi_pd_address
);
949 trace_megasas_dcmd_pd_get_list(cmd
->index
, num_pd_disks
,
950 max_pd_disks
, offset
);
952 info
.size
= cpu_to_le32(offset
);
953 info
.count
= cpu_to_le32(num_pd_disks
);
955 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, offset
, &cmd
->qsg
);
959 static int megasas_dcmd_pd_list_query(MegasasState
*s
, MegasasCmd
*cmd
)
963 /* mbox0 contains flags */
964 flags
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
965 trace_megasas_dcmd_pd_list_query(cmd
->index
, flags
);
966 if (flags
== MR_PD_QUERY_TYPE_ALL
||
967 megasas_is_jbod(s
)) {
968 return megasas_dcmd_pd_get_list(s
, cmd
);
974 static int megasas_pd_get_info_submit(SCSIDevice
*sdev
, int lun
,
977 struct mfi_pd_info
*info
= cmd
->iov_buf
;
978 size_t dcmd_size
= sizeof(struct mfi_pd_info
);
979 BlockConf
*conf
= &sdev
->conf
;
981 uint16_t sdev_id
= ((sdev
->id
& 0xFF) >> 8) | (lun
& 0xFF);
987 cmd
->iov_buf
= g_malloc(dcmd_size
);
988 memset(cmd
->iov_buf
, 0, dcmd_size
);
990 info
->inquiry_data
[0] = 0x7f; /* Force PQual 0x3, PType 0x1f */
991 info
->vpd_page83
[0] = 0x7f;
992 megasas_setup_inquiry(cmdbuf
, 0, sizeof(info
->inquiry_data
));
993 req
= scsi_req_new(sdev
, cmd
->index
, lun
, cmdbuf
, cmd
);
995 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
996 "PD get info std inquiry");
997 g_free(cmd
->iov_buf
);
999 return MFI_STAT_FLASH_ALLOC_FAIL
;
1001 trace_megasas_dcmd_internal_submit(cmd
->index
,
1002 "PD get info std inquiry", lun
);
1003 len
= scsi_req_enqueue(req
);
1005 cmd
->iov_size
= len
;
1006 scsi_req_continue(req
);
1008 return MFI_STAT_INVALID_STATUS
;
1009 } else if (info
->inquiry_data
[0] != 0x7f && info
->vpd_page83
[0] == 0x7f) {
1010 megasas_setup_inquiry(cmdbuf
, 0x83, sizeof(info
->vpd_page83
));
1011 req
= scsi_req_new(sdev
, cmd
->index
, lun
, cmdbuf
, cmd
);
1013 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
1014 "PD get info vpd inquiry");
1015 return MFI_STAT_FLASH_ALLOC_FAIL
;
1017 trace_megasas_dcmd_internal_submit(cmd
->index
,
1018 "PD get info vpd inquiry", lun
);
1019 len
= scsi_req_enqueue(req
);
1021 cmd
->iov_size
= len
;
1022 scsi_req_continue(req
);
1024 return MFI_STAT_INVALID_STATUS
;
1026 /* Finished, set FW state */
1027 if ((info
->inquiry_data
[0] >> 5) == 0) {
1028 if (megasas_is_jbod(cmd
->state
)) {
1029 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_SYSTEM
);
1031 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_ONLINE
);
1034 info
->fw_state
= cpu_to_le16(MFI_PD_STATE_OFFLINE
);
1037 info
->ref
.v
.device_id
= cpu_to_le16(sdev_id
);
1038 info
->state
.ddf
.pd_type
= cpu_to_le16(MFI_PD_DDF_TYPE_IN_VD
|
1039 MFI_PD_DDF_TYPE_INTF_SAS
);
1040 bdrv_get_geometry(conf
->bs
, &pd_size
);
1041 info
->raw_size
= cpu_to_le64(pd_size
);
1042 info
->non_coerced_size
= cpu_to_le64(pd_size
);
1043 info
->coerced_size
= cpu_to_le64(pd_size
);
1044 info
->encl_device_id
= 0xFFFF;
1045 info
->slot_number
= (sdev
->id
& 0xFF);
1046 info
->path_info
.count
= 1;
1047 info
->path_info
.sas_addr
[0] =
1048 cpu_to_le64(megasas_get_sata_addr(sdev_id
));
1049 info
->connected_port_bitmap
= 0x1;
1050 info
->device_speed
= 1;
1051 info
->link_speed
= 1;
1052 resid
= dma_buf_read(cmd
->iov_buf
, dcmd_size
, &cmd
->qsg
);
1053 g_free(cmd
->iov_buf
);
1054 cmd
->iov_size
= dcmd_size
- resid
;
1055 cmd
->iov_buf
= NULL
;
1059 static int megasas_dcmd_pd_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
1061 size_t dcmd_size
= sizeof(struct mfi_pd_info
);
1063 SCSIDevice
*sdev
= NULL
;
1064 int retval
= MFI_STAT_DEVICE_NOT_FOUND
;
1066 if (cmd
->iov_size
< dcmd_size
) {
1067 return MFI_STAT_INVALID_PARAMETER
;
1070 /* mbox0 has the ID */
1071 pd_id
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1072 sdev
= scsi_device_find(&s
->bus
, 0, pd_id
, 0);
1073 trace_megasas_dcmd_pd_get_info(cmd
->index
, pd_id
);
1076 /* Submit inquiry */
1077 retval
= megasas_pd_get_info_submit(sdev
, pd_id
, cmd
);
1083 static int megasas_dcmd_ld_get_list(MegasasState
*s
, MegasasCmd
*cmd
)
1085 struct mfi_ld_list info
;
1086 size_t dcmd_size
= sizeof(info
), resid
;
1087 uint32_t num_ld_disks
= 0, max_ld_disks
= s
->fw_luns
;
1091 memset(&info
, 0, dcmd_size
);
1092 if (cmd
->iov_size
< dcmd_size
) {
1093 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1095 return MFI_STAT_INVALID_PARAMETER
;
1098 if (megasas_is_jbod(s
)) {
1101 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1102 SCSIDevice
*sdev
= DO_UPCAST(SCSIDevice
, qdev
, kid
->child
);
1103 BlockConf
*conf
= &sdev
->conf
;
1105 if (num_ld_disks
>= max_ld_disks
) {
1108 /* Logical device size is in blocks */
1109 bdrv_get_geometry(conf
->bs
, &ld_size
);
1110 info
.ld_list
[num_ld_disks
].ld
.v
.target_id
= sdev
->id
;
1111 info
.ld_list
[num_ld_disks
].ld
.v
.lun_id
= sdev
->lun
;
1112 info
.ld_list
[num_ld_disks
].state
= MFI_LD_STATE_OPTIMAL
;
1113 info
.ld_list
[num_ld_disks
].size
= cpu_to_le64(ld_size
);
1116 info
.ld_count
= cpu_to_le32(num_ld_disks
);
1117 trace_megasas_dcmd_ld_get_list(cmd
->index
, num_ld_disks
, max_ld_disks
);
1119 resid
= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1120 cmd
->iov_size
= dcmd_size
- resid
;
1124 static int megasas_dcmd_ld_list_query(MegasasState
*s
, MegasasCmd
*cmd
)
1128 /* mbox0 contains flags */
1129 flags
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1130 trace_megasas_dcmd_ld_list_query(cmd
->index
, flags
);
1131 if (flags
== MR_LD_QUERY_TYPE_ALL
||
1132 flags
== MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
) {
1133 return megasas_dcmd_ld_get_list(s
, cmd
);
1139 static int megasas_ld_get_info_submit(SCSIDevice
*sdev
, int lun
,
1142 struct mfi_ld_info
*info
= cmd
->iov_buf
;
1143 size_t dcmd_size
= sizeof(struct mfi_ld_info
);
1147 BlockConf
*conf
= &sdev
->conf
;
1148 uint16_t sdev_id
= ((sdev
->id
& 0xFF) >> 8) | (lun
& 0xFF);
1151 if (!cmd
->iov_buf
) {
1152 cmd
->iov_buf
= g_malloc(dcmd_size
);
1153 memset(cmd
->iov_buf
, 0x0, dcmd_size
);
1154 info
= cmd
->iov_buf
;
1155 megasas_setup_inquiry(cdb
, 0x83, sizeof(info
->vpd_page83
));
1156 req
= scsi_req_new(sdev
, cmd
->index
, lun
, cdb
, cmd
);
1158 trace_megasas_dcmd_req_alloc_failed(cmd
->index
,
1159 "LD get info vpd inquiry");
1160 g_free(cmd
->iov_buf
);
1161 cmd
->iov_buf
= NULL
;
1162 return MFI_STAT_FLASH_ALLOC_FAIL
;
1164 trace_megasas_dcmd_internal_submit(cmd
->index
,
1165 "LD get info vpd inquiry", lun
);
1166 len
= scsi_req_enqueue(req
);
1168 cmd
->iov_size
= len
;
1169 scsi_req_continue(req
);
1171 return MFI_STAT_INVALID_STATUS
;
1174 info
->ld_config
.params
.state
= MFI_LD_STATE_OPTIMAL
;
1175 info
->ld_config
.properties
.ld
.v
.target_id
= lun
;
1176 info
->ld_config
.params
.stripe_size
= 3;
1177 info
->ld_config
.params
.num_drives
= 1;
1178 info
->ld_config
.params
.is_consistent
= 1;
1179 /* Logical device size is in blocks */
1180 bdrv_get_geometry(conf
->bs
, &ld_size
);
1181 info
->size
= cpu_to_le64(ld_size
);
1182 memset(info
->ld_config
.span
, 0, sizeof(info
->ld_config
.span
));
1183 info
->ld_config
.span
[0].start_block
= 0;
1184 info
->ld_config
.span
[0].num_blocks
= info
->size
;
1185 info
->ld_config
.span
[0].array_ref
= cpu_to_le16(sdev_id
);
1187 resid
= dma_buf_read(cmd
->iov_buf
, dcmd_size
, &cmd
->qsg
);
1188 g_free(cmd
->iov_buf
);
1189 cmd
->iov_size
= dcmd_size
- resid
;
1190 cmd
->iov_buf
= NULL
;
1194 static int megasas_dcmd_ld_get_info(MegasasState
*s
, MegasasCmd
*cmd
)
1196 struct mfi_ld_info info
;
1197 size_t dcmd_size
= sizeof(info
);
1199 uint32_t max_ld_disks
= s
->fw_luns
;
1200 SCSIDevice
*sdev
= NULL
;
1201 int retval
= MFI_STAT_DEVICE_NOT_FOUND
;
1203 if (cmd
->iov_size
< dcmd_size
) {
1204 return MFI_STAT_INVALID_PARAMETER
;
1207 /* mbox0 has the ID */
1208 ld_id
= le16_to_cpu(cmd
->frame
->dcmd
.mbox
[0]);
1209 trace_megasas_dcmd_ld_get_info(cmd
->index
, ld_id
);
1211 if (megasas_is_jbod(s
)) {
1212 return MFI_STAT_DEVICE_NOT_FOUND
;
1215 if (ld_id
< max_ld_disks
) {
1216 sdev
= scsi_device_find(&s
->bus
, 0, ld_id
, 0);
1220 retval
= megasas_ld_get_info_submit(sdev
, ld_id
, cmd
);
1226 static int megasas_dcmd_cfg_read(MegasasState
*s
, MegasasCmd
*cmd
)
1229 struct mfi_config_data
*info
;
1230 int num_pd_disks
= 0, array_offset
, ld_offset
;
1233 if (cmd
->iov_size
> 4096) {
1234 return MFI_STAT_INVALID_PARAMETER
;
1237 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1240 info
= (struct mfi_config_data
*)&data
;
1243 * - One array per SCSI device
1244 * - One logical drive per SCSI device
1245 * spanning the entire device
1247 info
->array_count
= num_pd_disks
;
1248 info
->array_size
= sizeof(struct mfi_array
) * num_pd_disks
;
1249 info
->log_drv_count
= num_pd_disks
;
1250 info
->log_drv_size
= sizeof(struct mfi_ld_config
) * num_pd_disks
;
1251 info
->spares_count
= 0;
1252 info
->spares_size
= sizeof(struct mfi_spare
);
1253 info
->size
= sizeof(struct mfi_config_data
) + info
->array_size
+
1255 if (info
->size
> 4096) {
1256 return MFI_STAT_INVALID_PARAMETER
;
1259 array_offset
= sizeof(struct mfi_config_data
);
1260 ld_offset
= array_offset
+ sizeof(struct mfi_array
) * num_pd_disks
;
1262 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
1263 SCSIDevice
*sdev
= DO_UPCAST(SCSIDevice
, qdev
, kid
->child
);
1264 BlockConf
*conf
= &sdev
->conf
;
1265 uint16_t sdev_id
= ((sdev
->id
& 0xFF) >> 8) | (sdev
->lun
& 0xFF);
1266 struct mfi_array
*array
;
1267 struct mfi_ld_config
*ld
;
1271 array
= (struct mfi_array
*)(data
+ array_offset
);
1272 bdrv_get_geometry(conf
->bs
, &pd_size
);
1273 array
->size
= cpu_to_le64(pd_size
);
1274 array
->num_drives
= 1;
1275 array
->array_ref
= cpu_to_le16(sdev_id
);
1276 array
->pd
[0].ref
.v
.device_id
= cpu_to_le16(sdev_id
);
1277 array
->pd
[0].ref
.v
.seq_num
= 0;
1278 array
->pd
[0].fw_state
= MFI_PD_STATE_ONLINE
;
1279 array
->pd
[0].encl
.pd
= 0xFF;
1280 array
->pd
[0].encl
.slot
= (sdev
->id
& 0xFF);
1281 for (i
= 1; i
< MFI_MAX_ROW_SIZE
; i
++) {
1282 array
->pd
[i
].ref
.v
.device_id
= 0xFFFF;
1283 array
->pd
[i
].ref
.v
.seq_num
= 0;
1284 array
->pd
[i
].fw_state
= MFI_PD_STATE_UNCONFIGURED_GOOD
;
1285 array
->pd
[i
].encl
.pd
= 0xFF;
1286 array
->pd
[i
].encl
.slot
= 0xFF;
1288 array_offset
+= sizeof(struct mfi_array
);
1289 ld
= (struct mfi_ld_config
*)(data
+ ld_offset
);
1290 memset(ld
, 0, sizeof(struct mfi_ld_config
));
1291 ld
->properties
.ld
.v
.target_id
= (sdev
->id
& 0xFF);
1292 ld
->properties
.default_cache_policy
= MR_LD_CACHE_READ_AHEAD
|
1293 MR_LD_CACHE_READ_ADAPTIVE
;
1294 ld
->properties
.current_cache_policy
= MR_LD_CACHE_READ_AHEAD
|
1295 MR_LD_CACHE_READ_ADAPTIVE
;
1296 ld
->params
.state
= MFI_LD_STATE_OPTIMAL
;
1297 ld
->params
.stripe_size
= 3;
1298 ld
->params
.num_drives
= 1;
1299 ld
->params
.span_depth
= 1;
1300 ld
->params
.is_consistent
= 1;
1301 ld
->span
[0].start_block
= 0;
1302 ld
->span
[0].num_blocks
= cpu_to_le64(pd_size
);
1303 ld
->span
[0].array_ref
= cpu_to_le16(sdev_id
);
1304 ld_offset
+= sizeof(struct mfi_ld_config
);
1307 cmd
->iov_size
-= dma_buf_read((uint8_t *)data
, info
->size
, &cmd
->qsg
);
1311 static int megasas_dcmd_get_properties(MegasasState
*s
, MegasasCmd
*cmd
)
1313 struct mfi_ctrl_props info
;
1314 size_t dcmd_size
= sizeof(info
);
1316 memset(&info
, 0x0, dcmd_size
);
1317 if (cmd
->iov_size
< dcmd_size
) {
1318 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1320 return MFI_STAT_INVALID_PARAMETER
;
1322 info
.pred_fail_poll_interval
= cpu_to_le16(300);
1323 info
.intr_throttle_cnt
= cpu_to_le16(16);
1324 info
.intr_throttle_timeout
= cpu_to_le16(50);
1325 info
.rebuild_rate
= 30;
1326 info
.patrol_read_rate
= 30;
1329 info
.recon_rate
= 30;
1330 info
.cache_flush_interval
= 4;
1331 info
.spinup_drv_cnt
= 2;
1332 info
.spinup_delay
= 6;
1333 info
.ecc_bucket_size
= 15;
1334 info
.ecc_bucket_leak_rate
= cpu_to_le16(1440);
1335 info
.expose_encl_devices
= 1;
1337 cmd
->iov_size
-= dma_buf_read((uint8_t *)&info
, dcmd_size
, &cmd
->qsg
);
1341 static int megasas_cache_flush(MegasasState
*s
, MegasasCmd
*cmd
)
1347 static int megasas_ctrl_shutdown(MegasasState
*s
, MegasasCmd
*cmd
)
1349 s
->fw_state
= MFI_FWSTATE_READY
;
1353 static int megasas_cluster_reset_ld(MegasasState
*s
, MegasasCmd
*cmd
)
1355 return MFI_STAT_INVALID_DCMD
;
1358 static int megasas_dcmd_set_properties(MegasasState
*s
, MegasasCmd
*cmd
)
1360 struct mfi_ctrl_props info
;
1361 size_t dcmd_size
= sizeof(info
);
1363 if (cmd
->iov_size
< dcmd_size
) {
1364 trace_megasas_dcmd_invalid_xfer_len(cmd
->index
, cmd
->iov_size
,
1366 return MFI_STAT_INVALID_PARAMETER
;
1368 dma_buf_write((uint8_t *)&info
, cmd
->iov_size
, &cmd
->qsg
);
1369 trace_megasas_dcmd_unsupported(cmd
->index
, cmd
->iov_size
);
1373 static int megasas_dcmd_dummy(MegasasState
*s
, MegasasCmd
*cmd
)
1375 trace_megasas_dcmd_dummy(cmd
->index
, cmd
->iov_size
);
1379 static const struct dcmd_cmd_tbl_t
{
1382 int (*func
)(MegasasState
*s
, MegasasCmd
*cmd
);
1383 } dcmd_cmd_tbl
[] = {
1384 { MFI_DCMD_CTRL_MFI_HOST_MEM_ALLOC
, "CTRL_HOST_MEM_ALLOC",
1385 megasas_dcmd_dummy
},
1386 { MFI_DCMD_CTRL_GET_INFO
, "CTRL_GET_INFO",
1387 megasas_ctrl_get_info
},
1388 { MFI_DCMD_CTRL_GET_PROPERTIES
, "CTRL_GET_PROPERTIES",
1389 megasas_dcmd_get_properties
},
1390 { MFI_DCMD_CTRL_SET_PROPERTIES
, "CTRL_SET_PROPERTIES",
1391 megasas_dcmd_set_properties
},
1392 { MFI_DCMD_CTRL_ALARM_GET
, "CTRL_ALARM_GET",
1393 megasas_dcmd_dummy
},
1394 { MFI_DCMD_CTRL_ALARM_ENABLE
, "CTRL_ALARM_ENABLE",
1395 megasas_dcmd_dummy
},
1396 { MFI_DCMD_CTRL_ALARM_DISABLE
, "CTRL_ALARM_DISABLE",
1397 megasas_dcmd_dummy
},
1398 { MFI_DCMD_CTRL_ALARM_SILENCE
, "CTRL_ALARM_SILENCE",
1399 megasas_dcmd_dummy
},
1400 { MFI_DCMD_CTRL_ALARM_TEST
, "CTRL_ALARM_TEST",
1401 megasas_dcmd_dummy
},
1402 { MFI_DCMD_CTRL_EVENT_GETINFO
, "CTRL_EVENT_GETINFO",
1403 megasas_event_info
},
1404 { MFI_DCMD_CTRL_EVENT_GET
, "CTRL_EVENT_GET",
1405 megasas_dcmd_dummy
},
1406 { MFI_DCMD_CTRL_EVENT_WAIT
, "CTRL_EVENT_WAIT",
1407 megasas_event_wait
},
1408 { MFI_DCMD_CTRL_SHUTDOWN
, "CTRL_SHUTDOWN",
1409 megasas_ctrl_shutdown
},
1410 { MFI_DCMD_HIBERNATE_STANDBY
, "CTRL_STANDBY",
1411 megasas_dcmd_dummy
},
1412 { MFI_DCMD_CTRL_GET_TIME
, "CTRL_GET_TIME",
1413 megasas_dcmd_get_fw_time
},
1414 { MFI_DCMD_CTRL_SET_TIME
, "CTRL_SET_TIME",
1415 megasas_dcmd_set_fw_time
},
1416 { MFI_DCMD_CTRL_BIOS_DATA_GET
, "CTRL_BIOS_DATA_GET",
1417 megasas_dcmd_get_bios_info
},
1418 { MFI_DCMD_CTRL_FACTORY_DEFAULTS
, "CTRL_FACTORY_DEFAULTS",
1419 megasas_dcmd_dummy
},
1420 { MFI_DCMD_CTRL_MFC_DEFAULTS_GET
, "CTRL_MFC_DEFAULTS_GET",
1421 megasas_mfc_get_defaults
},
1422 { MFI_DCMD_CTRL_MFC_DEFAULTS_SET
, "CTRL_MFC_DEFAULTS_SET",
1423 megasas_dcmd_dummy
},
1424 { MFI_DCMD_CTRL_CACHE_FLUSH
, "CTRL_CACHE_FLUSH",
1425 megasas_cache_flush
},
1426 { MFI_DCMD_PD_GET_LIST
, "PD_GET_LIST",
1427 megasas_dcmd_pd_get_list
},
1428 { MFI_DCMD_PD_LIST_QUERY
, "PD_LIST_QUERY",
1429 megasas_dcmd_pd_list_query
},
1430 { MFI_DCMD_PD_GET_INFO
, "PD_GET_INFO",
1431 megasas_dcmd_pd_get_info
},
1432 { MFI_DCMD_PD_STATE_SET
, "PD_STATE_SET",
1433 megasas_dcmd_dummy
},
1434 { MFI_DCMD_PD_REBUILD
, "PD_REBUILD",
1435 megasas_dcmd_dummy
},
1436 { MFI_DCMD_PD_BLINK
, "PD_BLINK",
1437 megasas_dcmd_dummy
},
1438 { MFI_DCMD_PD_UNBLINK
, "PD_UNBLINK",
1439 megasas_dcmd_dummy
},
1440 { MFI_DCMD_LD_GET_LIST
, "LD_GET_LIST",
1441 megasas_dcmd_ld_get_list
},
1442 { MFI_DCMD_LD_LIST_QUERY
, "LD_LIST_QUERY",
1443 megasas_dcmd_ld_list_query
},
1444 { MFI_DCMD_LD_GET_INFO
, "LD_GET_INFO",
1445 megasas_dcmd_ld_get_info
},
1446 { MFI_DCMD_LD_GET_PROP
, "LD_GET_PROP",
1447 megasas_dcmd_dummy
},
1448 { MFI_DCMD_LD_SET_PROP
, "LD_SET_PROP",
1449 megasas_dcmd_dummy
},
1450 { MFI_DCMD_LD_DELETE
, "LD_DELETE",
1451 megasas_dcmd_dummy
},
1452 { MFI_DCMD_CFG_READ
, "CFG_READ",
1453 megasas_dcmd_cfg_read
},
1454 { MFI_DCMD_CFG_ADD
, "CFG_ADD",
1455 megasas_dcmd_dummy
},
1456 { MFI_DCMD_CFG_CLEAR
, "CFG_CLEAR",
1457 megasas_dcmd_dummy
},
1458 { MFI_DCMD_CFG_FOREIGN_READ
, "CFG_FOREIGN_READ",
1459 megasas_dcmd_dummy
},
1460 { MFI_DCMD_CFG_FOREIGN_IMPORT
, "CFG_FOREIGN_IMPORT",
1461 megasas_dcmd_dummy
},
1462 { MFI_DCMD_BBU_STATUS
, "BBU_STATUS",
1463 megasas_dcmd_dummy
},
1464 { MFI_DCMD_BBU_CAPACITY_INFO
, "BBU_CAPACITY_INFO",
1465 megasas_dcmd_dummy
},
1466 { MFI_DCMD_BBU_DESIGN_INFO
, "BBU_DESIGN_INFO",
1467 megasas_dcmd_dummy
},
1468 { MFI_DCMD_BBU_PROP_GET
, "BBU_PROP_GET",
1469 megasas_dcmd_dummy
},
1470 { MFI_DCMD_CLUSTER
, "CLUSTER",
1471 megasas_dcmd_dummy
},
1472 { MFI_DCMD_CLUSTER_RESET_ALL
, "CLUSTER_RESET_ALL",
1473 megasas_dcmd_dummy
},
1474 { MFI_DCMD_CLUSTER_RESET_LD
, "CLUSTER_RESET_LD",
1475 megasas_cluster_reset_ld
},
1479 static int megasas_handle_dcmd(MegasasState
*s
, MegasasCmd
*cmd
)
1483 const struct dcmd_cmd_tbl_t
*cmdptr
= dcmd_cmd_tbl
;
1485 opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
1486 trace_megasas_handle_dcmd(cmd
->index
, opcode
);
1487 len
= megasas_map_dcmd(s
, cmd
);
1489 return MFI_STAT_MEMORY_NOT_AVAILABLE
;
1491 while (cmdptr
->opcode
!= -1 && cmdptr
->opcode
!= opcode
) {
1494 if (cmdptr
->opcode
== -1) {
1495 trace_megasas_dcmd_unhandled(cmd
->index
, opcode
, len
);
1496 retval
= megasas_dcmd_dummy(s
, cmd
);
1498 trace_megasas_dcmd_enter(cmd
->index
, cmdptr
->desc
, len
);
1499 retval
= cmdptr
->func(s
, cmd
);
1501 if (retval
!= MFI_STAT_INVALID_STATUS
) {
1502 megasas_finish_dcmd(cmd
, len
);
1507 static int megasas_finish_internal_dcmd(MegasasCmd
*cmd
,
1511 int retval
= MFI_STAT_OK
;
1514 opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
1515 scsi_req_unref(req
);
1516 trace_megasas_dcmd_internal_finish(cmd
->index
, opcode
, lun
);
1518 case MFI_DCMD_PD_GET_INFO
:
1519 retval
= megasas_pd_get_info_submit(req
->dev
, lun
, cmd
);
1521 case MFI_DCMD_LD_GET_INFO
:
1522 retval
= megasas_ld_get_info_submit(req
->dev
, lun
, cmd
);
1525 trace_megasas_dcmd_internal_invalid(cmd
->index
, opcode
);
1526 retval
= MFI_STAT_INVALID_DCMD
;
1529 if (retval
!= MFI_STAT_INVALID_STATUS
) {
1530 megasas_finish_dcmd(cmd
, cmd
->iov_size
);
1535 static int megasas_enqueue_req(MegasasCmd
*cmd
, bool is_write
)
1539 len
= scsi_req_enqueue(cmd
->req
);
1544 if (len
> cmd
->iov_size
) {
1546 trace_megasas_iov_write_overflow(cmd
->index
, len
,
1549 trace_megasas_iov_read_overflow(cmd
->index
, len
,
1553 if (len
< cmd
->iov_size
) {
1555 trace_megasas_iov_write_underflow(cmd
->index
, len
,
1558 trace_megasas_iov_read_underflow(cmd
->index
, len
,
1561 cmd
->iov_size
= len
;
1563 scsi_req_continue(cmd
->req
);
1568 static int megasas_handle_scsi(MegasasState
*s
, MegasasCmd
*cmd
,
1574 struct SCSIDevice
*sdev
= NULL
;
1576 cdb
= cmd
->frame
->pass
.cdb
;
1578 if (cmd
->frame
->header
.target_id
< s
->fw_luns
) {
1579 sdev
= scsi_device_find(&s
->bus
, 0, cmd
->frame
->header
.target_id
,
1580 cmd
->frame
->header
.lun_id
);
1582 cmd
->iov_size
= le32_to_cpu(cmd
->frame
->header
.data_len
);
1583 trace_megasas_handle_scsi(mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
],
1584 is_logical
, cmd
->frame
->header
.target_id
,
1585 cmd
->frame
->header
.lun_id
, sdev
, cmd
->iov_size
);
1587 if (!sdev
|| (megasas_is_jbod(s
) && is_logical
)) {
1588 trace_megasas_scsi_target_not_present(
1589 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
], is_logical
,
1590 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
);
1591 return MFI_STAT_DEVICE_NOT_FOUND
;
1594 if (cmd
->frame
->header
.cdb_len
> 16) {
1595 trace_megasas_scsi_invalid_cdb_len(
1596 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
], is_logical
,
1597 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
,
1598 cmd
->frame
->header
.cdb_len
);
1599 megasas_write_sense(cmd
, SENSE_CODE(INVALID_OPCODE
));
1600 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1602 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1605 if (megasas_map_sgl(s
, cmd
, &cmd
->frame
->pass
.sgl
)) {
1606 megasas_write_sense(cmd
, SENSE_CODE(TARGET_FAILURE
));
1607 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1609 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1612 cmd
->req
= scsi_req_new(sdev
, cmd
->index
,
1613 cmd
->frame
->header
.lun_id
, cdb
, cmd
);
1615 trace_megasas_scsi_req_alloc_failed(
1616 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
],
1617 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
);
1618 megasas_write_sense(cmd
, SENSE_CODE(NO_SENSE
));
1619 cmd
->frame
->header
.scsi_status
= BUSY
;
1621 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1624 is_write
= (cmd
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
);
1625 len
= megasas_enqueue_req(cmd
, is_write
);
1628 trace_megasas_scsi_write_start(cmd
->index
, len
);
1630 trace_megasas_scsi_read_start(cmd
->index
, len
);
1633 trace_megasas_scsi_nodata(cmd
->index
);
1635 return MFI_STAT_INVALID_STATUS
;
1638 static int megasas_handle_io(MegasasState
*s
, MegasasCmd
*cmd
)
1640 uint32_t lba_count
, lba_start_hi
, lba_start_lo
;
1642 bool is_write
= (cmd
->frame
->header
.frame_cmd
== MFI_CMD_LD_WRITE
);
1645 struct SCSIDevice
*sdev
= NULL
;
1647 lba_count
= le32_to_cpu(cmd
->frame
->io
.header
.data_len
);
1648 lba_start_lo
= le32_to_cpu(cmd
->frame
->io
.lba_lo
);
1649 lba_start_hi
= le32_to_cpu(cmd
->frame
->io
.lba_hi
);
1650 lba_start
= ((uint64_t)lba_start_hi
<< 32) | lba_start_lo
;
1652 if (cmd
->frame
->header
.target_id
< s
->fw_luns
) {
1653 sdev
= scsi_device_find(&s
->bus
, 0, cmd
->frame
->header
.target_id
,
1654 cmd
->frame
->header
.lun_id
);
1657 trace_megasas_handle_io(cmd
->index
,
1658 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
],
1659 cmd
->frame
->header
.target_id
,
1660 cmd
->frame
->header
.lun_id
,
1661 (unsigned long)lba_start
, (unsigned long)lba_count
);
1663 trace_megasas_io_target_not_present(cmd
->index
,
1664 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
],
1665 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
);
1666 return MFI_STAT_DEVICE_NOT_FOUND
;
1669 if (cmd
->frame
->header
.cdb_len
> 16) {
1670 trace_megasas_scsi_invalid_cdb_len(
1671 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
], 1,
1672 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
,
1673 cmd
->frame
->header
.cdb_len
);
1674 megasas_write_sense(cmd
, SENSE_CODE(INVALID_OPCODE
));
1675 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1677 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1680 cmd
->iov_size
= lba_count
* sdev
->blocksize
;
1681 if (megasas_map_sgl(s
, cmd
, &cmd
->frame
->io
.sgl
)) {
1682 megasas_write_sense(cmd
, SENSE_CODE(TARGET_FAILURE
));
1683 cmd
->frame
->header
.scsi_status
= CHECK_CONDITION
;
1685 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1688 megasas_encode_lba(cdb
, lba_start
, lba_count
, is_write
);
1689 cmd
->req
= scsi_req_new(sdev
, cmd
->index
,
1690 cmd
->frame
->header
.lun_id
, cdb
, cmd
);
1692 trace_megasas_scsi_req_alloc_failed(
1693 mfi_frame_desc
[cmd
->frame
->header
.frame_cmd
],
1694 cmd
->frame
->header
.target_id
, cmd
->frame
->header
.lun_id
);
1695 megasas_write_sense(cmd
, SENSE_CODE(NO_SENSE
));
1696 cmd
->frame
->header
.scsi_status
= BUSY
;
1698 return MFI_STAT_SCSI_DONE_WITH_ERROR
;
1700 len
= megasas_enqueue_req(cmd
, is_write
);
1703 trace_megasas_io_write_start(cmd
->index
, lba_start
, lba_count
, len
);
1705 trace_megasas_io_read_start(cmd
->index
, lba_start
, lba_count
, len
);
1708 return MFI_STAT_INVALID_STATUS
;
1711 static int megasas_finish_internal_command(MegasasCmd
*cmd
,
1712 SCSIRequest
*req
, size_t resid
)
1714 int retval
= MFI_STAT_INVALID_CMD
;
1716 if (cmd
->frame
->header
.frame_cmd
== MFI_CMD_DCMD
) {
1717 cmd
->iov_size
-= resid
;
1718 retval
= megasas_finish_internal_dcmd(cmd
, req
);
1723 static QEMUSGList
*megasas_get_sg_list(SCSIRequest
*req
)
1725 MegasasCmd
*cmd
= req
->hba_private
;
1727 if (cmd
->frame
->header
.frame_cmd
== MFI_CMD_DCMD
) {
1734 static void megasas_xfer_complete(SCSIRequest
*req
, uint32_t len
)
1736 MegasasCmd
*cmd
= req
->hba_private
;
1740 trace_megasas_io_complete(cmd
->index
, len
);
1742 if (cmd
->frame
->header
.frame_cmd
!= MFI_CMD_DCMD
) {
1743 scsi_req_continue(req
);
1747 buf
= scsi_req_get_buf(req
);
1748 opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
1749 if (opcode
== MFI_DCMD_PD_GET_INFO
&& cmd
->iov_buf
) {
1750 struct mfi_pd_info
*info
= cmd
->iov_buf
;
1752 if (info
->inquiry_data
[0] == 0x7f) {
1753 memset(info
->inquiry_data
, 0, sizeof(info
->inquiry_data
));
1754 memcpy(info
->inquiry_data
, buf
, len
);
1755 } else if (info
->vpd_page83
[0] == 0x7f) {
1756 memset(info
->vpd_page83
, 0, sizeof(info
->vpd_page83
));
1757 memcpy(info
->vpd_page83
, buf
, len
);
1759 scsi_req_continue(req
);
1760 } else if (opcode
== MFI_DCMD_LD_GET_INFO
) {
1761 struct mfi_ld_info
*info
= cmd
->iov_buf
;
1764 memcpy(info
->vpd_page83
, buf
, sizeof(info
->vpd_page83
));
1765 scsi_req_continue(req
);
1770 static void megasas_command_complete(SCSIRequest
*req
, uint32_t status
,
1773 MegasasCmd
*cmd
= req
->hba_private
;
1774 uint8_t cmd_status
= MFI_STAT_OK
;
1776 trace_megasas_command_complete(cmd
->index
, status
, resid
);
1778 if (cmd
->req
!= req
) {
1780 * Internal command complete
1782 cmd_status
= megasas_finish_internal_command(cmd
, req
, resid
);
1783 if (cmd_status
== MFI_STAT_INVALID_STATUS
) {
1787 req
->status
= status
;
1788 trace_megasas_scsi_complete(cmd
->index
, req
->status
,
1789 cmd
->iov_size
, req
->cmd
.xfer
);
1790 if (req
->status
!= GOOD
) {
1791 cmd_status
= MFI_STAT_SCSI_DONE_WITH_ERROR
;
1793 if (req
->status
== CHECK_CONDITION
) {
1794 megasas_copy_sense(cmd
);
1797 megasas_unmap_sgl(cmd
);
1798 cmd
->frame
->header
.scsi_status
= req
->status
;
1799 scsi_req_unref(cmd
->req
);
1802 cmd
->frame
->header
.cmd_status
= cmd_status
;
1803 megasas_complete_frame(cmd
->state
, cmd
->context
);
1806 static void megasas_command_cancel(SCSIRequest
*req
)
1808 MegasasCmd
*cmd
= req
->hba_private
;
1811 megasas_abort_command(cmd
);
1813 scsi_req_unref(req
);
1817 static int megasas_handle_abort(MegasasState
*s
, MegasasCmd
*cmd
)
1819 uint64_t abort_ctx
= le64_to_cpu(cmd
->frame
->abort
.abort_context
);
1820 hwaddr abort_addr
, addr_hi
, addr_lo
;
1821 MegasasCmd
*abort_cmd
;
1823 addr_hi
= le32_to_cpu(cmd
->frame
->abort
.abort_mfi_addr_hi
);
1824 addr_lo
= le32_to_cpu(cmd
->frame
->abort
.abort_mfi_addr_lo
);
1825 abort_addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1827 abort_cmd
= megasas_lookup_frame(s
, abort_addr
);
1829 trace_megasas_abort_no_cmd(cmd
->index
, abort_ctx
);
1833 if (!megasas_use_queue64(s
)) {
1834 abort_ctx
&= (uint64_t)0xFFFFFFFF;
1836 if (abort_cmd
->context
!= abort_ctx
) {
1837 trace_megasas_abort_invalid_context(cmd
->index
, abort_cmd
->index
,
1838 abort_cmd
->context
);
1840 return MFI_STAT_ABORT_NOT_POSSIBLE
;
1842 trace_megasas_abort_frame(cmd
->index
, abort_cmd
->index
);
1843 megasas_abort_command(abort_cmd
);
1844 if (!s
->event_cmd
|| abort_cmd
!= s
->event_cmd
) {
1845 s
->event_cmd
= NULL
;
1851 static void megasas_handle_frame(MegasasState
*s
, uint64_t frame_addr
,
1852 uint32_t frame_count
)
1854 uint8_t frame_status
= MFI_STAT_INVALID_CMD
;
1855 uint64_t frame_context
;
1859 * Always read 64bit context, top bits will be
1860 * masked out if required in megasas_enqueue_frame()
1862 frame_context
= megasas_frame_get_context(frame_addr
);
1864 cmd
= megasas_enqueue_frame(s
, frame_addr
, frame_context
, frame_count
);
1866 /* reply queue full */
1867 trace_megasas_frame_busy(frame_addr
);
1868 megasas_frame_set_scsi_status(frame_addr
, BUSY
);
1869 megasas_frame_set_cmd_status(frame_addr
, MFI_STAT_SCSI_DONE_WITH_ERROR
);
1870 megasas_complete_frame(s
, frame_context
);
1874 switch (cmd
->frame
->header
.frame_cmd
) {
1876 frame_status
= megasas_init_firmware(s
, cmd
);
1879 frame_status
= megasas_handle_dcmd(s
, cmd
);
1882 frame_status
= megasas_handle_abort(s
, cmd
);
1884 case MFI_CMD_PD_SCSI_IO
:
1885 frame_status
= megasas_handle_scsi(s
, cmd
, 0);
1887 case MFI_CMD_LD_SCSI_IO
:
1888 frame_status
= megasas_handle_scsi(s
, cmd
, 1);
1890 case MFI_CMD_LD_READ
:
1891 case MFI_CMD_LD_WRITE
:
1892 frame_status
= megasas_handle_io(s
, cmd
);
1895 trace_megasas_unhandled_frame_cmd(cmd
->index
,
1896 cmd
->frame
->header
.frame_cmd
);
1900 if (frame_status
!= MFI_STAT_INVALID_STATUS
) {
1902 cmd
->frame
->header
.cmd_status
= frame_status
;
1904 megasas_frame_set_cmd_status(frame_addr
, frame_status
);
1906 megasas_complete_frame(s
, cmd
->context
);
1910 static uint64_t megasas_mmio_read(void *opaque
, hwaddr addr
,
1913 MegasasState
*s
= opaque
;
1914 uint32_t retval
= 0;
1922 retval
= (megasas_use_msix(s
) ? MFI_FWSTATE_MSIX_SUPPORTED
: 0) |
1923 (s
->fw_state
& MFI_FWSTATE_MASK
) |
1924 ((s
->fw_sge
& 0xff) << 16) |
1925 (s
->fw_cmds
& 0xFFFF);
1928 if (megasas_intr_enabled(s
) && s
->doorbell
) {
1929 retval
= MFI_1078_RM
| 1;
1933 retval
= s
->intr_mask
;
1936 retval
= s
->doorbell
;
1939 trace_megasas_mmio_invalid_readl(addr
);
1942 trace_megasas_mmio_readl(addr
, retval
);
1946 static void megasas_mmio_write(void *opaque
, hwaddr addr
,
1947 uint64_t val
, unsigned size
)
1949 MegasasState
*s
= opaque
;
1950 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
1951 uint64_t frame_addr
;
1952 uint32_t frame_count
;
1955 trace_megasas_mmio_writel(addr
, val
);
1958 if (val
& MFI_FWINIT_ABORT
) {
1959 /* Abort all pending cmds */
1960 for (i
= 0; i
< s
->fw_cmds
; i
++) {
1961 megasas_abort_command(&s
->frames
[i
]);
1964 if (val
& MFI_FWINIT_READY
) {
1965 /* move to FW READY */
1966 megasas_soft_reset(s
);
1968 if (val
& MFI_FWINIT_MFIMODE
) {
1974 if (!megasas_intr_enabled(s
) &&
1975 !msi_enabled(pci_dev
) &&
1976 !msix_enabled(pci_dev
)) {
1977 trace_megasas_irq_lower();
1978 pci_irq_deassert(pci_dev
);
1980 if (megasas_intr_enabled(s
)) {
1981 if (msix_enabled(pci_dev
)) {
1982 trace_megasas_msix_enabled(0);
1983 } else if (msi_enabled(pci_dev
)) {
1984 trace_megasas_msi_enabled(0);
1986 trace_megasas_intr_enabled();
1989 trace_megasas_intr_disabled();
1994 if (s
->producer_pa
&& megasas_intr_enabled(s
)) {
1995 /* Update reply queue pointer */
1996 trace_megasas_qf_update(s
->reply_queue_head
, s
->busy
);
1997 stl_le_phys(&address_space_memory
,
1998 s
->producer_pa
, s
->reply_queue_head
);
1999 if (!msix_enabled(pci_dev
)) {
2000 trace_megasas_irq_lower();
2001 pci_irq_deassert(pci_dev
);
2006 /* Received high 32 bits of a 64 bit MFI frame address */
2010 /* Received low 32 bits of a 64 bit MFI frame address */
2012 /* Received 32 bit MFI frame address */
2013 frame_addr
= (val
& ~0x1F);
2014 /* Add possible 64 bit offset */
2015 frame_addr
|= ((uint64_t)s
->frame_hi
<< 32);
2017 frame_count
= (val
>> 1) & 0xF;
2018 megasas_handle_frame(s
, frame_addr
, frame_count
);
2021 trace_megasas_mmio_invalid_writel(addr
, val
);
2026 static const MemoryRegionOps megasas_mmio_ops
= {
2027 .read
= megasas_mmio_read
,
2028 .write
= megasas_mmio_write
,
2029 .endianness
= DEVICE_LITTLE_ENDIAN
,
2031 .min_access_size
= 8,
2032 .max_access_size
= 8,
2036 static uint64_t megasas_port_read(void *opaque
, hwaddr addr
,
2039 return megasas_mmio_read(opaque
, addr
& 0xff, size
);
2042 static void megasas_port_write(void *opaque
, hwaddr addr
,
2043 uint64_t val
, unsigned size
)
2045 megasas_mmio_write(opaque
, addr
& 0xff, val
, size
);
2048 static const MemoryRegionOps megasas_port_ops
= {
2049 .read
= megasas_port_read
,
2050 .write
= megasas_port_write
,
2051 .endianness
= DEVICE_LITTLE_ENDIAN
,
2053 .min_access_size
= 4,
2054 .max_access_size
= 4,
2058 static uint64_t megasas_queue_read(void *opaque
, hwaddr addr
,
2064 static const MemoryRegionOps megasas_queue_ops
= {
2065 .read
= megasas_queue_read
,
2066 .endianness
= DEVICE_LITTLE_ENDIAN
,
2068 .min_access_size
= 8,
2069 .max_access_size
= 8,
2073 static void megasas_soft_reset(MegasasState
*s
)
2078 trace_megasas_reset();
2079 for (i
= 0; i
< s
->fw_cmds
; i
++) {
2080 cmd
= &s
->frames
[i
];
2081 megasas_abort_command(cmd
);
2083 megasas_reset_frames(s
);
2084 s
->reply_queue_len
= s
->fw_cmds
;
2085 s
->reply_queue_pa
= 0;
2088 s
->fw_state
= MFI_FWSTATE_READY
;
2090 s
->intr_mask
= MEGASAS_INTR_DISABLED_MASK
;
2092 s
->flags
&= ~MEGASAS_MASK_USE_QUEUE64
;
2094 s
->boot_event
= s
->event_count
;
2097 static void megasas_scsi_reset(DeviceState
*dev
)
2099 MegasasState
*s
= MEGASAS(dev
);
2101 megasas_soft_reset(s
);
2104 static const VMStateDescription vmstate_megasas
= {
2107 .minimum_version_id
= 0,
2108 .fields
= (VMStateField
[]) {
2109 VMSTATE_PCI_DEVICE(parent_obj
, MegasasState
),
2110 VMSTATE_MSIX(parent_obj
, MegasasState
),
2112 VMSTATE_INT32(fw_state
, MegasasState
),
2113 VMSTATE_INT32(intr_mask
, MegasasState
),
2114 VMSTATE_INT32(doorbell
, MegasasState
),
2115 VMSTATE_UINT64(reply_queue_pa
, MegasasState
),
2116 VMSTATE_UINT64(consumer_pa
, MegasasState
),
2117 VMSTATE_UINT64(producer_pa
, MegasasState
),
2118 VMSTATE_END_OF_LIST()
2122 static void megasas_scsi_uninit(PCIDevice
*d
)
2124 MegasasState
*s
= MEGASAS(d
);
2126 if (megasas_use_msix(s
)) {
2127 msix_uninit(d
, &s
->mmio_io
, &s
->mmio_io
);
2129 if (megasas_use_msi(s
)) {
2132 memory_region_destroy(&s
->mmio_io
);
2133 memory_region_destroy(&s
->port_io
);
2134 memory_region_destroy(&s
->queue_io
);
2137 static const struct SCSIBusInfo megasas_scsi_info
= {
2139 .max_target
= MFI_MAX_LD
,
2142 .transfer_data
= megasas_xfer_complete
,
2143 .get_sg_list
= megasas_get_sg_list
,
2144 .complete
= megasas_command_complete
,
2145 .cancel
= megasas_command_cancel
,
2148 static int megasas_scsi_init(PCIDevice
*dev
)
2150 DeviceState
*d
= DEVICE(dev
);
2151 MegasasState
*s
= MEGASAS(dev
);
2156 pci_conf
= dev
->config
;
2158 /* PCI latency timer = 0 */
2159 pci_conf
[PCI_LATENCY_TIMER
] = 0;
2160 /* Interrupt pin 1 */
2161 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
2163 memory_region_init_io(&s
->mmio_io
, OBJECT(s
), &megasas_mmio_ops
, s
,
2164 "megasas-mmio", 0x4000);
2165 memory_region_init_io(&s
->port_io
, OBJECT(s
), &megasas_port_ops
, s
,
2167 memory_region_init_io(&s
->queue_io
, OBJECT(s
), &megasas_queue_ops
, s
,
2168 "megasas-queue", 0x40000);
2170 if (megasas_use_msi(s
) &&
2171 msi_init(dev
, 0x50, 1, true, false)) {
2172 s
->flags
&= ~MEGASAS_MASK_USE_MSI
;
2174 if (megasas_use_msix(s
) &&
2175 msix_init(dev
, 15, &s
->mmio_io
, 0, 0x2000,
2176 &s
->mmio_io
, 0, 0x3800, 0x68)) {
2177 s
->flags
&= ~MEGASAS_MASK_USE_MSIX
;
2180 bar_type
= PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
;
2181 pci_register_bar(dev
, 0, bar_type
, &s
->mmio_io
);
2182 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_IO
, &s
->port_io
);
2183 pci_register_bar(dev
, 3, bar_type
, &s
->queue_io
);
2185 if (megasas_use_msix(s
)) {
2186 msix_vector_use(dev
, 0);
2190 s
->sas_addr
= ((NAA_LOCALLY_ASSIGNED_ID
<< 24) |
2191 IEEE_COMPANY_LOCALLY_ASSIGNED
) << 36;
2192 s
->sas_addr
|= (pci_bus_num(dev
->bus
) << 16);
2193 s
->sas_addr
|= (PCI_SLOT(dev
->devfn
) << 8);
2194 s
->sas_addr
|= PCI_FUNC(dev
->devfn
);
2196 if (!s
->hba_serial
) {
2197 s
->hba_serial
= g_strdup(MEGASAS_HBA_SERIAL
);
2199 if (s
->fw_sge
>= MEGASAS_MAX_SGE
- MFI_PASS_FRAME_SIZE
) {
2200 s
->fw_sge
= MEGASAS_MAX_SGE
- MFI_PASS_FRAME_SIZE
;
2201 } else if (s
->fw_sge
>= 128 - MFI_PASS_FRAME_SIZE
) {
2202 s
->fw_sge
= 128 - MFI_PASS_FRAME_SIZE
;
2204 s
->fw_sge
= 64 - MFI_PASS_FRAME_SIZE
;
2206 if (s
->fw_cmds
> MEGASAS_MAX_FRAMES
) {
2207 s
->fw_cmds
= MEGASAS_MAX_FRAMES
;
2209 trace_megasas_init(s
->fw_sge
, s
->fw_cmds
,
2210 megasas_is_jbod(s
) ? "jbod" : "raid");
2211 s
->fw_luns
= (MFI_MAX_LD
> MAX_SCSI_DEVS
) ?
2212 MAX_SCSI_DEVS
: MFI_MAX_LD
;
2215 for (i
= 0; i
< s
->fw_cmds
; i
++) {
2216 s
->frames
[i
].index
= i
;
2217 s
->frames
[i
].context
= -1;
2218 s
->frames
[i
].pa
= 0;
2219 s
->frames
[i
].state
= s
;
2222 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
2223 &megasas_scsi_info
, NULL
);
2224 if (!d
->hotplugged
) {
2225 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
2235 megasas_write_config(PCIDevice
*pci
, uint32_t addr
, uint32_t val
, int len
)
2237 pci_default_write_config(pci
, addr
, val
, len
);
2238 msi_write_config(pci
, addr
, val
, len
);
2241 static Property megasas_properties
[] = {
2242 DEFINE_PROP_UINT32("max_sge", MegasasState
, fw_sge
,
2243 MEGASAS_DEFAULT_SGE
),
2244 DEFINE_PROP_UINT32("max_cmds", MegasasState
, fw_cmds
,
2245 MEGASAS_DEFAULT_FRAMES
),
2246 DEFINE_PROP_STRING("hba_serial", MegasasState
, hba_serial
),
2247 DEFINE_PROP_UINT64("sas_address", MegasasState
, sas_addr
, 0),
2248 DEFINE_PROP_BIT("use_msi", MegasasState
, flags
,
2249 MEGASAS_FLAG_USE_MSI
, false),
2250 DEFINE_PROP_BIT("use_msix", MegasasState
, flags
,
2251 MEGASAS_FLAG_USE_MSIX
, false),
2252 DEFINE_PROP_BIT("use_jbod", MegasasState
, flags
,
2253 MEGASAS_FLAG_USE_JBOD
, false),
2254 DEFINE_PROP_END_OF_LIST(),
2257 static void megasas_class_init(ObjectClass
*oc
, void *data
)
2259 DeviceClass
*dc
= DEVICE_CLASS(oc
);
2260 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
2262 pc
->init
= megasas_scsi_init
;
2263 pc
->exit
= megasas_scsi_uninit
;
2264 pc
->vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2265 pc
->device_id
= PCI_DEVICE_ID_LSI_SAS1078
;
2266 pc
->subsystem_vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2267 pc
->subsystem_id
= 0x1013;
2268 pc
->class_id
= PCI_CLASS_STORAGE_RAID
;
2269 dc
->props
= megasas_properties
;
2270 dc
->reset
= megasas_scsi_reset
;
2271 dc
->vmsd
= &vmstate_megasas
;
2272 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2273 dc
->desc
= "LSI MegaRAID SAS 1078";
2274 pc
->config_write
= megasas_write_config
;
2277 static const TypeInfo megasas_info
= {
2278 .name
= TYPE_MEGASAS
,
2279 .parent
= TYPE_PCI_DEVICE
,
2280 .instance_size
= sizeof(MegasasState
),
2281 .class_init
= megasas_class_init
,
2284 static void megasas_register_types(void)
2286 type_register_static(&megasas_info
);
2289 type_init(megasas_register_types
)