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