Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU ESP/NCR53C9x emulation |
| 3 | * |
| 4 | * Copyright (c) 2005-2006 Fabrice Bellard |
| 5 | * Copyright (c) 2012 Herve Poussineau |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
Peter Maydell | a4ab479 | 2016-01-26 18:17:16 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 27 | #include "hw/pci/pci.h" |
Markus Armbruster | 64552b6 | 2019-08-12 07:23:42 +0200 | [diff] [blame] | 28 | #include "hw/irq.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 29 | #include "hw/nvram/eeprom93xx.h" |
| 30 | #include "hw/scsi/esp.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 31 | #include "migration/vmstate.h" |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 32 | #include "trace.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 33 | #include "qapi/error.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 34 | #include "qemu/log.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 35 | #include "qemu/module.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 36 | #include "qom/object.h" |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 37 | |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 38 | #define TYPE_AM53C974_DEVICE "am53c974" |
| 39 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 40 | typedef struct PCIESPState PCIESPState; |
Eduardo Habkost | 8110fa1 | 2020-08-31 17:07:33 -0400 | [diff] [blame] | 41 | DECLARE_INSTANCE_CHECKER(PCIESPState, PCI_ESP, |
| 42 | TYPE_AM53C974_DEVICE) |
Peter Crosthwaite | 3a15eff | 2013-06-24 16:55:00 +1000 | [diff] [blame] | 43 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 44 | #define DMA_CMD 0x0 |
| 45 | #define DMA_STC 0x1 |
| 46 | #define DMA_SPA 0x2 |
| 47 | #define DMA_WBC 0x3 |
| 48 | #define DMA_WAC 0x4 |
| 49 | #define DMA_STAT 0x5 |
| 50 | #define DMA_SMDLA 0x6 |
| 51 | #define DMA_WMAC 0x7 |
| 52 | |
| 53 | #define DMA_CMD_MASK 0x03 |
| 54 | #define DMA_CMD_DIAG 0x04 |
| 55 | #define DMA_CMD_MDL 0x10 |
| 56 | #define DMA_CMD_INTE_P 0x20 |
| 57 | #define DMA_CMD_INTE_D 0x40 |
| 58 | #define DMA_CMD_DIR 0x80 |
| 59 | |
| 60 | #define DMA_STAT_PWDN 0x01 |
| 61 | #define DMA_STAT_ERROR 0x02 |
| 62 | #define DMA_STAT_ABORT 0x04 |
| 63 | #define DMA_STAT_DONE 0x08 |
| 64 | #define DMA_STAT_SCSIINT 0x10 |
| 65 | #define DMA_STAT_BCMBLT 0x20 |
| 66 | |
Guenter Roeck | c2d6eed | 2018-11-28 13:56:10 -0800 | [diff] [blame] | 67 | #define SBAC_STATUS (1 << 24) |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 68 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 69 | struct PCIESPState { |
Andreas Färber | 4e5dcc7 | 2013-06-30 14:07:49 +0200 | [diff] [blame] | 70 | /*< private >*/ |
| 71 | PCIDevice parent_obj; |
| 72 | /*< public >*/ |
| 73 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 74 | MemoryRegion io; |
| 75 | uint32_t dma_regs[8]; |
| 76 | uint32_t sbac; |
| 77 | ESPState esp; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 78 | }; |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 79 | |
| 80 | static void esp_pci_handle_idle(PCIESPState *pci, uint32_t val) |
| 81 | { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 82 | ESPState *s = ESP(&pci->esp); |
| 83 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 84 | trace_esp_pci_dma_idle(val); |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 85 | esp_dma_enable(s, 0, 0); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void esp_pci_handle_blast(PCIESPState *pci, uint32_t val) |
| 89 | { |
| 90 | trace_esp_pci_dma_blast(val); |
| 91 | qemu_log_mask(LOG_UNIMP, "am53c974: cmd BLAST not implemented\n"); |
| 92 | } |
| 93 | |
| 94 | static void esp_pci_handle_abort(PCIESPState *pci, uint32_t val) |
| 95 | { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 96 | ESPState *s = ESP(&pci->esp); |
| 97 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 98 | trace_esp_pci_dma_abort(val); |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 99 | if (s->current_req) { |
| 100 | scsi_req_cancel(s->current_req); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | static void esp_pci_handle_start(PCIESPState *pci, uint32_t val) |
| 105 | { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 106 | ESPState *s = ESP(&pci->esp); |
| 107 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 108 | trace_esp_pci_dma_start(val); |
| 109 | |
| 110 | pci->dma_regs[DMA_WBC] = pci->dma_regs[DMA_STC]; |
| 111 | pci->dma_regs[DMA_WAC] = pci->dma_regs[DMA_SPA]; |
| 112 | pci->dma_regs[DMA_WMAC] = pci->dma_regs[DMA_SMDLA]; |
| 113 | |
| 114 | pci->dma_regs[DMA_STAT] &= ~(DMA_STAT_BCMBLT | DMA_STAT_SCSIINT |
| 115 | | DMA_STAT_DONE | DMA_STAT_ABORT |
| 116 | | DMA_STAT_ERROR | DMA_STAT_PWDN); |
| 117 | |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 118 | esp_dma_enable(s, 0, 1); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void esp_pci_dma_write(PCIESPState *pci, uint32_t saddr, uint32_t val) |
| 122 | { |
| 123 | trace_esp_pci_dma_write(saddr, pci->dma_regs[saddr], val); |
| 124 | switch (saddr) { |
| 125 | case DMA_CMD: |
| 126 | pci->dma_regs[saddr] = val; |
| 127 | switch (val & DMA_CMD_MASK) { |
| 128 | case 0x0: /* IDLE */ |
| 129 | esp_pci_handle_idle(pci, val); |
| 130 | break; |
| 131 | case 0x1: /* BLAST */ |
| 132 | esp_pci_handle_blast(pci, val); |
| 133 | break; |
| 134 | case 0x2: /* ABORT */ |
| 135 | esp_pci_handle_abort(pci, val); |
| 136 | break; |
| 137 | case 0x3: /* START */ |
| 138 | esp_pci_handle_start(pci, val); |
| 139 | break; |
| 140 | default: /* can't happen */ |
| 141 | abort(); |
| 142 | } |
| 143 | break; |
| 144 | case DMA_STC: |
| 145 | case DMA_SPA: |
| 146 | case DMA_SMDLA: |
| 147 | pci->dma_regs[saddr] = val; |
| 148 | break; |
| 149 | case DMA_STAT: |
Guenter Roeck | c2d6eed | 2018-11-28 13:56:10 -0800 | [diff] [blame] | 150 | if (pci->sbac & SBAC_STATUS) { |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 151 | /* clear some bits on write */ |
| 152 | uint32_t mask = DMA_STAT_ERROR | DMA_STAT_ABORT | DMA_STAT_DONE; |
| 153 | pci->dma_regs[DMA_STAT] &= ~(val & mask); |
| 154 | } |
| 155 | break; |
| 156 | default: |
| 157 | trace_esp_pci_error_invalid_write_dma(val, saddr); |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static uint32_t esp_pci_dma_read(PCIESPState *pci, uint32_t saddr) |
| 163 | { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 164 | ESPState *s = ESP(&pci->esp); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 165 | uint32_t val; |
| 166 | |
| 167 | val = pci->dma_regs[saddr]; |
| 168 | if (saddr == DMA_STAT) { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 169 | if (s->rregs[ESP_RSTAT] & STAT_INT) { |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 170 | val |= DMA_STAT_SCSIINT; |
| 171 | } |
Guenter Roeck | c2d6eed | 2018-11-28 13:56:10 -0800 | [diff] [blame] | 172 | if (!(pci->sbac & SBAC_STATUS)) { |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 173 | pci->dma_regs[DMA_STAT] &= ~(DMA_STAT_ERROR | DMA_STAT_ABORT | |
| 174 | DMA_STAT_DONE); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | trace_esp_pci_dma_read(saddr, val); |
| 179 | return val; |
| 180 | } |
| 181 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 182 | static void esp_pci_io_write(void *opaque, hwaddr addr, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 183 | uint64_t val, unsigned int size) |
| 184 | { |
| 185 | PCIESPState *pci = opaque; |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 186 | ESPState *s = ESP(&pci->esp); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 187 | |
| 188 | if (size < 4 || addr & 3) { |
| 189 | /* need to upgrade request: we only support 4-bytes accesses */ |
| 190 | uint32_t current = 0, mask; |
| 191 | int shift; |
| 192 | |
| 193 | if (addr < 0x40) { |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 194 | current = s->wregs[addr >> 2]; |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 195 | } else if (addr < 0x60) { |
| 196 | current = pci->dma_regs[(addr - 0x40) >> 2]; |
| 197 | } else if (addr < 0x74) { |
| 198 | current = pci->sbac; |
| 199 | } |
| 200 | |
| 201 | shift = (4 - size) * 8; |
| 202 | mask = (~(uint32_t)0 << shift) >> shift; |
| 203 | |
| 204 | shift = ((4 - (addr & 3)) & 3) * 8; |
| 205 | val <<= shift; |
| 206 | val |= current & ~(mask << shift); |
| 207 | addr &= ~3; |
| 208 | size = 4; |
| 209 | } |
Chen Qun | d58f886 | 2020-03-25 10:59:17 +0800 | [diff] [blame] | 210 | g_assert(size >= 4); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 211 | |
| 212 | if (addr < 0x40) { |
| 213 | /* SCSI core reg */ |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 214 | esp_reg_write(s, addr >> 2, val); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 215 | } else if (addr < 0x60) { |
| 216 | /* PCI DMA CCB */ |
| 217 | esp_pci_dma_write(pci, (addr - 0x40) >> 2, val); |
| 218 | } else if (addr == 0x70) { |
| 219 | /* DMA SCSI Bus and control */ |
| 220 | trace_esp_pci_sbac_write(pci->sbac, val); |
| 221 | pci->sbac = val; |
| 222 | } else { |
| 223 | trace_esp_pci_error_invalid_write((int)addr); |
| 224 | } |
| 225 | } |
| 226 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 227 | static uint64_t esp_pci_io_read(void *opaque, hwaddr addr, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 228 | unsigned int size) |
| 229 | { |
| 230 | PCIESPState *pci = opaque; |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 231 | ESPState *s = ESP(&pci->esp); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 232 | uint32_t ret; |
| 233 | |
| 234 | if (addr < 0x40) { |
| 235 | /* SCSI core reg */ |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 236 | ret = esp_reg_read(s, addr >> 2); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 237 | } else if (addr < 0x60) { |
| 238 | /* PCI DMA CCB */ |
| 239 | ret = esp_pci_dma_read(pci, (addr - 0x40) >> 2); |
| 240 | } else if (addr == 0x70) { |
| 241 | /* DMA SCSI Bus and control */ |
| 242 | trace_esp_pci_sbac_read(pci->sbac); |
| 243 | ret = pci->sbac; |
| 244 | } else { |
| 245 | /* Invalid region */ |
| 246 | trace_esp_pci_error_invalid_read((int)addr); |
| 247 | ret = 0; |
| 248 | } |
| 249 | |
| 250 | /* give only requested data */ |
| 251 | ret >>= (addr & 3) * 8; |
| 252 | ret &= ~(~(uint64_t)0 << (8 * size)); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static void esp_pci_dma_memory_rw(PCIESPState *pci, uint8_t *buf, int len, |
| 258 | DMADirection dir) |
| 259 | { |
| 260 | dma_addr_t addr; |
| 261 | DMADirection expected_dir; |
| 262 | |
| 263 | if (pci->dma_regs[DMA_CMD] & DMA_CMD_DIR) { |
| 264 | expected_dir = DMA_DIRECTION_FROM_DEVICE; |
| 265 | } else { |
| 266 | expected_dir = DMA_DIRECTION_TO_DEVICE; |
| 267 | } |
| 268 | |
| 269 | if (dir != expected_dir) { |
| 270 | trace_esp_pci_error_invalid_dma_direction(); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | if (pci->dma_regs[DMA_STAT] & DMA_CMD_MDL) { |
| 275 | qemu_log_mask(LOG_UNIMP, "am53c974: MDL transfer not implemented\n"); |
| 276 | } |
| 277 | |
| 278 | addr = pci->dma_regs[DMA_SPA]; |
| 279 | if (pci->dma_regs[DMA_WBC] < len) { |
| 280 | len = pci->dma_regs[DMA_WBC]; |
| 281 | } |
| 282 | |
Philippe Mathieu-Daudé | e2d784b | 2021-12-15 22:18:19 +0100 | [diff] [blame] | 283 | pci_dma_rw(PCI_DEVICE(pci), addr, buf, len, dir, MEMTXATTRS_UNSPECIFIED); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 284 | |
| 285 | /* update status registers */ |
| 286 | pci->dma_regs[DMA_WBC] -= len; |
| 287 | pci->dma_regs[DMA_WAC] += len; |
Paolo Bonzini | 25aaa2c | 2014-11-10 13:58:14 +0100 | [diff] [blame] | 288 | if (pci->dma_regs[DMA_WBC] == 0) { |
Hannes Reinecke | c3543fb | 2014-11-07 13:22:32 +0100 | [diff] [blame] | 289 | pci->dma_regs[DMA_STAT] |= DMA_STAT_DONE; |
Paolo Bonzini | 25aaa2c | 2014-11-10 13:58:14 +0100 | [diff] [blame] | 290 | } |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static void esp_pci_dma_memory_read(void *opaque, uint8_t *buf, int len) |
| 294 | { |
| 295 | PCIESPState *pci = opaque; |
| 296 | esp_pci_dma_memory_rw(pci, buf, len, DMA_DIRECTION_TO_DEVICE); |
| 297 | } |
| 298 | |
| 299 | static void esp_pci_dma_memory_write(void *opaque, uint8_t *buf, int len) |
| 300 | { |
| 301 | PCIESPState *pci = opaque; |
| 302 | esp_pci_dma_memory_rw(pci, buf, len, DMA_DIRECTION_FROM_DEVICE); |
| 303 | } |
| 304 | |
| 305 | static const MemoryRegionOps esp_pci_io_ops = { |
| 306 | .read = esp_pci_io_read, |
| 307 | .write = esp_pci_io_write, |
| 308 | .endianness = DEVICE_LITTLE_ENDIAN, |
| 309 | .impl = { |
| 310 | .min_access_size = 1, |
| 311 | .max_access_size = 4, |
| 312 | }, |
| 313 | }; |
| 314 | |
| 315 | static void esp_pci_hard_reset(DeviceState *dev) |
| 316 | { |
Peter Crosthwaite | 3a15eff | 2013-06-24 16:55:00 +1000 | [diff] [blame] | 317 | PCIESPState *pci = PCI_ESP(dev); |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 318 | ESPState *s = ESP(&pci->esp); |
| 319 | |
| 320 | esp_hard_reset(s); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 321 | pci->dma_regs[DMA_CMD] &= ~(DMA_CMD_DIR | DMA_CMD_INTE_D | DMA_CMD_INTE_P |
| 322 | | DMA_CMD_MDL | DMA_CMD_DIAG | DMA_CMD_MASK); |
| 323 | pci->dma_regs[DMA_WBC] &= ~0xffff; |
| 324 | pci->dma_regs[DMA_WAC] = 0xffffffff; |
| 325 | pci->dma_regs[DMA_STAT] &= ~(DMA_STAT_BCMBLT | DMA_STAT_SCSIINT |
| 326 | | DMA_STAT_DONE | DMA_STAT_ABORT |
| 327 | | DMA_STAT_ERROR); |
| 328 | pci->dma_regs[DMA_WMAC] = 0xfffffffd; |
| 329 | } |
| 330 | |
| 331 | static const VMStateDescription vmstate_esp_pci_scsi = { |
| 332 | .name = "pciespscsi", |
Mark Cave-Ayland | 0bd005b | 2021-03-04 22:10:25 +0000 | [diff] [blame] | 333 | .version_id = 2, |
Guenter Roeck | ea84a44 | 2018-11-29 09:17:42 -0800 | [diff] [blame] | 334 | .minimum_version_id = 1, |
Mark Cave-Ayland | ff4a1da | 2021-04-07 13:48:42 +0100 | [diff] [blame] | 335 | .pre_save = esp_pre_save, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 336 | .fields = (VMStateField[]) { |
Andreas Färber | 4e5dcc7 | 2013-06-30 14:07:49 +0200 | [diff] [blame] | 337 | VMSTATE_PCI_DEVICE(parent_obj, PCIESPState), |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 338 | VMSTATE_BUFFER_UNSAFE(dma_regs, PCIESPState, 0, 8 * sizeof(uint32_t)), |
Mark Cave-Ayland | 0bd005b | 2021-03-04 22:10:25 +0000 | [diff] [blame] | 339 | VMSTATE_UINT8_V(esp.mig_version_id, PCIESPState, 2), |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 340 | VMSTATE_STRUCT(esp, PCIESPState, 0, vmstate_esp, ESPState), |
| 341 | VMSTATE_END_OF_LIST() |
| 342 | } |
| 343 | }; |
| 344 | |
Hannes Reinecke | 17ea26c2 | 2020-11-16 19:40:36 +0100 | [diff] [blame] | 345 | static void esp_pci_command_complete(SCSIRequest *req, size_t resid) |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 346 | { |
| 347 | ESPState *s = req->hba_private; |
| 348 | PCIESPState *pci = container_of(s, PCIESPState, esp); |
| 349 | |
Hannes Reinecke | 17ea26c2 | 2020-11-16 19:40:36 +0100 | [diff] [blame] | 350 | esp_command_complete(req, resid); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 351 | pci->dma_regs[DMA_WBC] = 0; |
| 352 | pci->dma_regs[DMA_STAT] |= DMA_STAT_DONE; |
| 353 | } |
| 354 | |
| 355 | static const struct SCSIBusInfo esp_pci_scsi_info = { |
| 356 | .tcq = false, |
| 357 | .max_target = ESP_MAX_DEVS, |
| 358 | .max_lun = 7, |
| 359 | |
| 360 | .transfer_data = esp_transfer_data, |
| 361 | .complete = esp_pci_command_complete, |
| 362 | .cancel = esp_request_cancelled, |
| 363 | }; |
| 364 | |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 365 | static void esp_pci_scsi_realize(PCIDevice *dev, Error **errp) |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 366 | { |
Peter Crosthwaite | 3a15eff | 2013-06-24 16:55:00 +1000 | [diff] [blame] | 367 | PCIESPState *pci = PCI_ESP(dev); |
| 368 | DeviceState *d = DEVICE(dev); |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 369 | ESPState *s = ESP(&pci->esp); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 370 | uint8_t *pci_conf; |
| 371 | |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 372 | if (!qdev_realize(DEVICE(s), NULL, errp)) { |
| 373 | return; |
| 374 | } |
| 375 | |
Andreas Färber | 4e5dcc7 | 2013-06-30 14:07:49 +0200 | [diff] [blame] | 376 | pci_conf = dev->config; |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 377 | |
| 378 | /* Interrupt pin A */ |
| 379 | pci_conf[PCI_INTERRUPT_PIN] = 0x01; |
| 380 | |
| 381 | s->dma_memory_read = esp_pci_dma_memory_read; |
| 382 | s->dma_memory_write = esp_pci_dma_memory_write; |
| 383 | s->dma_opaque = pci; |
| 384 | s->chip_id = TCHI_AM53C974; |
Paolo Bonzini | 2977673 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 385 | memory_region_init_io(&pci->io, OBJECT(pci), &esp_pci_io_ops, pci, |
| 386 | "esp-io", 0x80); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 387 | |
Andreas Färber | 4e5dcc7 | 2013-06-30 14:07:49 +0200 | [diff] [blame] | 388 | pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->io); |
Marcel Apfelbaum | 9e64f8a | 2013-10-07 10:36:39 +0300 | [diff] [blame] | 389 | s->irq = pci_allocate_irq(dev); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 390 | |
Peter Maydell | 739e95f | 2021-09-23 13:11:48 +0100 | [diff] [blame] | 391 | scsi_bus_init(&s->bus, sizeof(s->bus), d, &esp_pci_scsi_info); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 394 | static void esp_pci_scsi_exit(PCIDevice *d) |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 395 | { |
Peter Crosthwaite | 3a15eff | 2013-06-24 16:55:00 +1000 | [diff] [blame] | 396 | PCIESPState *pci = PCI_ESP(d); |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 397 | ESPState *s = ESP(&pci->esp); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 398 | |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 399 | qemu_free_irq(s->irq); |
| 400 | } |
| 401 | |
| 402 | static void esp_pci_init(Object *obj) |
| 403 | { |
| 404 | PCIESPState *pci = PCI_ESP(obj); |
| 405 | |
| 406 | object_initialize_child(obj, "esp", &pci->esp, TYPE_ESP); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void esp_pci_class_init(ObjectClass *klass, void *data) |
| 410 | { |
| 411 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 412 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 413 | |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 414 | k->realize = esp_pci_scsi_realize; |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 415 | k->exit = esp_pci_scsi_exit; |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 416 | k->vendor_id = PCI_VENDOR_ID_AMD; |
| 417 | k->device_id = PCI_DEVICE_ID_AMD_SCSI; |
| 418 | k->revision = 0x10; |
| 419 | k->class_id = PCI_CLASS_STORAGE_SCSI; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 420 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 421 | dc->desc = "AMD Am53c974 PCscsi-PCI SCSI adapter"; |
| 422 | dc->reset = esp_pci_hard_reset; |
| 423 | dc->vmsd = &vmstate_esp_pci_scsi; |
| 424 | } |
| 425 | |
| 426 | static const TypeInfo esp_pci_info = { |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 427 | .name = TYPE_AM53C974_DEVICE, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 428 | .parent = TYPE_PCI_DEVICE, |
Mark Cave-Ayland | eb169c7 | 2021-03-04 22:10:24 +0000 | [diff] [blame] | 429 | .instance_init = esp_pci_init, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 430 | .instance_size = sizeof(PCIESPState), |
| 431 | .class_init = esp_pci_class_init, |
Eduardo Habkost | fd3b02c | 2017-09-27 16:56:34 -0300 | [diff] [blame] | 432 | .interfaces = (InterfaceInfo[]) { |
| 433 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
| 434 | { }, |
| 435 | }, |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 436 | }; |
| 437 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 438 | struct DC390State { |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 439 | PCIESPState pci; |
| 440 | eeprom_t *eeprom; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 441 | }; |
| 442 | typedef struct DC390State DC390State; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 443 | |
| 444 | #define TYPE_DC390_DEVICE "dc390" |
Eduardo Habkost | 8110fa1 | 2020-08-31 17:07:33 -0400 | [diff] [blame] | 445 | DECLARE_INSTANCE_CHECKER(DC390State, DC390, |
| 446 | TYPE_DC390_DEVICE) |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 447 | |
| 448 | #define EE_ADAPT_SCSI_ID 64 |
| 449 | #define EE_MODE2 65 |
| 450 | #define EE_DELAY 66 |
| 451 | #define EE_TAG_CMD_NUM 67 |
| 452 | #define EE_ADAPT_OPTIONS 68 |
| 453 | #define EE_BOOT_SCSI_ID 69 |
| 454 | #define EE_BOOT_SCSI_LUN 70 |
| 455 | #define EE_CHKSUM1 126 |
| 456 | #define EE_CHKSUM2 127 |
| 457 | |
| 458 | #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01 |
| 459 | #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02 |
| 460 | #define EE_ADAPT_OPTION_INT13 0x04 |
| 461 | #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08 |
| 462 | |
| 463 | |
| 464 | static uint32_t dc390_read_config(PCIDevice *dev, uint32_t addr, int l) |
| 465 | { |
| 466 | DC390State *pci = DC390(dev); |
| 467 | uint32_t val; |
| 468 | |
| 469 | val = pci_default_read_config(dev, addr, l); |
| 470 | |
| 471 | if (addr == 0x00 && l == 1) { |
| 472 | /* First byte of address space is AND-ed with EEPROM DO line */ |
| 473 | if (!eeprom93xx_read(pci->eeprom)) { |
| 474 | val &= ~0xff; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return val; |
| 479 | } |
| 480 | |
| 481 | static void dc390_write_config(PCIDevice *dev, |
| 482 | uint32_t addr, uint32_t val, int l) |
| 483 | { |
| 484 | DC390State *pci = DC390(dev); |
| 485 | if (addr == 0x80) { |
| 486 | /* EEPROM write */ |
| 487 | int eesk = val & 0x80 ? 1 : 0; |
| 488 | int eedi = val & 0x40 ? 1 : 0; |
| 489 | eeprom93xx_write(pci->eeprom, 1, eesk, eedi); |
| 490 | } else if (addr == 0xc0) { |
| 491 | /* EEPROM CS low */ |
| 492 | eeprom93xx_write(pci->eeprom, 0, 0, 0); |
| 493 | } else { |
| 494 | pci_default_write_config(dev, addr, val, l); |
| 495 | } |
| 496 | } |
| 497 | |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 498 | static void dc390_scsi_realize(PCIDevice *dev, Error **errp) |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 499 | { |
| 500 | DC390State *pci = DC390(dev); |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 501 | Error *err = NULL; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 502 | uint8_t *contents; |
| 503 | uint16_t chksum = 0; |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 504 | int i; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 505 | |
| 506 | /* init base class */ |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 507 | esp_pci_scsi_realize(dev, &err); |
| 508 | if (err) { |
| 509 | error_propagate(errp, err); |
| 510 | return; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* EEPROM */ |
| 514 | pci->eeprom = eeprom93xx_new(DEVICE(dev), 64); |
| 515 | |
| 516 | /* set default eeprom values */ |
| 517 | contents = (uint8_t *)eeprom93xx_data(pci->eeprom); |
| 518 | |
| 519 | for (i = 0; i < 16; i++) { |
| 520 | contents[i * 2] = 0x57; |
| 521 | contents[i * 2 + 1] = 0x00; |
| 522 | } |
| 523 | contents[EE_ADAPT_SCSI_ID] = 7; |
| 524 | contents[EE_MODE2] = 0x0f; |
| 525 | contents[EE_TAG_CMD_NUM] = 0x04; |
| 526 | contents[EE_ADAPT_OPTIONS] = EE_ADAPT_OPTION_F6_F8_AT_BOOT |
| 527 | | EE_ADAPT_OPTION_BOOT_FROM_CDROM |
| 528 | | EE_ADAPT_OPTION_INT13; |
| 529 | |
| 530 | /* update eeprom checksum */ |
| 531 | for (i = 0; i < EE_CHKSUM1; i += 2) { |
| 532 | chksum += contents[i] + (((uint16_t)contents[i + 1]) << 8); |
| 533 | } |
| 534 | chksum = 0x1234 - chksum; |
| 535 | contents[EE_CHKSUM1] = chksum & 0xff; |
| 536 | contents[EE_CHKSUM2] = chksum >> 8; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void dc390_class_init(ObjectClass *klass, void *data) |
| 540 | { |
| 541 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 542 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 543 | |
Markus Armbruster | ae071cc | 2015-03-09 19:17:28 +0100 | [diff] [blame] | 544 | k->realize = dc390_scsi_realize; |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 545 | k->config_read = dc390_read_config; |
| 546 | k->config_write = dc390_write_config; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 547 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 548 | dc->desc = "Tekram DC-390 SCSI adapter"; |
| 549 | } |
| 550 | |
| 551 | static const TypeInfo dc390_info = { |
Eduardo Habkost | 9295131 | 2020-08-26 14:43:34 -0400 | [diff] [blame] | 552 | .name = TYPE_DC390_DEVICE, |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 553 | .parent = TYPE_AM53C974_DEVICE, |
| 554 | .instance_size = sizeof(DC390State), |
| 555 | .class_init = dc390_class_init, |
| 556 | }; |
| 557 | |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 558 | static void esp_pci_register_types(void) |
| 559 | { |
| 560 | type_register_static(&esp_pci_info); |
Hervé Poussineau | cea936b | 2012-08-04 21:10:06 +0200 | [diff] [blame] | 561 | type_register_static(&dc390_info); |
Hervé Poussineau | aebcf56 | 2012-08-04 21:10:04 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | type_init(esp_pci_register_types) |