Merge remote-tracking branch 'remotes/berrange/tags/misc-next-pull-request' into...
[qemu.git] / hw / scsi / megasas.c
1 /*
2 * QEMU MegaRAID SAS 8708EM2 Host Bus Adapter emulation
3 * Based on the linux driver code at drivers/scsi/megaraid
4 *
5 * Copyright (c) 2009-2012 Hannes Reinecke, SUSE Labs
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "hw/hw.h"
23 #include "hw/pci/pci.h"
24 #include "sysemu/dma.h"
25 #include "sysemu/block-backend.h"
26 #include "hw/pci/msi.h"
27 #include "hw/pci/msix.h"
28 #include "qemu/iov.h"
29 #include "hw/scsi/scsi.h"
30 #include "block/scsi.h"
31 #include "trace.h"
32
33 #include "mfi.h"
34
35 #define MEGASAS_VERSION_GEN1 "1.70"
36 #define MEGASAS_VERSION_GEN2 "1.80"
37 #define MEGASAS_MAX_FRAMES 2048 /* Firmware limit at 65535 */
38 #define MEGASAS_DEFAULT_FRAMES 1000 /* Windows requires this */
39 #define MEGASAS_GEN2_DEFAULT_FRAMES 1008 /* Windows requires this */
40 #define MEGASAS_MAX_SGE 128 /* Firmware limit */
41 #define MEGASAS_DEFAULT_SGE 80
42 #define MEGASAS_MAX_SECTORS 0xFFFF /* No real limit */
43 #define MEGASAS_MAX_ARRAYS 128
44
45 #define MEGASAS_HBA_SERIAL "QEMU123456"
46 #define NAA_LOCALLY_ASSIGNED_ID 0x3ULL
47 #define IEEE_COMPANY_LOCALLY_ASSIGNED 0x525400
48
49 #define MEGASAS_FLAG_USE_JBOD 0
50 #define MEGASAS_MASK_USE_JBOD (1 << MEGASAS_FLAG_USE_JBOD)
51 #define MEGASAS_FLAG_USE_MSI 1
52 #define MEGASAS_MASK_USE_MSI (1 << MEGASAS_FLAG_USE_MSI)
53 #define MEGASAS_FLAG_USE_MSIX 2
54 #define MEGASAS_MASK_USE_MSIX (1 << MEGASAS_FLAG_USE_MSIX)
55 #define MEGASAS_FLAG_USE_QUEUE64 3
56 #define MEGASAS_MASK_USE_QUEUE64 (1 << MEGASAS_FLAG_USE_QUEUE64)
57
58 static const char *mfi_frame_desc[] = {
59 "MFI init", "LD Read", "LD Write", "LD SCSI", "PD SCSI",
60 "MFI Doorbell", "MFI Abort", "MFI SMP", "MFI Stop"};
61
62 typedef struct MegasasCmd {
63 uint32_t index;
64 uint16_t flags;
65 uint16_t count;
66 uint64_t context;
67
68 hwaddr pa;
69 hwaddr pa_size;
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;
78
79 typedef struct MegasasState {
80 /*< private >*/
81 PCIDevice parent_obj;
82 /*< public >*/
83
84 MemoryRegion mmio_io;
85 MemoryRegion port_io;
86 MemoryRegion queue_io;
87 uint32_t frame_hi;
88
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
100 MegasasCmd *event_cmd;
101 int event_locale;
102 int event_class;
103 int event_count;
104 int shutdown_event;
105 int boot_event;
106
107 uint64_t sas_addr;
108 char *hba_serial;
109
110 uint64_t reply_queue_pa;
111 void *reply_queue;
112 int reply_queue_len;
113 int reply_queue_head;
114 int reply_queue_tail;
115 uint64_t consumer_pa;
116 uint64_t producer_pa;
117
118 MegasasCmd frames[MEGASAS_MAX_FRAMES];
119 DECLARE_BITMAP(frame_map, MEGASAS_MAX_FRAMES);
120 SCSIBus bus;
121 } MegasasState;
122
123 typedef struct MegasasBaseClass {
124 PCIDeviceClass parent_class;
125 const char *product_name;
126 const char *product_version;
127 int mmio_bar;
128 int ioport_bar;
129 int osts;
130 } MegasasBaseClass;
131
132 #define TYPE_MEGASAS_BASE "megasas-base"
133 #define TYPE_MEGASAS_GEN1 "megasas"
134 #define TYPE_MEGASAS_GEN2 "megasas-gen2"
135
136 #define MEGASAS(obj) \
137 OBJECT_CHECK(MegasasState, (obj), TYPE_MEGASAS_BASE)
138
139 #define MEGASAS_DEVICE_CLASS(oc) \
140 OBJECT_CLASS_CHECK(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
141 #define MEGASAS_DEVICE_GET_CLASS(oc) \
142 OBJECT_GET_CLASS(MegasasBaseClass, (oc), TYPE_MEGASAS_BASE)
143
144 #define MEGASAS_INTR_DISABLED_MASK 0xFFFFFFFF
145
146 static bool megasas_intr_enabled(MegasasState *s)
147 {
148 if ((s->intr_mask & MEGASAS_INTR_DISABLED_MASK) !=
149 MEGASAS_INTR_DISABLED_MASK) {
150 return true;
151 }
152 return false;
153 }
154
155 static bool megasas_use_queue64(MegasasState *s)
156 {
157 return s->flags & MEGASAS_MASK_USE_QUEUE64;
158 }
159
160 static bool megasas_use_msi(MegasasState *s)
161 {
162 return s->flags & MEGASAS_MASK_USE_MSI;
163 }
164
165 static bool megasas_use_msix(MegasasState *s)
166 {
167 return s->flags & MEGASAS_MASK_USE_MSIX;
168 }
169
170 static bool megasas_is_jbod(MegasasState *s)
171 {
172 return s->flags & MEGASAS_MASK_USE_JBOD;
173 }
174
175 static void megasas_frame_set_cmd_status(MegasasState *s,
176 unsigned long frame, uint8_t v)
177 {
178 PCIDevice *pci = &s->parent_obj;
179 stb_pci_dma(pci, frame + offsetof(struct mfi_frame_header, cmd_status), v);
180 }
181
182 static void megasas_frame_set_scsi_status(MegasasState *s,
183 unsigned long frame, uint8_t v)
184 {
185 PCIDevice *pci = &s->parent_obj;
186 stb_pci_dma(pci, frame + offsetof(struct mfi_frame_header, scsi_status), v);
187 }
188
189 /*
190 * Context is considered opaque, but the HBA firmware is running
191 * in little endian mode. So convert it to little endian, too.
192 */
193 static uint64_t megasas_frame_get_context(MegasasState *s,
194 unsigned long frame)
195 {
196 PCIDevice *pci = &s->parent_obj;
197 return ldq_le_pci_dma(pci, frame + offsetof(struct mfi_frame_header, context));
198 }
199
200 static bool megasas_frame_is_ieee_sgl(MegasasCmd *cmd)
201 {
202 return cmd->flags & MFI_FRAME_IEEE_SGL;
203 }
204
205 static bool megasas_frame_is_sgl64(MegasasCmd *cmd)
206 {
207 return cmd->flags & MFI_FRAME_SGL64;
208 }
209
210 static bool megasas_frame_is_sense64(MegasasCmd *cmd)
211 {
212 return cmd->flags & MFI_FRAME_SENSE64;
213 }
214
215 static uint64_t megasas_sgl_get_addr(MegasasCmd *cmd,
216 union mfi_sgl *sgl)
217 {
218 uint64_t addr;
219
220 if (megasas_frame_is_ieee_sgl(cmd)) {
221 addr = le64_to_cpu(sgl->sg_skinny->addr);
222 } else if (megasas_frame_is_sgl64(cmd)) {
223 addr = le64_to_cpu(sgl->sg64->addr);
224 } else {
225 addr = le32_to_cpu(sgl->sg32->addr);
226 }
227 return addr;
228 }
229
230 static uint32_t megasas_sgl_get_len(MegasasCmd *cmd,
231 union mfi_sgl *sgl)
232 {
233 uint32_t len;
234
235 if (megasas_frame_is_ieee_sgl(cmd)) {
236 len = le32_to_cpu(sgl->sg_skinny->len);
237 } else if (megasas_frame_is_sgl64(cmd)) {
238 len = le32_to_cpu(sgl->sg64->len);
239 } else {
240 len = le32_to_cpu(sgl->sg32->len);
241 }
242 return len;
243 }
244
245 static union mfi_sgl *megasas_sgl_next(MegasasCmd *cmd,
246 union mfi_sgl *sgl)
247 {
248 uint8_t *next = (uint8_t *)sgl;
249
250 if (megasas_frame_is_ieee_sgl(cmd)) {
251 next += sizeof(struct mfi_sg_skinny);
252 } else if (megasas_frame_is_sgl64(cmd)) {
253 next += sizeof(struct mfi_sg64);
254 } else {
255 next += sizeof(struct mfi_sg32);
256 }
257
258 if (next >= (uint8_t *)cmd->frame + cmd->pa_size) {
259 return NULL;
260 }
261 return (union mfi_sgl *)next;
262 }
263
264 static void megasas_soft_reset(MegasasState *s);
265
266 static int megasas_map_sgl(MegasasState *s, MegasasCmd *cmd, union mfi_sgl *sgl)
267 {
268 int i;
269 int iov_count = 0;
270 size_t iov_size = 0;
271
272 cmd->flags = le16_to_cpu(cmd->frame->header.flags);
273 iov_count = cmd->frame->header.sge_count;
274 if (iov_count > MEGASAS_MAX_SGE) {
275 trace_megasas_iovec_sgl_overflow(cmd->index, iov_count,
276 MEGASAS_MAX_SGE);
277 return iov_count;
278 }
279 pci_dma_sglist_init(&cmd->qsg, PCI_DEVICE(s), iov_count);
280 for (i = 0; i < iov_count; i++) {
281 dma_addr_t iov_pa, iov_size_p;
282
283 if (!sgl) {
284 trace_megasas_iovec_sgl_underflow(cmd->index, i);
285 goto unmap;
286 }
287 iov_pa = megasas_sgl_get_addr(cmd, sgl);
288 iov_size_p = megasas_sgl_get_len(cmd, sgl);
289 if (!iov_pa || !iov_size_p) {
290 trace_megasas_iovec_sgl_invalid(cmd->index, i,
291 iov_pa, iov_size_p);
292 goto unmap;
293 }
294 qemu_sglist_add(&cmd->qsg, iov_pa, iov_size_p);
295 sgl = megasas_sgl_next(cmd, sgl);
296 iov_size += (size_t)iov_size_p;
297 }
298 if (cmd->iov_size > iov_size) {
299 trace_megasas_iovec_overflow(cmd->index, iov_size, cmd->iov_size);
300 } else if (cmd->iov_size < iov_size) {
301 trace_megasas_iovec_underflow(cmd->iov_size, iov_size, cmd->iov_size);
302 }
303 cmd->iov_offset = 0;
304 return 0;
305 unmap:
306 qemu_sglist_destroy(&cmd->qsg);
307 return iov_count - i;
308 }
309
310 static void megasas_unmap_sgl(MegasasCmd *cmd)
311 {
312 qemu_sglist_destroy(&cmd->qsg);
313 cmd->iov_offset = 0;
314 }
315
316 /*
317 * passthrough sense and io sense are at the same offset
318 */
319 static int megasas_build_sense(MegasasCmd *cmd, uint8_t *sense_ptr,
320 uint8_t sense_len)
321 {
322 PCIDevice *pcid = PCI_DEVICE(cmd->state);
323 uint32_t pa_hi = 0, pa_lo;
324 hwaddr pa;
325
326 if (sense_len > cmd->frame->header.sense_len) {
327 sense_len = cmd->frame->header.sense_len;
328 }
329 if (sense_len) {
330 pa_lo = le32_to_cpu(cmd->frame->pass.sense_addr_lo);
331 if (megasas_frame_is_sense64(cmd)) {
332 pa_hi = le32_to_cpu(cmd->frame->pass.sense_addr_hi);
333 }
334 pa = ((uint64_t) pa_hi << 32) | pa_lo;
335 pci_dma_write(pcid, pa, sense_ptr, sense_len);
336 cmd->frame->header.sense_len = sense_len;
337 }
338 return sense_len;
339 }
340
341 static void megasas_write_sense(MegasasCmd *cmd, SCSISense sense)
342 {
343 uint8_t sense_buf[SCSI_SENSE_BUF_SIZE];
344 uint8_t sense_len = 18;
345
346 memset(sense_buf, 0, sense_len);
347 sense_buf[0] = 0xf0;
348 sense_buf[2] = sense.key;
349 sense_buf[7] = 10;
350 sense_buf[12] = sense.asc;
351 sense_buf[13] = sense.ascq;
352 megasas_build_sense(cmd, sense_buf, sense_len);
353 }
354
355 static void megasas_copy_sense(MegasasCmd *cmd)
356 {
357 uint8_t sense_buf[SCSI_SENSE_BUF_SIZE];
358 uint8_t sense_len;
359
360 sense_len = scsi_req_get_sense(cmd->req, sense_buf,
361 SCSI_SENSE_BUF_SIZE);
362 megasas_build_sense(cmd, sense_buf, sense_len);
363 }
364
365 /*
366 * Format an INQUIRY CDB
367 */
368 static int megasas_setup_inquiry(uint8_t *cdb, int pg, int len)
369 {
370 memset(cdb, 0, 6);
371 cdb[0] = INQUIRY;
372 if (pg > 0) {
373 cdb[1] = 0x1;
374 cdb[2] = pg;
375 }
376 cdb[3] = (len >> 8) & 0xff;
377 cdb[4] = (len & 0xff);
378 return len;
379 }
380
381 /*
382 * Encode lba and len into a READ_16/WRITE_16 CDB
383 */
384 static void megasas_encode_lba(uint8_t *cdb, uint64_t lba,
385 uint32_t len, bool is_write)
386 {
387 memset(cdb, 0x0, 16);
388 if (is_write) {
389 cdb[0] = WRITE_16;
390 } else {
391 cdb[0] = READ_16;
392 }
393 cdb[2] = (lba >> 56) & 0xff;
394 cdb[3] = (lba >> 48) & 0xff;
395 cdb[4] = (lba >> 40) & 0xff;
396 cdb[5] = (lba >> 32) & 0xff;
397 cdb[6] = (lba >> 24) & 0xff;
398 cdb[7] = (lba >> 16) & 0xff;
399 cdb[8] = (lba >> 8) & 0xff;
400 cdb[9] = (lba) & 0xff;
401 cdb[10] = (len >> 24) & 0xff;
402 cdb[11] = (len >> 16) & 0xff;
403 cdb[12] = (len >> 8) & 0xff;
404 cdb[13] = (len) & 0xff;
405 }
406
407 /*
408 * Utility functions
409 */
410 static uint64_t megasas_fw_time(void)
411 {
412 struct tm curtime;
413 uint64_t bcd_time;
414
415 qemu_get_timedate(&curtime, 0);
416 bcd_time = ((uint64_t)curtime.tm_sec & 0xff) << 48 |
417 ((uint64_t)curtime.tm_min & 0xff) << 40 |
418 ((uint64_t)curtime.tm_hour & 0xff) << 32 |
419 ((uint64_t)curtime.tm_mday & 0xff) << 24 |
420 ((uint64_t)curtime.tm_mon & 0xff) << 16 |
421 ((uint64_t)(curtime.tm_year + 1900) & 0xffff);
422
423 return bcd_time;
424 }
425
426 /*
427 * Default disk sata address
428 * 0x1221 is the magic number as
429 * present in real hardware,
430 * so use it here, too.
431 */
432 static uint64_t megasas_get_sata_addr(uint16_t id)
433 {
434 uint64_t addr = (0x1221ULL << 48);
435 return addr | ((uint64_t)id << 24);
436 }
437
438 /*
439 * Frame handling
440 */
441 static int megasas_next_index(MegasasState *s, int index, int limit)
442 {
443 index++;
444 if (index == limit) {
445 index = 0;
446 }
447 return index;
448 }
449
450 static MegasasCmd *megasas_lookup_frame(MegasasState *s,
451 hwaddr frame)
452 {
453 MegasasCmd *cmd = NULL;
454 int num = 0, index;
455
456 index = s->reply_queue_head;
457
458 while (num < s->fw_cmds) {
459 if (s->frames[index].pa && s->frames[index].pa == frame) {
460 cmd = &s->frames[index];
461 break;
462 }
463 index = megasas_next_index(s, index, s->fw_cmds);
464 num++;
465 }
466
467 return cmd;
468 }
469
470 static void megasas_unmap_frame(MegasasState *s, MegasasCmd *cmd)
471 {
472 PCIDevice *p = PCI_DEVICE(s);
473
474 pci_dma_unmap(p, cmd->frame, cmd->pa_size, 0, 0);
475 cmd->frame = NULL;
476 cmd->pa = 0;
477 clear_bit(cmd->index, s->frame_map);
478 }
479
480 /*
481 * This absolutely needs to be locked if
482 * qemu ever goes multithreaded.
483 */
484 static MegasasCmd *megasas_enqueue_frame(MegasasState *s,
485 hwaddr frame, uint64_t context, int count)
486 {
487 PCIDevice *pcid = PCI_DEVICE(s);
488 MegasasCmd *cmd = NULL;
489 int frame_size = MFI_FRAME_SIZE * 16;
490 hwaddr frame_size_p = frame_size;
491 unsigned long index;
492
493 index = 0;
494 while (index < s->fw_cmds) {
495 index = find_next_zero_bit(s->frame_map, s->fw_cmds, index);
496 if (!s->frames[index].pa)
497 break;
498 /* Busy frame found */
499 trace_megasas_qf_mapped(index);
500 }
501 if (index >= s->fw_cmds) {
502 /* All frames busy */
503 trace_megasas_qf_busy(frame);
504 return NULL;
505 }
506 cmd = &s->frames[index];
507 set_bit(index, s->frame_map);
508 trace_megasas_qf_new(index, frame);
509
510 cmd->pa = frame;
511 /* Map all possible frames */
512 cmd->frame = pci_dma_map(pcid, frame, &frame_size_p, 0);
513 if (frame_size_p != frame_size) {
514 trace_megasas_qf_map_failed(cmd->index, (unsigned long)frame);
515 if (cmd->frame) {
516 megasas_unmap_frame(s, cmd);
517 }
518 s->event_count++;
519 return NULL;
520 }
521 cmd->pa_size = frame_size_p;
522 cmd->context = context;
523 if (!megasas_use_queue64(s)) {
524 cmd->context &= (uint64_t)0xFFFFFFFF;
525 }
526 cmd->count = count;
527 s->busy++;
528
529 if (s->consumer_pa) {
530 s->reply_queue_tail = ldl_le_pci_dma(pcid, s->consumer_pa);
531 }
532 trace_megasas_qf_enqueue(cmd->index, cmd->count, cmd->context,
533 s->reply_queue_head, s->reply_queue_tail, s->busy);
534
535 return cmd;
536 }
537
538 static void megasas_complete_frame(MegasasState *s, uint64_t context)
539 {
540 PCIDevice *pci_dev = PCI_DEVICE(s);
541 int tail, queue_offset;
542
543 /* Decrement busy count */
544 s->busy--;
545 if (s->reply_queue_pa) {
546 /*
547 * Put command on the reply queue.
548 * Context is opaque, but emulation is running in
549 * little endian. So convert it.
550 */
551 if (megasas_use_queue64(s)) {
552 queue_offset = s->reply_queue_head * sizeof(uint64_t);
553 stq_le_pci_dma(pci_dev, s->reply_queue_pa + queue_offset, context);
554 } else {
555 queue_offset = s->reply_queue_head * sizeof(uint32_t);
556 stl_le_pci_dma(pci_dev, s->reply_queue_pa + queue_offset, context);
557 }
558 s->reply_queue_tail = ldl_le_pci_dma(pci_dev, s->consumer_pa);
559 trace_megasas_qf_complete(context, s->reply_queue_head,
560 s->reply_queue_tail, s->busy);
561 }
562
563 if (megasas_intr_enabled(s)) {
564 /* Update reply queue pointer */
565 s->reply_queue_tail = ldl_le_pci_dma(pci_dev, s->consumer_pa);
566 tail = s->reply_queue_head;
567 s->reply_queue_head = megasas_next_index(s, tail, s->fw_cmds);
568 trace_megasas_qf_update(s->reply_queue_head, s->reply_queue_tail,
569 s->busy);
570 stl_le_pci_dma(pci_dev, s->producer_pa, s->reply_queue_head);
571 /* Notify HBA */
572 if (msix_enabled(pci_dev)) {
573 trace_megasas_msix_raise(0);
574 msix_notify(pci_dev, 0);
575 } else if (msi_enabled(pci_dev)) {
576 trace_megasas_msi_raise(0);
577 msi_notify(pci_dev, 0);
578 } else {
579 s->doorbell++;
580 if (s->doorbell == 1) {
581 trace_megasas_irq_raise();
582 pci_irq_assert(pci_dev);
583 }
584 }
585 } else {
586 trace_megasas_qf_complete_noirq(context);
587 }
588 }
589
590 static void megasas_reset_frames(MegasasState *s)
591 {
592 int i;
593 MegasasCmd *cmd;
594
595 for (i = 0; i < s->fw_cmds; i++) {
596 cmd = &s->frames[i];
597 if (cmd->pa) {
598 megasas_unmap_frame(s, cmd);
599 }
600 }
601 bitmap_zero(s->frame_map, MEGASAS_MAX_FRAMES);
602 }
603
604 static void megasas_abort_command(MegasasCmd *cmd)
605 {
606 if (cmd->req) {
607 scsi_req_cancel(cmd->req);
608 cmd->req = NULL;
609 }
610 }
611
612 static int megasas_init_firmware(MegasasState *s, MegasasCmd *cmd)
613 {
614 PCIDevice *pcid = PCI_DEVICE(s);
615 uint32_t pa_hi, pa_lo;
616 hwaddr iq_pa, initq_size = sizeof(struct mfi_init_qinfo);
617 struct mfi_init_qinfo *initq = NULL;
618 uint32_t flags;
619 int ret = MFI_STAT_OK;
620
621 if (s->reply_queue_pa) {
622 trace_megasas_initq_mapped(s->reply_queue_pa);
623 goto out;
624 }
625 pa_lo = le32_to_cpu(cmd->frame->init.qinfo_new_addr_lo);
626 pa_hi = le32_to_cpu(cmd->frame->init.qinfo_new_addr_hi);
627 iq_pa = (((uint64_t) pa_hi << 32) | pa_lo);
628 trace_megasas_init_firmware((uint64_t)iq_pa);
629 initq = pci_dma_map(pcid, iq_pa, &initq_size, 0);
630 if (!initq || initq_size != sizeof(*initq)) {
631 trace_megasas_initq_map_failed(cmd->index);
632 s->event_count++;
633 ret = MFI_STAT_MEMORY_NOT_AVAILABLE;
634 goto out;
635 }
636 s->reply_queue_len = le32_to_cpu(initq->rq_entries) & 0xFFFF;
637 if (s->reply_queue_len > s->fw_cmds) {
638 trace_megasas_initq_mismatch(s->reply_queue_len, s->fw_cmds);
639 s->event_count++;
640 ret = MFI_STAT_INVALID_PARAMETER;
641 goto out;
642 }
643 pa_lo = le32_to_cpu(initq->rq_addr_lo);
644 pa_hi = le32_to_cpu(initq->rq_addr_hi);
645 s->reply_queue_pa = ((uint64_t) pa_hi << 32) | pa_lo;
646 pa_lo = le32_to_cpu(initq->ci_addr_lo);
647 pa_hi = le32_to_cpu(initq->ci_addr_hi);
648 s->consumer_pa = ((uint64_t) pa_hi << 32) | pa_lo;
649 pa_lo = le32_to_cpu(initq->pi_addr_lo);
650 pa_hi = le32_to_cpu(initq->pi_addr_hi);
651 s->producer_pa = ((uint64_t) pa_hi << 32) | pa_lo;
652 s->reply_queue_head = ldl_le_pci_dma(pcid, s->producer_pa);
653 s->reply_queue_tail = ldl_le_pci_dma(pcid, s->consumer_pa);
654 flags = le32_to_cpu(initq->flags);
655 if (flags & MFI_QUEUE_FLAG_CONTEXT64) {
656 s->flags |= MEGASAS_MASK_USE_QUEUE64;
657 }
658 trace_megasas_init_queue((unsigned long)s->reply_queue_pa,
659 s->reply_queue_len, s->reply_queue_head,
660 s->reply_queue_tail, flags);
661 megasas_reset_frames(s);
662 s->fw_state = MFI_FWSTATE_OPERATIONAL;
663 out:
664 if (initq) {
665 pci_dma_unmap(pcid, initq, initq_size, 0, 0);
666 }
667 return ret;
668 }
669
670 static int megasas_map_dcmd(MegasasState *s, MegasasCmd *cmd)
671 {
672 dma_addr_t iov_pa, iov_size;
673
674 cmd->flags = le16_to_cpu(cmd->frame->header.flags);
675 if (!cmd->frame->header.sge_count) {
676 trace_megasas_dcmd_zero_sge(cmd->index);
677 cmd->iov_size = 0;
678 return 0;
679 } else if (cmd->frame->header.sge_count > 1) {
680 trace_megasas_dcmd_invalid_sge(cmd->index,
681 cmd->frame->header.sge_count);
682 cmd->iov_size = 0;
683 return -1;
684 }
685 iov_pa = megasas_sgl_get_addr(cmd, &cmd->frame->dcmd.sgl);
686 iov_size = megasas_sgl_get_len(cmd, &cmd->frame->dcmd.sgl);
687 pci_dma_sglist_init(&cmd->qsg, PCI_DEVICE(s), 1);
688 qemu_sglist_add(&cmd->qsg, iov_pa, iov_size);
689 cmd->iov_size = iov_size;
690 return cmd->iov_size;
691 }
692
693 static void megasas_finish_dcmd(MegasasCmd *cmd, uint32_t iov_size)
694 {
695 trace_megasas_finish_dcmd(cmd->index, iov_size);
696
697 if (cmd->frame->header.sge_count) {
698 qemu_sglist_destroy(&cmd->qsg);
699 }
700 if (iov_size > cmd->iov_size) {
701 if (megasas_frame_is_ieee_sgl(cmd)) {
702 cmd->frame->dcmd.sgl.sg_skinny->len = cpu_to_le32(iov_size);
703 } else if (megasas_frame_is_sgl64(cmd)) {
704 cmd->frame->dcmd.sgl.sg64->len = cpu_to_le32(iov_size);
705 } else {
706 cmd->frame->dcmd.sgl.sg32->len = cpu_to_le32(iov_size);
707 }
708 }
709 cmd->iov_size = 0;
710 }
711
712 static int megasas_ctrl_get_info(MegasasState *s, MegasasCmd *cmd)
713 {
714 PCIDevice *pci_dev = PCI_DEVICE(s);
715 PCIDeviceClass *pci_class = PCI_DEVICE_GET_CLASS(pci_dev);
716 MegasasBaseClass *base_class = MEGASAS_DEVICE_GET_CLASS(s);
717 struct mfi_ctrl_info info;
718 size_t dcmd_size = sizeof(info);
719 BusChild *kid;
720 int num_pd_disks = 0;
721
722 memset(&info, 0x0, dcmd_size);
723 if (cmd->iov_size < dcmd_size) {
724 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
725 dcmd_size);
726 return MFI_STAT_INVALID_PARAMETER;
727 }
728
729 info.pci.vendor = cpu_to_le16(pci_class->vendor_id);
730 info.pci.device = cpu_to_le16(pci_class->device_id);
731 info.pci.subvendor = cpu_to_le16(pci_class->subsystem_vendor_id);
732 info.pci.subdevice = cpu_to_le16(pci_class->subsystem_id);
733
734 /*
735 * For some reason the firmware supports
736 * only up to 8 device ports.
737 * Despite supporting a far larger number
738 * of devices for the physical devices.
739 * So just display the first 8 devices
740 * in the device port list, independent
741 * of how many logical devices are actually
742 * present.
743 */
744 info.host.type = MFI_INFO_HOST_PCIE;
745 info.device.type = MFI_INFO_DEV_SAS3G;
746 info.device.port_count = 8;
747 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
748 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
749 uint16_t pd_id;
750
751 if (num_pd_disks < 8) {
752 pd_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
753 info.device.port_addr[num_pd_disks] =
754 cpu_to_le64(megasas_get_sata_addr(pd_id));
755 }
756 num_pd_disks++;
757 }
758
759 memcpy(info.product_name, base_class->product_name, 24);
760 snprintf(info.serial_number, 32, "%s", s->hba_serial);
761 snprintf(info.package_version, 0x60, "%s-QEMU", qemu_hw_version());
762 memcpy(info.image_component[0].name, "APP", 3);
763 snprintf(info.image_component[0].version, 10, "%s-QEMU",
764 base_class->product_version);
765 memcpy(info.image_component[0].build_date, "Apr 1 2014", 11);
766 memcpy(info.image_component[0].build_time, "12:34:56", 8);
767 info.image_component_count = 1;
768 if (pci_dev->has_rom) {
769 uint8_t biosver[32];
770 uint8_t *ptr;
771
772 ptr = memory_region_get_ram_ptr(&pci_dev->rom);
773 memcpy(biosver, ptr + 0x41, 31);
774 memcpy(info.image_component[1].name, "BIOS", 4);
775 memcpy(info.image_component[1].version, biosver,
776 strlen((const char *)biosver));
777 info.image_component_count++;
778 }
779 info.current_fw_time = cpu_to_le32(megasas_fw_time());
780 info.max_arms = 32;
781 info.max_spans = 8;
782 info.max_arrays = MEGASAS_MAX_ARRAYS;
783 info.max_lds = MFI_MAX_LD;
784 info.max_cmds = cpu_to_le16(s->fw_cmds);
785 info.max_sg_elements = cpu_to_le16(s->fw_sge);
786 info.max_request_size = cpu_to_le32(MEGASAS_MAX_SECTORS);
787 if (!megasas_is_jbod(s))
788 info.lds_present = cpu_to_le16(num_pd_disks);
789 info.pd_present = cpu_to_le16(num_pd_disks);
790 info.pd_disks_present = cpu_to_le16(num_pd_disks);
791 info.hw_present = cpu_to_le32(MFI_INFO_HW_NVRAM |
792 MFI_INFO_HW_MEM |
793 MFI_INFO_HW_FLASH);
794 info.memory_size = cpu_to_le16(512);
795 info.nvram_size = cpu_to_le16(32);
796 info.flash_size = cpu_to_le16(16);
797 info.raid_levels = cpu_to_le32(MFI_INFO_RAID_0);
798 info.adapter_ops = cpu_to_le32(MFI_INFO_AOPS_RBLD_RATE |
799 MFI_INFO_AOPS_SELF_DIAGNOSTIC |
800 MFI_INFO_AOPS_MIXED_ARRAY);
801 info.ld_ops = cpu_to_le32(MFI_INFO_LDOPS_DISK_CACHE_POLICY |
802 MFI_INFO_LDOPS_ACCESS_POLICY |
803 MFI_INFO_LDOPS_IO_POLICY |
804 MFI_INFO_LDOPS_WRITE_POLICY |
805 MFI_INFO_LDOPS_READ_POLICY);
806 info.max_strips_per_io = cpu_to_le16(s->fw_sge);
807 info.stripe_sz_ops.min = 3;
808 info.stripe_sz_ops.max = ctz32(MEGASAS_MAX_SECTORS + 1);
809 info.properties.pred_fail_poll_interval = cpu_to_le16(300);
810 info.properties.intr_throttle_cnt = cpu_to_le16(16);
811 info.properties.intr_throttle_timeout = cpu_to_le16(50);
812 info.properties.rebuild_rate = 30;
813 info.properties.patrol_read_rate = 30;
814 info.properties.bgi_rate = 30;
815 info.properties.cc_rate = 30;
816 info.properties.recon_rate = 30;
817 info.properties.cache_flush_interval = 4;
818 info.properties.spinup_drv_cnt = 2;
819 info.properties.spinup_delay = 6;
820 info.properties.ecc_bucket_size = 15;
821 info.properties.ecc_bucket_leak_rate = cpu_to_le16(1440);
822 info.properties.expose_encl_devices = 1;
823 info.properties.OnOffProperties = cpu_to_le32(MFI_CTRL_PROP_EnableJBOD);
824 info.pd_ops = cpu_to_le32(MFI_INFO_PDOPS_FORCE_ONLINE |
825 MFI_INFO_PDOPS_FORCE_OFFLINE);
826 info.pd_mix_support = cpu_to_le32(MFI_INFO_PDMIX_SAS |
827 MFI_INFO_PDMIX_SATA |
828 MFI_INFO_PDMIX_LD);
829
830 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
831 return MFI_STAT_OK;
832 }
833
834 static int megasas_mfc_get_defaults(MegasasState *s, MegasasCmd *cmd)
835 {
836 struct mfi_defaults info;
837 size_t dcmd_size = sizeof(struct mfi_defaults);
838
839 memset(&info, 0x0, dcmd_size);
840 if (cmd->iov_size < dcmd_size) {
841 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
842 dcmd_size);
843 return MFI_STAT_INVALID_PARAMETER;
844 }
845
846 info.sas_addr = cpu_to_le64(s->sas_addr);
847 info.stripe_size = 3;
848 info.flush_time = 4;
849 info.background_rate = 30;
850 info.allow_mix_in_enclosure = 1;
851 info.allow_mix_in_ld = 1;
852 info.direct_pd_mapping = 1;
853 /* Enable for BIOS support */
854 info.bios_enumerate_lds = 1;
855 info.disable_ctrl_r = 1;
856 info.expose_enclosure_devices = 1;
857 info.disable_preboot_cli = 1;
858 info.cluster_disable = 1;
859
860 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
861 return MFI_STAT_OK;
862 }
863
864 static int megasas_dcmd_get_bios_info(MegasasState *s, MegasasCmd *cmd)
865 {
866 struct mfi_bios_data info;
867 size_t dcmd_size = sizeof(info);
868
869 memset(&info, 0x0, dcmd_size);
870 if (cmd->iov_size < dcmd_size) {
871 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
872 dcmd_size);
873 return MFI_STAT_INVALID_PARAMETER;
874 }
875 info.continue_on_error = 1;
876 info.verbose = 1;
877 if (megasas_is_jbod(s)) {
878 info.expose_all_drives = 1;
879 }
880
881 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
882 return MFI_STAT_OK;
883 }
884
885 static int megasas_dcmd_get_fw_time(MegasasState *s, MegasasCmd *cmd)
886 {
887 uint64_t fw_time;
888 size_t dcmd_size = sizeof(fw_time);
889
890 fw_time = cpu_to_le64(megasas_fw_time());
891
892 cmd->iov_size -= dma_buf_read((uint8_t *)&fw_time, dcmd_size, &cmd->qsg);
893 return MFI_STAT_OK;
894 }
895
896 static int megasas_dcmd_set_fw_time(MegasasState *s, MegasasCmd *cmd)
897 {
898 uint64_t fw_time;
899
900 /* This is a dummy; setting of firmware time is not allowed */
901 memcpy(&fw_time, cmd->frame->dcmd.mbox, sizeof(fw_time));
902
903 trace_megasas_dcmd_set_fw_time(cmd->index, fw_time);
904 fw_time = cpu_to_le64(megasas_fw_time());
905 return MFI_STAT_OK;
906 }
907
908 static int megasas_event_info(MegasasState *s, MegasasCmd *cmd)
909 {
910 struct mfi_evt_log_state info;
911 size_t dcmd_size = sizeof(info);
912
913 memset(&info, 0, dcmd_size);
914
915 info.newest_seq_num = cpu_to_le32(s->event_count);
916 info.shutdown_seq_num = cpu_to_le32(s->shutdown_event);
917 info.boot_seq_num = cpu_to_le32(s->boot_event);
918
919 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
920 return MFI_STAT_OK;
921 }
922
923 static int megasas_event_wait(MegasasState *s, MegasasCmd *cmd)
924 {
925 union mfi_evt event;
926
927 if (cmd->iov_size < sizeof(struct mfi_evt_detail)) {
928 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
929 sizeof(struct mfi_evt_detail));
930 return MFI_STAT_INVALID_PARAMETER;
931 }
932 s->event_count = cpu_to_le32(cmd->frame->dcmd.mbox[0]);
933 event.word = cpu_to_le32(cmd->frame->dcmd.mbox[4]);
934 s->event_locale = event.members.locale;
935 s->event_class = event.members.class;
936 s->event_cmd = cmd;
937 /* Decrease busy count; event frame doesn't count here */
938 s->busy--;
939 cmd->iov_size = sizeof(struct mfi_evt_detail);
940 return MFI_STAT_INVALID_STATUS;
941 }
942
943 static int megasas_dcmd_pd_get_list(MegasasState *s, MegasasCmd *cmd)
944 {
945 struct mfi_pd_list info;
946 size_t dcmd_size = sizeof(info);
947 BusChild *kid;
948 uint32_t offset, dcmd_limit, num_pd_disks = 0, max_pd_disks;
949
950 memset(&info, 0, dcmd_size);
951 offset = 8;
952 dcmd_limit = offset + sizeof(struct mfi_pd_address);
953 if (cmd->iov_size < dcmd_limit) {
954 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
955 dcmd_limit);
956 return MFI_STAT_INVALID_PARAMETER;
957 }
958
959 max_pd_disks = (cmd->iov_size - offset) / sizeof(struct mfi_pd_address);
960 if (max_pd_disks > MFI_MAX_SYS_PDS) {
961 max_pd_disks = MFI_MAX_SYS_PDS;
962 }
963 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
964 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
965 uint16_t pd_id;
966
967 if (num_pd_disks >= max_pd_disks)
968 break;
969
970 pd_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
971 info.addr[num_pd_disks].device_id = cpu_to_le16(pd_id);
972 info.addr[num_pd_disks].encl_device_id = 0xFFFF;
973 info.addr[num_pd_disks].encl_index = 0;
974 info.addr[num_pd_disks].slot_number = sdev->id & 0xFF;
975 info.addr[num_pd_disks].scsi_dev_type = sdev->type;
976 info.addr[num_pd_disks].connect_port_bitmap = 0x1;
977 info.addr[num_pd_disks].sas_addr[0] =
978 cpu_to_le64(megasas_get_sata_addr(pd_id));
979 num_pd_disks++;
980 offset += sizeof(struct mfi_pd_address);
981 }
982 trace_megasas_dcmd_pd_get_list(cmd->index, num_pd_disks,
983 max_pd_disks, offset);
984
985 info.size = cpu_to_le32(offset);
986 info.count = cpu_to_le32(num_pd_disks);
987
988 cmd->iov_size -= dma_buf_read((uint8_t *)&info, offset, &cmd->qsg);
989 return MFI_STAT_OK;
990 }
991
992 static int megasas_dcmd_pd_list_query(MegasasState *s, MegasasCmd *cmd)
993 {
994 uint16_t flags;
995
996 /* mbox0 contains flags */
997 flags = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
998 trace_megasas_dcmd_pd_list_query(cmd->index, flags);
999 if (flags == MR_PD_QUERY_TYPE_ALL ||
1000 megasas_is_jbod(s)) {
1001 return megasas_dcmd_pd_get_list(s, cmd);
1002 }
1003
1004 return MFI_STAT_OK;
1005 }
1006
1007 static int megasas_pd_get_info_submit(SCSIDevice *sdev, int lun,
1008 MegasasCmd *cmd)
1009 {
1010 struct mfi_pd_info *info = cmd->iov_buf;
1011 size_t dcmd_size = sizeof(struct mfi_pd_info);
1012 uint64_t pd_size;
1013 uint16_t pd_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
1014 uint8_t cmdbuf[6];
1015 SCSIRequest *req;
1016 size_t len, resid;
1017
1018 if (!cmd->iov_buf) {
1019 cmd->iov_buf = g_malloc0(dcmd_size);
1020 info = cmd->iov_buf;
1021 info->inquiry_data[0] = 0x7f; /* Force PQual 0x3, PType 0x1f */
1022 info->vpd_page83[0] = 0x7f;
1023 megasas_setup_inquiry(cmdbuf, 0, sizeof(info->inquiry_data));
1024 req = scsi_req_new(sdev, cmd->index, lun, cmdbuf, cmd);
1025 if (!req) {
1026 trace_megasas_dcmd_req_alloc_failed(cmd->index,
1027 "PD get info std inquiry");
1028 g_free(cmd->iov_buf);
1029 cmd->iov_buf = NULL;
1030 return MFI_STAT_FLASH_ALLOC_FAIL;
1031 }
1032 trace_megasas_dcmd_internal_submit(cmd->index,
1033 "PD get info std inquiry", lun);
1034 len = scsi_req_enqueue(req);
1035 if (len > 0) {
1036 cmd->iov_size = len;
1037 scsi_req_continue(req);
1038 }
1039 return MFI_STAT_INVALID_STATUS;
1040 } else if (info->inquiry_data[0] != 0x7f && info->vpd_page83[0] == 0x7f) {
1041 megasas_setup_inquiry(cmdbuf, 0x83, sizeof(info->vpd_page83));
1042 req = scsi_req_new(sdev, cmd->index, lun, cmdbuf, cmd);
1043 if (!req) {
1044 trace_megasas_dcmd_req_alloc_failed(cmd->index,
1045 "PD get info vpd inquiry");
1046 return MFI_STAT_FLASH_ALLOC_FAIL;
1047 }
1048 trace_megasas_dcmd_internal_submit(cmd->index,
1049 "PD get info vpd inquiry", lun);
1050 len = scsi_req_enqueue(req);
1051 if (len > 0) {
1052 cmd->iov_size = len;
1053 scsi_req_continue(req);
1054 }
1055 return MFI_STAT_INVALID_STATUS;
1056 }
1057 /* Finished, set FW state */
1058 if ((info->inquiry_data[0] >> 5) == 0) {
1059 if (megasas_is_jbod(cmd->state)) {
1060 info->fw_state = cpu_to_le16(MFI_PD_STATE_SYSTEM);
1061 } else {
1062 info->fw_state = cpu_to_le16(MFI_PD_STATE_ONLINE);
1063 }
1064 } else {
1065 info->fw_state = cpu_to_le16(MFI_PD_STATE_OFFLINE);
1066 }
1067
1068 info->ref.v.device_id = cpu_to_le16(pd_id);
1069 info->state.ddf.pd_type = cpu_to_le16(MFI_PD_DDF_TYPE_IN_VD|
1070 MFI_PD_DDF_TYPE_INTF_SAS);
1071 blk_get_geometry(sdev->conf.blk, &pd_size);
1072 info->raw_size = cpu_to_le64(pd_size);
1073 info->non_coerced_size = cpu_to_le64(pd_size);
1074 info->coerced_size = cpu_to_le64(pd_size);
1075 info->encl_device_id = 0xFFFF;
1076 info->slot_number = (sdev->id & 0xFF);
1077 info->path_info.count = 1;
1078 info->path_info.sas_addr[0] =
1079 cpu_to_le64(megasas_get_sata_addr(pd_id));
1080 info->connected_port_bitmap = 0x1;
1081 info->device_speed = 1;
1082 info->link_speed = 1;
1083 resid = dma_buf_read(cmd->iov_buf, dcmd_size, &cmd->qsg);
1084 g_free(cmd->iov_buf);
1085 cmd->iov_size = dcmd_size - resid;
1086 cmd->iov_buf = NULL;
1087 return MFI_STAT_OK;
1088 }
1089
1090 static int megasas_dcmd_pd_get_info(MegasasState *s, MegasasCmd *cmd)
1091 {
1092 size_t dcmd_size = sizeof(struct mfi_pd_info);
1093 uint16_t pd_id;
1094 uint8_t target_id, lun_id;
1095 SCSIDevice *sdev = NULL;
1096 int retval = MFI_STAT_DEVICE_NOT_FOUND;
1097
1098 if (cmd->iov_size < dcmd_size) {
1099 return MFI_STAT_INVALID_PARAMETER;
1100 }
1101
1102 /* mbox0 has the ID */
1103 pd_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
1104 target_id = (pd_id >> 8) & 0xFF;
1105 lun_id = pd_id & 0xFF;
1106 sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
1107 trace_megasas_dcmd_pd_get_info(cmd->index, pd_id);
1108
1109 if (sdev) {
1110 /* Submit inquiry */
1111 retval = megasas_pd_get_info_submit(sdev, pd_id, cmd);
1112 }
1113
1114 return retval;
1115 }
1116
1117 static int megasas_dcmd_ld_get_list(MegasasState *s, MegasasCmd *cmd)
1118 {
1119 struct mfi_ld_list info;
1120 size_t dcmd_size = sizeof(info), resid;
1121 uint32_t num_ld_disks = 0, max_ld_disks;
1122 uint64_t ld_size;
1123 BusChild *kid;
1124
1125 memset(&info, 0, dcmd_size);
1126 if (cmd->iov_size > dcmd_size) {
1127 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1128 dcmd_size);
1129 return MFI_STAT_INVALID_PARAMETER;
1130 }
1131
1132 max_ld_disks = (cmd->iov_size - 8) / 16;
1133 if (megasas_is_jbod(s)) {
1134 max_ld_disks = 0;
1135 }
1136 if (max_ld_disks > MFI_MAX_LD) {
1137 max_ld_disks = MFI_MAX_LD;
1138 }
1139 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1140 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
1141
1142 if (num_ld_disks >= max_ld_disks) {
1143 break;
1144 }
1145 /* Logical device size is in blocks */
1146 blk_get_geometry(sdev->conf.blk, &ld_size);
1147 info.ld_list[num_ld_disks].ld.v.target_id = sdev->id;
1148 info.ld_list[num_ld_disks].state = MFI_LD_STATE_OPTIMAL;
1149 info.ld_list[num_ld_disks].size = cpu_to_le64(ld_size);
1150 num_ld_disks++;
1151 }
1152 info.ld_count = cpu_to_le32(num_ld_disks);
1153 trace_megasas_dcmd_ld_get_list(cmd->index, num_ld_disks, max_ld_disks);
1154
1155 resid = dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
1156 cmd->iov_size = dcmd_size - resid;
1157 return MFI_STAT_OK;
1158 }
1159
1160 static int megasas_dcmd_ld_list_query(MegasasState *s, MegasasCmd *cmd)
1161 {
1162 uint16_t flags;
1163 struct mfi_ld_targetid_list info;
1164 size_t dcmd_size = sizeof(info), resid;
1165 uint32_t num_ld_disks = 0, max_ld_disks = s->fw_luns;
1166 BusChild *kid;
1167
1168 /* mbox0 contains flags */
1169 flags = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
1170 trace_megasas_dcmd_ld_list_query(cmd->index, flags);
1171 if (flags != MR_LD_QUERY_TYPE_ALL &&
1172 flags != MR_LD_QUERY_TYPE_EXPOSED_TO_HOST) {
1173 max_ld_disks = 0;
1174 }
1175
1176 memset(&info, 0, dcmd_size);
1177 if (cmd->iov_size < 12) {
1178 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1179 dcmd_size);
1180 return MFI_STAT_INVALID_PARAMETER;
1181 }
1182 dcmd_size = sizeof(uint32_t) * 2 + 3;
1183 max_ld_disks = cmd->iov_size - dcmd_size;
1184 if (megasas_is_jbod(s)) {
1185 max_ld_disks = 0;
1186 }
1187 if (max_ld_disks > MFI_MAX_LD) {
1188 max_ld_disks = MFI_MAX_LD;
1189 }
1190 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1191 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
1192
1193 if (num_ld_disks >= max_ld_disks) {
1194 break;
1195 }
1196 info.targetid[num_ld_disks] = sdev->lun;
1197 num_ld_disks++;
1198 dcmd_size++;
1199 }
1200 info.ld_count = cpu_to_le32(num_ld_disks);
1201 info.size = dcmd_size;
1202 trace_megasas_dcmd_ld_get_list(cmd->index, num_ld_disks, max_ld_disks);
1203
1204 resid = dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
1205 cmd->iov_size = dcmd_size - resid;
1206 return MFI_STAT_OK;
1207 }
1208
1209 static int megasas_ld_get_info_submit(SCSIDevice *sdev, int lun,
1210 MegasasCmd *cmd)
1211 {
1212 struct mfi_ld_info *info = cmd->iov_buf;
1213 size_t dcmd_size = sizeof(struct mfi_ld_info);
1214 uint8_t cdb[6];
1215 SCSIRequest *req;
1216 ssize_t len, resid;
1217 uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
1218 uint64_t ld_size;
1219
1220 if (!cmd->iov_buf) {
1221 cmd->iov_buf = g_malloc0(dcmd_size);
1222 info = cmd->iov_buf;
1223 megasas_setup_inquiry(cdb, 0x83, sizeof(info->vpd_page83));
1224 req = scsi_req_new(sdev, cmd->index, lun, cdb, cmd);
1225 if (!req) {
1226 trace_megasas_dcmd_req_alloc_failed(cmd->index,
1227 "LD get info vpd inquiry");
1228 g_free(cmd->iov_buf);
1229 cmd->iov_buf = NULL;
1230 return MFI_STAT_FLASH_ALLOC_FAIL;
1231 }
1232 trace_megasas_dcmd_internal_submit(cmd->index,
1233 "LD get info vpd inquiry", lun);
1234 len = scsi_req_enqueue(req);
1235 if (len > 0) {
1236 cmd->iov_size = len;
1237 scsi_req_continue(req);
1238 }
1239 return MFI_STAT_INVALID_STATUS;
1240 }
1241
1242 info->ld_config.params.state = MFI_LD_STATE_OPTIMAL;
1243 info->ld_config.properties.ld.v.target_id = lun;
1244 info->ld_config.params.stripe_size = 3;
1245 info->ld_config.params.num_drives = 1;
1246 info->ld_config.params.is_consistent = 1;
1247 /* Logical device size is in blocks */
1248 blk_get_geometry(sdev->conf.blk, &ld_size);
1249 info->size = cpu_to_le64(ld_size);
1250 memset(info->ld_config.span, 0, sizeof(info->ld_config.span));
1251 info->ld_config.span[0].start_block = 0;
1252 info->ld_config.span[0].num_blocks = info->size;
1253 info->ld_config.span[0].array_ref = cpu_to_le16(sdev_id);
1254
1255 resid = dma_buf_read(cmd->iov_buf, dcmd_size, &cmd->qsg);
1256 g_free(cmd->iov_buf);
1257 cmd->iov_size = dcmd_size - resid;
1258 cmd->iov_buf = NULL;
1259 return MFI_STAT_OK;
1260 }
1261
1262 static int megasas_dcmd_ld_get_info(MegasasState *s, MegasasCmd *cmd)
1263 {
1264 struct mfi_ld_info info;
1265 size_t dcmd_size = sizeof(info);
1266 uint16_t ld_id;
1267 uint32_t max_ld_disks = s->fw_luns;
1268 SCSIDevice *sdev = NULL;
1269 int retval = MFI_STAT_DEVICE_NOT_FOUND;
1270
1271 if (cmd->iov_size < dcmd_size) {
1272 return MFI_STAT_INVALID_PARAMETER;
1273 }
1274
1275 /* mbox0 has the ID */
1276 ld_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
1277 trace_megasas_dcmd_ld_get_info(cmd->index, ld_id);
1278
1279 if (megasas_is_jbod(s)) {
1280 return MFI_STAT_DEVICE_NOT_FOUND;
1281 }
1282
1283 if (ld_id < max_ld_disks) {
1284 sdev = scsi_device_find(&s->bus, 0, ld_id, 0);
1285 }
1286
1287 if (sdev) {
1288 retval = megasas_ld_get_info_submit(sdev, ld_id, cmd);
1289 }
1290
1291 return retval;
1292 }
1293
1294 static int megasas_dcmd_cfg_read(MegasasState *s, MegasasCmd *cmd)
1295 {
1296 uint8_t data[4096];
1297 struct mfi_config_data *info;
1298 int num_pd_disks = 0, array_offset, ld_offset;
1299 BusChild *kid;
1300
1301 if (cmd->iov_size > 4096) {
1302 return MFI_STAT_INVALID_PARAMETER;
1303 }
1304
1305 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1306 num_pd_disks++;
1307 }
1308 info = (struct mfi_config_data *)&data;
1309 /*
1310 * Array mapping:
1311 * - One array per SCSI device
1312 * - One logical drive per SCSI device
1313 * spanning the entire device
1314 */
1315 info->array_count = num_pd_disks;
1316 info->array_size = sizeof(struct mfi_array) * num_pd_disks;
1317 info->log_drv_count = num_pd_disks;
1318 info->log_drv_size = sizeof(struct mfi_ld_config) * num_pd_disks;
1319 info->spares_count = 0;
1320 info->spares_size = sizeof(struct mfi_spare);
1321 info->size = sizeof(struct mfi_config_data) + info->array_size +
1322 info->log_drv_size;
1323 if (info->size > 4096) {
1324 return MFI_STAT_INVALID_PARAMETER;
1325 }
1326
1327 array_offset = sizeof(struct mfi_config_data);
1328 ld_offset = array_offset + sizeof(struct mfi_array) * num_pd_disks;
1329
1330 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
1331 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
1332 uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (sdev->lun & 0xFF);
1333 struct mfi_array *array;
1334 struct mfi_ld_config *ld;
1335 uint64_t pd_size;
1336 int i;
1337
1338 array = (struct mfi_array *)(data + array_offset);
1339 blk_get_geometry(sdev->conf.blk, &pd_size);
1340 array->size = cpu_to_le64(pd_size);
1341 array->num_drives = 1;
1342 array->array_ref = cpu_to_le16(sdev_id);
1343 array->pd[0].ref.v.device_id = cpu_to_le16(sdev_id);
1344 array->pd[0].ref.v.seq_num = 0;
1345 array->pd[0].fw_state = MFI_PD_STATE_ONLINE;
1346 array->pd[0].encl.pd = 0xFF;
1347 array->pd[0].encl.slot = (sdev->id & 0xFF);
1348 for (i = 1; i < MFI_MAX_ROW_SIZE; i++) {
1349 array->pd[i].ref.v.device_id = 0xFFFF;
1350 array->pd[i].ref.v.seq_num = 0;
1351 array->pd[i].fw_state = MFI_PD_STATE_UNCONFIGURED_GOOD;
1352 array->pd[i].encl.pd = 0xFF;
1353 array->pd[i].encl.slot = 0xFF;
1354 }
1355 array_offset += sizeof(struct mfi_array);
1356 ld = (struct mfi_ld_config *)(data + ld_offset);
1357 memset(ld, 0, sizeof(struct mfi_ld_config));
1358 ld->properties.ld.v.target_id = sdev->id;
1359 ld->properties.default_cache_policy = MR_LD_CACHE_READ_AHEAD |
1360 MR_LD_CACHE_READ_ADAPTIVE;
1361 ld->properties.current_cache_policy = MR_LD_CACHE_READ_AHEAD |
1362 MR_LD_CACHE_READ_ADAPTIVE;
1363 ld->params.state = MFI_LD_STATE_OPTIMAL;
1364 ld->params.stripe_size = 3;
1365 ld->params.num_drives = 1;
1366 ld->params.span_depth = 1;
1367 ld->params.is_consistent = 1;
1368 ld->span[0].start_block = 0;
1369 ld->span[0].num_blocks = cpu_to_le64(pd_size);
1370 ld->span[0].array_ref = cpu_to_le16(sdev_id);
1371 ld_offset += sizeof(struct mfi_ld_config);
1372 }
1373
1374 cmd->iov_size -= dma_buf_read((uint8_t *)data, info->size, &cmd->qsg);
1375 return MFI_STAT_OK;
1376 }
1377
1378 static int megasas_dcmd_get_properties(MegasasState *s, MegasasCmd *cmd)
1379 {
1380 struct mfi_ctrl_props info;
1381 size_t dcmd_size = sizeof(info);
1382
1383 memset(&info, 0x0, dcmd_size);
1384 if (cmd->iov_size < dcmd_size) {
1385 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1386 dcmd_size);
1387 return MFI_STAT_INVALID_PARAMETER;
1388 }
1389 info.pred_fail_poll_interval = cpu_to_le16(300);
1390 info.intr_throttle_cnt = cpu_to_le16(16);
1391 info.intr_throttle_timeout = cpu_to_le16(50);
1392 info.rebuild_rate = 30;
1393 info.patrol_read_rate = 30;
1394 info.bgi_rate = 30;
1395 info.cc_rate = 30;
1396 info.recon_rate = 30;
1397 info.cache_flush_interval = 4;
1398 info.spinup_drv_cnt = 2;
1399 info.spinup_delay = 6;
1400 info.ecc_bucket_size = 15;
1401 info.ecc_bucket_leak_rate = cpu_to_le16(1440);
1402 info.expose_encl_devices = 1;
1403
1404 cmd->iov_size -= dma_buf_read((uint8_t *)&info, dcmd_size, &cmd->qsg);
1405 return MFI_STAT_OK;
1406 }
1407
1408 static int megasas_cache_flush(MegasasState *s, MegasasCmd *cmd)
1409 {
1410 blk_drain_all();
1411 return MFI_STAT_OK;
1412 }
1413
1414 static int megasas_ctrl_shutdown(MegasasState *s, MegasasCmd *cmd)
1415 {
1416 s->fw_state = MFI_FWSTATE_READY;
1417 return MFI_STAT_OK;
1418 }
1419
1420 /* Some implementations use CLUSTER RESET LD to simulate a device reset */
1421 static int megasas_cluster_reset_ld(MegasasState *s, MegasasCmd *cmd)
1422 {
1423 uint16_t target_id;
1424 int i;
1425
1426 /* mbox0 contains the device index */
1427 target_id = le16_to_cpu(cmd->frame->dcmd.mbox[0]);
1428 trace_megasas_dcmd_reset_ld(cmd->index, target_id);
1429 for (i = 0; i < s->fw_cmds; i++) {
1430 MegasasCmd *tmp_cmd = &s->frames[i];
1431 if (tmp_cmd->req && tmp_cmd->req->dev->id == target_id) {
1432 SCSIDevice *d = tmp_cmd->req->dev;
1433 qdev_reset_all(&d->qdev);
1434 }
1435 }
1436 return MFI_STAT_OK;
1437 }
1438
1439 static int megasas_dcmd_set_properties(MegasasState *s, MegasasCmd *cmd)
1440 {
1441 struct mfi_ctrl_props info;
1442 size_t dcmd_size = sizeof(info);
1443
1444 if (cmd->iov_size < dcmd_size) {
1445 trace_megasas_dcmd_invalid_xfer_len(cmd->index, cmd->iov_size,
1446 dcmd_size);
1447 return MFI_STAT_INVALID_PARAMETER;
1448 }
1449 dma_buf_write((uint8_t *)&info, cmd->iov_size, &cmd->qsg);
1450 trace_megasas_dcmd_unsupported(cmd->index, cmd->iov_size);
1451 return MFI_STAT_OK;
1452 }
1453
1454 static int megasas_dcmd_dummy(MegasasState *s, MegasasCmd *cmd)
1455 {
1456 trace_megasas_dcmd_dummy(cmd->index, cmd->iov_size);
1457 return MFI_STAT_OK;
1458 }
1459
1460 static const struct dcmd_cmd_tbl_t {
1461 int opcode;
1462 const char *desc;
1463 int (*func)(MegasasState *s, MegasasCmd *cmd);
1464 } dcmd_cmd_tbl[] = {
1465 { MFI_DCMD_CTRL_MFI_HOST_MEM_ALLOC, "CTRL_HOST_MEM_ALLOC",
1466 megasas_dcmd_dummy },
1467 { MFI_DCMD_CTRL_GET_INFO, "CTRL_GET_INFO",
1468 megasas_ctrl_get_info },
1469 { MFI_DCMD_CTRL_GET_PROPERTIES, "CTRL_GET_PROPERTIES",
1470 megasas_dcmd_get_properties },
1471 { MFI_DCMD_CTRL_SET_PROPERTIES, "CTRL_SET_PROPERTIES",
1472 megasas_dcmd_set_properties },
1473 { MFI_DCMD_CTRL_ALARM_GET, "CTRL_ALARM_GET",
1474 megasas_dcmd_dummy },
1475 { MFI_DCMD_CTRL_ALARM_ENABLE, "CTRL_ALARM_ENABLE",
1476 megasas_dcmd_dummy },
1477 { MFI_DCMD_CTRL_ALARM_DISABLE, "CTRL_ALARM_DISABLE",
1478 megasas_dcmd_dummy },
1479 { MFI_DCMD_CTRL_ALARM_SILENCE, "CTRL_ALARM_SILENCE",
1480 megasas_dcmd_dummy },
1481 { MFI_DCMD_CTRL_ALARM_TEST, "CTRL_ALARM_TEST",
1482 megasas_dcmd_dummy },
1483 { MFI_DCMD_CTRL_EVENT_GETINFO, "CTRL_EVENT_GETINFO",
1484 megasas_event_info },
1485 { MFI_DCMD_CTRL_EVENT_GET, "CTRL_EVENT_GET",
1486 megasas_dcmd_dummy },
1487 { MFI_DCMD_CTRL_EVENT_WAIT, "CTRL_EVENT_WAIT",
1488 megasas_event_wait },
1489 { MFI_DCMD_CTRL_SHUTDOWN, "CTRL_SHUTDOWN",
1490 megasas_ctrl_shutdown },
1491 { MFI_DCMD_HIBERNATE_STANDBY, "CTRL_STANDBY",
1492 megasas_dcmd_dummy },
1493 { MFI_DCMD_CTRL_GET_TIME, "CTRL_GET_TIME",
1494 megasas_dcmd_get_fw_time },
1495 { MFI_DCMD_CTRL_SET_TIME, "CTRL_SET_TIME",
1496 megasas_dcmd_set_fw_time },
1497 { MFI_DCMD_CTRL_BIOS_DATA_GET, "CTRL_BIOS_DATA_GET",
1498 megasas_dcmd_get_bios_info },
1499 { MFI_DCMD_CTRL_FACTORY_DEFAULTS, "CTRL_FACTORY_DEFAULTS",
1500 megasas_dcmd_dummy },
1501 { MFI_DCMD_CTRL_MFC_DEFAULTS_GET, "CTRL_MFC_DEFAULTS_GET",
1502 megasas_mfc_get_defaults },
1503 { MFI_DCMD_CTRL_MFC_DEFAULTS_SET, "CTRL_MFC_DEFAULTS_SET",
1504 megasas_dcmd_dummy },
1505 { MFI_DCMD_CTRL_CACHE_FLUSH, "CTRL_CACHE_FLUSH",
1506 megasas_cache_flush },
1507 { MFI_DCMD_PD_GET_LIST, "PD_GET_LIST",
1508 megasas_dcmd_pd_get_list },
1509 { MFI_DCMD_PD_LIST_QUERY, "PD_LIST_QUERY",
1510 megasas_dcmd_pd_list_query },
1511 { MFI_DCMD_PD_GET_INFO, "PD_GET_INFO",
1512 megasas_dcmd_pd_get_info },
1513 { MFI_DCMD_PD_STATE_SET, "PD_STATE_SET",
1514 megasas_dcmd_dummy },
1515 { MFI_DCMD_PD_REBUILD, "PD_REBUILD",
1516 megasas_dcmd_dummy },
1517 { MFI_DCMD_PD_BLINK, "PD_BLINK",
1518 megasas_dcmd_dummy },
1519 { MFI_DCMD_PD_UNBLINK, "PD_UNBLINK",
1520 megasas_dcmd_dummy },
1521 { MFI_DCMD_LD_GET_LIST, "LD_GET_LIST",
1522 megasas_dcmd_ld_get_list},
1523 { MFI_DCMD_LD_LIST_QUERY, "LD_LIST_QUERY",
1524 megasas_dcmd_ld_list_query },
1525 { MFI_DCMD_LD_GET_INFO, "LD_GET_INFO",
1526 megasas_dcmd_ld_get_info },
1527 { MFI_DCMD_LD_GET_PROP, "LD_GET_PROP",
1528 megasas_dcmd_dummy },
1529 { MFI_DCMD_LD_SET_PROP, "LD_SET_PROP",
1530 megasas_dcmd_dummy },
1531 { MFI_DCMD_LD_DELETE, "LD_DELETE",
1532 megasas_dcmd_dummy },
1533 { MFI_DCMD_CFG_READ, "CFG_READ",
1534 megasas_dcmd_cfg_read },
1535 { MFI_DCMD_CFG_ADD, "CFG_ADD",
1536 megasas_dcmd_dummy },
1537 { MFI_DCMD_CFG_CLEAR, "CFG_CLEAR",
1538 megasas_dcmd_dummy },
1539 { MFI_DCMD_CFG_FOREIGN_READ, "CFG_FOREIGN_READ",
1540 megasas_dcmd_dummy },
1541 { MFI_DCMD_CFG_FOREIGN_IMPORT, "CFG_FOREIGN_IMPORT",
1542 megasas_dcmd_dummy },
1543 { MFI_DCMD_BBU_STATUS, "BBU_STATUS",
1544 megasas_dcmd_dummy },
1545 { MFI_DCMD_BBU_CAPACITY_INFO, "BBU_CAPACITY_INFO",
1546 megasas_dcmd_dummy },
1547 { MFI_DCMD_BBU_DESIGN_INFO, "BBU_DESIGN_INFO",
1548 megasas_dcmd_dummy },
1549 { MFI_DCMD_BBU_PROP_GET, "BBU_PROP_GET",
1550 megasas_dcmd_dummy },
1551 { MFI_DCMD_CLUSTER, "CLUSTER",
1552 megasas_dcmd_dummy },
1553 { MFI_DCMD_CLUSTER_RESET_ALL, "CLUSTER_RESET_ALL",
1554 megasas_dcmd_dummy },
1555 { MFI_DCMD_CLUSTER_RESET_LD, "CLUSTER_RESET_LD",
1556 megasas_cluster_reset_ld },
1557 { -1, NULL, NULL }
1558 };
1559
1560 static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
1561 {
1562 int opcode, len;
1563 int retval = 0;
1564 const struct dcmd_cmd_tbl_t *cmdptr = dcmd_cmd_tbl;
1565
1566 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
1567 trace_megasas_handle_dcmd(cmd->index, opcode);
1568 len = megasas_map_dcmd(s, cmd);
1569 if (len < 0) {
1570 return MFI_STAT_MEMORY_NOT_AVAILABLE;
1571 }
1572 while (cmdptr->opcode != -1 && cmdptr->opcode != opcode) {
1573 cmdptr++;
1574 }
1575 if (cmdptr->opcode == -1) {
1576 trace_megasas_dcmd_unhandled(cmd->index, opcode, len);
1577 retval = megasas_dcmd_dummy(s, cmd);
1578 } else {
1579 trace_megasas_dcmd_enter(cmd->index, cmdptr->desc, len);
1580 retval = cmdptr->func(s, cmd);
1581 }
1582 if (retval != MFI_STAT_INVALID_STATUS) {
1583 megasas_finish_dcmd(cmd, len);
1584 }
1585 return retval;
1586 }
1587
1588 static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
1589 SCSIRequest *req)
1590 {
1591 int opcode;
1592 int retval = MFI_STAT_OK;
1593 int lun = req->lun;
1594
1595 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
1596 scsi_req_unref(req);
1597 trace_megasas_dcmd_internal_finish(cmd->index, opcode, lun);
1598 switch (opcode) {
1599 case MFI_DCMD_PD_GET_INFO:
1600 retval = megasas_pd_get_info_submit(req->dev, lun, cmd);
1601 break;
1602 case MFI_DCMD_LD_GET_INFO:
1603 retval = megasas_ld_get_info_submit(req->dev, lun, cmd);
1604 break;
1605 default:
1606 trace_megasas_dcmd_internal_invalid(cmd->index, opcode);
1607 retval = MFI_STAT_INVALID_DCMD;
1608 break;
1609 }
1610 if (retval != MFI_STAT_INVALID_STATUS) {
1611 megasas_finish_dcmd(cmd, cmd->iov_size);
1612 }
1613 return retval;
1614 }
1615
1616 static int megasas_enqueue_req(MegasasCmd *cmd, bool is_write)
1617 {
1618 int len;
1619
1620 len = scsi_req_enqueue(cmd->req);
1621 if (len < 0) {
1622 len = -len;
1623 }
1624 if (len > 0) {
1625 if (len > cmd->iov_size) {
1626 if (is_write) {
1627 trace_megasas_iov_write_overflow(cmd->index, len,
1628 cmd->iov_size);
1629 } else {
1630 trace_megasas_iov_read_overflow(cmd->index, len,
1631 cmd->iov_size);
1632 }
1633 }
1634 if (len < cmd->iov_size) {
1635 if (is_write) {
1636 trace_megasas_iov_write_underflow(cmd->index, len,
1637 cmd->iov_size);
1638 } else {
1639 trace_megasas_iov_read_underflow(cmd->index, len,
1640 cmd->iov_size);
1641 }
1642 cmd->iov_size = len;
1643 }
1644 scsi_req_continue(cmd->req);
1645 }
1646 return len;
1647 }
1648
1649 static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
1650 bool is_logical)
1651 {
1652 uint8_t *cdb;
1653 bool is_write;
1654 struct SCSIDevice *sdev = NULL;
1655
1656 cdb = cmd->frame->pass.cdb;
1657
1658 if (is_logical) {
1659 if (cmd->frame->header.target_id >= MFI_MAX_LD ||
1660 cmd->frame->header.lun_id != 0) {
1661 trace_megasas_scsi_target_not_present(
1662 mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
1663 cmd->frame->header.target_id, cmd->frame->header.lun_id);
1664 return MFI_STAT_DEVICE_NOT_FOUND;
1665 }
1666 }
1667 sdev = scsi_device_find(&s->bus, 0, cmd->frame->header.target_id,
1668 cmd->frame->header.lun_id);
1669
1670 cmd->iov_size = le32_to_cpu(cmd->frame->header.data_len);
1671 trace_megasas_handle_scsi(mfi_frame_desc[cmd->frame->header.frame_cmd],
1672 is_logical, cmd->frame->header.target_id,
1673 cmd->frame->header.lun_id, sdev, cmd->iov_size);
1674
1675 if (!sdev || (megasas_is_jbod(s) && is_logical)) {
1676 trace_megasas_scsi_target_not_present(
1677 mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
1678 cmd->frame->header.target_id, cmd->frame->header.lun_id);
1679 return MFI_STAT_DEVICE_NOT_FOUND;
1680 }
1681
1682 if (cmd->frame->header.cdb_len > 16) {
1683 trace_megasas_scsi_invalid_cdb_len(
1684 mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
1685 cmd->frame->header.target_id, cmd->frame->header.lun_id,
1686 cmd->frame->header.cdb_len);
1687 megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
1688 cmd->frame->header.scsi_status = CHECK_CONDITION;
1689 s->event_count++;
1690 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1691 }
1692
1693 if (megasas_map_sgl(s, cmd, &cmd->frame->pass.sgl)) {
1694 megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
1695 cmd->frame->header.scsi_status = CHECK_CONDITION;
1696 s->event_count++;
1697 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1698 }
1699
1700 cmd->req = scsi_req_new(sdev, cmd->index,
1701 cmd->frame->header.lun_id, cdb, cmd);
1702 if (!cmd->req) {
1703 trace_megasas_scsi_req_alloc_failed(
1704 mfi_frame_desc[cmd->frame->header.frame_cmd],
1705 cmd->frame->header.target_id, cmd->frame->header.lun_id);
1706 megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
1707 cmd->frame->header.scsi_status = BUSY;
1708 s->event_count++;
1709 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1710 }
1711
1712 is_write = (cmd->req->cmd.mode == SCSI_XFER_TO_DEV);
1713 if (cmd->iov_size) {
1714 if (is_write) {
1715 trace_megasas_scsi_write_start(cmd->index, cmd->iov_size);
1716 } else {
1717 trace_megasas_scsi_read_start(cmd->index, cmd->iov_size);
1718 }
1719 } else {
1720 trace_megasas_scsi_nodata(cmd->index);
1721 }
1722 megasas_enqueue_req(cmd, is_write);
1723 return MFI_STAT_INVALID_STATUS;
1724 }
1725
1726 static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd)
1727 {
1728 uint32_t lba_count, lba_start_hi, lba_start_lo;
1729 uint64_t lba_start;
1730 bool is_write = (cmd->frame->header.frame_cmd == MFI_CMD_LD_WRITE);
1731 uint8_t cdb[16];
1732 int len;
1733 struct SCSIDevice *sdev = NULL;
1734
1735 lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
1736 lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
1737 lba_start_hi = le32_to_cpu(cmd->frame->io.lba_hi);
1738 lba_start = ((uint64_t)lba_start_hi << 32) | lba_start_lo;
1739
1740 if (cmd->frame->header.target_id < MFI_MAX_LD &&
1741 cmd->frame->header.lun_id == 0) {
1742 sdev = scsi_device_find(&s->bus, 0, cmd->frame->header.target_id,
1743 cmd->frame->header.lun_id);
1744 }
1745
1746 trace_megasas_handle_io(cmd->index,
1747 mfi_frame_desc[cmd->frame->header.frame_cmd],
1748 cmd->frame->header.target_id,
1749 cmd->frame->header.lun_id,
1750 (unsigned long)lba_start, (unsigned long)lba_count);
1751 if (!sdev) {
1752 trace_megasas_io_target_not_present(cmd->index,
1753 mfi_frame_desc[cmd->frame->header.frame_cmd],
1754 cmd->frame->header.target_id, cmd->frame->header.lun_id);
1755 return MFI_STAT_DEVICE_NOT_FOUND;
1756 }
1757
1758 if (cmd->frame->header.cdb_len > 16) {
1759 trace_megasas_scsi_invalid_cdb_len(
1760 mfi_frame_desc[cmd->frame->header.frame_cmd], 1,
1761 cmd->frame->header.target_id, cmd->frame->header.lun_id,
1762 cmd->frame->header.cdb_len);
1763 megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
1764 cmd->frame->header.scsi_status = CHECK_CONDITION;
1765 s->event_count++;
1766 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1767 }
1768
1769 cmd->iov_size = lba_count * sdev->blocksize;
1770 if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
1771 megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
1772 cmd->frame->header.scsi_status = CHECK_CONDITION;
1773 s->event_count++;
1774 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1775 }
1776
1777 megasas_encode_lba(cdb, lba_start, lba_count, is_write);
1778 cmd->req = scsi_req_new(sdev, cmd->index,
1779 cmd->frame->header.lun_id, cdb, cmd);
1780 if (!cmd->req) {
1781 trace_megasas_scsi_req_alloc_failed(
1782 mfi_frame_desc[cmd->frame->header.frame_cmd],
1783 cmd->frame->header.target_id, cmd->frame->header.lun_id);
1784 megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
1785 cmd->frame->header.scsi_status = BUSY;
1786 s->event_count++;
1787 return MFI_STAT_SCSI_DONE_WITH_ERROR;
1788 }
1789 len = megasas_enqueue_req(cmd, is_write);
1790 if (len > 0) {
1791 if (is_write) {
1792 trace_megasas_io_write_start(cmd->index, lba_start, lba_count, len);
1793 } else {
1794 trace_megasas_io_read_start(cmd->index, lba_start, lba_count, len);
1795 }
1796 }
1797 return MFI_STAT_INVALID_STATUS;
1798 }
1799
1800 static int megasas_finish_internal_command(MegasasCmd *cmd,
1801 SCSIRequest *req, size_t resid)
1802 {
1803 int retval = MFI_STAT_INVALID_CMD;
1804
1805 if (cmd->frame->header.frame_cmd == MFI_CMD_DCMD) {
1806 cmd->iov_size -= resid;
1807 retval = megasas_finish_internal_dcmd(cmd, req);
1808 }
1809 return retval;
1810 }
1811
1812 static QEMUSGList *megasas_get_sg_list(SCSIRequest *req)
1813 {
1814 MegasasCmd *cmd = req->hba_private;
1815
1816 if (cmd->frame->header.frame_cmd == MFI_CMD_DCMD) {
1817 return NULL;
1818 } else {
1819 return &cmd->qsg;
1820 }
1821 }
1822
1823 static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
1824 {
1825 MegasasCmd *cmd = req->hba_private;
1826 uint8_t *buf;
1827 uint32_t opcode;
1828
1829 trace_megasas_io_complete(cmd->index, len);
1830
1831 if (cmd->frame->header.frame_cmd != MFI_CMD_DCMD) {
1832 scsi_req_continue(req);
1833 return;
1834 }
1835
1836 buf = scsi_req_get_buf(req);
1837 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
1838 if (opcode == MFI_DCMD_PD_GET_INFO && cmd->iov_buf) {
1839 struct mfi_pd_info *info = cmd->iov_buf;
1840
1841 if (info->inquiry_data[0] == 0x7f) {
1842 memset(info->inquiry_data, 0, sizeof(info->inquiry_data));
1843 memcpy(info->inquiry_data, buf, len);
1844 } else if (info->vpd_page83[0] == 0x7f) {
1845 memset(info->vpd_page83, 0, sizeof(info->vpd_page83));
1846 memcpy(info->vpd_page83, buf, len);
1847 }
1848 scsi_req_continue(req);
1849 } else if (opcode == MFI_DCMD_LD_GET_INFO) {
1850 struct mfi_ld_info *info = cmd->iov_buf;
1851
1852 if (cmd->iov_buf) {
1853 memcpy(info->vpd_page83, buf, sizeof(info->vpd_page83));
1854 scsi_req_continue(req);
1855 }
1856 }
1857 }
1858
1859 static void megasas_command_complete(SCSIRequest *req, uint32_t status,
1860 size_t resid)
1861 {
1862 MegasasCmd *cmd = req->hba_private;
1863 uint8_t cmd_status = MFI_STAT_OK;
1864
1865 trace_megasas_command_complete(cmd->index, status, resid);
1866
1867 if (cmd->req != req) {
1868 /*
1869 * Internal command complete
1870 */
1871 cmd_status = megasas_finish_internal_command(cmd, req, resid);
1872 if (cmd_status == MFI_STAT_INVALID_STATUS) {
1873 return;
1874 }
1875 } else {
1876 req->status = status;
1877 trace_megasas_scsi_complete(cmd->index, req->status,
1878 cmd->iov_size, req->cmd.xfer);
1879 if (req->status != GOOD) {
1880 cmd_status = MFI_STAT_SCSI_DONE_WITH_ERROR;
1881 }
1882 if (req->status == CHECK_CONDITION) {
1883 megasas_copy_sense(cmd);
1884 }
1885
1886 megasas_unmap_sgl(cmd);
1887 cmd->frame->header.scsi_status = req->status;
1888 scsi_req_unref(cmd->req);
1889 cmd->req = NULL;
1890 }
1891 cmd->frame->header.cmd_status = cmd_status;
1892 megasas_unmap_frame(cmd->state, cmd);
1893 megasas_complete_frame(cmd->state, cmd->context);
1894 }
1895
1896 static void megasas_command_cancel(SCSIRequest *req)
1897 {
1898 MegasasCmd *cmd = req->hba_private;
1899
1900 if (cmd) {
1901 megasas_abort_command(cmd);
1902 } else {
1903 scsi_req_unref(req);
1904 }
1905 }
1906
1907 static int megasas_handle_abort(MegasasState *s, MegasasCmd *cmd)
1908 {
1909 uint64_t abort_ctx = le64_to_cpu(cmd->frame->abort.abort_context);
1910 hwaddr abort_addr, addr_hi, addr_lo;
1911 MegasasCmd *abort_cmd;
1912
1913 addr_hi = le32_to_cpu(cmd->frame->abort.abort_mfi_addr_hi);
1914 addr_lo = le32_to_cpu(cmd->frame->abort.abort_mfi_addr_lo);
1915 abort_addr = ((uint64_t)addr_hi << 32) | addr_lo;
1916
1917 abort_cmd = megasas_lookup_frame(s, abort_addr);
1918 if (!abort_cmd) {
1919 trace_megasas_abort_no_cmd(cmd->index, abort_ctx);
1920 s->event_count++;
1921 return MFI_STAT_OK;
1922 }
1923 if (!megasas_use_queue64(s)) {
1924 abort_ctx &= (uint64_t)0xFFFFFFFF;
1925 }
1926 if (abort_cmd->context != abort_ctx) {
1927 trace_megasas_abort_invalid_context(cmd->index, abort_cmd->index,
1928 abort_cmd->context);
1929 s->event_count++;
1930 return MFI_STAT_ABORT_NOT_POSSIBLE;
1931 }
1932 trace_megasas_abort_frame(cmd->index, abort_cmd->index);
1933 megasas_abort_command(abort_cmd);
1934 if (!s->event_cmd || abort_cmd != s->event_cmd) {
1935 s->event_cmd = NULL;
1936 }
1937 s->event_count++;
1938 return MFI_STAT_OK;
1939 }
1940
1941 static void megasas_handle_frame(MegasasState *s, uint64_t frame_addr,
1942 uint32_t frame_count)
1943 {
1944 uint8_t frame_status = MFI_STAT_INVALID_CMD;
1945 uint64_t frame_context;
1946 MegasasCmd *cmd;
1947
1948 /*
1949 * Always read 64bit context, top bits will be
1950 * masked out if required in megasas_enqueue_frame()
1951 */
1952 frame_context = megasas_frame_get_context(s, frame_addr);
1953
1954 cmd = megasas_enqueue_frame(s, frame_addr, frame_context, frame_count);
1955 if (!cmd) {
1956 /* reply queue full */
1957 trace_megasas_frame_busy(frame_addr);
1958 megasas_frame_set_scsi_status(s, frame_addr, BUSY);
1959 megasas_frame_set_cmd_status(s, frame_addr, MFI_STAT_SCSI_DONE_WITH_ERROR);
1960 megasas_complete_frame(s, frame_context);
1961 s->event_count++;
1962 return;
1963 }
1964 switch (cmd->frame->header.frame_cmd) {
1965 case MFI_CMD_INIT:
1966 frame_status = megasas_init_firmware(s, cmd);
1967 break;
1968 case MFI_CMD_DCMD:
1969 frame_status = megasas_handle_dcmd(s, cmd);
1970 break;
1971 case MFI_CMD_ABORT:
1972 frame_status = megasas_handle_abort(s, cmd);
1973 break;
1974 case MFI_CMD_PD_SCSI_IO:
1975 frame_status = megasas_handle_scsi(s, cmd, 0);
1976 break;
1977 case MFI_CMD_LD_SCSI_IO:
1978 frame_status = megasas_handle_scsi(s, cmd, 1);
1979 break;
1980 case MFI_CMD_LD_READ:
1981 case MFI_CMD_LD_WRITE:
1982 frame_status = megasas_handle_io(s, cmd);
1983 break;
1984 default:
1985 trace_megasas_unhandled_frame_cmd(cmd->index,
1986 cmd->frame->header.frame_cmd);
1987 s->event_count++;
1988 break;
1989 }
1990 if (frame_status != MFI_STAT_INVALID_STATUS) {
1991 if (cmd->frame) {
1992 cmd->frame->header.cmd_status = frame_status;
1993 } else {
1994 megasas_frame_set_cmd_status(s, frame_addr, frame_status);
1995 }
1996 megasas_unmap_frame(s, cmd);
1997 megasas_complete_frame(s, cmd->context);
1998 }
1999 }
2000
2001 static uint64_t megasas_mmio_read(void *opaque, hwaddr addr,
2002 unsigned size)
2003 {
2004 MegasasState *s = opaque;
2005 PCIDevice *pci_dev = PCI_DEVICE(s);
2006 MegasasBaseClass *base_class = MEGASAS_DEVICE_GET_CLASS(s);
2007 uint32_t retval = 0;
2008
2009 switch (addr) {
2010 case MFI_IDB:
2011 retval = 0;
2012 trace_megasas_mmio_readl("MFI_IDB", retval);
2013 break;
2014 case MFI_OMSG0:
2015 case MFI_OSP0:
2016 retval = (msix_present(pci_dev) ? MFI_FWSTATE_MSIX_SUPPORTED : 0) |
2017 (s->fw_state & MFI_FWSTATE_MASK) |
2018 ((s->fw_sge & 0xff) << 16) |
2019 (s->fw_cmds & 0xFFFF);
2020 trace_megasas_mmio_readl(addr == MFI_OMSG0 ? "MFI_OMSG0" : "MFI_OSP0",
2021 retval);
2022 break;
2023 case MFI_OSTS:
2024 if (megasas_intr_enabled(s) && s->doorbell) {
2025 retval = base_class->osts;
2026 }
2027 trace_megasas_mmio_readl("MFI_OSTS", retval);
2028 break;
2029 case MFI_OMSK:
2030 retval = s->intr_mask;
2031 trace_megasas_mmio_readl("MFI_OMSK", retval);
2032 break;
2033 case MFI_ODCR0:
2034 retval = s->doorbell ? 1 : 0;
2035 trace_megasas_mmio_readl("MFI_ODCR0", retval);
2036 break;
2037 case MFI_DIAG:
2038 retval = s->diag;
2039 trace_megasas_mmio_readl("MFI_DIAG", retval);
2040 break;
2041 case MFI_OSP1:
2042 retval = 15;
2043 trace_megasas_mmio_readl("MFI_OSP1", retval);
2044 break;
2045 default:
2046 trace_megasas_mmio_invalid_readl(addr);
2047 break;
2048 }
2049 return retval;
2050 }
2051
2052 static int adp_reset_seq[] = {0x00, 0x04, 0x0b, 0x02, 0x07, 0x0d};
2053
2054 static void megasas_mmio_write(void *opaque, hwaddr addr,
2055 uint64_t val, unsigned size)
2056 {
2057 MegasasState *s = opaque;
2058 PCIDevice *pci_dev = PCI_DEVICE(s);
2059 uint64_t frame_addr;
2060 uint32_t frame_count;
2061 int i;
2062
2063 switch (addr) {
2064 case MFI_IDB:
2065 trace_megasas_mmio_writel("MFI_IDB", val);
2066 if (val & MFI_FWINIT_ABORT) {
2067 /* Abort all pending cmds */
2068 for (i = 0; i < s->fw_cmds; i++) {
2069 megasas_abort_command(&s->frames[i]);
2070 }
2071 }
2072 if (val & MFI_FWINIT_READY) {
2073 /* move to FW READY */
2074 megasas_soft_reset(s);
2075 }
2076 if (val & MFI_FWINIT_MFIMODE) {
2077 /* discard MFIs */
2078 }
2079 if (val & MFI_FWINIT_STOP_ADP) {
2080 /* Terminal error, stop processing */
2081 s->fw_state = MFI_FWSTATE_FAULT;
2082 }
2083 break;
2084 case MFI_OMSK:
2085 trace_megasas_mmio_writel("MFI_OMSK", val);
2086 s->intr_mask = val;
2087 if (!megasas_intr_enabled(s) &&
2088 !msi_enabled(pci_dev) &&
2089 !msix_enabled(pci_dev)) {
2090 trace_megasas_irq_lower();
2091 pci_irq_deassert(pci_dev);
2092 }
2093 if (megasas_intr_enabled(s)) {
2094 if (msix_enabled(pci_dev)) {
2095 trace_megasas_msix_enabled(0);
2096 } else if (msi_enabled(pci_dev)) {
2097 trace_megasas_msi_enabled(0);
2098 } else {
2099 trace_megasas_intr_enabled();
2100 }
2101 } else {
2102 trace_megasas_intr_disabled();
2103 megasas_soft_reset(s);
2104 }
2105 break;
2106 case MFI_ODCR0:
2107 trace_megasas_mmio_writel("MFI_ODCR0", val);
2108 s->doorbell = 0;
2109 if (megasas_intr_enabled(s)) {
2110 if (!msix_enabled(pci_dev) && !msi_enabled(pci_dev)) {
2111 trace_megasas_irq_lower();
2112 pci_irq_deassert(pci_dev);
2113 }
2114 }
2115 break;
2116 case MFI_IQPH:
2117 trace_megasas_mmio_writel("MFI_IQPH", val);
2118 /* Received high 32 bits of a 64 bit MFI frame address */
2119 s->frame_hi = val;
2120 break;
2121 case MFI_IQPL:
2122 trace_megasas_mmio_writel("MFI_IQPL", val);
2123 /* Received low 32 bits of a 64 bit MFI frame address */
2124 /* Fallthrough */
2125 case MFI_IQP:
2126 if (addr == MFI_IQP) {
2127 trace_megasas_mmio_writel("MFI_IQP", val);
2128 /* Received 64 bit MFI frame address */
2129 s->frame_hi = 0;
2130 }
2131 frame_addr = (val & ~0x1F);
2132 /* Add possible 64 bit offset */
2133 frame_addr |= ((uint64_t)s->frame_hi << 32);
2134 s->frame_hi = 0;
2135 frame_count = (val >> 1) & 0xF;
2136 megasas_handle_frame(s, frame_addr, frame_count);
2137 break;
2138 case MFI_SEQ:
2139 trace_megasas_mmio_writel("MFI_SEQ", val);
2140 /* Magic sequence to start ADP reset */
2141 if (adp_reset_seq[s->adp_reset] == val) {
2142 s->adp_reset++;
2143 } else {
2144 s->adp_reset = 0;
2145 s->diag = 0;
2146 }
2147 if (s->adp_reset == 6) {
2148 s->diag = MFI_DIAG_WRITE_ENABLE;
2149 }
2150 break;
2151 case MFI_DIAG:
2152 trace_megasas_mmio_writel("MFI_DIAG", val);
2153 /* ADP reset */
2154 if ((s->diag & MFI_DIAG_WRITE_ENABLE) &&
2155 (val & MFI_DIAG_RESET_ADP)) {
2156 s->diag |= MFI_DIAG_RESET_ADP;
2157 megasas_soft_reset(s);
2158 s->adp_reset = 0;
2159 s->diag = 0;
2160 }
2161 break;
2162 default:
2163 trace_megasas_mmio_invalid_writel(addr, val);
2164 break;
2165 }
2166 }
2167
2168 static const MemoryRegionOps megasas_mmio_ops = {
2169 .read = megasas_mmio_read,
2170 .write = megasas_mmio_write,
2171 .endianness = DEVICE_LITTLE_ENDIAN,
2172 .impl = {
2173 .min_access_size = 8,
2174 .max_access_size = 8,
2175 }
2176 };
2177
2178 static uint64_t megasas_port_read(void *opaque, hwaddr addr,
2179 unsigned size)
2180 {
2181 return megasas_mmio_read(opaque, addr & 0xff, size);
2182 }
2183
2184 static void megasas_port_write(void *opaque, hwaddr addr,
2185 uint64_t val, unsigned size)
2186 {
2187 megasas_mmio_write(opaque, addr & 0xff, val, size);
2188 }
2189
2190 static const MemoryRegionOps megasas_port_ops = {
2191 .read = megasas_port_read,
2192 .write = megasas_port_write,
2193 .endianness = DEVICE_LITTLE_ENDIAN,
2194 .impl = {
2195 .min_access_size = 4,
2196 .max_access_size = 4,
2197 }
2198 };
2199
2200 static uint64_t megasas_queue_read(void *opaque, hwaddr addr,
2201 unsigned size)
2202 {
2203 return 0;
2204 }
2205
2206 static void megasas_queue_write(void *opaque, hwaddr addr,
2207 uint64_t val, unsigned size)
2208 {
2209 return;
2210 }
2211
2212 static const MemoryRegionOps megasas_queue_ops = {
2213 .read = megasas_queue_read,
2214 .write = megasas_queue_write,
2215 .endianness = DEVICE_LITTLE_ENDIAN,
2216 .impl = {
2217 .min_access_size = 8,
2218 .max_access_size = 8,
2219 }
2220 };
2221
2222 static void megasas_soft_reset(MegasasState *s)
2223 {
2224 int i;
2225 MegasasCmd *cmd;
2226
2227 trace_megasas_reset(s->fw_state);
2228 for (i = 0; i < s->fw_cmds; i++) {
2229 cmd = &s->frames[i];
2230 megasas_abort_command(cmd);
2231 }
2232 if (s->fw_state == MFI_FWSTATE_READY) {
2233 BusChild *kid;
2234
2235 /*
2236 * The EFI firmware doesn't handle UA,
2237 * so we need to clear the Power On/Reset UA
2238 * after the initial reset.
2239 */
2240 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
2241 SCSIDevice *sdev = SCSI_DEVICE(kid->child);
2242
2243 sdev->unit_attention = SENSE_CODE(NO_SENSE);
2244 scsi_device_unit_attention_reported(sdev);
2245 }
2246 }
2247 megasas_reset_frames(s);
2248 s->reply_queue_len = s->fw_cmds;
2249 s->reply_queue_pa = 0;
2250 s->consumer_pa = 0;
2251 s->producer_pa = 0;
2252 s->fw_state = MFI_FWSTATE_READY;
2253 s->doorbell = 0;
2254 s->intr_mask = MEGASAS_INTR_DISABLED_MASK;
2255 s->frame_hi = 0;
2256 s->flags &= ~MEGASAS_MASK_USE_QUEUE64;
2257 s->event_count++;
2258 s->boot_event = s->event_count;
2259 }
2260
2261 static void megasas_scsi_reset(DeviceState *dev)
2262 {
2263 MegasasState *s = MEGASAS(dev);
2264
2265 megasas_soft_reset(s);
2266 }
2267
2268 static const VMStateDescription vmstate_megasas_gen1 = {
2269 .name = "megasas",
2270 .version_id = 0,
2271 .minimum_version_id = 0,
2272 .fields = (VMStateField[]) {
2273 VMSTATE_PCI_DEVICE(parent_obj, MegasasState),
2274 VMSTATE_MSIX(parent_obj, MegasasState),
2275
2276 VMSTATE_INT32(fw_state, MegasasState),
2277 VMSTATE_INT32(intr_mask, MegasasState),
2278 VMSTATE_INT32(doorbell, MegasasState),
2279 VMSTATE_UINT64(reply_queue_pa, MegasasState),
2280 VMSTATE_UINT64(consumer_pa, MegasasState),
2281 VMSTATE_UINT64(producer_pa, MegasasState),
2282 VMSTATE_END_OF_LIST()
2283 }
2284 };
2285
2286 static const VMStateDescription vmstate_megasas_gen2 = {
2287 .name = "megasas-gen2",
2288 .version_id = 0,
2289 .minimum_version_id = 0,
2290 .minimum_version_id_old = 0,
2291 .fields = (VMStateField[]) {
2292 VMSTATE_PCIE_DEVICE(parent_obj, MegasasState),
2293 VMSTATE_MSIX(parent_obj, MegasasState),
2294
2295 VMSTATE_INT32(fw_state, MegasasState),
2296 VMSTATE_INT32(intr_mask, MegasasState),
2297 VMSTATE_INT32(doorbell, MegasasState),
2298 VMSTATE_UINT64(reply_queue_pa, MegasasState),
2299 VMSTATE_UINT64(consumer_pa, MegasasState),
2300 VMSTATE_UINT64(producer_pa, MegasasState),
2301 VMSTATE_END_OF_LIST()
2302 }
2303 };
2304
2305 static void megasas_scsi_uninit(PCIDevice *d)
2306 {
2307 MegasasState *s = MEGASAS(d);
2308
2309 if (megasas_use_msix(s)) {
2310 msix_uninit(d, &s->mmio_io, &s->mmio_io);
2311 }
2312 if (megasas_use_msi(s)) {
2313 msi_uninit(d);
2314 }
2315 }
2316
2317 static const struct SCSIBusInfo megasas_scsi_info = {
2318 .tcq = true,
2319 .max_target = MFI_MAX_LD,
2320 .max_lun = 255,
2321
2322 .transfer_data = megasas_xfer_complete,
2323 .get_sg_list = megasas_get_sg_list,
2324 .complete = megasas_command_complete,
2325 .cancel = megasas_command_cancel,
2326 };
2327
2328 static void megasas_scsi_realize(PCIDevice *dev, Error **errp)
2329 {
2330 DeviceState *d = DEVICE(dev);
2331 MegasasState *s = MEGASAS(dev);
2332 MegasasBaseClass *b = MEGASAS_DEVICE_GET_CLASS(s);
2333 uint8_t *pci_conf;
2334 int i, bar_type;
2335
2336 pci_conf = dev->config;
2337
2338 /* PCI latency timer = 0 */
2339 pci_conf[PCI_LATENCY_TIMER] = 0;
2340 /* Interrupt pin 1 */
2341 pci_conf[PCI_INTERRUPT_PIN] = 0x01;
2342
2343 memory_region_init_io(&s->mmio_io, OBJECT(s), &megasas_mmio_ops, s,
2344 "megasas-mmio", 0x4000);
2345 memory_region_init_io(&s->port_io, OBJECT(s), &megasas_port_ops, s,
2346 "megasas-io", 256);
2347 memory_region_init_io(&s->queue_io, OBJECT(s), &megasas_queue_ops, s,
2348 "megasas-queue", 0x40000);
2349
2350 if (megasas_use_msi(s) &&
2351 msi_init(dev, 0x50, 1, true, false)) {
2352 s->flags &= ~MEGASAS_MASK_USE_MSI;
2353 }
2354 if (megasas_use_msix(s) &&
2355 msix_init(dev, 15, &s->mmio_io, b->mmio_bar, 0x2000,
2356 &s->mmio_io, b->mmio_bar, 0x3800, 0x68)) {
2357 s->flags &= ~MEGASAS_MASK_USE_MSIX;
2358 }
2359 if (pci_is_express(dev)) {
2360 pcie_endpoint_cap_init(dev, 0xa0);
2361 }
2362
2363 bar_type = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64;
2364 pci_register_bar(dev, b->ioport_bar,
2365 PCI_BASE_ADDRESS_SPACE_IO, &s->port_io);
2366 pci_register_bar(dev, b->mmio_bar, bar_type, &s->mmio_io);
2367 pci_register_bar(dev, 3, bar_type, &s->queue_io);
2368
2369 if (megasas_use_msix(s)) {
2370 msix_vector_use(dev, 0);
2371 }
2372
2373 s->fw_state = MFI_FWSTATE_READY;
2374 if (!s->sas_addr) {
2375 s->sas_addr = ((NAA_LOCALLY_ASSIGNED_ID << 24) |
2376 IEEE_COMPANY_LOCALLY_ASSIGNED) << 36;
2377 s->sas_addr |= (pci_bus_num(dev->bus) << 16);
2378 s->sas_addr |= (PCI_SLOT(dev->devfn) << 8);
2379 s->sas_addr |= PCI_FUNC(dev->devfn);
2380 }
2381 if (!s->hba_serial) {
2382 s->hba_serial = g_strdup(MEGASAS_HBA_SERIAL);
2383 }
2384 if (s->fw_sge >= MEGASAS_MAX_SGE - MFI_PASS_FRAME_SIZE) {
2385 s->fw_sge = MEGASAS_MAX_SGE - MFI_PASS_FRAME_SIZE;
2386 } else if (s->fw_sge >= 128 - MFI_PASS_FRAME_SIZE) {
2387 s->fw_sge = 128 - MFI_PASS_FRAME_SIZE;
2388 } else {
2389 s->fw_sge = 64 - MFI_PASS_FRAME_SIZE;
2390 }
2391 if (s->fw_cmds > MEGASAS_MAX_FRAMES) {
2392 s->fw_cmds = MEGASAS_MAX_FRAMES;
2393 }
2394 trace_megasas_init(s->fw_sge, s->fw_cmds,
2395 megasas_is_jbod(s) ? "jbod" : "raid");
2396
2397 if (megasas_is_jbod(s)) {
2398 s->fw_luns = MFI_MAX_SYS_PDS;
2399 } else {
2400 s->fw_luns = MFI_MAX_LD;
2401 }
2402 s->producer_pa = 0;
2403 s->consumer_pa = 0;
2404 for (i = 0; i < s->fw_cmds; i++) {
2405 s->frames[i].index = i;
2406 s->frames[i].context = -1;
2407 s->frames[i].pa = 0;
2408 s->frames[i].state = s;
2409 }
2410
2411 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
2412 &megasas_scsi_info, NULL);
2413 if (!d->hotplugged) {
2414 scsi_bus_legacy_handle_cmdline(&s->bus, errp);
2415 }
2416 }
2417
2418 static Property megasas_properties_gen1[] = {
2419 DEFINE_PROP_UINT32("max_sge", MegasasState, fw_sge,
2420 MEGASAS_DEFAULT_SGE),
2421 DEFINE_PROP_UINT32("max_cmds", MegasasState, fw_cmds,
2422 MEGASAS_DEFAULT_FRAMES),
2423 DEFINE_PROP_STRING("hba_serial", MegasasState, hba_serial),
2424 DEFINE_PROP_UINT64("sas_address", MegasasState, sas_addr, 0),
2425 DEFINE_PROP_BIT("use_msi", MegasasState, flags,
2426 MEGASAS_FLAG_USE_MSI, false),
2427 DEFINE_PROP_BIT("use_msix", MegasasState, flags,
2428 MEGASAS_FLAG_USE_MSIX, false),
2429 DEFINE_PROP_BIT("use_jbod", MegasasState, flags,
2430 MEGASAS_FLAG_USE_JBOD, false),
2431 DEFINE_PROP_END_OF_LIST(),
2432 };
2433
2434 static Property megasas_properties_gen2[] = {
2435 DEFINE_PROP_UINT32("max_sge", MegasasState, fw_sge,
2436 MEGASAS_DEFAULT_SGE),
2437 DEFINE_PROP_UINT32("max_cmds", MegasasState, fw_cmds,
2438 MEGASAS_GEN2_DEFAULT_FRAMES),
2439 DEFINE_PROP_STRING("hba_serial", MegasasState, hba_serial),
2440 DEFINE_PROP_UINT64("sas_address", MegasasState, sas_addr, 0),
2441 DEFINE_PROP_BIT("use_msi", MegasasState, flags,
2442 MEGASAS_FLAG_USE_MSI, true),
2443 DEFINE_PROP_BIT("use_msix", MegasasState, flags,
2444 MEGASAS_FLAG_USE_MSIX, true),
2445 DEFINE_PROP_BIT("use_jbod", MegasasState, flags,
2446 MEGASAS_FLAG_USE_JBOD, false),
2447 DEFINE_PROP_END_OF_LIST(),
2448 };
2449
2450 typedef struct MegasasInfo {
2451 const char *name;
2452 const char *desc;
2453 const char *product_name;
2454 const char *product_version;
2455 uint16_t device_id;
2456 uint16_t subsystem_id;
2457 int ioport_bar;
2458 int mmio_bar;
2459 bool is_express;
2460 int osts;
2461 const VMStateDescription *vmsd;
2462 Property *props;
2463 } MegasasInfo;
2464
2465 static struct MegasasInfo megasas_devices[] = {
2466 {
2467 .name = TYPE_MEGASAS_GEN1,
2468 .desc = "LSI MegaRAID SAS 1078",
2469 .product_name = "LSI MegaRAID SAS 8708EM2",
2470 .product_version = MEGASAS_VERSION_GEN1,
2471 .device_id = PCI_DEVICE_ID_LSI_SAS1078,
2472 .subsystem_id = 0x1013,
2473 .ioport_bar = 2,
2474 .mmio_bar = 0,
2475 .osts = MFI_1078_RM | 1,
2476 .is_express = false,
2477 .vmsd = &vmstate_megasas_gen1,
2478 .props = megasas_properties_gen1,
2479 },{
2480 .name = TYPE_MEGASAS_GEN2,
2481 .desc = "LSI MegaRAID SAS 2108",
2482 .product_name = "LSI MegaRAID SAS 9260-8i",
2483 .product_version = MEGASAS_VERSION_GEN2,
2484 .device_id = PCI_DEVICE_ID_LSI_SAS0079,
2485 .subsystem_id = 0x9261,
2486 .ioport_bar = 0,
2487 .mmio_bar = 1,
2488 .osts = MFI_GEN2_RM,
2489 .is_express = true,
2490 .vmsd = &vmstate_megasas_gen2,
2491 .props = megasas_properties_gen2,
2492 }
2493 };
2494
2495 static void megasas_class_init(ObjectClass *oc, void *data)
2496 {
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;
2501
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 pc->is_express = info->is_express;
2510 e->mmio_bar = info->mmio_bar;
2511 e->ioport_bar = info->ioport_bar;
2512 e->osts = info->osts;
2513 e->product_name = info->product_name;
2514 e->product_version = info->product_version;
2515 dc->props = info->props;
2516 dc->reset = megasas_scsi_reset;
2517 dc->vmsd = info->vmsd;
2518 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2519 dc->desc = info->desc;
2520 }
2521
2522 static const TypeInfo megasas_info = {
2523 .name = TYPE_MEGASAS_BASE,
2524 .parent = TYPE_PCI_DEVICE,
2525 .instance_size = sizeof(MegasasState),
2526 .class_size = sizeof(MegasasBaseClass),
2527 .abstract = true,
2528 };
2529
2530 static void megasas_register_types(void)
2531 {
2532 int i;
2533
2534 type_register_static(&megasas_info);
2535 for (i = 0; i < ARRAY_SIZE(megasas_devices); i++) {
2536 const MegasasInfo *info = &megasas_devices[i];
2537 TypeInfo type_info = {};
2538
2539 type_info.name = info->name;
2540 type_info.parent = TYPE_MEGASAS_BASE;
2541 type_info.class_data = (void *)info;
2542 type_info.class_init = megasas_class_init;
2543
2544 type_register(&type_info);
2545 }
2546 }
2547
2548 type_init(megasas_register_types)