1 /* 2 * QEMU AHCI Emulation 3 * 4 * Copyright (c) 2010 qiaochong@loongson.cn 5 * Copyright (c) 2010 Roland Elek <elek.roland@gmail.com> 6 * Copyright (c) 2010 Sebastian Herbszt <herbszt@gmx.de> 7 * Copyright (c) 2010 Alexander Graf <agraf@suse.de> 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 21 * 22 */ 23 24 #include "qemu/osdep.h" 25 #include "hw/hw.h" 26 #include "hw/pci/msi.h" 27 #include "hw/pci/pci.h" 28 29 #include "qemu/error-report.h" 30 #include "qemu/log.h" 31 #include "sysemu/block-backend.h" 32 #include "sysemu/dma.h" 33 #include "hw/ide/internal.h" 34 #include "hw/ide/pci.h" 35 #include "ahci_internal.h" 36 37 #include "trace.h" 38 39 static void check_cmd(AHCIState *s, int port); 40 static int handle_cmd(AHCIState *s, int port, uint8_t slot); 41 static void ahci_reset_port(AHCIState *s, int port); 42 static bool ahci_write_fis_d2h(AHCIDevice *ad); 43 static void ahci_init_d2h(AHCIDevice *ad); 44 static int ahci_dma_prepare_buf(IDEDMA *dma, int32_t limit); 45 static bool ahci_map_clb_address(AHCIDevice *ad); 46 static bool ahci_map_fis_address(AHCIDevice *ad); 47 static void ahci_unmap_clb_address(AHCIDevice *ad); 48 static void ahci_unmap_fis_address(AHCIDevice *ad); 49 50 static const char *AHCIHostReg_lookup[AHCI_HOST_REG__COUNT] = { 51 [AHCI_HOST_REG_CAP] = "CAP", 52 [AHCI_HOST_REG_CTL] = "GHC", 53 [AHCI_HOST_REG_IRQ_STAT] = "IS", 54 [AHCI_HOST_REG_PORTS_IMPL] = "PI", 55 [AHCI_HOST_REG_VERSION] = "VS", 56 [AHCI_HOST_REG_CCC_CTL] = "CCC_CTL", 57 [AHCI_HOST_REG_CCC_PORTS] = "CCC_PORTS", 58 [AHCI_HOST_REG_EM_LOC] = "EM_LOC", 59 [AHCI_HOST_REG_EM_CTL] = "EM_CTL", 60 [AHCI_HOST_REG_CAP2] = "CAP2", 61 [AHCI_HOST_REG_BOHC] = "BOHC", 62 }; 63 64 static const char *AHCIPortReg_lookup[AHCI_PORT_REG__COUNT] = { 65 [AHCI_PORT_REG_LST_ADDR] = "PxCLB", 66 [AHCI_PORT_REG_LST_ADDR_HI] = "PxCLBU", 67 [AHCI_PORT_REG_FIS_ADDR] = "PxFB", 68 [AHCI_PORT_REG_FIS_ADDR_HI] = "PxFBU", 69 [AHCI_PORT_REG_IRQ_STAT] = "PxIS", 70 [AHCI_PORT_REG_IRQ_MASK] = "PXIE", 71 [AHCI_PORT_REG_CMD] = "PxCMD", 72 [7] = "Reserved", 73 [AHCI_PORT_REG_TFDATA] = "PxTFD", 74 [AHCI_PORT_REG_SIG] = "PxSIG", 75 [AHCI_PORT_REG_SCR_STAT] = "PxSSTS", 76 [AHCI_PORT_REG_SCR_CTL] = "PxSCTL", 77 [AHCI_PORT_REG_SCR_ERR] = "PxSERR", 78 [AHCI_PORT_REG_SCR_ACT] = "PxSACT", 79 [AHCI_PORT_REG_CMD_ISSUE] = "PxCI", 80 [AHCI_PORT_REG_SCR_NOTIF] = "PxSNTF", 81 [AHCI_PORT_REG_FIS_CTL] = "PxFBS", 82 [AHCI_PORT_REG_DEV_SLEEP] = "PxDEVSLP", 83 [18 ... 27] = "Reserved", 84 [AHCI_PORT_REG_VENDOR_1 ... 85 AHCI_PORT_REG_VENDOR_4] = "PxVS", 86 }; 87 88 static const char *AHCIPortIRQ_lookup[AHCI_PORT_IRQ__COUNT] = { 89 [AHCI_PORT_IRQ_BIT_DHRS] = "DHRS", 90 [AHCI_PORT_IRQ_BIT_PSS] = "PSS", 91 [AHCI_PORT_IRQ_BIT_DSS] = "DSS", 92 [AHCI_PORT_IRQ_BIT_SDBS] = "SDBS", 93 [AHCI_PORT_IRQ_BIT_UFS] = "UFS", 94 [AHCI_PORT_IRQ_BIT_DPS] = "DPS", 95 [AHCI_PORT_IRQ_BIT_PCS] = "PCS", 96 [AHCI_PORT_IRQ_BIT_DMPS] = "DMPS", 97 [8 ... 21] = "RESERVED", 98 [AHCI_PORT_IRQ_BIT_PRCS] = "PRCS", 99 [AHCI_PORT_IRQ_BIT_IPMS] = "IPMS", 100 [AHCI_PORT_IRQ_BIT_OFS] = "OFS", 101 [25] = "RESERVED", 102 [AHCI_PORT_IRQ_BIT_INFS] = "INFS", 103 [AHCI_PORT_IRQ_BIT_IFS] = "IFS", 104 [AHCI_PORT_IRQ_BIT_HBDS] = "HBDS", 105 [AHCI_PORT_IRQ_BIT_HBFS] = "HBFS", 106 [AHCI_PORT_IRQ_BIT_TFES] = "TFES", 107 [AHCI_PORT_IRQ_BIT_CPDS] = "CPDS" 108 }; 109 110 static uint32_t ahci_port_read(AHCIState *s, int port, int offset) 111 { 112 uint32_t val; 113 AHCIPortRegs *pr = &s->dev[port].port_regs; 114 enum AHCIPortReg regnum = offset / sizeof(uint32_t); 115 assert(regnum < (AHCI_PORT_ADDR_OFFSET_LEN / sizeof(uint32_t))); 116 117 switch (regnum) { 118 case AHCI_PORT_REG_LST_ADDR: 119 val = pr->lst_addr; 120 break; 121 case AHCI_PORT_REG_LST_ADDR_HI: 122 val = pr->lst_addr_hi; 123 break; 124 case AHCI_PORT_REG_FIS_ADDR: 125 val = pr->fis_addr; 126 break; 127 case AHCI_PORT_REG_FIS_ADDR_HI: 128 val = pr->fis_addr_hi; 129 break; 130 case AHCI_PORT_REG_IRQ_STAT: 131 val = pr->irq_stat; 132 break; 133 case AHCI_PORT_REG_IRQ_MASK: 134 val = pr->irq_mask; 135 break; 136 case AHCI_PORT_REG_CMD: 137 val = pr->cmd; 138 break; 139 case AHCI_PORT_REG_TFDATA: 140 val = pr->tfdata; 141 break; 142 case AHCI_PORT_REG_SIG: 143 val = pr->sig; 144 break; 145 case AHCI_PORT_REG_SCR_STAT: 146 if (s->dev[port].port.ifs[0].blk) { 147 val = SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP | 148 SATA_SCR_SSTATUS_SPD_GEN1 | SATA_SCR_SSTATUS_IPM_ACTIVE; 149 } else { 150 val = SATA_SCR_SSTATUS_DET_NODEV; 151 } 152 break; 153 case AHCI_PORT_REG_SCR_CTL: 154 val = pr->scr_ctl; 155 break; 156 case AHCI_PORT_REG_SCR_ERR: 157 val = pr->scr_err; 158 break; 159 case AHCI_PORT_REG_SCR_ACT: 160 val = pr->scr_act; 161 break; 162 case AHCI_PORT_REG_CMD_ISSUE: 163 val = pr->cmd_issue; 164 break; 165 default: 166 trace_ahci_port_read_default(s, port, AHCIPortReg_lookup[regnum], 167 offset); 168 val = 0; 169 } 170 171 trace_ahci_port_read(s, port, AHCIPortReg_lookup[regnum], offset, val); 172 return val; 173 } 174 175 static void ahci_irq_raise(AHCIState *s) 176 { 177 DeviceState *dev_state = s->container; 178 PCIDevice *pci_dev = (PCIDevice *) object_dynamic_cast(OBJECT(dev_state), 179 TYPE_PCI_DEVICE); 180 181 trace_ahci_irq_raise(s); 182 183 if (pci_dev && msi_enabled(pci_dev)) { 184 msi_notify(pci_dev, 0); 185 } else { 186 qemu_irq_raise(s->irq); 187 } 188 } 189 190 static void ahci_irq_lower(AHCIState *s) 191 { 192 DeviceState *dev_state = s->container; 193 PCIDevice *pci_dev = (PCIDevice *) object_dynamic_cast(OBJECT(dev_state), 194 TYPE_PCI_DEVICE); 195 196 trace_ahci_irq_lower(s); 197 198 if (!pci_dev || !msi_enabled(pci_dev)) { 199 qemu_irq_lower(s->irq); 200 } 201 } 202 203 static void ahci_check_irq(AHCIState *s) 204 { 205 int i; 206 uint32_t old_irq = s->control_regs.irqstatus; 207 208 s->control_regs.irqstatus = 0; 209 for (i = 0; i < s->ports; i++) { 210 AHCIPortRegs *pr = &s->dev[i].port_regs; 211 if (pr->irq_stat & pr->irq_mask) { 212 s->control_regs.irqstatus |= (1 << i); 213 } 214 } 215 trace_ahci_check_irq(s, old_irq, s->control_regs.irqstatus); 216 if (s->control_regs.irqstatus && 217 (s->control_regs.ghc & HOST_CTL_IRQ_EN)) { 218 ahci_irq_raise(s); 219 } else { 220 ahci_irq_lower(s); 221 } 222 } 223 224 static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d, 225 enum AHCIPortIRQ irqbit) 226 { 227 g_assert((unsigned)irqbit < 32); 228 uint32_t irq = 1U << irqbit; 229 uint32_t irqstat = d->port_regs.irq_stat | irq; 230 231 trace_ahci_trigger_irq(s, d->port_no, 232 AHCIPortIRQ_lookup[irqbit], irq, 233 d->port_regs.irq_stat, irqstat, 234 irqstat & d->port_regs.irq_mask); 235 236 d->port_regs.irq_stat = irqstat; 237 ahci_check_irq(s); 238 } 239 240 static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr, 241 uint32_t wanted) 242 { 243 hwaddr len = wanted; 244 245 if (*ptr) { 246 dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len); 247 } 248 249 *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE); 250 if (len < wanted) { 251 dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len); 252 *ptr = NULL; 253 } 254 } 255 256 /** 257 * Check the cmd register to see if we should start or stop 258 * the DMA or FIS RX engines. 259 * 260 * @ad: Device to dis/engage. 261 * 262 * @return 0 on success, -1 on error. 263 */ 264 static int ahci_cond_start_engines(AHCIDevice *ad) 265 { 266 AHCIPortRegs *pr = &ad->port_regs; 267 bool cmd_start = pr->cmd & PORT_CMD_START; 268 bool cmd_on = pr->cmd & PORT_CMD_LIST_ON; 269 bool fis_start = pr->cmd & PORT_CMD_FIS_RX; 270 bool fis_on = pr->cmd & PORT_CMD_FIS_ON; 271 272 if (cmd_start && !cmd_on) { 273 if (!ahci_map_clb_address(ad)) { 274 pr->cmd &= ~PORT_CMD_START; 275 error_report("AHCI: Failed to start DMA engine: " 276 "bad command list buffer address"); 277 return -1; 278 } 279 } else if (!cmd_start && cmd_on) { 280 ahci_unmap_clb_address(ad); 281 } 282 283 if (fis_start && !fis_on) { 284 if (!ahci_map_fis_address(ad)) { 285 pr->cmd &= ~PORT_CMD_FIS_RX; 286 error_report("AHCI: Failed to start FIS receive engine: " 287 "bad FIS receive buffer address"); 288 return -1; 289 } 290 } else if (!fis_start && fis_on) { 291 ahci_unmap_fis_address(ad); 292 } 293 294 return 0; 295 } 296 297 static void ahci_port_write(AHCIState *s, int port, int offset, uint32_t val) 298 { 299 AHCIPortRegs *pr = &s->dev[port].port_regs; 300 enum AHCIPortReg regnum = offset / sizeof(uint32_t); 301 assert(regnum < (AHCI_PORT_ADDR_OFFSET_LEN / sizeof(uint32_t))); 302 trace_ahci_port_write(s, port, AHCIPortReg_lookup[regnum], offset, val); 303 304 switch (regnum) { 305 case AHCI_PORT_REG_LST_ADDR: 306 pr->lst_addr = val; 307 break; 308 case AHCI_PORT_REG_LST_ADDR_HI: 309 pr->lst_addr_hi = val; 310 break; 311 case AHCI_PORT_REG_FIS_ADDR: 312 pr->fis_addr = val; 313 break; 314 case AHCI_PORT_REG_FIS_ADDR_HI: 315 pr->fis_addr_hi = val; 316 break; 317 case AHCI_PORT_REG_IRQ_STAT: 318 pr->irq_stat &= ~val; 319 ahci_check_irq(s); 320 break; 321 case AHCI_PORT_REG_IRQ_MASK: 322 pr->irq_mask = val & 0xfdc000ff; 323 ahci_check_irq(s); 324 break; 325 case AHCI_PORT_REG_CMD: 326 /* Block any Read-only fields from being set; 327 * including LIST_ON and FIS_ON. 328 * The spec requires to set ICC bits to zero after the ICC change 329 * is done. We don't support ICC state changes, therefore always 330 * force the ICC bits to zero. 331 */ 332 pr->cmd = (pr->cmd & PORT_CMD_RO_MASK) | 333 (val & ~(PORT_CMD_RO_MASK | PORT_CMD_ICC_MASK)); 334 335 /* Check FIS RX and CLB engines */ 336 ahci_cond_start_engines(&s->dev[port]); 337 338 /* XXX usually the FIS would be pending on the bus here and 339 issuing deferred until the OS enables FIS receival. 340 Instead, we only submit it once - which works in most 341 cases, but is a hack. */ 342 if ((pr->cmd & PORT_CMD_FIS_ON) && 343 !s->dev[port].init_d2h_sent) { 344 ahci_init_d2h(&s->dev[port]); 345 } 346 347 check_cmd(s, port); 348 break; 349 case AHCI_PORT_REG_TFDATA: 350 case AHCI_PORT_REG_SIG: 351 case AHCI_PORT_REG_SCR_STAT: 352 /* Read Only */ 353 break; 354 case AHCI_PORT_REG_SCR_CTL: 355 if (((pr->scr_ctl & AHCI_SCR_SCTL_DET) == 1) && 356 ((val & AHCI_SCR_SCTL_DET) == 0)) { 357 ahci_reset_port(s, port); 358 } 359 pr->scr_ctl = val; 360 break; 361 case AHCI_PORT_REG_SCR_ERR: 362 pr->scr_err &= ~val; 363 break; 364 case AHCI_PORT_REG_SCR_ACT: 365 /* RW1 */ 366 pr->scr_act |= val; 367 break; 368 case AHCI_PORT_REG_CMD_ISSUE: 369 pr->cmd_issue |= val; 370 check_cmd(s, port); 371 break; 372 default: 373 trace_ahci_port_write_unimpl(s, port, AHCIPortReg_lookup[regnum], 374 offset, val); 375 qemu_log_mask(LOG_UNIMP, "Attempted write to unimplemented register: " 376 "AHCI port %d register %s, offset 0x%x: 0x%"PRIx32, 377 port, AHCIPortReg_lookup[regnum], offset, val); 378 break; 379 } 380 } 381 382 static uint64_t ahci_mem_read_32(void *opaque, hwaddr addr) 383 { 384 AHCIState *s = opaque; 385 uint32_t val = 0; 386 387 if (addr < AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR) { 388 enum AHCIHostReg regnum = addr / 4; 389 assert(regnum < AHCI_HOST_REG__COUNT); 390 391 switch (regnum) { 392 case AHCI_HOST_REG_CAP: 393 val = s->control_regs.cap; 394 break; 395 case AHCI_HOST_REG_CTL: 396 val = s->control_regs.ghc; 397 break; 398 case AHCI_HOST_REG_IRQ_STAT: 399 val = s->control_regs.irqstatus; 400 break; 401 case AHCI_HOST_REG_PORTS_IMPL: 402 val = s->control_regs.impl; 403 break; 404 case AHCI_HOST_REG_VERSION: 405 val = s->control_regs.version; 406 break; 407 default: 408 trace_ahci_mem_read_32_host_default(s, AHCIHostReg_lookup[regnum], 409 addr); 410 } 411 trace_ahci_mem_read_32_host(s, AHCIHostReg_lookup[regnum], addr, val); 412 } else if ((addr >= AHCI_PORT_REGS_START_ADDR) && 413 (addr < (AHCI_PORT_REGS_START_ADDR + 414 (s->ports * AHCI_PORT_ADDR_OFFSET_LEN)))) { 415 val = ahci_port_read(s, (addr - AHCI_PORT_REGS_START_ADDR) >> 7, 416 addr & AHCI_PORT_ADDR_OFFSET_MASK); 417 } else { 418 trace_ahci_mem_read_32_default(s, addr, val); 419 } 420 421 trace_ahci_mem_read_32(s, addr, val); 422 return val; 423 } 424 425 426 /** 427 * AHCI 1.3 section 3 ("HBA Memory Registers") 428 * Support unaligned 8/16/32 bit reads, and 64 bit aligned reads. 429 * Caller is responsible for masking unwanted higher order bytes. 430 */ 431 static uint64_t ahci_mem_read(void *opaque, hwaddr addr, unsigned size) 432 { 433 hwaddr aligned = addr & ~0x3; 434 int ofst = addr - aligned; 435 uint64_t lo = ahci_mem_read_32(opaque, aligned); 436 uint64_t hi; 437 uint64_t val; 438 439 /* if < 8 byte read does not cross 4 byte boundary */ 440 if (ofst + size <= 4) { 441 val = lo >> (ofst * 8); 442 } else { 443 g_assert(size > 1); 444 445 /* If the 64bit read is unaligned, we will produce undefined 446 * results. AHCI does not support unaligned 64bit reads. */ 447 hi = ahci_mem_read_32(opaque, aligned + 4); 448 val = (hi << 32 | lo) >> (ofst * 8); 449 } 450 451 trace_ahci_mem_read(opaque, size, addr, val); 452 return val; 453 } 454 455 456 static void ahci_mem_write(void *opaque, hwaddr addr, 457 uint64_t val, unsigned size) 458 { 459 AHCIState *s = opaque; 460 461 trace_ahci_mem_write(s, size, addr, val); 462 463 /* Only aligned reads are allowed on AHCI */ 464 if (addr & 3) { 465 fprintf(stderr, "ahci: Mis-aligned write to addr 0x" 466 TARGET_FMT_plx "\n", addr); 467 return; 468 } 469 470 if (addr < AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR) { 471 enum AHCIHostReg regnum = addr / 4; 472 assert(regnum < AHCI_HOST_REG__COUNT); 473 474 switch (regnum) { 475 case AHCI_HOST_REG_CAP: /* R/WO, RO */ 476 /* FIXME handle R/WO */ 477 break; 478 case AHCI_HOST_REG_CTL: /* R/W */ 479 if (val & HOST_CTL_RESET) { 480 ahci_reset(s); 481 } else { 482 s->control_regs.ghc = (val & 0x3) | HOST_CTL_AHCI_EN; 483 ahci_check_irq(s); 484 } 485 break; 486 case AHCI_HOST_REG_IRQ_STAT: /* R/WC, RO */ 487 s->control_regs.irqstatus &= ~val; 488 ahci_check_irq(s); 489 break; 490 case AHCI_HOST_REG_PORTS_IMPL: /* R/WO, RO */ 491 /* FIXME handle R/WO */ 492 break; 493 case AHCI_HOST_REG_VERSION: /* RO */ 494 /* FIXME report write? */ 495 break; 496 default: 497 qemu_log_mask(LOG_UNIMP, 498 "Attempted write to unimplemented register: " 499 "AHCI host register %s, " 500 "offset 0x%"PRIx64": 0x%"PRIx64, 501 AHCIHostReg_lookup[regnum], addr, val); 502 trace_ahci_mem_write_host_unimpl(s, size, 503 AHCIHostReg_lookup[regnum], addr); 504 } 505 trace_ahci_mem_write_host(s, size, AHCIHostReg_lookup[regnum], 506 addr, val); 507 } else if ((addr >= AHCI_PORT_REGS_START_ADDR) && 508 (addr < (AHCI_PORT_REGS_START_ADDR + 509 (s->ports * AHCI_PORT_ADDR_OFFSET_LEN)))) { 510 ahci_port_write(s, (addr - AHCI_PORT_REGS_START_ADDR) >> 7, 511 addr & AHCI_PORT_ADDR_OFFSET_MASK, val); 512 } else { 513 qemu_log_mask(LOG_UNIMP, "Attempted write to unimplemented register: " 514 "AHCI global register at offset 0x%"PRIx64": 0x%"PRIx64, 515 addr, val); 516 trace_ahci_mem_write_unimpl(s, size, addr, val); 517 } 518 } 519 520 static const MemoryRegionOps ahci_mem_ops = { 521 .read = ahci_mem_read, 522 .write = ahci_mem_write, 523 .endianness = DEVICE_LITTLE_ENDIAN, 524 }; 525 526 static uint64_t ahci_idp_read(void *opaque, hwaddr addr, 527 unsigned size) 528 { 529 AHCIState *s = opaque; 530 531 if (addr == s->idp_offset) { 532 /* index register */ 533 return s->idp_index; 534 } else if (addr == s->idp_offset + 4) { 535 /* data register - do memory read at location selected by index */ 536 return ahci_mem_read(opaque, s->idp_index, size); 537 } else { 538 return 0; 539 } 540 } 541 542 static void ahci_idp_write(void *opaque, hwaddr addr, 543 uint64_t val, unsigned size) 544 { 545 AHCIState *s = opaque; 546 547 if (addr == s->idp_offset) { 548 /* index register - mask off reserved bits */ 549 s->idp_index = (uint32_t)val & ((AHCI_MEM_BAR_SIZE - 1) & ~3); 550 } else if (addr == s->idp_offset + 4) { 551 /* data register - do memory write at location selected by index */ 552 ahci_mem_write(opaque, s->idp_index, val, size); 553 } 554 } 555 556 static const MemoryRegionOps ahci_idp_ops = { 557 .read = ahci_idp_read, 558 .write = ahci_idp_write, 559 .endianness = DEVICE_LITTLE_ENDIAN, 560 }; 561 562 563 static void ahci_reg_init(AHCIState *s) 564 { 565 int i; 566 567 s->control_regs.cap = (s->ports - 1) | 568 (AHCI_NUM_COMMAND_SLOTS << 8) | 569 (AHCI_SUPPORTED_SPEED_GEN1 << AHCI_SUPPORTED_SPEED) | 570 HOST_CAP_NCQ | HOST_CAP_AHCI | HOST_CAP_64; 571 572 s->control_regs.impl = (1 << s->ports) - 1; 573 574 s->control_regs.version = AHCI_VERSION_1_0; 575 576 for (i = 0; i < s->ports; i++) { 577 s->dev[i].port_state = STATE_RUN; 578 } 579 } 580 581 static void check_cmd(AHCIState *s, int port) 582 { 583 AHCIPortRegs *pr = &s->dev[port].port_regs; 584 uint8_t slot; 585 586 if ((pr->cmd & PORT_CMD_START) && pr->cmd_issue) { 587 for (slot = 0; (slot < 32) && pr->cmd_issue; slot++) { 588 if ((pr->cmd_issue & (1U << slot)) && 589 !handle_cmd(s, port, slot)) { 590 pr->cmd_issue &= ~(1U << slot); 591 } 592 } 593 } 594 } 595 596 static void ahci_check_cmd_bh(void *opaque) 597 { 598 AHCIDevice *ad = opaque; 599 600 qemu_bh_delete(ad->check_bh); 601 ad->check_bh = NULL; 602 603 check_cmd(ad->hba, ad->port_no); 604 } 605 606 static void ahci_init_d2h(AHCIDevice *ad) 607 { 608 IDEState *ide_state = &ad->port.ifs[0]; 609 AHCIPortRegs *pr = &ad->port_regs; 610 611 if (ad->init_d2h_sent) { 612 return; 613 } 614 615 if (ahci_write_fis_d2h(ad)) { 616 ad->init_d2h_sent = true; 617 /* We're emulating receiving the first Reg H2D Fis from the device; 618 * Update the SIG register, but otherwise proceed as normal. */ 619 pr->sig = ((uint32_t)ide_state->hcyl << 24) | 620 (ide_state->lcyl << 16) | 621 (ide_state->sector << 8) | 622 (ide_state->nsector & 0xFF); 623 } 624 } 625 626 static void ahci_set_signature(AHCIDevice *ad, uint32_t sig) 627 { 628 IDEState *s = &ad->port.ifs[0]; 629 s->hcyl = sig >> 24 & 0xFF; 630 s->lcyl = sig >> 16 & 0xFF; 631 s->sector = sig >> 8 & 0xFF; 632 s->nsector = sig & 0xFF; 633 634 trace_ahci_set_signature(ad->hba, ad->port_no, s->nsector, s->sector, 635 s->lcyl, s->hcyl, sig); 636 } 637 638 static void ahci_reset_port(AHCIState *s, int port) 639 { 640 AHCIDevice *d = &s->dev[port]; 641 AHCIPortRegs *pr = &d->port_regs; 642 IDEState *ide_state = &d->port.ifs[0]; 643 int i; 644 645 trace_ahci_reset_port(s, port); 646 647 ide_bus_reset(&d->port); 648 ide_state->ncq_queues = AHCI_MAX_CMDS; 649 650 pr->scr_stat = 0; 651 pr->scr_err = 0; 652 pr->scr_act = 0; 653 pr->tfdata = 0x7F; 654 pr->sig = 0xFFFFFFFF; 655 d->busy_slot = -1; 656 d->init_d2h_sent = false; 657 658 ide_state = &s->dev[port].port.ifs[0]; 659 if (!ide_state->blk) { 660 return; 661 } 662 663 /* reset ncq queue */ 664 for (i = 0; i < AHCI_MAX_CMDS; i++) { 665 NCQTransferState *ncq_tfs = &s->dev[port].ncq_tfs[i]; 666 ncq_tfs->halt = false; 667 if (!ncq_tfs->used) { 668 continue; 669 } 670 671 if (ncq_tfs->aiocb) { 672 blk_aio_cancel(ncq_tfs->aiocb); 673 ncq_tfs->aiocb = NULL; 674 } 675 676 /* Maybe we just finished the request thanks to blk_aio_cancel() */ 677 if (!ncq_tfs->used) { 678 continue; 679 } 680 681 qemu_sglist_destroy(&ncq_tfs->sglist); 682 ncq_tfs->used = 0; 683 } 684 685 s->dev[port].port_state = STATE_RUN; 686 if (ide_state->drive_kind == IDE_CD) { 687 ahci_set_signature(d, SATA_SIGNATURE_CDROM);\ 688 ide_state->status = SEEK_STAT | WRERR_STAT | READY_STAT; 689 } else { 690 ahci_set_signature(d, SATA_SIGNATURE_DISK); 691 ide_state->status = SEEK_STAT | WRERR_STAT; 692 } 693 694 ide_state->error = 1; 695 ahci_init_d2h(d); 696 } 697 698 /* Buffer pretty output based on a raw FIS structure. */ 699 static char *ahci_pretty_buffer_fis(uint8_t *fis, int cmd_len) 700 { 701 int i; 702 GString *s = g_string_new("FIS:"); 703 704 for (i = 0; i < cmd_len; i++) { 705 if ((i & 0xf) == 0) { 706 g_string_append_printf(s, "\n0x%02x: ", i); 707 } 708 g_string_append_printf(s, "%02x ", fis[i]); 709 } 710 g_string_append_c(s, '\n'); 711 712 return g_string_free(s, FALSE); 713 } 714 715 static bool ahci_map_fis_address(AHCIDevice *ad) 716 { 717 AHCIPortRegs *pr = &ad->port_regs; 718 map_page(ad->hba->as, &ad->res_fis, 719 ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256); 720 if (ad->res_fis != NULL) { 721 pr->cmd |= PORT_CMD_FIS_ON; 722 return true; 723 } 724 725 pr->cmd &= ~PORT_CMD_FIS_ON; 726 return false; 727 } 728 729 static void ahci_unmap_fis_address(AHCIDevice *ad) 730 { 731 if (ad->res_fis == NULL) { 732 trace_ahci_unmap_fis_address_null(ad->hba, ad->port_no); 733 return; 734 } 735 ad->port_regs.cmd &= ~PORT_CMD_FIS_ON; 736 dma_memory_unmap(ad->hba->as, ad->res_fis, 256, 737 DMA_DIRECTION_FROM_DEVICE, 256); 738 ad->res_fis = NULL; 739 } 740 741 static bool ahci_map_clb_address(AHCIDevice *ad) 742 { 743 AHCIPortRegs *pr = &ad->port_regs; 744 ad->cur_cmd = NULL; 745 map_page(ad->hba->as, &ad->lst, 746 ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024); 747 if (ad->lst != NULL) { 748 pr->cmd |= PORT_CMD_LIST_ON; 749 return true; 750 } 751 752 pr->cmd &= ~PORT_CMD_LIST_ON; 753 return false; 754 } 755 756 static void ahci_unmap_clb_address(AHCIDevice *ad) 757 { 758 if (ad->lst == NULL) { 759 trace_ahci_unmap_clb_address_null(ad->hba, ad->port_no); 760 return; 761 } 762 ad->port_regs.cmd &= ~PORT_CMD_LIST_ON; 763 dma_memory_unmap(ad->hba->as, ad->lst, 1024, 764 DMA_DIRECTION_FROM_DEVICE, 1024); 765 ad->lst = NULL; 766 } 767 768 static void ahci_write_fis_sdb(AHCIState *s, NCQTransferState *ncq_tfs) 769 { 770 AHCIDevice *ad = ncq_tfs->drive; 771 AHCIPortRegs *pr = &ad->port_regs; 772 IDEState *ide_state; 773 SDBFIS *sdb_fis; 774 775 if (!ad->res_fis || 776 !(pr->cmd & PORT_CMD_FIS_RX)) { 777 return; 778 } 779 780 sdb_fis = (SDBFIS *)&ad->res_fis[RES_FIS_SDBFIS]; 781 ide_state = &ad->port.ifs[0]; 782 783 sdb_fis->type = SATA_FIS_TYPE_SDB; 784 /* Interrupt pending & Notification bit */ 785 sdb_fis->flags = 0x40; /* Interrupt bit, always 1 for NCQ */ 786 sdb_fis->status = ide_state->status & 0x77; 787 sdb_fis->error = ide_state->error; 788 /* update SAct field in SDB_FIS */ 789 sdb_fis->payload = cpu_to_le32(ad->finished); 790 791 /* Update shadow registers (except BSY 0x80 and DRQ 0x08) */ 792 pr->tfdata = (ad->port.ifs[0].error << 8) | 793 (ad->port.ifs[0].status & 0x77) | 794 (pr->tfdata & 0x88); 795 pr->scr_act &= ~ad->finished; 796 ad->finished = 0; 797 798 /* Trigger IRQ if interrupt bit is set (which currently, it always is) */ 799 if (sdb_fis->flags & 0x40) { 800 ahci_trigger_irq(s, ad, AHCI_PORT_IRQ_BIT_SDBS); 801 } 802 } 803 804 static void ahci_write_fis_pio(AHCIDevice *ad, uint16_t len) 805 { 806 AHCIPortRegs *pr = &ad->port_regs; 807 uint8_t *pio_fis; 808 IDEState *s = &ad->port.ifs[0]; 809 810 if (!ad->res_fis || !(pr->cmd & PORT_CMD_FIS_RX)) { 811 return; 812 } 813 814 pio_fis = &ad->res_fis[RES_FIS_PSFIS]; 815 816 pio_fis[0] = SATA_FIS_TYPE_PIO_SETUP; 817 pio_fis[1] = (ad->hba->control_regs.irqstatus ? (1 << 6) : 0); 818 pio_fis[2] = s->status; 819 pio_fis[3] = s->error; 820 821 pio_fis[4] = s->sector; 822 pio_fis[5] = s->lcyl; 823 pio_fis[6] = s->hcyl; 824 pio_fis[7] = s->select; 825 pio_fis[8] = s->hob_sector; 826 pio_fis[9] = s->hob_lcyl; 827 pio_fis[10] = s->hob_hcyl; 828 pio_fis[11] = 0; 829 pio_fis[12] = s->nsector & 0xFF; 830 pio_fis[13] = (s->nsector >> 8) & 0xFF; 831 pio_fis[14] = 0; 832 pio_fis[15] = s->status; 833 pio_fis[16] = len & 255; 834 pio_fis[17] = len >> 8; 835 pio_fis[18] = 0; 836 pio_fis[19] = 0; 837 838 /* Update shadow registers: */ 839 pr->tfdata = (ad->port.ifs[0].error << 8) | 840 ad->port.ifs[0].status; 841 842 if (pio_fis[2] & ERR_STAT) { 843 ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_TFES); 844 } 845 846 ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_PSS); 847 } 848 849 static bool ahci_write_fis_d2h(AHCIDevice *ad) 850 { 851 AHCIPortRegs *pr = &ad->port_regs; 852 uint8_t *d2h_fis; 853 int i; 854 IDEState *s = &ad->port.ifs[0]; 855 856 if (!ad->res_fis || !(pr->cmd & PORT_CMD_FIS_RX)) { 857 return false; 858 } 859 860 d2h_fis = &ad->res_fis[RES_FIS_RFIS]; 861 862 d2h_fis[0] = SATA_FIS_TYPE_REGISTER_D2H; 863 d2h_fis[1] = (ad->hba->control_regs.irqstatus ? (1 << 6) : 0); 864 d2h_fis[2] = s->status; 865 d2h_fis[3] = s->error; 866 867 d2h_fis[4] = s->sector; 868 d2h_fis[5] = s->lcyl; 869 d2h_fis[6] = s->hcyl; 870 d2h_fis[7] = s->select; 871 d2h_fis[8] = s->hob_sector; 872 d2h_fis[9] = s->hob_lcyl; 873 d2h_fis[10] = s->hob_hcyl; 874 d2h_fis[11] = 0; 875 d2h_fis[12] = s->nsector & 0xFF; 876 d2h_fis[13] = (s->nsector >> 8) & 0xFF; 877 for (i = 14; i < 20; i++) { 878 d2h_fis[i] = 0; 879 } 880 881 /* Update shadow registers: */ 882 pr->tfdata = (ad->port.ifs[0].error << 8) | 883 ad->port.ifs[0].status; 884 885 if (d2h_fis[2] & ERR_STAT) { 886 ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_TFES); 887 } 888 889 ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_DHRS); 890 return true; 891 } 892 893 static int prdt_tbl_entry_size(const AHCI_SG *tbl) 894 { 895 /* flags_size is zero-based */ 896 return (le32_to_cpu(tbl->flags_size) & AHCI_PRDT_SIZE_MASK) + 1; 897 } 898 899 /** 900 * Fetch entries in a guest-provided PRDT and convert it into a QEMU SGlist. 901 * @ad: The AHCIDevice for whom we are building the SGList. 902 * @sglist: The SGList target to add PRD entries to. 903 * @cmd: The AHCI Command Header that describes where the PRDT is. 904 * @limit: The remaining size of the S/ATA transaction, in bytes. 905 * @offset: The number of bytes already transferred, in bytes. 906 * 907 * The AHCI PRDT can describe up to 256GiB. S/ATA only support transactions of 908 * up to 32MiB as of ATA8-ACS3 rev 1b, assuming a 512 byte sector size. We stop 909 * building the sglist from the PRDT as soon as we hit @limit bytes, 910 * which is <= INT32_MAX/2GiB. 911 */ 912 static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, 913 AHCICmdHdr *cmd, int64_t limit, uint64_t offset) 914 { 915 uint16_t opts = le16_to_cpu(cmd->opts); 916 uint16_t prdtl = le16_to_cpu(cmd->prdtl); 917 uint64_t cfis_addr = le64_to_cpu(cmd->tbl_addr); 918 uint64_t prdt_addr = cfis_addr + 0x80; 919 dma_addr_t prdt_len = (prdtl * sizeof(AHCI_SG)); 920 dma_addr_t real_prdt_len = prdt_len; 921 uint8_t *prdt; 922 int i; 923 int r = 0; 924 uint64_t sum = 0; 925 int off_idx = -1; 926 int64_t off_pos = -1; 927 int tbl_entry_size; 928 IDEBus *bus = &ad->port; 929 BusState *qbus = BUS(bus); 930 931 trace_ahci_populate_sglist(ad->hba, ad->port_no); 932 933 if (!prdtl) { 934 trace_ahci_populate_sglist_no_prdtl(ad->hba, ad->port_no, opts); 935 return -1; 936 } 937 938 /* map PRDT */ 939 if (!(prdt = dma_memory_map(ad->hba->as, prdt_addr, &prdt_len, 940 DMA_DIRECTION_TO_DEVICE))){ 941 trace_ahci_populate_sglist_no_map(ad->hba, ad->port_no); 942 return -1; 943 } 944 945 if (prdt_len < real_prdt_len) { 946 trace_ahci_populate_sglist_short_map(ad->hba, ad->port_no); 947 r = -1; 948 goto out; 949 } 950 951 /* Get entries in the PRDT, init a qemu sglist accordingly */ 952 if (prdtl > 0) { 953 AHCI_SG *tbl = (AHCI_SG *)prdt; 954 sum = 0; 955 for (i = 0; i < prdtl; i++) { 956 tbl_entry_size = prdt_tbl_entry_size(&tbl[i]); 957 if (offset < (sum + tbl_entry_size)) { 958 off_idx = i; 959 off_pos = offset - sum; 960 break; 961 } 962 sum += tbl_entry_size; 963 } 964 if ((off_idx == -1) || (off_pos < 0) || (off_pos > tbl_entry_size)) { 965 trace_ahci_populate_sglist_bad_offset(ad->hba, ad->port_no, 966 off_idx, off_pos); 967 r = -1; 968 goto out; 969 } 970 971 qemu_sglist_init(sglist, qbus->parent, (prdtl - off_idx), 972 ad->hba->as); 973 qemu_sglist_add(sglist, le64_to_cpu(tbl[off_idx].addr) + off_pos, 974 MIN(prdt_tbl_entry_size(&tbl[off_idx]) - off_pos, 975 limit)); 976 977 for (i = off_idx + 1; i < prdtl && sglist->size < limit; i++) { 978 qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr), 979 MIN(prdt_tbl_entry_size(&tbl[i]), 980 limit - sglist->size)); 981 } 982 } 983 984 out: 985 dma_memory_unmap(ad->hba->as, prdt, prdt_len, 986 DMA_DIRECTION_TO_DEVICE, prdt_len); 987 return r; 988 } 989 990 static void ncq_err(NCQTransferState *ncq_tfs) 991 { 992 IDEState *ide_state = &ncq_tfs->drive->port.ifs[0]; 993 994 ide_state->error = ABRT_ERR; 995 ide_state->status = READY_STAT | ERR_STAT; 996 ncq_tfs->drive->port_regs.scr_err |= (1 << ncq_tfs->tag); 997 qemu_sglist_destroy(&ncq_tfs->sglist); 998 ncq_tfs->used = 0; 999 } 1000 1001 static void ncq_finish(NCQTransferState *ncq_tfs) 1002 { 1003 /* If we didn't error out, set our finished bit. Errored commands 1004 * do not get a bit set for the SDB FIS ACT register, nor do they 1005 * clear the outstanding bit in scr_act (PxSACT). */ 1006 if (!(ncq_tfs->drive->port_regs.scr_err & (1 << ncq_tfs->tag))) { 1007 ncq_tfs->drive->finished |= (1 << ncq_tfs->tag); 1008 } 1009 1010 ahci_write_fis_sdb(ncq_tfs->drive->hba, ncq_tfs); 1011 1012 trace_ncq_finish(ncq_tfs->drive->hba, ncq_tfs->drive->port_no, 1013 ncq_tfs->tag); 1014 1015 block_acct_done(blk_get_stats(ncq_tfs->drive->port.ifs[0].blk), 1016 &ncq_tfs->acct); 1017 qemu_sglist_destroy(&ncq_tfs->sglist); 1018 ncq_tfs->used = 0; 1019 } 1020 1021 static void ncq_cb(void *opaque, int ret) 1022 { 1023 NCQTransferState *ncq_tfs = (NCQTransferState *)opaque; 1024 IDEState *ide_state = &ncq_tfs->drive->port.ifs[0]; 1025 1026 ncq_tfs->aiocb = NULL; 1027 if (ret == -ECANCELED) { 1028 return; 1029 } 1030 1031 if (ret < 0) { 1032 bool is_read = ncq_tfs->cmd == READ_FPDMA_QUEUED; 1033 BlockErrorAction action = blk_get_error_action(ide_state->blk, 1034 is_read, -ret); 1035 if (action == BLOCK_ERROR_ACTION_STOP) { 1036 ncq_tfs->halt = true; 1037 ide_state->bus->error_status = IDE_RETRY_HBA; 1038 } else if (action == BLOCK_ERROR_ACTION_REPORT) { 1039 ncq_err(ncq_tfs); 1040 } 1041 blk_error_action(ide_state->blk, action, is_read, -ret); 1042 } else { 1043 ide_state->status = READY_STAT | SEEK_STAT; 1044 } 1045 1046 if (!ncq_tfs->halt) { 1047 ncq_finish(ncq_tfs); 1048 } 1049 } 1050 1051 static int is_ncq(uint8_t ata_cmd) 1052 { 1053 /* Based on SATA 3.2 section 13.6.3.2 */ 1054 switch (ata_cmd) { 1055 case READ_FPDMA_QUEUED: 1056 case WRITE_FPDMA_QUEUED: 1057 case NCQ_NON_DATA: 1058 case RECEIVE_FPDMA_QUEUED: 1059 case SEND_FPDMA_QUEUED: 1060 return 1; 1061 default: 1062 return 0; 1063 } 1064 } 1065 1066 static void execute_ncq_command(NCQTransferState *ncq_tfs) 1067 { 1068 AHCIDevice *ad = ncq_tfs->drive; 1069 IDEState *ide_state = &ad->port.ifs[0]; 1070 int port = ad->port_no; 1071 1072 g_assert(is_ncq(ncq_tfs->cmd)); 1073 ncq_tfs->halt = false; 1074 1075 switch (ncq_tfs->cmd) { 1076 case READ_FPDMA_QUEUED: 1077 trace_execute_ncq_command_read(ad->hba, port, ncq_tfs->tag, 1078 ncq_tfs->sector_count, ncq_tfs->lba); 1079 dma_acct_start(ide_state->blk, &ncq_tfs->acct, 1080 &ncq_tfs->sglist, BLOCK_ACCT_READ); 1081 ncq_tfs->aiocb = dma_blk_read(ide_state->blk, &ncq_tfs->sglist, 1082 ncq_tfs->lba << BDRV_SECTOR_BITS, 1083 BDRV_SECTOR_SIZE, 1084 ncq_cb, ncq_tfs); 1085 break; 1086 case WRITE_FPDMA_QUEUED: 1087 trace_execute_ncq_command_read(ad->hba, port, ncq_tfs->tag, 1088 ncq_tfs->sector_count, ncq_tfs->lba); 1089 dma_acct_start(ide_state->blk, &ncq_tfs->acct, 1090 &ncq_tfs->sglist, BLOCK_ACCT_WRITE); 1091 ncq_tfs->aiocb = dma_blk_write(ide_state->blk, &ncq_tfs->sglist, 1092 ncq_tfs->lba << BDRV_SECTOR_BITS, 1093 BDRV_SECTOR_SIZE, 1094 ncq_cb, ncq_tfs); 1095 break; 1096 default: 1097 trace_execute_ncq_command_unsup(ad->hba, port, 1098 ncq_tfs->tag, ncq_tfs->cmd); 1099 ncq_err(ncq_tfs); 1100 } 1101 } 1102 1103 1104 static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis, 1105 uint8_t slot) 1106 { 1107 AHCIDevice *ad = &s->dev[port]; 1108 NCQFrame *ncq_fis = (NCQFrame*)cmd_fis; 1109 uint8_t tag = ncq_fis->tag >> 3; 1110 NCQTransferState *ncq_tfs = &ad->ncq_tfs[tag]; 1111 size_t size; 1112 1113 g_assert(is_ncq(ncq_fis->command)); 1114 if (ncq_tfs->used) { 1115 /* error - already in use */ 1116 fprintf(stderr, "%s: tag %d already used\n", __func__, tag); 1117 return; 1118 } 1119 1120 ncq_tfs->used = 1; 1121 ncq_tfs->drive = ad; 1122 ncq_tfs->slot = slot; 1123 ncq_tfs->cmdh = &((AHCICmdHdr *)ad->lst)[slot]; 1124 ncq_tfs->cmd = ncq_fis->command; 1125 ncq_tfs->lba = ((uint64_t)ncq_fis->lba5 << 40) | 1126 ((uint64_t)ncq_fis->lba4 << 32) | 1127 ((uint64_t)ncq_fis->lba3 << 24) | 1128 ((uint64_t)ncq_fis->lba2 << 16) | 1129 ((uint64_t)ncq_fis->lba1 << 8) | 1130 (uint64_t)ncq_fis->lba0; 1131 ncq_tfs->tag = tag; 1132 1133 /* Sanity-check the NCQ packet */ 1134 if (tag != slot) { 1135 trace_process_ncq_command_mismatch(s, port, tag, slot); 1136 } 1137 1138 if (ncq_fis->aux0 || ncq_fis->aux1 || ncq_fis->aux2 || ncq_fis->aux3) { 1139 trace_process_ncq_command_aux(s, port, tag); 1140 } 1141 if (ncq_fis->prio || ncq_fis->icc) { 1142 trace_process_ncq_command_prioicc(s, port, tag); 1143 } 1144 if (ncq_fis->fua & NCQ_FIS_FUA_MASK) { 1145 trace_process_ncq_command_fua(s, port, tag); 1146 } 1147 if (ncq_fis->tag & NCQ_FIS_RARC_MASK) { 1148 trace_process_ncq_command_rarc(s, port, tag); 1149 } 1150 1151 ncq_tfs->sector_count = ((ncq_fis->sector_count_high << 8) | 1152 ncq_fis->sector_count_low); 1153 if (!ncq_tfs->sector_count) { 1154 ncq_tfs->sector_count = 0x10000; 1155 } 1156 size = ncq_tfs->sector_count * 512; 1157 ahci_populate_sglist(ad, &ncq_tfs->sglist, ncq_tfs->cmdh, size, 0); 1158 1159 if (ncq_tfs->sglist.size < size) { 1160 error_report("ahci: PRDT length for NCQ command (0x%zx) " 1161 "is smaller than the requested size (0x%zx)", 1162 ncq_tfs->sglist.size, size); 1163 ncq_err(ncq_tfs); 1164 ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_OFS); 1165 return; 1166 } else if (ncq_tfs->sglist.size != size) { 1167 trace_process_ncq_command_large(s, port, tag, 1168 ncq_tfs->sglist.size, size); 1169 } 1170 1171 trace_process_ncq_command(s, port, tag, 1172 ncq_fis->command, 1173 ncq_tfs->lba, 1174 ncq_tfs->lba + ncq_tfs->sector_count - 1); 1175 execute_ncq_command(ncq_tfs); 1176 } 1177 1178 static AHCICmdHdr *get_cmd_header(AHCIState *s, uint8_t port, uint8_t slot) 1179 { 1180 if (port >= s->ports || slot >= AHCI_MAX_CMDS) { 1181 return NULL; 1182 } 1183 1184 return s->dev[port].lst ? &((AHCICmdHdr *)s->dev[port].lst)[slot] : NULL; 1185 } 1186 1187 static void handle_reg_h2d_fis(AHCIState *s, int port, 1188 uint8_t slot, uint8_t *cmd_fis) 1189 { 1190 IDEState *ide_state = &s->dev[port].port.ifs[0]; 1191 AHCICmdHdr *cmd = get_cmd_header(s, port, slot); 1192 uint16_t opts = le16_to_cpu(cmd->opts); 1193 1194 if (cmd_fis[1] & 0x0F) { 1195 trace_handle_reg_h2d_fis_pmp(s, port, cmd_fis[1], 1196 cmd_fis[2], cmd_fis[3]); 1197 return; 1198 } 1199 1200 if (cmd_fis[1] & 0x70) { 1201 trace_handle_reg_h2d_fis_res(s, port, cmd_fis[1], 1202 cmd_fis[2], cmd_fis[3]); 1203 return; 1204 } 1205 1206 if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) { 1207 switch (s->dev[port].port_state) { 1208 case STATE_RUN: 1209 if (cmd_fis[15] & ATA_SRST) { 1210 s->dev[port].port_state = STATE_RESET; 1211 } 1212 break; 1213 case STATE_RESET: 1214 if (!(cmd_fis[15] & ATA_SRST)) { 1215 ahci_reset_port(s, port); 1216 } 1217 break; 1218 } 1219 return; 1220 } 1221 1222 /* Check for NCQ command */ 1223 if (is_ncq(cmd_fis[2])) { 1224 process_ncq_command(s, port, cmd_fis, slot); 1225 return; 1226 } 1227 1228 /* Decompose the FIS: 1229 * AHCI does not interpret FIS packets, it only forwards them. 1230 * SATA 1.0 describes how to decode LBA28 and CHS FIS packets. 1231 * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets. 1232 * 1233 * ATA4 describes sector number for LBA28/CHS commands. 1234 * ATA6 describes sector number for LBA48 commands. 1235 * ATA8 deprecates CHS fully, describing only LBA28/48. 1236 * 1237 * We dutifully convert the FIS into IDE registers, and allow the 1238 * core layer to interpret them as needed. */ 1239 ide_state->feature = cmd_fis[3]; 1240 ide_state->sector = cmd_fis[4]; /* LBA 7:0 */ 1241 ide_state->lcyl = cmd_fis[5]; /* LBA 15:8 */ 1242 ide_state->hcyl = cmd_fis[6]; /* LBA 23:16 */ 1243 ide_state->select = cmd_fis[7]; /* LBA 27:24 (LBA28) */ 1244 ide_state->hob_sector = cmd_fis[8]; /* LBA 31:24 */ 1245 ide_state->hob_lcyl = cmd_fis[9]; /* LBA 39:32 */ 1246 ide_state->hob_hcyl = cmd_fis[10]; /* LBA 47:40 */ 1247 ide_state->hob_feature = cmd_fis[11]; 1248 ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]); 1249 /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */ 1250 /* 15: Only valid when UPDATE_COMMAND not set. */ 1251 1252 /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command 1253 * table to ide_state->io_buffer */ 1254 if (opts & AHCI_CMD_ATAPI) { 1255 memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10); 1256 if (trace_event_get_state_backends(TRACE_HANDLE_REG_H2D_FIS_DUMP)) { 1257 char *pretty_fis = ahci_pretty_buffer_fis(ide_state->io_buffer, 0x10); 1258 trace_handle_reg_h2d_fis_dump(s, port, pretty_fis); 1259 g_free(pretty_fis); 1260 } 1261 s->dev[port].done_atapi_packet = false; 1262 } 1263 1264 ide_state->error = 0; 1265 1266 /* Reset transferred byte counter */ 1267 cmd->status = 0; 1268 1269 /* We're ready to process the command in FIS byte 2. */ 1270 ide_exec_cmd(&s->dev[port].port, cmd_fis[2]); 1271 } 1272 1273 static int handle_cmd(AHCIState *s, int port, uint8_t slot) 1274 { 1275 IDEState *ide_state; 1276 uint64_t tbl_addr; 1277 AHCICmdHdr *cmd; 1278 uint8_t *cmd_fis; 1279 dma_addr_t cmd_len; 1280 1281 if (s->dev[port].port.ifs[0].status & (BUSY_STAT|DRQ_STAT)) { 1282 /* Engine currently busy, try again later */ 1283 trace_handle_cmd_busy(s, port); 1284 return -1; 1285 } 1286 1287 if (!s->dev[port].lst) { 1288 trace_handle_cmd_nolist(s, port); 1289 return -1; 1290 } 1291 cmd = get_cmd_header(s, port, slot); 1292 /* remember current slot handle for later */ 1293 s->dev[port].cur_cmd = cmd; 1294 1295 /* The device we are working for */ 1296 ide_state = &s->dev[port].port.ifs[0]; 1297 if (!ide_state->blk) { 1298 trace_handle_cmd_badport(s, port); 1299 return -1; 1300 } 1301 1302 tbl_addr = le64_to_cpu(cmd->tbl_addr); 1303 cmd_len = 0x80; 1304 cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len, 1305 DMA_DIRECTION_FROM_DEVICE); 1306 if (!cmd_fis) { 1307 trace_handle_cmd_badfis(s, port); 1308 return -1; 1309 } else if (cmd_len != 0x80) { 1310 ahci_trigger_irq(s, &s->dev[port], AHCI_PORT_IRQ_BIT_HBFS); 1311 trace_handle_cmd_badmap(s, port, cmd_len); 1312 goto out; 1313 } 1314 if (trace_event_get_state_backends(TRACE_HANDLE_CMD_FIS_DUMP)) { 1315 char *pretty_fis = ahci_pretty_buffer_fis(cmd_fis, 0x80); 1316 trace_handle_cmd_fis_dump(s, port, pretty_fis); 1317 g_free(pretty_fis); 1318 } 1319 switch (cmd_fis[0]) { 1320 case SATA_FIS_TYPE_REGISTER_H2D: 1321 handle_reg_h2d_fis(s, port, slot, cmd_fis); 1322 break; 1323 default: 1324 trace_handle_cmd_unhandled_fis(s, port, 1325 cmd_fis[0], cmd_fis[1], cmd_fis[2]); 1326 break; 1327 } 1328 1329 out: 1330 dma_memory_unmap(s->as, cmd_fis, cmd_len, DMA_DIRECTION_FROM_DEVICE, 1331 cmd_len); 1332 1333 if (s->dev[port].port.ifs[0].status & (BUSY_STAT|DRQ_STAT)) { 1334 /* async command, complete later */ 1335 s->dev[port].busy_slot = slot; 1336 return -1; 1337 } 1338 1339 /* done handling the command */ 1340 return 0; 1341 } 1342 1343 /* Transfer PIO data between RAM and device */ 1344 static void ahci_pio_transfer(IDEDMA *dma) 1345 { 1346 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1347 IDEState *s = &ad->port.ifs[0]; 1348 uint32_t size = (uint32_t)(s->data_end - s->data_ptr); 1349 /* write == ram -> device */ 1350 uint16_t opts = le16_to_cpu(ad->cur_cmd->opts); 1351 int is_write = opts & AHCI_CMD_WRITE; 1352 int is_atapi = opts & AHCI_CMD_ATAPI; 1353 int has_sglist = 0; 1354 1355 /* PIO FIS gets written prior to transfer */ 1356 ahci_write_fis_pio(ad, size); 1357 1358 if (is_atapi && !ad->done_atapi_packet) { 1359 /* already prepopulated iobuffer */ 1360 ad->done_atapi_packet = true; 1361 goto out; 1362 } 1363 1364 if (ahci_dma_prepare_buf(dma, size)) { 1365 has_sglist = 1; 1366 } 1367 1368 trace_ahci_pio_transfer(ad->hba, ad->port_no, is_write ? "writ" : "read", 1369 size, is_atapi ? "atapi" : "ata", 1370 has_sglist ? "" : "o"); 1371 1372 if (has_sglist && size) { 1373 if (is_write) { 1374 dma_buf_write(s->data_ptr, size, &s->sg); 1375 } else { 1376 dma_buf_read(s->data_ptr, size, &s->sg); 1377 } 1378 } 1379 1380 /* Update number of transferred bytes, destroy sglist */ 1381 dma_buf_commit(s, size); 1382 out: 1383 /* declare that we processed everything */ 1384 s->data_ptr = s->data_end; 1385 } 1386 1387 static void ahci_start_dma(IDEDMA *dma, IDEState *s, 1388 BlockCompletionFunc *dma_cb) 1389 { 1390 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1391 trace_ahci_start_dma(ad->hba, ad->port_no); 1392 s->io_buffer_offset = 0; 1393 dma_cb(s, 0); 1394 } 1395 1396 static void ahci_restart_dma(IDEDMA *dma) 1397 { 1398 /* Nothing to do, ahci_start_dma already resets s->io_buffer_offset. */ 1399 } 1400 1401 /** 1402 * IDE/PIO restarts are handled by the core layer, but NCQ commands 1403 * need an extra kick from the AHCI HBA. 1404 */ 1405 static void ahci_restart(IDEDMA *dma) 1406 { 1407 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1408 int i; 1409 1410 for (i = 0; i < AHCI_MAX_CMDS; i++) { 1411 NCQTransferState *ncq_tfs = &ad->ncq_tfs[i]; 1412 if (ncq_tfs->halt) { 1413 execute_ncq_command(ncq_tfs); 1414 } 1415 } 1416 } 1417 1418 /** 1419 * Called in DMA and PIO R/W chains to read the PRDT. 1420 * Not shared with NCQ pathways. 1421 */ 1422 static int32_t ahci_dma_prepare_buf(IDEDMA *dma, int32_t limit) 1423 { 1424 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1425 IDEState *s = &ad->port.ifs[0]; 1426 1427 if (ahci_populate_sglist(ad, &s->sg, ad->cur_cmd, 1428 limit, s->io_buffer_offset) == -1) { 1429 trace_ahci_dma_prepare_buf_fail(ad->hba, ad->port_no); 1430 return -1; 1431 } 1432 s->io_buffer_size = s->sg.size; 1433 1434 trace_ahci_dma_prepare_buf(ad->hba, ad->port_no, limit, s->io_buffer_size); 1435 return s->io_buffer_size; 1436 } 1437 1438 /** 1439 * Updates the command header with a bytes-read value. 1440 * Called via dma_buf_commit, for both DMA and PIO paths. 1441 * sglist destruction is handled within dma_buf_commit. 1442 */ 1443 static void ahci_commit_buf(IDEDMA *dma, uint32_t tx_bytes) 1444 { 1445 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1446 1447 tx_bytes += le32_to_cpu(ad->cur_cmd->status); 1448 ad->cur_cmd->status = cpu_to_le32(tx_bytes); 1449 } 1450 1451 static int ahci_dma_rw_buf(IDEDMA *dma, int is_write) 1452 { 1453 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1454 IDEState *s = &ad->port.ifs[0]; 1455 uint8_t *p = s->io_buffer + s->io_buffer_index; 1456 int l = s->io_buffer_size - s->io_buffer_index; 1457 1458 if (ahci_populate_sglist(ad, &s->sg, ad->cur_cmd, l, s->io_buffer_offset)) { 1459 return 0; 1460 } 1461 1462 if (is_write) { 1463 dma_buf_read(p, l, &s->sg); 1464 } else { 1465 dma_buf_write(p, l, &s->sg); 1466 } 1467 1468 /* free sglist, update byte count */ 1469 dma_buf_commit(s, l); 1470 s->io_buffer_index += l; 1471 1472 trace_ahci_dma_rw_buf(ad->hba, ad->port_no, l); 1473 return 1; 1474 } 1475 1476 static void ahci_cmd_done(IDEDMA *dma) 1477 { 1478 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma); 1479 1480 trace_ahci_cmd_done(ad->hba, ad->port_no); 1481 1482 /* no longer busy */ 1483 if (ad->busy_slot != -1) { 1484 ad->port_regs.cmd_issue &= ~(1 << ad->busy_slot); 1485 ad->busy_slot = -1; 1486 } 1487 1488 /* update d2h status */ 1489 ahci_write_fis_d2h(ad); 1490 1491 if (ad->port_regs.cmd_issue && !ad->check_bh) { 1492 ad->check_bh = qemu_bh_new(ahci_check_cmd_bh, ad); 1493 qemu_bh_schedule(ad->check_bh); 1494 } 1495 } 1496 1497 static void ahci_irq_set(void *opaque, int n, int level) 1498 { 1499 } 1500 1501 static const IDEDMAOps ahci_dma_ops = { 1502 .start_dma = ahci_start_dma, 1503 .restart = ahci_restart, 1504 .restart_dma = ahci_restart_dma, 1505 .pio_transfer = ahci_pio_transfer, 1506 .prepare_buf = ahci_dma_prepare_buf, 1507 .commit_buf = ahci_commit_buf, 1508 .rw_buf = ahci_dma_rw_buf, 1509 .cmd_done = ahci_cmd_done, 1510 }; 1511 1512 void ahci_init(AHCIState *s, DeviceState *qdev) 1513 { 1514 s->container = qdev; 1515 /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */ 1516 memory_region_init_io(&s->mem, OBJECT(qdev), &ahci_mem_ops, s, 1517 "ahci", AHCI_MEM_BAR_SIZE); 1518 memory_region_init_io(&s->idp, OBJECT(qdev), &ahci_idp_ops, s, 1519 "ahci-idp", 32); 1520 } 1521 1522 void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports) 1523 { 1524 qemu_irq *irqs; 1525 int i; 1526 1527 s->as = as; 1528 s->ports = ports; 1529 s->dev = g_new0(AHCIDevice, ports); 1530 ahci_reg_init(s); 1531 irqs = qemu_allocate_irqs(ahci_irq_set, s, s->ports); 1532 for (i = 0; i < s->ports; i++) { 1533 AHCIDevice *ad = &s->dev[i]; 1534 1535 ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1); 1536 ide_init2(&ad->port, irqs[i]); 1537 1538 ad->hba = s; 1539 ad->port_no = i; 1540 ad->port.dma = &ad->dma; 1541 ad->port.dma->ops = &ahci_dma_ops; 1542 ide_register_restart_cb(&ad->port); 1543 } 1544 g_free(irqs); 1545 } 1546 1547 void ahci_uninit(AHCIState *s) 1548 { 1549 int i, j; 1550 1551 for (i = 0; i < s->ports; i++) { 1552 AHCIDevice *ad = &s->dev[i]; 1553 1554 for (j = 0; j < 2; j++) { 1555 IDEState *s = &ad->port.ifs[j]; 1556 1557 ide_exit(s); 1558 } 1559 object_unparent(OBJECT(&ad->port)); 1560 } 1561 1562 g_free(s->dev); 1563 } 1564 1565 void ahci_reset(AHCIState *s) 1566 { 1567 AHCIPortRegs *pr; 1568 int i; 1569 1570 trace_ahci_reset(s); 1571 1572 s->control_regs.irqstatus = 0; 1573 /* AHCI Enable (AE) 1574 * The implementation of this bit is dependent upon the value of the 1575 * CAP.SAM bit. If CAP.SAM is '0', then GHC.AE shall be read-write and 1576 * shall have a reset value of '0'. If CAP.SAM is '1', then AE shall be 1577 * read-only and shall have a reset value of '1'. 1578 * 1579 * We set HOST_CAP_AHCI so we must enable AHCI at reset. 1580 */ 1581 s->control_regs.ghc = HOST_CTL_AHCI_EN; 1582 1583 for (i = 0; i < s->ports; i++) { 1584 pr = &s->dev[i].port_regs; 1585 pr->irq_stat = 0; 1586 pr->irq_mask = 0; 1587 pr->scr_ctl = 0; 1588 pr->cmd = PORT_CMD_SPIN_UP | PORT_CMD_POWER_ON; 1589 ahci_reset_port(s, i); 1590 } 1591 } 1592 1593 static const VMStateDescription vmstate_ncq_tfs = { 1594 .name = "ncq state", 1595 .version_id = 1, 1596 .fields = (VMStateField[]) { 1597 VMSTATE_UINT32(sector_count, NCQTransferState), 1598 VMSTATE_UINT64(lba, NCQTransferState), 1599 VMSTATE_UINT8(tag, NCQTransferState), 1600 VMSTATE_UINT8(cmd, NCQTransferState), 1601 VMSTATE_UINT8(slot, NCQTransferState), 1602 VMSTATE_BOOL(used, NCQTransferState), 1603 VMSTATE_BOOL(halt, NCQTransferState), 1604 VMSTATE_END_OF_LIST() 1605 }, 1606 }; 1607 1608 static const VMStateDescription vmstate_ahci_device = { 1609 .name = "ahci port", 1610 .version_id = 1, 1611 .fields = (VMStateField[]) { 1612 VMSTATE_IDE_BUS(port, AHCIDevice), 1613 VMSTATE_IDE_DRIVE(port.ifs[0], AHCIDevice), 1614 VMSTATE_UINT32(port_state, AHCIDevice), 1615 VMSTATE_UINT32(finished, AHCIDevice), 1616 VMSTATE_UINT32(port_regs.lst_addr, AHCIDevice), 1617 VMSTATE_UINT32(port_regs.lst_addr_hi, AHCIDevice), 1618 VMSTATE_UINT32(port_regs.fis_addr, AHCIDevice), 1619 VMSTATE_UINT32(port_regs.fis_addr_hi, AHCIDevice), 1620 VMSTATE_UINT32(port_regs.irq_stat, AHCIDevice), 1621 VMSTATE_UINT32(port_regs.irq_mask, AHCIDevice), 1622 VMSTATE_UINT32(port_regs.cmd, AHCIDevice), 1623 VMSTATE_UINT32(port_regs.tfdata, AHCIDevice), 1624 VMSTATE_UINT32(port_regs.sig, AHCIDevice), 1625 VMSTATE_UINT32(port_regs.scr_stat, AHCIDevice), 1626 VMSTATE_UINT32(port_regs.scr_ctl, AHCIDevice), 1627 VMSTATE_UINT32(port_regs.scr_err, AHCIDevice), 1628 VMSTATE_UINT32(port_regs.scr_act, AHCIDevice), 1629 VMSTATE_UINT32(port_regs.cmd_issue, AHCIDevice), 1630 VMSTATE_BOOL(done_atapi_packet, AHCIDevice), 1631 VMSTATE_INT32(busy_slot, AHCIDevice), 1632 VMSTATE_BOOL(init_d2h_sent, AHCIDevice), 1633 VMSTATE_STRUCT_ARRAY(ncq_tfs, AHCIDevice, AHCI_MAX_CMDS, 1634 1, vmstate_ncq_tfs, NCQTransferState), 1635 VMSTATE_END_OF_LIST() 1636 }, 1637 }; 1638 1639 static int ahci_state_post_load(void *opaque, int version_id) 1640 { 1641 int i, j; 1642 struct AHCIDevice *ad; 1643 NCQTransferState *ncq_tfs; 1644 AHCIPortRegs *pr; 1645 AHCIState *s = opaque; 1646 1647 for (i = 0; i < s->ports; i++) { 1648 ad = &s->dev[i]; 1649 pr = &ad->port_regs; 1650 1651 if (!(pr->cmd & PORT_CMD_START) && (pr->cmd & PORT_CMD_LIST_ON)) { 1652 error_report("AHCI: DMA engine should be off, but status bit " 1653 "indicates it is still running."); 1654 return -1; 1655 } 1656 if (!(pr->cmd & PORT_CMD_FIS_RX) && (pr->cmd & PORT_CMD_FIS_ON)) { 1657 error_report("AHCI: FIS RX engine should be off, but status bit " 1658 "indicates it is still running."); 1659 return -1; 1660 } 1661 1662 /* After a migrate, the DMA/FIS engines are "off" and 1663 * need to be conditionally restarted */ 1664 pr->cmd &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON); 1665 if (ahci_cond_start_engines(ad) != 0) { 1666 return -1; 1667 } 1668 1669 for (j = 0; j < AHCI_MAX_CMDS; j++) { 1670 ncq_tfs = &ad->ncq_tfs[j]; 1671 ncq_tfs->drive = ad; 1672 1673 if (ncq_tfs->used != ncq_tfs->halt) { 1674 return -1; 1675 } 1676 if (!ncq_tfs->halt) { 1677 continue; 1678 } 1679 if (!is_ncq(ncq_tfs->cmd)) { 1680 return -1; 1681 } 1682 if (ncq_tfs->slot != ncq_tfs->tag) { 1683 return -1; 1684 } 1685 /* If ncq_tfs->halt is justly set, the engine should be engaged, 1686 * and the command list buffer should be mapped. */ 1687 ncq_tfs->cmdh = get_cmd_header(s, i, ncq_tfs->slot); 1688 if (!ncq_tfs->cmdh) { 1689 return -1; 1690 } 1691 ahci_populate_sglist(ncq_tfs->drive, &ncq_tfs->sglist, 1692 ncq_tfs->cmdh, ncq_tfs->sector_count * 512, 1693 0); 1694 if (ncq_tfs->sector_count != ncq_tfs->sglist.size >> 9) { 1695 return -1; 1696 } 1697 } 1698 1699 1700 /* 1701 * If an error is present, ad->busy_slot will be valid and not -1. 1702 * In this case, an operation is waiting to resume and will re-check 1703 * for additional AHCI commands to execute upon completion. 1704 * 1705 * In the case where no error was present, busy_slot will be -1, 1706 * and we should check to see if there are additional commands waiting. 1707 */ 1708 if (ad->busy_slot == -1) { 1709 check_cmd(s, i); 1710 } else { 1711 /* We are in the middle of a command, and may need to access 1712 * the command header in guest memory again. */ 1713 if (ad->busy_slot < 0 || ad->busy_slot >= AHCI_MAX_CMDS) { 1714 return -1; 1715 } 1716 ad->cur_cmd = get_cmd_header(s, i, ad->busy_slot); 1717 } 1718 } 1719 1720 return 0; 1721 } 1722 1723 const VMStateDescription vmstate_ahci = { 1724 .name = "ahci", 1725 .version_id = 1, 1726 .post_load = ahci_state_post_load, 1727 .fields = (VMStateField[]) { 1728 VMSTATE_STRUCT_VARRAY_POINTER_INT32(dev, AHCIState, ports, 1729 vmstate_ahci_device, AHCIDevice), 1730 VMSTATE_UINT32(control_regs.cap, AHCIState), 1731 VMSTATE_UINT32(control_regs.ghc, AHCIState), 1732 VMSTATE_UINT32(control_regs.irqstatus, AHCIState), 1733 VMSTATE_UINT32(control_regs.impl, AHCIState), 1734 VMSTATE_UINT32(control_regs.version, AHCIState), 1735 VMSTATE_UINT32(idp_index, AHCIState), 1736 VMSTATE_INT32_EQUAL(ports, AHCIState, NULL), 1737 VMSTATE_END_OF_LIST() 1738 }, 1739 }; 1740 1741 static const VMStateDescription vmstate_sysbus_ahci = { 1742 .name = "sysbus-ahci", 1743 .fields = (VMStateField[]) { 1744 VMSTATE_AHCI(ahci, SysbusAHCIState), 1745 VMSTATE_END_OF_LIST() 1746 }, 1747 }; 1748 1749 static void sysbus_ahci_reset(DeviceState *dev) 1750 { 1751 SysbusAHCIState *s = SYSBUS_AHCI(dev); 1752 1753 ahci_reset(&s->ahci); 1754 } 1755 1756 static void sysbus_ahci_init(Object *obj) 1757 { 1758 SysbusAHCIState *s = SYSBUS_AHCI(obj); 1759 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 1760 1761 ahci_init(&s->ahci, DEVICE(obj)); 1762 1763 sysbus_init_mmio(sbd, &s->ahci.mem); 1764 sysbus_init_irq(sbd, &s->ahci.irq); 1765 } 1766 1767 static void sysbus_ahci_realize(DeviceState *dev, Error **errp) 1768 { 1769 SysbusAHCIState *s = SYSBUS_AHCI(dev); 1770 1771 ahci_realize(&s->ahci, dev, &address_space_memory, s->num_ports); 1772 } 1773 1774 static Property sysbus_ahci_properties[] = { 1775 DEFINE_PROP_UINT32("num-ports", SysbusAHCIState, num_ports, 1), 1776 DEFINE_PROP_END_OF_LIST(), 1777 }; 1778 1779 static void sysbus_ahci_class_init(ObjectClass *klass, void *data) 1780 { 1781 DeviceClass *dc = DEVICE_CLASS(klass); 1782 1783 dc->realize = sysbus_ahci_realize; 1784 dc->vmsd = &vmstate_sysbus_ahci; 1785 dc->props = sysbus_ahci_properties; 1786 dc->reset = sysbus_ahci_reset; 1787 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1788 } 1789 1790 static const TypeInfo sysbus_ahci_info = { 1791 .name = TYPE_SYSBUS_AHCI, 1792 .parent = TYPE_SYS_BUS_DEVICE, 1793 .instance_size = sizeof(SysbusAHCIState), 1794 .instance_init = sysbus_ahci_init, 1795 .class_init = sysbus_ahci_class_init, 1796 }; 1797 1798 static void sysbus_ahci_register_types(void) 1799 { 1800 type_register_static(&sysbus_ahci_info); 1801 } 1802 1803 type_init(sysbus_ahci_register_types) 1804 1805 int32_t ahci_get_num_ports(PCIDevice *dev) 1806 { 1807 AHCIPCIState *d = ICH_AHCI(dev); 1808 AHCIState *ahci = &d->ahci; 1809 1810 return ahci->ports; 1811 } 1812 1813 void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd) 1814 { 1815 AHCIPCIState *d = ICH_AHCI(dev); 1816 AHCIState *ahci = &d->ahci; 1817 int i; 1818 1819 for (i = 0; i < ahci->ports; i++) { 1820 if (hd[i] == NULL) { 1821 continue; 1822 } 1823 ide_create_drive(&ahci->dev[i].port, 0, hd[i]); 1824 } 1825 1826 } 1827