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>
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.
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.
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/>.
25 #include <hw/pci/msi.h>
26 #include <hw/i386/pc.h>
27 #include <hw/pci/pci.h>
28 #include <hw/sysbus.h>
30 #include "monitor/monitor.h"
31 #include "sysemu/block-backend.h"
32 #include "sysemu/dma.h"
34 #include <hw/ide/pci.h>
35 #include <hw/ide/ahci.h>
39 #define DPRINTF(port, fmt, ...) \
42 fprintf(stderr, "ahci: %s: [%d] ", __func__, port); \
43 fprintf(stderr, fmt, ## __VA_ARGS__); \
47 static void check_cmd(AHCIState
*s
, int port
);
48 static int handle_cmd(AHCIState
*s
,int port
,int slot
);
49 static void ahci_reset_port(AHCIState
*s
, int port
);
50 static void ahci_write_fis_d2h(AHCIDevice
*ad
, uint8_t *cmd_fis
);
51 static void ahci_init_d2h(AHCIDevice
*ad
);
52 static int ahci_dma_prepare_buf(IDEDMA
*dma
, int is_write
);
53 static void ahci_commit_buf(IDEDMA
*dma
, uint32_t tx_bytes
);
54 static bool ahci_map_clb_address(AHCIDevice
*ad
);
55 static bool ahci_map_fis_address(AHCIDevice
*ad
);
56 static void ahci_unmap_clb_address(AHCIDevice
*ad
);
57 static void ahci_unmap_fis_address(AHCIDevice
*ad
);
60 static uint32_t ahci_port_read(AHCIState
*s
, int port
, int offset
)
64 pr
= &s
->dev
[port
].port_regs
;
70 case PORT_LST_ADDR_HI
:
71 val
= pr
->lst_addr_hi
;
76 case PORT_FIS_ADDR_HI
:
77 val
= pr
->fis_addr_hi
;
95 if (s
->dev
[port
].port
.ifs
[0].blk
) {
96 val
= SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP
|
97 SATA_SCR_SSTATUS_SPD_GEN1
| SATA_SCR_SSTATUS_IPM_ACTIVE
;
99 val
= SATA_SCR_SSTATUS_DET_NODEV
;
109 pr
->scr_act
&= ~s
->dev
[port
].finished
;
110 s
->dev
[port
].finished
= 0;
120 DPRINTF(port
, "offset: 0x%x val: 0x%x\n", offset
, val
);
125 static void ahci_irq_raise(AHCIState
*s
, AHCIDevice
*dev
)
127 AHCIPCIState
*d
= container_of(s
, AHCIPCIState
, ahci
);
129 (PCIDevice
*)object_dynamic_cast(OBJECT(d
), TYPE_PCI_DEVICE
);
131 DPRINTF(0, "raise irq\n");
133 if (pci_dev
&& msi_enabled(pci_dev
)) {
134 msi_notify(pci_dev
, 0);
136 qemu_irq_raise(s
->irq
);
140 static void ahci_irq_lower(AHCIState
*s
, AHCIDevice
*dev
)
142 AHCIPCIState
*d
= container_of(s
, AHCIPCIState
, ahci
);
144 (PCIDevice
*)object_dynamic_cast(OBJECT(d
), TYPE_PCI_DEVICE
);
146 DPRINTF(0, "lower irq\n");
148 if (!pci_dev
|| !msi_enabled(pci_dev
)) {
149 qemu_irq_lower(s
->irq
);
153 static void ahci_check_irq(AHCIState
*s
)
157 DPRINTF(-1, "check irq %#x\n", s
->control_regs
.irqstatus
);
159 s
->control_regs
.irqstatus
= 0;
160 for (i
= 0; i
< s
->ports
; i
++) {
161 AHCIPortRegs
*pr
= &s
->dev
[i
].port_regs
;
162 if (pr
->irq_stat
& pr
->irq_mask
) {
163 s
->control_regs
.irqstatus
|= (1 << i
);
167 if (s
->control_regs
.irqstatus
&&
168 (s
->control_regs
.ghc
& HOST_CTL_IRQ_EN
)) {
169 ahci_irq_raise(s
, NULL
);
171 ahci_irq_lower(s
, NULL
);
175 static void ahci_trigger_irq(AHCIState
*s
, AHCIDevice
*d
,
178 DPRINTF(d
->port_no
, "trigger irq %#x -> %x\n",
179 irq_type
, d
->port_regs
.irq_mask
& irq_type
);
181 d
->port_regs
.irq_stat
|= irq_type
;
185 static void map_page(AddressSpace
*as
, uint8_t **ptr
, uint64_t addr
,
191 dma_memory_unmap(as
, *ptr
, len
, DMA_DIRECTION_FROM_DEVICE
, len
);
194 *ptr
= dma_memory_map(as
, addr
, &len
, DMA_DIRECTION_FROM_DEVICE
);
196 dma_memory_unmap(as
, *ptr
, len
, DMA_DIRECTION_FROM_DEVICE
, len
);
202 * Check the cmd register to see if we should start or stop
203 * the DMA or FIS RX engines.
205 * @ad: Device to engage.
206 * @allow_stop: Allow device to transition from started to stopped?
207 * 'no' is useful for migration post_load, which does not expect a transition.
209 * @return 0 on success, -1 on error.
211 static int ahci_cond_start_engines(AHCIDevice
*ad
, bool allow_stop
)
213 AHCIPortRegs
*pr
= &ad
->port_regs
;
215 if (pr
->cmd
& PORT_CMD_START
) {
216 if (ahci_map_clb_address(ad
)) {
217 pr
->cmd
|= PORT_CMD_LIST_ON
;
219 error_report("AHCI: Failed to start DMA engine: "
220 "bad command list buffer address");
223 } else if (pr
->cmd
& PORT_CMD_LIST_ON
) {
225 ahci_unmap_clb_address(ad
);
226 pr
->cmd
= pr
->cmd
& ~(PORT_CMD_LIST_ON
);
228 error_report("AHCI: DMA engine should be off, "
229 "but appears to still be running");
234 if (pr
->cmd
& PORT_CMD_FIS_RX
) {
235 if (ahci_map_fis_address(ad
)) {
236 pr
->cmd
|= PORT_CMD_FIS_ON
;
238 error_report("AHCI: Failed to start FIS receive engine: "
239 "bad FIS receive buffer address");
242 } else if (pr
->cmd
& PORT_CMD_FIS_ON
) {
244 ahci_unmap_fis_address(ad
);
245 pr
->cmd
= pr
->cmd
& ~(PORT_CMD_FIS_ON
);
247 error_report("AHCI: FIS receive engine should be off, "
248 "but appears to still be running");
256 static void ahci_port_write(AHCIState
*s
, int port
, int offset
, uint32_t val
)
258 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
260 DPRINTF(port
, "offset: 0x%x val: 0x%x\n", offset
, val
);
265 case PORT_LST_ADDR_HI
:
266 pr
->lst_addr_hi
= val
;
271 case PORT_FIS_ADDR_HI
:
272 pr
->fis_addr_hi
= val
;
275 pr
->irq_stat
&= ~val
;
279 pr
->irq_mask
= val
& 0xfdc000ff;
283 /* Block any Read-only fields from being set;
284 * including LIST_ON and FIS_ON. */
285 pr
->cmd
= (pr
->cmd
& PORT_CMD_RO_MASK
) | (val
& ~PORT_CMD_RO_MASK
);
287 /* Check FIS RX and CLB engines, allow transition to false: */
288 ahci_cond_start_engines(&s
->dev
[port
], true);
290 /* XXX usually the FIS would be pending on the bus here and
291 issuing deferred until the OS enables FIS receival.
292 Instead, we only submit it once - which works in most
293 cases, but is a hack. */
294 if ((pr
->cmd
& PORT_CMD_FIS_ON
) &&
295 !s
->dev
[port
].init_d2h_sent
) {
296 ahci_init_d2h(&s
->dev
[port
]);
297 s
->dev
[port
].init_d2h_sent
= true;
312 if (((pr
->scr_ctl
& AHCI_SCR_SCTL_DET
) == 1) &&
313 ((val
& AHCI_SCR_SCTL_DET
) == 0)) {
314 ahci_reset_port(s
, port
);
326 pr
->cmd_issue
|= val
;
334 static uint64_t ahci_mem_read(void *opaque
, hwaddr addr
,
337 AHCIState
*s
= opaque
;
340 if (addr
< AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR
) {
343 val
= s
->control_regs
.cap
;
346 val
= s
->control_regs
.ghc
;
349 val
= s
->control_regs
.irqstatus
;
351 case HOST_PORTS_IMPL
:
352 val
= s
->control_regs
.impl
;
355 val
= s
->control_regs
.version
;
359 DPRINTF(-1, "(addr 0x%08X), val 0x%08X\n", (unsigned) addr
, val
);
360 } else if ((addr
>= AHCI_PORT_REGS_START_ADDR
) &&
361 (addr
< (AHCI_PORT_REGS_START_ADDR
+
362 (s
->ports
* AHCI_PORT_ADDR_OFFSET_LEN
)))) {
363 val
= ahci_port_read(s
, (addr
- AHCI_PORT_REGS_START_ADDR
) >> 7,
364 addr
& AHCI_PORT_ADDR_OFFSET_MASK
);
372 static void ahci_mem_write(void *opaque
, hwaddr addr
,
373 uint64_t val
, unsigned size
)
375 AHCIState
*s
= opaque
;
377 /* Only aligned reads are allowed on AHCI */
379 fprintf(stderr
, "ahci: Mis-aligned write to addr 0x"
380 TARGET_FMT_plx
"\n", addr
);
384 if (addr
< AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR
) {
385 DPRINTF(-1, "(addr 0x%08X), val 0x%08"PRIX64
"\n", (unsigned) addr
, val
);
388 case HOST_CAP
: /* R/WO, RO */
389 /* FIXME handle R/WO */
391 case HOST_CTL
: /* R/W */
392 if (val
& HOST_CTL_RESET
) {
393 DPRINTF(-1, "HBA Reset\n");
396 s
->control_regs
.ghc
= (val
& 0x3) | HOST_CTL_AHCI_EN
;
400 case HOST_IRQ_STAT
: /* R/WC, RO */
401 s
->control_regs
.irqstatus
&= ~val
;
404 case HOST_PORTS_IMPL
: /* R/WO, RO */
405 /* FIXME handle R/WO */
407 case HOST_VERSION
: /* RO */
408 /* FIXME report write? */
411 DPRINTF(-1, "write to unknown register 0x%x\n", (unsigned)addr
);
413 } else if ((addr
>= AHCI_PORT_REGS_START_ADDR
) &&
414 (addr
< (AHCI_PORT_REGS_START_ADDR
+
415 (s
->ports
* AHCI_PORT_ADDR_OFFSET_LEN
)))) {
416 ahci_port_write(s
, (addr
- AHCI_PORT_REGS_START_ADDR
) >> 7,
417 addr
& AHCI_PORT_ADDR_OFFSET_MASK
, val
);
422 static const MemoryRegionOps ahci_mem_ops
= {
423 .read
= ahci_mem_read
,
424 .write
= ahci_mem_write
,
425 .endianness
= DEVICE_LITTLE_ENDIAN
,
428 static uint64_t ahci_idp_read(void *opaque
, hwaddr addr
,
431 AHCIState
*s
= opaque
;
433 if (addr
== s
->idp_offset
) {
436 } else if (addr
== s
->idp_offset
+ 4) {
437 /* data register - do memory read at location selected by index */
438 return ahci_mem_read(opaque
, s
->idp_index
, size
);
444 static void ahci_idp_write(void *opaque
, hwaddr addr
,
445 uint64_t val
, unsigned size
)
447 AHCIState
*s
= opaque
;
449 if (addr
== s
->idp_offset
) {
450 /* index register - mask off reserved bits */
451 s
->idp_index
= (uint32_t)val
& ((AHCI_MEM_BAR_SIZE
- 1) & ~3);
452 } else if (addr
== s
->idp_offset
+ 4) {
453 /* data register - do memory write at location selected by index */
454 ahci_mem_write(opaque
, s
->idp_index
, val
, size
);
458 static const MemoryRegionOps ahci_idp_ops
= {
459 .read
= ahci_idp_read
,
460 .write
= ahci_idp_write
,
461 .endianness
= DEVICE_LITTLE_ENDIAN
,
465 static void ahci_reg_init(AHCIState
*s
)
469 s
->control_regs
.cap
= (s
->ports
- 1) |
470 (AHCI_NUM_COMMAND_SLOTS
<< 8) |
471 (AHCI_SUPPORTED_SPEED_GEN1
<< AHCI_SUPPORTED_SPEED
) |
472 HOST_CAP_NCQ
| HOST_CAP_AHCI
;
474 s
->control_regs
.impl
= (1 << s
->ports
) - 1;
476 s
->control_regs
.version
= AHCI_VERSION_1_0
;
478 for (i
= 0; i
< s
->ports
; i
++) {
479 s
->dev
[i
].port_state
= STATE_RUN
;
483 static void check_cmd(AHCIState
*s
, int port
)
485 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
488 if ((pr
->cmd
& PORT_CMD_START
) && pr
->cmd_issue
) {
489 for (slot
= 0; (slot
< 32) && pr
->cmd_issue
; slot
++) {
490 if ((pr
->cmd_issue
& (1U << slot
)) &&
491 !handle_cmd(s
, port
, slot
)) {
492 pr
->cmd_issue
&= ~(1U << slot
);
498 static void ahci_check_cmd_bh(void *opaque
)
500 AHCIDevice
*ad
= opaque
;
502 qemu_bh_delete(ad
->check_bh
);
505 if ((ad
->busy_slot
!= -1) &&
506 !(ad
->port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
))) {
508 ad
->port_regs
.cmd_issue
&= ~(1 << ad
->busy_slot
);
512 check_cmd(ad
->hba
, ad
->port_no
);
515 static void ahci_init_d2h(AHCIDevice
*ad
)
517 uint8_t init_fis
[20];
518 IDEState
*ide_state
= &ad
->port
.ifs
[0];
520 memset(init_fis
, 0, sizeof(init_fis
));
525 if (ide_state
->drive_kind
== IDE_CD
) {
526 init_fis
[5] = ide_state
->lcyl
;
527 init_fis
[6] = ide_state
->hcyl
;
530 ahci_write_fis_d2h(ad
, init_fis
);
533 static void ahci_reset_port(AHCIState
*s
, int port
)
535 AHCIDevice
*d
= &s
->dev
[port
];
536 AHCIPortRegs
*pr
= &d
->port_regs
;
537 IDEState
*ide_state
= &d
->port
.ifs
[0];
540 DPRINTF(port
, "reset port\n");
542 ide_bus_reset(&d
->port
);
543 ide_state
->ncq_queues
= AHCI_MAX_CMDS
;
549 pr
->sig
= 0xFFFFFFFF;
551 d
->init_d2h_sent
= false;
553 ide_state
= &s
->dev
[port
].port
.ifs
[0];
554 if (!ide_state
->blk
) {
558 /* reset ncq queue */
559 for (i
= 0; i
< AHCI_MAX_CMDS
; i
++) {
560 NCQTransferState
*ncq_tfs
= &s
->dev
[port
].ncq_tfs
[i
];
561 if (!ncq_tfs
->used
) {
565 if (ncq_tfs
->aiocb
) {
566 blk_aio_cancel(ncq_tfs
->aiocb
);
567 ncq_tfs
->aiocb
= NULL
;
570 /* Maybe we just finished the request thanks to blk_aio_cancel() */
571 if (!ncq_tfs
->used
) {
575 qemu_sglist_destroy(&ncq_tfs
->sglist
);
579 s
->dev
[port
].port_state
= STATE_RUN
;
580 if (!ide_state
->blk
) {
582 ide_state
->status
= SEEK_STAT
| WRERR_STAT
;
583 } else if (ide_state
->drive_kind
== IDE_CD
) {
584 pr
->sig
= SATA_SIGNATURE_CDROM
;
585 ide_state
->lcyl
= 0x14;
586 ide_state
->hcyl
= 0xeb;
587 DPRINTF(port
, "set lcyl = %d\n", ide_state
->lcyl
);
588 ide_state
->status
= SEEK_STAT
| WRERR_STAT
| READY_STAT
;
590 pr
->sig
= SATA_SIGNATURE_DISK
;
591 ide_state
->status
= SEEK_STAT
| WRERR_STAT
;
594 ide_state
->error
= 1;
598 static void debug_print_fis(uint8_t *fis
, int cmd_len
)
603 fprintf(stderr
, "fis:");
604 for (i
= 0; i
< cmd_len
; i
++) {
605 if ((i
& 0xf) == 0) {
606 fprintf(stderr
, "\n%02x:",i
);
608 fprintf(stderr
, "%02x ",fis
[i
]);
610 fprintf(stderr
, "\n");
614 static bool ahci_map_fis_address(AHCIDevice
*ad
)
616 AHCIPortRegs
*pr
= &ad
->port_regs
;
617 map_page(ad
->hba
->as
, &ad
->res_fis
,
618 ((uint64_t)pr
->fis_addr_hi
<< 32) | pr
->fis_addr
, 256);
619 return ad
->res_fis
!= NULL
;
622 static void ahci_unmap_fis_address(AHCIDevice
*ad
)
624 dma_memory_unmap(ad
->hba
->as
, ad
->res_fis
, 256,
625 DMA_DIRECTION_FROM_DEVICE
, 256);
629 static bool ahci_map_clb_address(AHCIDevice
*ad
)
631 AHCIPortRegs
*pr
= &ad
->port_regs
;
633 map_page(ad
->hba
->as
, &ad
->lst
,
634 ((uint64_t)pr
->lst_addr_hi
<< 32) | pr
->lst_addr
, 1024);
635 return ad
->lst
!= NULL
;
638 static void ahci_unmap_clb_address(AHCIDevice
*ad
)
640 dma_memory_unmap(ad
->hba
->as
, ad
->lst
, 1024,
641 DMA_DIRECTION_FROM_DEVICE
, 1024);
645 static void ahci_write_fis_sdb(AHCIState
*s
, int port
, uint32_t finished
)
647 AHCIDevice
*ad
= &s
->dev
[port
];
648 AHCIPortRegs
*pr
= &ad
->port_regs
;
652 if (!s
->dev
[port
].res_fis
||
653 !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
657 sdb_fis
= (SDBFIS
*)&ad
->res_fis
[RES_FIS_SDBFIS
];
658 ide_state
= &ad
->port
.ifs
[0];
660 sdb_fis
->type
= SATA_FIS_TYPE_SDB
;
661 /* Interrupt pending & Notification bit */
662 sdb_fis
->flags
= (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
663 sdb_fis
->status
= ide_state
->status
& 0x77;
664 sdb_fis
->error
= ide_state
->error
;
665 /* update SAct field in SDB_FIS */
666 s
->dev
[port
].finished
|= finished
;
667 sdb_fis
->payload
= cpu_to_le32(ad
->finished
);
669 /* Update shadow registers (except BSY 0x80 and DRQ 0x08) */
670 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
671 (ad
->port
.ifs
[0].status
& 0x77) |
674 ahci_trigger_irq(s
, ad
, PORT_IRQ_SDB_FIS
);
677 static void ahci_write_fis_pio(AHCIDevice
*ad
, uint16_t len
)
679 AHCIPortRegs
*pr
= &ad
->port_regs
;
680 uint8_t *pio_fis
, *cmd_fis
;
682 dma_addr_t cmd_len
= 0x80;
683 IDEState
*s
= &ad
->port
.ifs
[0];
685 if (!ad
->res_fis
|| !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
690 tbl_addr
= le64_to_cpu(ad
->cur_cmd
->tbl_addr
);
691 cmd_fis
= dma_memory_map(ad
->hba
->as
, tbl_addr
, &cmd_len
,
692 DMA_DIRECTION_TO_DEVICE
);
694 if (cmd_fis
== NULL
) {
695 DPRINTF(ad
->port_no
, "dma_memory_map failed in ahci_write_fis_pio");
696 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_HBUS_ERR
);
700 if (cmd_len
!= 0x80) {
702 "dma_memory_map mapped too few bytes in ahci_write_fis_pio");
703 dma_memory_unmap(ad
->hba
->as
, cmd_fis
, cmd_len
,
704 DMA_DIRECTION_TO_DEVICE
, cmd_len
);
705 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_HBUS_ERR
);
709 pio_fis
= &ad
->res_fis
[RES_FIS_PSFIS
];
711 pio_fis
[0] = SATA_FIS_TYPE_PIO_SETUP
;
712 pio_fis
[1] = (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
713 pio_fis
[2] = s
->status
;
714 pio_fis
[3] = s
->error
;
716 pio_fis
[4] = s
->sector
;
717 pio_fis
[5] = s
->lcyl
;
718 pio_fis
[6] = s
->hcyl
;
719 pio_fis
[7] = s
->select
;
720 pio_fis
[8] = s
->hob_sector
;
721 pio_fis
[9] = s
->hob_lcyl
;
722 pio_fis
[10] = s
->hob_hcyl
;
724 pio_fis
[12] = cmd_fis
[12];
725 pio_fis
[13] = cmd_fis
[13];
727 pio_fis
[15] = s
->status
;
728 pio_fis
[16] = len
& 255;
729 pio_fis
[17] = len
>> 8;
733 /* Update shadow registers: */
734 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
735 ad
->port
.ifs
[0].status
;
737 if (pio_fis
[2] & ERR_STAT
) {
738 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_TF_ERR
);
741 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_PIOS_FIS
);
743 dma_memory_unmap(ad
->hba
->as
, cmd_fis
, cmd_len
,
744 DMA_DIRECTION_TO_DEVICE
, cmd_len
);
747 static void ahci_write_fis_d2h(AHCIDevice
*ad
, uint8_t *cmd_fis
)
749 AHCIPortRegs
*pr
= &ad
->port_regs
;
752 dma_addr_t cmd_len
= 0x80;
754 IDEState
*s
= &ad
->port
.ifs
[0];
756 if (!ad
->res_fis
|| !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
762 uint64_t tbl_addr
= le64_to_cpu(ad
->cur_cmd
->tbl_addr
);
763 cmd_fis
= dma_memory_map(ad
->hba
->as
, tbl_addr
, &cmd_len
,
764 DMA_DIRECTION_TO_DEVICE
);
768 d2h_fis
= &ad
->res_fis
[RES_FIS_RFIS
];
770 d2h_fis
[0] = SATA_FIS_TYPE_REGISTER_D2H
;
771 d2h_fis
[1] = (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
772 d2h_fis
[2] = s
->status
;
773 d2h_fis
[3] = s
->error
;
775 d2h_fis
[4] = s
->sector
;
776 d2h_fis
[5] = s
->lcyl
;
777 d2h_fis
[6] = s
->hcyl
;
778 d2h_fis
[7] = s
->select
;
779 d2h_fis
[8] = s
->hob_sector
;
780 d2h_fis
[9] = s
->hob_lcyl
;
781 d2h_fis
[10] = s
->hob_hcyl
;
783 d2h_fis
[12] = cmd_fis
[12];
784 d2h_fis
[13] = cmd_fis
[13];
785 for (i
= 14; i
< 20; i
++) {
789 /* Update shadow registers: */
790 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
791 ad
->port
.ifs
[0].status
;
793 if (d2h_fis
[2] & ERR_STAT
) {
794 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_TF_ERR
);
797 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_D2H_REG_FIS
);
800 dma_memory_unmap(ad
->hba
->as
, cmd_fis
, cmd_len
,
801 DMA_DIRECTION_TO_DEVICE
, cmd_len
);
805 static int prdt_tbl_entry_size(const AHCI_SG
*tbl
)
807 return (le32_to_cpu(tbl
->flags_size
) & AHCI_PRDT_SIZE_MASK
) + 1;
810 static int ahci_populate_sglist(AHCIDevice
*ad
, QEMUSGList
*sglist
,
813 AHCICmdHdr
*cmd
= ad
->cur_cmd
;
814 uint32_t opts
= le32_to_cpu(cmd
->opts
);
815 uint64_t prdt_addr
= le64_to_cpu(cmd
->tbl_addr
) + 0x80;
816 int sglist_alloc_hint
= opts
>> AHCI_CMD_HDR_PRDT_LEN
;
817 dma_addr_t prdt_len
= (sglist_alloc_hint
* sizeof(AHCI_SG
));
818 dma_addr_t real_prdt_len
= prdt_len
;
824 int64_t off_pos
= -1;
826 IDEBus
*bus
= &ad
->port
;
827 BusState
*qbus
= BUS(bus
);
830 * Note: AHCI PRDT can describe up to 256GiB. SATA/ATA only support
831 * transactions of up to 32MiB as of ATA8-ACS3 rev 1b, assuming a
832 * 512 byte sector size. We limit the PRDT in this implementation to
833 * a reasonably large 2GiB, which can accommodate the maximum transfer
834 * request for sector sizes up to 32K.
837 if (!sglist_alloc_hint
) {
838 DPRINTF(ad
->port_no
, "no sg list given by guest: 0x%08x\n", opts
);
843 if (!(prdt
= dma_memory_map(ad
->hba
->as
, prdt_addr
, &prdt_len
,
844 DMA_DIRECTION_TO_DEVICE
))){
845 DPRINTF(ad
->port_no
, "map failed\n");
849 if (prdt_len
< real_prdt_len
) {
850 DPRINTF(ad
->port_no
, "mapped less than expected\n");
855 /* Get entries in the PRDT, init a qemu sglist accordingly */
856 if (sglist_alloc_hint
> 0) {
857 AHCI_SG
*tbl
= (AHCI_SG
*)prdt
;
859 for (i
= 0; i
< sglist_alloc_hint
; i
++) {
860 /* flags_size is zero-based */
861 tbl_entry_size
= prdt_tbl_entry_size(&tbl
[i
]);
862 if (offset
<= (sum
+ tbl_entry_size
)) {
864 off_pos
= offset
- sum
;
867 sum
+= tbl_entry_size
;
869 if ((off_idx
== -1) || (off_pos
< 0) || (off_pos
> tbl_entry_size
)) {
870 DPRINTF(ad
->port_no
, "%s: Incorrect offset! "
871 "off_idx: %d, off_pos: %"PRId64
"\n",
872 __func__
, off_idx
, off_pos
);
877 qemu_sglist_init(sglist
, qbus
->parent
, (sglist_alloc_hint
- off_idx
),
879 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[off_idx
].addr
) + off_pos
,
880 prdt_tbl_entry_size(&tbl
[off_idx
]) - off_pos
);
882 for (i
= off_idx
+ 1; i
< sglist_alloc_hint
; i
++) {
883 /* flags_size is zero-based */
884 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[i
].addr
),
885 prdt_tbl_entry_size(&tbl
[i
]));
886 if (sglist
->size
> INT32_MAX
) {
887 error_report("AHCI Physical Region Descriptor Table describes "
888 "more than 2 GiB.\n");
889 qemu_sglist_destroy(sglist
);
897 dma_memory_unmap(ad
->hba
->as
, prdt
, prdt_len
,
898 DMA_DIRECTION_TO_DEVICE
, prdt_len
);
902 static void ncq_cb(void *opaque
, int ret
)
904 NCQTransferState
*ncq_tfs
= (NCQTransferState
*)opaque
;
905 IDEState
*ide_state
= &ncq_tfs
->drive
->port
.ifs
[0];
907 if (ret
== -ECANCELED
) {
910 /* Clear bit for this tag in SActive */
911 ncq_tfs
->drive
->port_regs
.scr_act
&= ~(1 << ncq_tfs
->tag
);
915 ide_state
->error
= ABRT_ERR
;
916 ide_state
->status
= READY_STAT
| ERR_STAT
;
917 ncq_tfs
->drive
->port_regs
.scr_err
|= (1 << ncq_tfs
->tag
);
919 ide_state
->status
= READY_STAT
| SEEK_STAT
;
922 ahci_write_fis_sdb(ncq_tfs
->drive
->hba
, ncq_tfs
->drive
->port_no
,
923 (1 << ncq_tfs
->tag
));
925 DPRINTF(ncq_tfs
->drive
->port_no
, "NCQ transfer tag %d finished\n",
928 block_acct_done(blk_get_stats(ncq_tfs
->drive
->port
.ifs
[0].blk
),
930 qemu_sglist_destroy(&ncq_tfs
->sglist
);
934 static int is_ncq(uint8_t ata_cmd
)
936 /* Based on SATA 3.2 section 13.6.3.2 */
938 case READ_FPDMA_QUEUED
:
939 case WRITE_FPDMA_QUEUED
:
941 case RECEIVE_FPDMA_QUEUED
:
942 case SEND_FPDMA_QUEUED
:
949 static void process_ncq_command(AHCIState
*s
, int port
, uint8_t *cmd_fis
,
952 NCQFrame
*ncq_fis
= (NCQFrame
*)cmd_fis
;
953 uint8_t tag
= ncq_fis
->tag
>> 3;
954 NCQTransferState
*ncq_tfs
= &s
->dev
[port
].ncq_tfs
[tag
];
957 /* error - already in use */
958 fprintf(stderr
, "%s: tag %d already used\n", __FUNCTION__
, tag
);
963 ncq_tfs
->drive
= &s
->dev
[port
];
964 ncq_tfs
->slot
= slot
;
965 ncq_tfs
->lba
= ((uint64_t)ncq_fis
->lba5
<< 40) |
966 ((uint64_t)ncq_fis
->lba4
<< 32) |
967 ((uint64_t)ncq_fis
->lba3
<< 24) |
968 ((uint64_t)ncq_fis
->lba2
<< 16) |
969 ((uint64_t)ncq_fis
->lba1
<< 8) |
970 (uint64_t)ncq_fis
->lba0
;
972 /* Note: We calculate the sector count, but don't currently rely on it.
973 * The total size of the DMA buffer tells us the transfer size instead. */
974 ncq_tfs
->sector_count
= ((uint16_t)ncq_fis
->sector_count_high
<< 8) |
975 ncq_fis
->sector_count_low
;
977 DPRINTF(port
, "NCQ transfer LBA from %"PRId64
" to %"PRId64
", "
978 "drive max %"PRId64
"\n",
979 ncq_tfs
->lba
, ncq_tfs
->lba
+ ncq_tfs
->sector_count
- 2,
980 s
->dev
[port
].port
.ifs
[0].nb_sectors
- 1);
982 ahci_populate_sglist(&s
->dev
[port
], &ncq_tfs
->sglist
, 0);
985 switch(ncq_fis
->command
) {
986 case READ_FPDMA_QUEUED
:
987 DPRINTF(port
, "NCQ reading %d sectors from LBA %"PRId64
", "
989 ncq_tfs
->sector_count
-1, ncq_tfs
->lba
, ncq_tfs
->tag
);
991 DPRINTF(port
, "tag %d aio read %"PRId64
"\n",
992 ncq_tfs
->tag
, ncq_tfs
->lba
);
994 dma_acct_start(ncq_tfs
->drive
->port
.ifs
[0].blk
, &ncq_tfs
->acct
,
995 &ncq_tfs
->sglist
, BLOCK_ACCT_READ
);
996 ncq_tfs
->aiocb
= dma_blk_read(ncq_tfs
->drive
->port
.ifs
[0].blk
,
997 &ncq_tfs
->sglist
, ncq_tfs
->lba
,
1000 case WRITE_FPDMA_QUEUED
:
1001 DPRINTF(port
, "NCQ writing %d sectors to LBA %"PRId64
", tag %d\n",
1002 ncq_tfs
->sector_count
-1, ncq_tfs
->lba
, ncq_tfs
->tag
);
1004 DPRINTF(port
, "tag %d aio write %"PRId64
"\n",
1005 ncq_tfs
->tag
, ncq_tfs
->lba
);
1007 dma_acct_start(ncq_tfs
->drive
->port
.ifs
[0].blk
, &ncq_tfs
->acct
,
1008 &ncq_tfs
->sglist
, BLOCK_ACCT_WRITE
);
1009 ncq_tfs
->aiocb
= dma_blk_write(ncq_tfs
->drive
->port
.ifs
[0].blk
,
1010 &ncq_tfs
->sglist
, ncq_tfs
->lba
,
1014 if (is_ncq(cmd_fis
[2])) {
1016 "error: unsupported NCQ command (0x%02x) received\n",
1020 "error: tried to process non-NCQ command as NCQ\n");
1022 qemu_sglist_destroy(&ncq_tfs
->sglist
);
1026 static void handle_reg_h2d_fis(AHCIState
*s
, int port
,
1027 int slot
, uint8_t *cmd_fis
)
1029 IDEState
*ide_state
= &s
->dev
[port
].port
.ifs
[0];
1030 AHCICmdHdr
*cmd
= s
->dev
[port
].cur_cmd
;
1031 uint32_t opts
= le32_to_cpu(cmd
->opts
);
1033 if (cmd_fis
[1] & 0x0F) {
1034 DPRINTF(port
, "Port Multiplier not supported."
1035 " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
1036 cmd_fis
[0], cmd_fis
[1], cmd_fis
[2]);
1040 if (cmd_fis
[1] & 0x70) {
1041 DPRINTF(port
, "Reserved flags set in H2D Register FIS."
1042 " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
1043 cmd_fis
[0], cmd_fis
[1], cmd_fis
[2]);
1047 if (!(cmd_fis
[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER
)) {
1048 switch (s
->dev
[port
].port_state
) {
1050 if (cmd_fis
[15] & ATA_SRST
) {
1051 s
->dev
[port
].port_state
= STATE_RESET
;
1055 if (!(cmd_fis
[15] & ATA_SRST
)) {
1056 ahci_reset_port(s
, port
);
1063 /* Check for NCQ command */
1064 if (is_ncq(cmd_fis
[2])) {
1065 process_ncq_command(s
, port
, cmd_fis
, slot
);
1069 /* Decompose the FIS:
1070 * AHCI does not interpret FIS packets, it only forwards them.
1071 * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
1072 * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
1074 * ATA4 describes sector number for LBA28/CHS commands.
1075 * ATA6 describes sector number for LBA48 commands.
1076 * ATA8 deprecates CHS fully, describing only LBA28/48.
1078 * We dutifully convert the FIS into IDE registers, and allow the
1079 * core layer to interpret them as needed. */
1080 ide_state
->feature
= cmd_fis
[3];
1081 ide_state
->sector
= cmd_fis
[4]; /* LBA 7:0 */
1082 ide_state
->lcyl
= cmd_fis
[5]; /* LBA 15:8 */
1083 ide_state
->hcyl
= cmd_fis
[6]; /* LBA 23:16 */
1084 ide_state
->select
= cmd_fis
[7]; /* LBA 27:24 (LBA28) */
1085 ide_state
->hob_sector
= cmd_fis
[8]; /* LBA 31:24 */
1086 ide_state
->hob_lcyl
= cmd_fis
[9]; /* LBA 39:32 */
1087 ide_state
->hob_hcyl
= cmd_fis
[10]; /* LBA 47:40 */
1088 ide_state
->hob_feature
= cmd_fis
[11];
1089 ide_state
->nsector
= (int64_t)((cmd_fis
[13] << 8) | cmd_fis
[12]);
1090 /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
1091 /* 15: Only valid when UPDATE_COMMAND not set. */
1093 /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
1094 * table to ide_state->io_buffer */
1095 if (opts
& AHCI_CMD_ATAPI
) {
1096 memcpy(ide_state
->io_buffer
, &cmd_fis
[AHCI_COMMAND_TABLE_ACMD
], 0x10);
1097 debug_print_fis(ide_state
->io_buffer
, 0x10);
1098 s
->dev
[port
].done_atapi_packet
= false;
1099 /* XXX send PIO setup FIS */
1102 ide_state
->error
= 0;
1104 /* Reset transferred byte counter */
1107 /* We're ready to process the command in FIS byte 2. */
1108 ide_exec_cmd(&s
->dev
[port
].port
, cmd_fis
[2]);
1111 static int handle_cmd(AHCIState
*s
, int port
, int slot
)
1113 IDEState
*ide_state
;
1119 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
1120 /* Engine currently busy, try again later */
1121 DPRINTF(port
, "engine busy\n");
1125 if (!s
->dev
[port
].lst
) {
1126 DPRINTF(port
, "error: lst not given but cmd handled");
1129 cmd
= &((AHCICmdHdr
*)s
->dev
[port
].lst
)[slot
];
1130 /* remember current slot handle for later */
1131 s
->dev
[port
].cur_cmd
= cmd
;
1133 /* The device we are working for */
1134 ide_state
= &s
->dev
[port
].port
.ifs
[0];
1135 if (!ide_state
->blk
) {
1136 DPRINTF(port
, "error: guest accessed unused port");
1140 tbl_addr
= le64_to_cpu(cmd
->tbl_addr
);
1142 cmd_fis
= dma_memory_map(s
->as
, tbl_addr
, &cmd_len
,
1143 DMA_DIRECTION_FROM_DEVICE
);
1145 DPRINTF(port
, "error: guest passed us an invalid cmd fis\n");
1147 } else if (cmd_len
!= 0x80) {
1148 ahci_trigger_irq(s
, &s
->dev
[port
], PORT_IRQ_HBUS_ERR
);
1149 DPRINTF(port
, "error: dma_memory_map failed: "
1150 "(len(%02"PRIx64
") != 0x80)\n",
1154 debug_print_fis(cmd_fis
, 0x80);
1156 switch (cmd_fis
[0]) {
1157 case SATA_FIS_TYPE_REGISTER_H2D
:
1158 handle_reg_h2d_fis(s
, port
, slot
, cmd_fis
);
1161 DPRINTF(port
, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
1162 "cmd_fis[2]=%02x\n", cmd_fis
[0], cmd_fis
[1],
1168 dma_memory_unmap(s
->as
, cmd_fis
, cmd_len
, DMA_DIRECTION_FROM_DEVICE
,
1171 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
1172 /* async command, complete later */
1173 s
->dev
[port
].busy_slot
= slot
;
1177 /* done handling the command */
1181 /* DMA dev <-> ram */
1182 static void ahci_start_transfer(IDEDMA
*dma
)
1184 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1185 IDEState
*s
= &ad
->port
.ifs
[0];
1186 uint32_t size
= (uint32_t)(s
->data_end
- s
->data_ptr
);
1187 /* write == ram -> device */
1188 uint32_t opts
= le32_to_cpu(ad
->cur_cmd
->opts
);
1189 int is_write
= opts
& AHCI_CMD_WRITE
;
1190 int is_atapi
= opts
& AHCI_CMD_ATAPI
;
1193 if (is_atapi
&& !ad
->done_atapi_packet
) {
1194 /* already prepopulated iobuffer */
1195 ad
->done_atapi_packet
= true;
1200 if (ahci_dma_prepare_buf(dma
, is_write
)) {
1204 DPRINTF(ad
->port_no
, "%sing %d bytes on %s w/%s sglist\n",
1205 is_write
? "writ" : "read", size
, is_atapi
? "atapi" : "ata",
1206 has_sglist
? "" : "o");
1208 if (has_sglist
&& size
) {
1210 dma_buf_write(s
->data_ptr
, size
, &s
->sg
);
1212 dma_buf_read(s
->data_ptr
, size
, &s
->sg
);
1217 /* declare that we processed everything */
1218 s
->data_ptr
= s
->data_end
;
1220 /* Update number of transferred bytes, destroy sglist */
1221 ahci_commit_buf(dma
, size
);
1223 s
->end_transfer_func(s
);
1225 if (!(s
->status
& DRQ_STAT
)) {
1226 /* done with PIO send/receive */
1227 ahci_write_fis_pio(ad
, le32_to_cpu(ad
->cur_cmd
->status
));
1231 static void ahci_start_dma(IDEDMA
*dma
, IDEState
*s
,
1232 BlockCompletionFunc
*dma_cb
)
1234 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1235 DPRINTF(ad
->port_no
, "\n");
1236 s
->io_buffer_offset
= 0;
1240 static void ahci_restart_dma(IDEDMA
*dma
)
1242 /* Nothing to do, ahci_start_dma already resets s->io_buffer_offset. */
1246 * Called in DMA R/W chains to read the PRDT, utilizing ahci_populate_sglist.
1247 * Not currently invoked by PIO R/W chains,
1248 * which invoke ahci_populate_sglist via ahci_start_transfer.
1250 static int32_t ahci_dma_prepare_buf(IDEDMA
*dma
, int is_write
)
1252 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1253 IDEState
*s
= &ad
->port
.ifs
[0];
1255 if (ahci_populate_sglist(ad
, &s
->sg
, s
->io_buffer_offset
) == -1) {
1256 DPRINTF(ad
->port_no
, "ahci_dma_prepare_buf failed.\n");
1259 s
->io_buffer_size
= s
->sg
.size
;
1261 DPRINTF(ad
->port_no
, "len=%#x\n", s
->io_buffer_size
);
1262 return s
->io_buffer_size
;
1266 * Destroys the scatter-gather list,
1267 * and updates the command header with a bytes-read value.
1268 * called explicitly via ahci_dma_rw_buf (ATAPI DMA),
1269 * and ahci_start_transfer (PIO R/W),
1270 * and called via callback from ide_dma_cb for DMA R/W paths.
1272 static void ahci_commit_buf(IDEDMA
*dma
, uint32_t tx_bytes
)
1274 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1275 IDEState
*s
= &ad
->port
.ifs
[0];
1277 tx_bytes
+= le32_to_cpu(ad
->cur_cmd
->status
);
1278 ad
->cur_cmd
->status
= cpu_to_le32(tx_bytes
);
1280 qemu_sglist_destroy(&s
->sg
);
1283 static int ahci_dma_rw_buf(IDEDMA
*dma
, int is_write
)
1285 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1286 IDEState
*s
= &ad
->port
.ifs
[0];
1287 uint8_t *p
= s
->io_buffer
+ s
->io_buffer_index
;
1288 int l
= s
->io_buffer_size
- s
->io_buffer_index
;
1290 if (ahci_populate_sglist(ad
, &s
->sg
, s
->io_buffer_offset
)) {
1295 dma_buf_read(p
, l
, &s
->sg
);
1297 dma_buf_write(p
, l
, &s
->sg
);
1300 /* free sglist, update byte count */
1301 ahci_commit_buf(dma
, l
);
1303 s
->io_buffer_index
+= l
;
1304 s
->io_buffer_offset
+= l
;
1306 DPRINTF(ad
->port_no
, "len=%#x\n", l
);
1311 static void ahci_cmd_done(IDEDMA
*dma
)
1313 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1315 DPRINTF(ad
->port_no
, "cmd done\n");
1317 /* update d2h status */
1318 ahci_write_fis_d2h(ad
, NULL
);
1320 if (!ad
->check_bh
) {
1321 /* maybe we still have something to process, check later */
1322 ad
->check_bh
= qemu_bh_new(ahci_check_cmd_bh
, ad
);
1323 qemu_bh_schedule(ad
->check_bh
);
1327 static void ahci_irq_set(void *opaque
, int n
, int level
)
1331 static const IDEDMAOps ahci_dma_ops
= {
1332 .start_dma
= ahci_start_dma
,
1333 .restart_dma
= ahci_restart_dma
,
1334 .start_transfer
= ahci_start_transfer
,
1335 .prepare_buf
= ahci_dma_prepare_buf
,
1336 .commit_buf
= ahci_commit_buf
,
1337 .rw_buf
= ahci_dma_rw_buf
,
1338 .cmd_done
= ahci_cmd_done
,
1341 void ahci_init(AHCIState
*s
, DeviceState
*qdev
, AddressSpace
*as
, int ports
)
1348 s
->dev
= g_new0(AHCIDevice
, ports
);
1350 /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
1351 memory_region_init_io(&s
->mem
, OBJECT(qdev
), &ahci_mem_ops
, s
,
1352 "ahci", AHCI_MEM_BAR_SIZE
);
1353 memory_region_init_io(&s
->idp
, OBJECT(qdev
), &ahci_idp_ops
, s
,
1356 irqs
= qemu_allocate_irqs(ahci_irq_set
, s
, s
->ports
);
1358 for (i
= 0; i
< s
->ports
; i
++) {
1359 AHCIDevice
*ad
= &s
->dev
[i
];
1361 ide_bus_new(&ad
->port
, sizeof(ad
->port
), qdev
, i
, 1);
1362 ide_init2(&ad
->port
, irqs
[i
]);
1366 ad
->port
.dma
= &ad
->dma
;
1367 ad
->port
.dma
->ops
= &ahci_dma_ops
;
1368 ide_register_restart_cb(&ad
->port
);
1372 void ahci_uninit(AHCIState
*s
)
1377 void ahci_reset(AHCIState
*s
)
1382 s
->control_regs
.irqstatus
= 0;
1384 * The implementation of this bit is dependent upon the value of the
1385 * CAP.SAM bit. If CAP.SAM is '0', then GHC.AE shall be read-write and
1386 * shall have a reset value of '0'. If CAP.SAM is '1', then AE shall be
1387 * read-only and shall have a reset value of '1'.
1389 * We set HOST_CAP_AHCI so we must enable AHCI at reset.
1391 s
->control_regs
.ghc
= HOST_CTL_AHCI_EN
;
1393 for (i
= 0; i
< s
->ports
; i
++) {
1394 pr
= &s
->dev
[i
].port_regs
;
1398 pr
->cmd
= PORT_CMD_SPIN_UP
| PORT_CMD_POWER_ON
;
1399 ahci_reset_port(s
, i
);
1403 static const VMStateDescription vmstate_ahci_device
= {
1404 .name
= "ahci port",
1406 .fields
= (VMStateField
[]) {
1407 VMSTATE_IDE_BUS(port
, AHCIDevice
),
1408 VMSTATE_IDE_DRIVE(port
.ifs
[0], AHCIDevice
),
1409 VMSTATE_UINT32(port_state
, AHCIDevice
),
1410 VMSTATE_UINT32(finished
, AHCIDevice
),
1411 VMSTATE_UINT32(port_regs
.lst_addr
, AHCIDevice
),
1412 VMSTATE_UINT32(port_regs
.lst_addr_hi
, AHCIDevice
),
1413 VMSTATE_UINT32(port_regs
.fis_addr
, AHCIDevice
),
1414 VMSTATE_UINT32(port_regs
.fis_addr_hi
, AHCIDevice
),
1415 VMSTATE_UINT32(port_regs
.irq_stat
, AHCIDevice
),
1416 VMSTATE_UINT32(port_regs
.irq_mask
, AHCIDevice
),
1417 VMSTATE_UINT32(port_regs
.cmd
, AHCIDevice
),
1418 VMSTATE_UINT32(port_regs
.tfdata
, AHCIDevice
),
1419 VMSTATE_UINT32(port_regs
.sig
, AHCIDevice
),
1420 VMSTATE_UINT32(port_regs
.scr_stat
, AHCIDevice
),
1421 VMSTATE_UINT32(port_regs
.scr_ctl
, AHCIDevice
),
1422 VMSTATE_UINT32(port_regs
.scr_err
, AHCIDevice
),
1423 VMSTATE_UINT32(port_regs
.scr_act
, AHCIDevice
),
1424 VMSTATE_UINT32(port_regs
.cmd_issue
, AHCIDevice
),
1425 VMSTATE_BOOL(done_atapi_packet
, AHCIDevice
),
1426 VMSTATE_INT32(busy_slot
, AHCIDevice
),
1427 VMSTATE_BOOL(init_d2h_sent
, AHCIDevice
),
1428 VMSTATE_END_OF_LIST()
1432 static int ahci_state_post_load(void *opaque
, int version_id
)
1435 struct AHCIDevice
*ad
;
1436 AHCIState
*s
= opaque
;
1438 for (i
= 0; i
< s
->ports
; i
++) {
1441 /* Only remap the CLB address if appropriate, disallowing a state
1442 * transition from 'on' to 'off' it should be consistent here. */
1443 if (ahci_cond_start_engines(ad
, false) != 0) {
1448 * If an error is present, ad->busy_slot will be valid and not -1.
1449 * In this case, an operation is waiting to resume and will re-check
1450 * for additional AHCI commands to execute upon completion.
1452 * In the case where no error was present, busy_slot will be -1,
1453 * and we should check to see if there are additional commands waiting.
1455 if (ad
->busy_slot
== -1) {
1458 /* We are in the middle of a command, and may need to access
1459 * the command header in guest memory again. */
1460 if (ad
->busy_slot
< 0 || ad
->busy_slot
>= AHCI_MAX_CMDS
) {
1463 ad
->cur_cmd
= &((AHCICmdHdr
*)ad
->lst
)[ad
->busy_slot
];
1470 const VMStateDescription vmstate_ahci
= {
1473 .post_load
= ahci_state_post_load
,
1474 .fields
= (VMStateField
[]) {
1475 VMSTATE_STRUCT_VARRAY_POINTER_INT32(dev
, AHCIState
, ports
,
1476 vmstate_ahci_device
, AHCIDevice
),
1477 VMSTATE_UINT32(control_regs
.cap
, AHCIState
),
1478 VMSTATE_UINT32(control_regs
.ghc
, AHCIState
),
1479 VMSTATE_UINT32(control_regs
.irqstatus
, AHCIState
),
1480 VMSTATE_UINT32(control_regs
.impl
, AHCIState
),
1481 VMSTATE_UINT32(control_regs
.version
, AHCIState
),
1482 VMSTATE_UINT32(idp_index
, AHCIState
),
1483 VMSTATE_INT32_EQUAL(ports
, AHCIState
),
1484 VMSTATE_END_OF_LIST()
1488 #define TYPE_SYSBUS_AHCI "sysbus-ahci"
1489 #define SYSBUS_AHCI(obj) OBJECT_CHECK(SysbusAHCIState, (obj), TYPE_SYSBUS_AHCI)
1491 typedef struct SysbusAHCIState
{
1493 SysBusDevice parent_obj
;
1500 static const VMStateDescription vmstate_sysbus_ahci
= {
1501 .name
= "sysbus-ahci",
1502 .fields
= (VMStateField
[]) {
1503 VMSTATE_AHCI(ahci
, SysbusAHCIState
),
1504 VMSTATE_END_OF_LIST()
1508 static void sysbus_ahci_reset(DeviceState
*dev
)
1510 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1512 ahci_reset(&s
->ahci
);
1515 static void sysbus_ahci_realize(DeviceState
*dev
, Error
**errp
)
1517 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1518 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1520 ahci_init(&s
->ahci
, dev
, &address_space_memory
, s
->num_ports
);
1522 sysbus_init_mmio(sbd
, &s
->ahci
.mem
);
1523 sysbus_init_irq(sbd
, &s
->ahci
.irq
);
1526 static Property sysbus_ahci_properties
[] = {
1527 DEFINE_PROP_UINT32("num-ports", SysbusAHCIState
, num_ports
, 1),
1528 DEFINE_PROP_END_OF_LIST(),
1531 static void sysbus_ahci_class_init(ObjectClass
*klass
, void *data
)
1533 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1535 dc
->realize
= sysbus_ahci_realize
;
1536 dc
->vmsd
= &vmstate_sysbus_ahci
;
1537 dc
->props
= sysbus_ahci_properties
;
1538 dc
->reset
= sysbus_ahci_reset
;
1539 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1542 static const TypeInfo sysbus_ahci_info
= {
1543 .name
= TYPE_SYSBUS_AHCI
,
1544 .parent
= TYPE_SYS_BUS_DEVICE
,
1545 .instance_size
= sizeof(SysbusAHCIState
),
1546 .class_init
= sysbus_ahci_class_init
,
1549 static void sysbus_ahci_register_types(void)
1551 type_register_static(&sysbus_ahci_info
);
1554 type_init(sysbus_ahci_register_types
)
1556 void ahci_ide_create_devs(PCIDevice
*dev
, DriveInfo
**hd
)
1558 AHCIPCIState
*d
= ICH_AHCI(dev
);
1559 AHCIState
*ahci
= &d
->ahci
;
1562 for (i
= 0; i
< ahci
->ports
; i
++) {
1563 if (hd
[i
] == NULL
) {
1566 ide_create_drive(&ahci
->dev
[i
].port
, 0, hd
[i
]);