hw/scsi/spapr_vscsi: Convert debug fprintf() to trace event
[qemu/ar7.git] / hw / scsi / megasas.c
blobaf18c88b656e7f5fa299c5f0dda1158ad66472e9
1 /*
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"
29 #include "qemu/iov.h"
30 #include "qemu/module.h"
31 #include "hw/scsi/scsi.h"
32 #include "scsi/constants.h"
33 #include "trace.h"
34 #include "qapi/error.h"
35 #include "mfi.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 {
62 uint32_t index;
63 uint16_t flags;
64 uint16_t count;
65 uint64_t context;
67 hwaddr pa;
68 hwaddr pa_size;
69 uint32_t dcmd_opcode;
70 union mfi_frame *frame;
71 SCSIRequest *req;
72 QEMUSGList qsg;
73 void *iov_buf;
74 size_t iov_size;
75 size_t iov_offset;
76 struct MegasasState *state;
77 } MegasasCmd;
79 typedef struct MegasasState {
80 /*< private >*/
81 PCIDevice parent_obj;
82 /*< public >*/
84 MemoryRegion mmio_io;
85 MemoryRegion port_io;
86 MemoryRegion queue_io;
87 uint32_t frame_hi;
89 int fw_state;
90 uint32_t fw_sge;
91 uint32_t fw_cmds;
92 uint32_t flags;
93 int fw_luns;
94 int intr_mask;
95 int doorbell;
96 int busy;
97 int diag;
98 int adp_reset;
99 OnOffAuto msi;
100 OnOffAuto msix;
102 MegasasCmd *event_cmd;
103 int event_locale;
104 int event_class;
105 int event_count;
106 int shutdown_event;
107 int boot_event;
109 uint64_t sas_addr;
110 char *hba_serial;
112 uint64_t reply_queue_pa;
113 void *reply_queue;
114 int reply_queue_len;
115 int reply_queue_head;
116 int 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);
122 SCSIBus bus;
123 } MegasasState;
125 typedef struct MegasasBaseClass {
126 PCIDeviceClass parent_class;
127 const char *product_name;
128 const char *product_version;
129 int mmio_bar;
130 int ioport_bar;
131 int osts;
132 } MegasasBaseClass;
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) {
152 return true;
154 return false;
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,
191 unsigned long frame)
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,
213 union mfi_sgl *sgl)
215 uint64_t addr;
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);
221 } else {
222 addr = le32_to_cpu(sgl->sg32->addr);
224 return addr;
227 static uint32_t megasas_sgl_get_len(MegasasCmd *cmd,
228 union mfi_sgl *sgl)
230 uint32_t len;
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);
236 } else {
237 len = le32_to_cpu(sgl->sg32->len);
239 return len;
242 static union mfi_sgl *megasas_sgl_next(MegasasCmd *cmd,
243 union mfi_sgl *sgl)
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);
251 } else {
252 next += sizeof(struct mfi_sg32);
255 if (next >= (uint8_t *)cmd->frame + cmd->pa_size) {
256 return NULL;
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)
265 int i;
266 int iov_count = 0;
267 size_t iov_size = 0;
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,
273 MEGASAS_MAX_SGE);
274 return 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;
280 if (!sgl) {
281 trace_megasas_iovec_sgl_underflow(cmd->index, i);
282 goto unmap;
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,
288 iov_pa, iov_size_p);
289 goto unmap;
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);
300 cmd->iov_offset = 0;
301 return 0;
302 unmap:
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,
311 uint8_t sense_len)
313 PCIDevice *pcid = PCI_DEVICE(cmd->state);
314 uint32_t pa_hi = 0, pa_lo;
315 hwaddr pa;
316 int frame_sense_len;
318 frame_sense_len = cmd->frame->header.sense_len;
319 if (sense_len > frame_sense_len) {
320 sense_len = frame_sense_len;
322 if (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;
331 return 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);
340 sense_buf[0] = 0xf0;
341 sense_buf[2] = sense.key;
342 sense_buf[7] = 10;
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];
351 uint8_t sense_len;
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)
363 memset(cdb, 0, 6);
364 cdb[0] = INQUIRY;
365 if (pg > 0) {
366 cdb[1] = 0x1;
367 cdb[2] = pg;
369 cdb[3] = (len >> 8) & 0xff;
370 cdb[4] = (len & 0xff);
371 return len;
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);
381 if (is_write) {
382 cdb[0] = WRITE_16;
383 } else {
384 cdb[0] = READ_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;
401 * Utility functions
403 static uint64_t megasas_fw_time(void)
405 struct tm curtime;
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);
429 * Frame handling
431 static int megasas_next_index(MegasasState *s, int index, int limit)
433 index++;
434 if (index == limit) {
435 index = 0;
437 return index;
440 static MegasasCmd *megasas_lookup_frame(MegasasState *s,
441 hwaddr frame)
443 MegasasCmd *cmd = NULL;
444 int num = 0, index;
446 index = s->reply_queue_head;
448 while (num < s->fw_cmds) {
449 if (s->frames[index].pa && s->frames[index].pa == frame) {
450 cmd = &s->frames[index];
451 break;
453 index = megasas_next_index(s, index, s->fw_cmds);
454 num++;
457 return cmd;
460 static void megasas_unmap_frame(MegasasState *s, MegasasCmd *cmd)
462 PCIDevice *p = PCI_DEVICE(s);
464 if (cmd->pa_size) {
465 pci_dma_unmap(p, cmd->frame, cmd->pa_size, 0, 0);
467 cmd->frame = NULL;
468 cmd->pa = 0;
469 cmd->pa_size = 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;
485 unsigned long index;
487 index = 0;
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)
491 break;
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);
498 return NULL;
500 cmd = &s->frames[index];
501 set_bit(index, s->frame_map);
502 trace_megasas_qf_new(index, frame);
504 cmd->pa = frame;
505 /* Map all possible frames */
506 cmd->frame = pci_dma_map(pcid, frame, &frame_size_p, 0);
507 if (frame_size_p != frame_size) {
508 trace_megasas_qf_map_failed(cmd->index, (unsigned long)frame);
509 if (cmd->frame) {
510 megasas_unmap_frame(s, cmd);
512 s->event_count++;
513 return NULL;
515 cmd->pa_size = frame_size_p;
516 cmd->context = context;
517 if (!megasas_use_queue64(s)) {
518 cmd->context &= (uint64_t)0xFFFFFFFF;
520 cmd->count = count;
521 cmd->dcmd_opcode = -1;
522 s->busy++;
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);
530 return cmd;
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 */
539 s->busy--;
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);
549 } else {
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,
564 s->busy);
565 stl_le_pci_dma(pci_dev, s->producer_pa, s->reply_queue_head);
566 /* Notify HBA */
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);
573 } else {
574 s->doorbell++;
575 if (s->doorbell == 1) {
576 trace_megasas_irq_raise();
577 pci_irq_assert(pci_dev);
580 } else {
581 trace_megasas_qf_complete_noirq(context);
585 static void megasas_complete_command(MegasasCmd *cmd)
587 cmd->iov_size = 0;
588 cmd->iov_offset = 0;
590 cmd->req->hba_private = NULL;
591 scsi_req_unref(cmd->req);
592 cmd->req = NULL;
594 megasas_unmap_frame(cmd->state, cmd);
595 megasas_complete_frame(cmd->state, cmd->context);
598 static void megasas_reset_frames(MegasasState *s)
600 int i;
601 MegasasCmd *cmd;
603 for (i = 0; i < s->fw_cmds; i++) {
604 cmd = &s->frames[i];
605 if (cmd->pa) {
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) {
616 return;
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;
629 uint32_t flags;
630 int ret = MFI_STAT_OK;
632 if (s->reply_queue_pa) {
633 trace_megasas_initq_mapped(s->reply_queue_pa);
634 goto out;
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);
643 s->event_count++;
644 ret = MFI_STAT_MEMORY_NOT_AVAILABLE;
645 goto out;
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);
650 s->event_count++;
651 ret = MFI_STAT_INVALID_PARAMETER;
652 goto out;
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;
676 out:
677 if (initq) {
678 pci_dma_unmap(pcid, initq, initq_size, 0, 0);
680 return ret;
683 static int megasas_map_dcmd(MegasasState *s, MegasasCmd *cmd)
685 dma_addr_t iov_pa, iov_size;
686 int iov_count;
688 cmd->flags = le16_to_cpu(cmd->frame->header.flags);
689 iov_count = cmd->frame->header.sge_count;
690 if (!iov_count) {
691 trace_megasas_dcmd_zero_sge(cmd->index);
692 cmd->iov_size = 0;
693 return 0;
694 } else if (iov_count > 1) {
695 trace_megasas_dcmd_invalid_sge(cmd->index, iov_count);
696 cmd->iov_size = 0;
697 return -EINVAL;
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;
704 return 0;
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);
716 } else {
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);
729 BusChild *kid;
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,
735 dcmd_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
752 * present.
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);
759 uint16_t pd_id;
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));
766 num_pd_disks++;
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) {
779 uint8_t biosver[32];
780 uint8_t *ptr;
782 ptr = memory_region_get_ram_ptr(&pci_dev->rom);
783 memcpy(biosver, ptr + 0x41, 31);
784 biosver[31] = 0;
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());
791 info.max_arms = 32;
792 info.max_spans = 8;
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 |
803 MFI_INFO_HW_MEM |
804 MFI_INFO_HW_FLASH);
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 |
839 MFI_INFO_PDMIX_LD);
841 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
842 return MFI_STAT_OK;
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,
853 dcmd_size);
854 return MFI_STAT_INVALID_PARAMETER;
857 info.sas_addr = cpu_to_le64(s->sas_addr);
858 info.stripe_size = 3;
859 info.flush_time = 4;
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);
872 return MFI_STAT_OK;
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,
883 dcmd_size);
884 return MFI_STAT_INVALID_PARAMETER;
886 info.continue_on_error = 1;
887 info.verbose = 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);
893 return MFI_STAT_OK;
896 static int megasas_dcmd_get_fw_time(MegasasState *s, MegasasCmd *cmd)
898 uint64_t fw_time;
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);
904 return MFI_STAT_OK;
907 static int megasas_dcmd_set_fw_time(MegasasState *s, MegasasCmd *cmd)
909 uint64_t fw_time;
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());
916 return MFI_STAT_OK;
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);
931 return MFI_STAT_OK;
934 static int megasas_event_wait(MegasasState *s, MegasasCmd *cmd)
936 union mfi_evt event;
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;
947 s->event_cmd = cmd;
948 /* Decrease busy count; event frame doesn't count here */
949 s->busy--;
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);
958 BusChild *kid;
959 uint32_t offset, dcmd_limit, num_pd_disks = 0, max_pd_disks;
961 memset(&info, 0, dcmd_size);
962 offset = 8;
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,
966 dcmd_limit);
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);
976 uint16_t pd_id;
978 if (num_pd_disks >= max_pd_disks)
979 break;
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));
990 num_pd_disks++;
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);
1000 return MFI_STAT_OK;
1003 static int megasas_dcmd_pd_list_query(MegasasState *s, MegasasCmd *cmd)
1005 uint16_t flags;
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);
1015 return MFI_STAT_OK;
1018 static int megasas_pd_get_info_submit(SCSIDevice *sdev, int lun,
1019 MegasasCmd *cmd)
1021 struct mfi_pd_info *info = cmd->iov_buf;
1022 size_t dcmd_size = sizeof(struct mfi_pd_info);
1023 uint64_t pd_size;
1024 uint16_t pd_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
1025 uint8_t cmdbuf[6];
1026 size_t len, resid;
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);
1035 if (!cmd->req) {
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);
1045 if (len > 0) {
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);
1053 if (!cmd->req) {
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);
1061 if (len > 0) {
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);
1071 } else {
1072 info->fw_state = cpu_to_le16(MFI_PD_STATE_ONLINE);
1074 } else {
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;
1097 return MFI_STAT_OK;
1100 static int megasas_dcmd_pd_get_info(MegasasState *s, MegasasCmd *cmd)
1102 size_t dcmd_size = sizeof(struct mfi_pd_info);
1103 uint16_t pd_id;
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);
1119 if (sdev) {
1120 /* Submit inquiry */
1121 retval = megasas_pd_get_info_submit(sdev, pd_id, cmd);
1124 return retval;
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;
1132 uint64_t ld_size;
1133 BusChild *kid;
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,
1138 dcmd_size);
1139 return MFI_STAT_INVALID_PARAMETER;
1142 max_ld_disks = (cmd->iov_size - 8) / 16;
1143 if (megasas_is_jbod(s)) {
1144 max_ld_disks = 0;
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) {
1153 break;
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);
1160 num_ld_disks++;
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;
1167 return MFI_STAT_OK;
1170 static int megasas_dcmd_ld_list_query(MegasasState *s, MegasasCmd *cmd)
1172 uint16_t flags;
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;
1176 BusChild *kid;
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) {
1183 max_ld_disks = 0;
1186 memset(&info, 0, dcmd_size);
1187 if (cmd->iov_size < 12) {
1188 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1189 dcmd_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)) {
1195 max_ld_disks = 0;
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) {
1204 break;
1206 info.targetid[num_ld_disks] = sdev->lun;
1207 num_ld_disks++;
1208 dcmd_size++;
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;
1216 return MFI_STAT_OK;
1219 static int megasas_ld_get_info_submit(SCSIDevice *sdev, int lun,
1220 MegasasCmd *cmd)
1222 struct mfi_ld_info *info = cmd->iov_buf;
1223 size_t dcmd_size = sizeof(struct mfi_ld_info);
1224 uint8_t cdb[6];
1225 ssize_t len, resid;
1226 uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
1227 uint64_t ld_size;
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);
1234 if (!cmd->req) {
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);
1244 if (len > 0) {
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;
1268 return MFI_STAT_OK;
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);
1275 uint16_t ld_id;
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);
1296 if (sdev) {
1297 retval = megasas_ld_get_info_submit(sdev, ld_id, cmd);
1300 return retval;
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;
1308 BusChild *kid;
1310 if (cmd->iov_size > 4096) {
1311 return MFI_STAT_INVALID_PARAMETER;
1314 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1315 num_pd_disks++;
1317 info = (struct mfi_config_data *)&data;
1319 * Array mapping:
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 +
1331 info->log_drv_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;
1344 uint64_t pd_size;
1345 int i;
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);
1384 return MFI_STAT_OK;
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,
1395 dcmd_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;
1403 info.bgi_rate = 30;
1404 info.cc_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);
1414 return MFI_STAT_OK;
1417 static int megasas_cache_flush(MegasasState *s, MegasasCmd *cmd)
1419 blk_drain_all();
1420 return MFI_STAT_OK;
1423 static int megasas_ctrl_shutdown(MegasasState *s, MegasasCmd *cmd)
1425 s->fw_state = MFI_FWSTATE_READY;
1426 return MFI_STAT_OK;
1429 /* Some implementations use CLUSTER RESET LD to simulate a device reset */
1430 static int megasas_cluster_reset_ld(MegasasState *s, MegasasCmd *cmd)
1432 uint16_t target_id;
1433 int i;
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);
1445 return MFI_STAT_OK;
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,
1455 dcmd_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);
1460 return MFI_STAT_OK;
1463 static int megasas_dcmd_dummy(MegasasState *s, MegasasCmd *cmd)
1465 trace_megasas_dcmd_dummy(cmd->index, cmd->iov_size);
1466 return MFI_STAT_OK;
1469 static const struct dcmd_cmd_tbl_t {
1470 int opcode;
1471 const char *desc;
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 },
1566 { -1, NULL, NULL }
1569 static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
1571 int retval = 0;
1572 size_t len;
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) {
1581 cmdptr++;
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);
1587 } else {
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);
1594 return retval;
1597 static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
1598 SCSIRequest *req, size_t resid)
1600 int retval = MFI_STAT_OK;
1601 int lun = req->lun;
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);
1608 break;
1609 case MFI_DCMD_LD_GET_INFO:
1610 retval = megasas_ld_get_info_submit(req->dev, lun, cmd);
1611 break;
1612 default:
1613 trace_megasas_dcmd_internal_invalid(cmd->index, cmd->dcmd_opcode);
1614 retval = MFI_STAT_INVALID_DCMD;
1615 break;
1617 if (retval != MFI_STAT_INVALID_STATUS) {
1618 megasas_finish_dcmd(cmd, cmd->iov_size);
1620 return retval;
1623 static int megasas_enqueue_req(MegasasCmd *cmd, bool is_write)
1625 int len;
1627 len = scsi_req_enqueue(cmd->req);
1628 if (len < 0) {
1629 len = -len;
1631 if (len > 0) {
1632 if (len > cmd->iov_size) {
1633 if (is_write) {
1634 trace_megasas_iov_write_overflow(cmd->index, len,
1635 cmd->iov_size);
1636 } else {
1637 trace_megasas_iov_read_overflow(cmd->index, len,
1638 cmd->iov_size);
1641 if (len < cmd->iov_size) {
1642 if (is_write) {
1643 trace_megasas_iov_write_underflow(cmd->index, len,
1644 cmd->iov_size);
1645 } else {
1646 trace_megasas_iov_read_underflow(cmd->index, len,
1647 cmd->iov_size);
1649 cmd->iov_size = len;
1651 scsi_req_continue(cmd->req);
1653 return len;
1656 static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
1657 int frame_cmd)
1659 uint8_t *cdb;
1660 int target_id, lun_id, cdb_len;
1661 bool is_write;
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;
1670 if (is_logical) {
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;
1689 if (cdb_len > 16) {
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;
1695 s->event_count++;
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;
1702 s->event_count++;
1703 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1706 cmd->req = scsi_req_new(sdev, cmd->index, lun_id, cdb, cmd);
1707 if (!cmd->req) {
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;
1712 s->event_count++;
1713 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1716 is_write = (cmd->req->cmd.mode == SCSI_XFER_TO_DEV);
1717 if (cmd->iov_size) {
1718 if (is_write) {
1719 trace_megasas_scsi_write_start(cmd->index, cmd->iov_size);
1720 } else {
1721 trace_megasas_scsi_read_start(cmd->index, cmd->iov_size);
1723 } else {
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;
1733 uint64_t lba_start;
1734 bool is_write = (frame_cmd == MFI_CMD_LD_WRITE);
1735 uint8_t cdb[16];
1736 int len;
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);
1756 if (!sdev) {
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;
1762 if (cdb_len > 16) {
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;
1767 s->event_count++;
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;
1775 s->event_count++;
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,
1781 lun_id, cdb, cmd);
1782 if (!cmd->req) {
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;
1787 s->event_count++;
1788 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1790 len = megasas_enqueue_req(cmd, is_write);
1791 if (len > 0) {
1792 if (is_write) {
1793 trace_megasas_io_write_start(cmd->index, lba_start, lba_count, len);
1794 } else {
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) {
1806 return NULL;
1807 } else {
1808 return &cmd->qsg;
1812 static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
1814 MegasasCmd *cmd = req->hba_private;
1815 uint8_t *buf;
1817 trace_megasas_io_complete(cmd->index, len);
1819 if (cmd->dcmd_opcode != -1) {
1820 scsi_req_continue(req);
1821 return;
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;
1839 if (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,
1847 size_t resid)
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) {
1855 return;
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) {
1864 return;
1866 } else {
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;
1887 if (!cmd) {
1888 return;
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);
1905 if (!abort_cmd) {
1906 trace_megasas_abort_no_cmd(cmd->index, abort_ctx);
1907 s->event_count++;
1908 return MFI_STAT_OK;
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,
1915 abort_cmd->index);
1916 s->event_count++;
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;
1924 s->event_count++;
1925 return MFI_STAT_OK;
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;
1933 int frame_cmd;
1934 MegasasCmd *cmd;
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);
1943 if (!cmd) {
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);
1949 s->event_count++;
1950 return;
1952 frame_cmd = cmd->frame->header.frame_cmd;
1953 switch (frame_cmd) {
1954 case MFI_CMD_INIT:
1955 frame_status = megasas_init_firmware(s, cmd);
1956 break;
1957 case MFI_CMD_DCMD:
1958 frame_status = megasas_handle_dcmd(s, cmd);
1959 break;
1960 case MFI_CMD_ABORT:
1961 frame_status = megasas_handle_abort(s, cmd);
1962 break;
1963 case MFI_CMD_PD_SCSI_IO:
1964 case MFI_CMD_LD_SCSI_IO:
1965 frame_status = megasas_handle_scsi(s, cmd, frame_cmd);
1966 break;
1967 case MFI_CMD_LD_READ:
1968 case MFI_CMD_LD_WRITE:
1969 frame_status = megasas_handle_io(s, cmd, frame_cmd);
1970 break;
1971 default:
1972 trace_megasas_unhandled_frame_cmd(cmd->index, frame_cmd);
1973 s->event_count++;
1974 break;
1976 if (frame_status != MFI_STAT_INVALID_STATUS) {
1977 if (cmd->frame) {
1978 cmd->frame->header.cmd_status = frame_status;
1979 } else {
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,
1988 unsigned size)
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;
1995 switch (addr) {
1996 case MFI_IDB:
1997 retval = 0;
1998 trace_megasas_mmio_readl("MFI_IDB", retval);
1999 break;
2000 case MFI_OMSG0:
2001 case MFI_OSP0:
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",
2007 retval);
2008 break;
2009 case MFI_OSTS:
2010 if (megasas_intr_enabled(s) && s->doorbell) {
2011 retval = base_class->osts;
2013 trace_megasas_mmio_readl("MFI_OSTS", retval);
2014 break;
2015 case MFI_OMSK:
2016 retval = s->intr_mask;
2017 trace_megasas_mmio_readl("MFI_OMSK", retval);
2018 break;
2019 case MFI_ODCR0:
2020 retval = s->doorbell ? 1 : 0;
2021 trace_megasas_mmio_readl("MFI_ODCR0", retval);
2022 break;
2023 case MFI_DIAG:
2024 retval = s->diag;
2025 trace_megasas_mmio_readl("MFI_DIAG", retval);
2026 break;
2027 case MFI_OSP1:
2028 retval = 15;
2029 trace_megasas_mmio_readl("MFI_OSP1", retval);
2030 break;
2031 default:
2032 trace_megasas_mmio_invalid_readl(addr);
2033 break;
2035 return retval;
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;
2047 int i;
2049 switch (addr) {
2050 case MFI_IDB:
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) {
2063 /* discard MFIs */
2065 if (val & MFI_FWINIT_STOP_ADP) {
2066 /* Terminal error, stop processing */
2067 s->fw_state = MFI_FWSTATE_FAULT;
2069 break;
2070 case MFI_OMSK:
2071 trace_megasas_mmio_writel("MFI_OMSK", val);
2072 s->intr_mask = 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);
2084 } else {
2085 trace_megasas_intr_enabled();
2087 } else {
2088 trace_megasas_intr_disabled();
2089 megasas_soft_reset(s);
2091 break;
2092 case MFI_ODCR0:
2093 trace_megasas_mmio_writel("MFI_ODCR0", val);
2094 s->doorbell = 0;
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);
2101 break;
2102 case MFI_IQPH:
2103 trace_megasas_mmio_writel("MFI_IQPH", val);
2104 /* Received high 32 bits of a 64 bit MFI frame address */
2105 s->frame_hi = val;
2106 break;
2107 case MFI_IQPL:
2108 trace_megasas_mmio_writel("MFI_IQPL", val);
2109 /* Received low 32 bits of a 64 bit MFI frame address */
2110 /* Fallthrough */
2111 case MFI_IQP:
2112 if (addr == MFI_IQP) {
2113 trace_megasas_mmio_writel("MFI_IQP", val);
2114 /* Received 64 bit MFI frame address */
2115 s->frame_hi = 0;
2117 frame_addr = (val & ~0x1F);
2118 /* Add possible 64 bit offset */
2119 frame_addr |= ((uint64_t)s->frame_hi << 32);
2120 s->frame_hi = 0;
2121 frame_count = (val >> 1) & 0xF;
2122 megasas_handle_frame(s, frame_addr, frame_count);
2123 break;
2124 case MFI_SEQ:
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) {
2129 s->adp_reset = 0;
2130 s->diag = MFI_DIAG_WRITE_ENABLE;
2132 } else {
2133 s->adp_reset = 0;
2134 s->diag = 0;
2136 break;
2137 case MFI_DIAG:
2138 trace_megasas_mmio_writel("MFI_DIAG", val);
2139 /* ADP reset */
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);
2144 s->adp_reset = 0;
2145 s->diag = 0;
2147 break;
2148 default:
2149 trace_megasas_mmio_invalid_writel(addr, val);
2150 break;
2154 static const MemoryRegionOps megasas_mmio_ops = {
2155 .read = megasas_mmio_read,
2156 .write = megasas_mmio_write,
2157 .endianness = DEVICE_LITTLE_ENDIAN,
2158 .impl = {
2159 .min_access_size = 8,
2160 .max_access_size = 8,
2164 static uint64_t megasas_port_read(void *opaque, hwaddr addr,
2165 unsigned size)
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,
2180 .impl = {
2181 .min_access_size = 4,
2182 .max_access_size = 4,
2186 static uint64_t megasas_queue_read(void *opaque, hwaddr addr,
2187 unsigned size)
2189 return 0;
2192 static void megasas_queue_write(void *opaque, hwaddr addr,
2193 uint64_t val, unsigned size)
2195 return;
2198 static const MemoryRegionOps megasas_queue_ops = {
2199 .read = megasas_queue_read,
2200 .write = megasas_queue_write,
2201 .endianness = DEVICE_LITTLE_ENDIAN,
2202 .impl = {
2203 .min_access_size = 8,
2204 .max_access_size = 8,
2208 static void megasas_soft_reset(MegasasState *s)
2210 int i;
2211 MegasasCmd *cmd;
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) {
2219 BusChild *kid;
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;
2236 s->consumer_pa = 0;
2237 s->producer_pa = 0;
2238 s->fw_state = MFI_FWSTATE_READY;
2239 s->doorbell = 0;
2240 s->intr_mask = MEGASAS_INTR_DISABLED_MASK;
2241 s->frame_hi = 0;
2242 s->flags &= ~MEGASAS_MASK_USE_QUEUE64;
2243 s->event_count++;
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 = {
2255 .name = "megasas",
2256 .version_id = 0,
2257 .minimum_version_id = 0,
2258 .fields = (VMStateField[]) {
2259 VMSTATE_PCI_DEVICE(parent_obj, MegasasState),
2260 VMSTATE_MSIX(parent_obj, MegasasState),
2262 VMSTATE_INT32(fw_state, MegasasState),
2263 VMSTATE_INT32(intr_mask, MegasasState),
2264 VMSTATE_INT32(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",
2274 .version_id = 0,
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_INT32(fw_state, MegasasState),
2282 VMSTATE_INT32(intr_mask, MegasasState),
2283 VMSTATE_INT32(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);
2298 msi_uninit(d);
2301 static const struct SCSIBusInfo megasas_scsi_info = {
2302 .tcq = true,
2303 .max_target = MFI_MAX_LD,
2304 .max_lun = 255,
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);
2316 uint8_t *pci_conf;
2317 int i, bar_type;
2318 Error *err = NULL;
2319 int ret;
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);
2338 return;
2339 } else if (ret) {
2340 /* With msi=auto, we fall back to MSI off silently */
2341 s->msi = ON_OFF_AUTO_OFF;
2342 error_free(err);
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,
2349 "megasas-io", 256);
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;
2375 if (!s->sas_addr) {
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;
2389 } else {
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;
2400 } else {
2401 s->fw_luns = MFI_MAX_LD;
2403 s->producer_pa = 0;
2404 s->consumer_pa = 0;
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 {
2445 const char *name;
2446 const char *desc;
2447 const char *product_name;
2448 const char *product_version;
2449 uint16_t device_id;
2450 uint16_t subsystem_id;
2451 int ioport_bar;
2452 int mmio_bar;
2453 int osts;
2454 const VMStateDescription *vmsd;
2455 Property *props;
2456 InterfaceInfo *interfaces;
2457 } MegasasInfo;
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,
2467 .ioport_bar = 2,
2468 .mmio_bar = 0,
2469 .osts = MFI_1078_RM | 1,
2470 .vmsd = &vmstate_megasas_gen1,
2471 .props = megasas_properties_gen1,
2472 .interfaces = (InterfaceInfo[]) {
2473 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2474 { },
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,
2483 .ioport_bar = 0,
2484 .mmio_bar = 1,
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),
2526 .abstract = true,
2529 static void megasas_register_types(void)
2531 int i;
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)