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/>.
24 #include "qemu/osdep.h"
26 #include "hw/pci/msi.h"
27 #include "hw/i386/pc.h"
28 #include "hw/pci/pci.h"
30 #include "qemu/error-report.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 "hw/ide/ahci_internal.h"
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
);
50 static const char *AHCIPortIRQ_lookup
[AHCI_PORT_IRQ__COUNT
] = {
51 [AHCI_PORT_IRQ_BIT_DHRS
] = "DHRS",
52 [AHCI_PORT_IRQ_BIT_PSS
] = "PSS",
53 [AHCI_PORT_IRQ_BIT_DSS
] = "DSS",
54 [AHCI_PORT_IRQ_BIT_SDBS
] = "SDBS",
55 [AHCI_PORT_IRQ_BIT_UFS
] = "UFS",
56 [AHCI_PORT_IRQ_BIT_DPS
] = "DPS",
57 [AHCI_PORT_IRQ_BIT_PCS
] = "PCS",
58 [AHCI_PORT_IRQ_BIT_DMPS
] = "DMPS",
59 [8 ... 21] = "RESERVED",
60 [AHCI_PORT_IRQ_BIT_PRCS
] = "PRCS",
61 [AHCI_PORT_IRQ_BIT_IPMS
] = "IPMS",
62 [AHCI_PORT_IRQ_BIT_OFS
] = "OFS",
64 [AHCI_PORT_IRQ_BIT_INFS
] = "INFS",
65 [AHCI_PORT_IRQ_BIT_IFS
] = "IFS",
66 [AHCI_PORT_IRQ_BIT_HBDS
] = "HBDS",
67 [AHCI_PORT_IRQ_BIT_HBFS
] = "HBFS",
68 [AHCI_PORT_IRQ_BIT_TFES
] = "TFES",
69 [AHCI_PORT_IRQ_BIT_CPDS
] = "CPDS"
72 static uint32_t ahci_port_read(AHCIState
*s
, int port
, int offset
)
76 pr
= &s
->dev
[port
].port_regs
;
82 case PORT_LST_ADDR_HI
:
83 val
= pr
->lst_addr_hi
;
88 case PORT_FIS_ADDR_HI
:
89 val
= pr
->fis_addr_hi
;
107 if (s
->dev
[port
].port
.ifs
[0].blk
) {
108 val
= SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP
|
109 SATA_SCR_SSTATUS_SPD_GEN1
| SATA_SCR_SSTATUS_IPM_ACTIVE
;
111 val
= SATA_SCR_SSTATUS_DET_NODEV
;
131 trace_ahci_port_read(s
, port
, offset
, val
);
135 static void ahci_irq_raise(AHCIState
*s
, AHCIDevice
*dev
)
137 DeviceState
*dev_state
= s
->container
;
138 PCIDevice
*pci_dev
= (PCIDevice
*) object_dynamic_cast(OBJECT(dev_state
),
141 trace_ahci_irq_raise(s
);
143 if (pci_dev
&& msi_enabled(pci_dev
)) {
144 msi_notify(pci_dev
, 0);
146 qemu_irq_raise(s
->irq
);
150 static void ahci_irq_lower(AHCIState
*s
, AHCIDevice
*dev
)
152 DeviceState
*dev_state
= s
->container
;
153 PCIDevice
*pci_dev
= (PCIDevice
*) object_dynamic_cast(OBJECT(dev_state
),
156 trace_ahci_irq_lower(s
);
158 if (!pci_dev
|| !msi_enabled(pci_dev
)) {
159 qemu_irq_lower(s
->irq
);
163 static void ahci_check_irq(AHCIState
*s
)
166 uint32_t old_irq
= s
->control_regs
.irqstatus
;
168 s
->control_regs
.irqstatus
= 0;
169 for (i
= 0; i
< s
->ports
; i
++) {
170 AHCIPortRegs
*pr
= &s
->dev
[i
].port_regs
;
171 if (pr
->irq_stat
& pr
->irq_mask
) {
172 s
->control_regs
.irqstatus
|= (1 << i
);
175 trace_ahci_check_irq(s
, old_irq
, s
->control_regs
.irqstatus
);
176 if (s
->control_regs
.irqstatus
&&
177 (s
->control_regs
.ghc
& HOST_CTL_IRQ_EN
)) {
178 ahci_irq_raise(s
, NULL
);
180 ahci_irq_lower(s
, NULL
);
184 static void ahci_trigger_irq(AHCIState
*s
, AHCIDevice
*d
,
185 enum AHCIPortIRQ irqbit
)
187 g_assert(irqbit
>= 0 && irqbit
< 32);
188 uint32_t irq
= 1U << irqbit
;
189 uint32_t irqstat
= d
->port_regs
.irq_stat
| irq
;
191 trace_ahci_trigger_irq(s
, d
->port_no
,
192 AHCIPortIRQ_lookup
[irqbit
], irq
,
193 d
->port_regs
.irq_stat
, irqstat
,
194 irqstat
& d
->port_regs
.irq_mask
);
196 d
->port_regs
.irq_stat
= irqstat
;
200 static void map_page(AddressSpace
*as
, uint8_t **ptr
, uint64_t addr
,
206 dma_memory_unmap(as
, *ptr
, len
, DMA_DIRECTION_FROM_DEVICE
, len
);
209 *ptr
= dma_memory_map(as
, addr
, &len
, DMA_DIRECTION_FROM_DEVICE
);
211 dma_memory_unmap(as
, *ptr
, len
, DMA_DIRECTION_FROM_DEVICE
, len
);
217 * Check the cmd register to see if we should start or stop
218 * the DMA or FIS RX engines.
220 * @ad: Device to dis/engage.
222 * @return 0 on success, -1 on error.
224 static int ahci_cond_start_engines(AHCIDevice
*ad
)
226 AHCIPortRegs
*pr
= &ad
->port_regs
;
227 bool cmd_start
= pr
->cmd
& PORT_CMD_START
;
228 bool cmd_on
= pr
->cmd
& PORT_CMD_LIST_ON
;
229 bool fis_start
= pr
->cmd
& PORT_CMD_FIS_RX
;
230 bool fis_on
= pr
->cmd
& PORT_CMD_FIS_ON
;
232 if (cmd_start
&& !cmd_on
) {
233 if (!ahci_map_clb_address(ad
)) {
234 pr
->cmd
&= ~PORT_CMD_START
;
235 error_report("AHCI: Failed to start DMA engine: "
236 "bad command list buffer address");
239 } else if (!cmd_start
&& cmd_on
) {
240 ahci_unmap_clb_address(ad
);
243 if (fis_start
&& !fis_on
) {
244 if (!ahci_map_fis_address(ad
)) {
245 pr
->cmd
&= ~PORT_CMD_FIS_RX
;
246 error_report("AHCI: Failed to start FIS receive engine: "
247 "bad FIS receive buffer address");
250 } else if (!fis_start
&& fis_on
) {
251 ahci_unmap_fis_address(ad
);
257 static void ahci_port_write(AHCIState
*s
, int port
, int offset
, uint32_t val
)
259 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
261 trace_ahci_port_write(s
, port
, offset
, val
);
266 case PORT_LST_ADDR_HI
:
267 pr
->lst_addr_hi
= val
;
272 case PORT_FIS_ADDR_HI
:
273 pr
->fis_addr_hi
= val
;
276 pr
->irq_stat
&= ~val
;
280 pr
->irq_mask
= val
& 0xfdc000ff;
284 /* Block any Read-only fields from being set;
285 * including LIST_ON and FIS_ON.
286 * The spec requires to set ICC bits to zero after the ICC change
287 * is done. We don't support ICC state changes, therefore always
288 * force the ICC bits to zero.
290 pr
->cmd
= (pr
->cmd
& PORT_CMD_RO_MASK
) |
291 (val
& ~(PORT_CMD_RO_MASK
|PORT_CMD_ICC_MASK
));
293 /* Check FIS RX and CLB engines */
294 ahci_cond_start_engines(&s
->dev
[port
]);
296 /* XXX usually the FIS would be pending on the bus here and
297 issuing deferred until the OS enables FIS receival.
298 Instead, we only submit it once - which works in most
299 cases, but is a hack. */
300 if ((pr
->cmd
& PORT_CMD_FIS_ON
) &&
301 !s
->dev
[port
].init_d2h_sent
) {
302 ahci_init_d2h(&s
->dev
[port
]);
317 if (((pr
->scr_ctl
& AHCI_SCR_SCTL_DET
) == 1) &&
318 ((val
& AHCI_SCR_SCTL_DET
) == 0)) {
319 ahci_reset_port(s
, port
);
331 pr
->cmd_issue
|= val
;
339 static uint64_t ahci_mem_read_32(void *opaque
, hwaddr addr
)
341 AHCIState
*s
= opaque
;
344 if (addr
< AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR
) {
347 val
= s
->control_regs
.cap
;
350 val
= s
->control_regs
.ghc
;
353 val
= s
->control_regs
.irqstatus
;
355 case HOST_PORTS_IMPL
:
356 val
= s
->control_regs
.impl
;
359 val
= s
->control_regs
.version
;
362 } else if ((addr
>= AHCI_PORT_REGS_START_ADDR
) &&
363 (addr
< (AHCI_PORT_REGS_START_ADDR
+
364 (s
->ports
* AHCI_PORT_ADDR_OFFSET_LEN
)))) {
365 val
= ahci_port_read(s
, (addr
- AHCI_PORT_REGS_START_ADDR
) >> 7,
366 addr
& AHCI_PORT_ADDR_OFFSET_MASK
);
369 trace_ahci_mem_read_32(s
, addr
, val
);
375 * AHCI 1.3 section 3 ("HBA Memory Registers")
376 * Support unaligned 8/16/32 bit reads, and 64 bit aligned reads.
377 * Caller is responsible for masking unwanted higher order bytes.
379 static uint64_t ahci_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
381 hwaddr aligned
= addr
& ~0x3;
382 int ofst
= addr
- aligned
;
383 uint64_t lo
= ahci_mem_read_32(opaque
, aligned
);
387 /* if < 8 byte read does not cross 4 byte boundary */
388 if (ofst
+ size
<= 4) {
389 val
= lo
>> (ofst
* 8);
391 g_assert_cmpint(size
, >, 1);
393 /* If the 64bit read is unaligned, we will produce undefined
394 * results. AHCI does not support unaligned 64bit reads. */
395 hi
= ahci_mem_read_32(opaque
, aligned
+ 4);
396 val
= (hi
<< 32 | lo
) >> (ofst
* 8);
399 trace_ahci_mem_read(opaque
, size
, addr
, val
);
404 static void ahci_mem_write(void *opaque
, hwaddr addr
,
405 uint64_t val
, unsigned size
)
407 AHCIState
*s
= opaque
;
409 trace_ahci_mem_write(s
, size
, addr
, val
);
411 /* Only aligned reads are allowed on AHCI */
413 fprintf(stderr
, "ahci: Mis-aligned write to addr 0x"
414 TARGET_FMT_plx
"\n", addr
);
418 if (addr
< AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR
) {
420 case HOST_CAP
: /* R/WO, RO */
421 /* FIXME handle R/WO */
423 case HOST_CTL
: /* R/W */
424 if (val
& HOST_CTL_RESET
) {
427 s
->control_regs
.ghc
= (val
& 0x3) | HOST_CTL_AHCI_EN
;
431 case HOST_IRQ_STAT
: /* R/WC, RO */
432 s
->control_regs
.irqstatus
&= ~val
;
435 case HOST_PORTS_IMPL
: /* R/WO, RO */
436 /* FIXME handle R/WO */
438 case HOST_VERSION
: /* RO */
439 /* FIXME report write? */
442 trace_ahci_mem_write_unknown(s
, size
, addr
, val
);
444 } else if ((addr
>= AHCI_PORT_REGS_START_ADDR
) &&
445 (addr
< (AHCI_PORT_REGS_START_ADDR
+
446 (s
->ports
* AHCI_PORT_ADDR_OFFSET_LEN
)))) {
447 ahci_port_write(s
, (addr
- AHCI_PORT_REGS_START_ADDR
) >> 7,
448 addr
& AHCI_PORT_ADDR_OFFSET_MASK
, val
);
453 static const MemoryRegionOps ahci_mem_ops
= {
454 .read
= ahci_mem_read
,
455 .write
= ahci_mem_write
,
456 .endianness
= DEVICE_LITTLE_ENDIAN
,
459 static uint64_t ahci_idp_read(void *opaque
, hwaddr addr
,
462 AHCIState
*s
= opaque
;
464 if (addr
== s
->idp_offset
) {
467 } else if (addr
== s
->idp_offset
+ 4) {
468 /* data register - do memory read at location selected by index */
469 return ahci_mem_read(opaque
, s
->idp_index
, size
);
475 static void ahci_idp_write(void *opaque
, hwaddr addr
,
476 uint64_t val
, unsigned size
)
478 AHCIState
*s
= opaque
;
480 if (addr
== s
->idp_offset
) {
481 /* index register - mask off reserved bits */
482 s
->idp_index
= (uint32_t)val
& ((AHCI_MEM_BAR_SIZE
- 1) & ~3);
483 } else if (addr
== s
->idp_offset
+ 4) {
484 /* data register - do memory write at location selected by index */
485 ahci_mem_write(opaque
, s
->idp_index
, val
, size
);
489 static const MemoryRegionOps ahci_idp_ops
= {
490 .read
= ahci_idp_read
,
491 .write
= ahci_idp_write
,
492 .endianness
= DEVICE_LITTLE_ENDIAN
,
496 static void ahci_reg_init(AHCIState
*s
)
500 s
->control_regs
.cap
= (s
->ports
- 1) |
501 (AHCI_NUM_COMMAND_SLOTS
<< 8) |
502 (AHCI_SUPPORTED_SPEED_GEN1
<< AHCI_SUPPORTED_SPEED
) |
503 HOST_CAP_NCQ
| HOST_CAP_AHCI
| HOST_CAP_64
;
505 s
->control_regs
.impl
= (1 << s
->ports
) - 1;
507 s
->control_regs
.version
= AHCI_VERSION_1_0
;
509 for (i
= 0; i
< s
->ports
; i
++) {
510 s
->dev
[i
].port_state
= STATE_RUN
;
514 static void check_cmd(AHCIState
*s
, int port
)
516 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
519 if ((pr
->cmd
& PORT_CMD_START
) && pr
->cmd_issue
) {
520 for (slot
= 0; (slot
< 32) && pr
->cmd_issue
; slot
++) {
521 if ((pr
->cmd_issue
& (1U << slot
)) &&
522 !handle_cmd(s
, port
, slot
)) {
523 pr
->cmd_issue
&= ~(1U << slot
);
529 static void ahci_check_cmd_bh(void *opaque
)
531 AHCIDevice
*ad
= opaque
;
533 qemu_bh_delete(ad
->check_bh
);
536 if ((ad
->busy_slot
!= -1) &&
537 !(ad
->port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
))) {
539 ad
->port_regs
.cmd_issue
&= ~(1 << ad
->busy_slot
);
543 check_cmd(ad
->hba
, ad
->port_no
);
546 static void ahci_init_d2h(AHCIDevice
*ad
)
548 IDEState
*ide_state
= &ad
->port
.ifs
[0];
549 AHCIPortRegs
*pr
= &ad
->port_regs
;
551 if (ad
->init_d2h_sent
) {
555 if (ahci_write_fis_d2h(ad
)) {
556 ad
->init_d2h_sent
= true;
557 /* We're emulating receiving the first Reg H2D Fis from the device;
558 * Update the SIG register, but otherwise proceed as normal. */
559 pr
->sig
= ((uint32_t)ide_state
->hcyl
<< 24) |
560 (ide_state
->lcyl
<< 16) |
561 (ide_state
->sector
<< 8) |
562 (ide_state
->nsector
& 0xFF);
566 static void ahci_set_signature(AHCIDevice
*ad
, uint32_t sig
)
568 IDEState
*s
= &ad
->port
.ifs
[0];
569 s
->hcyl
= sig
>> 24 & 0xFF;
570 s
->lcyl
= sig
>> 16 & 0xFF;
571 s
->sector
= sig
>> 8 & 0xFF;
572 s
->nsector
= sig
& 0xFF;
574 trace_ahci_set_signature(ad
->hba
, ad
->port_no
, s
->nsector
, s
->sector
,
575 s
->lcyl
, s
->hcyl
, sig
);
578 static void ahci_reset_port(AHCIState
*s
, int port
)
580 AHCIDevice
*d
= &s
->dev
[port
];
581 AHCIPortRegs
*pr
= &d
->port_regs
;
582 IDEState
*ide_state
= &d
->port
.ifs
[0];
585 trace_ahci_reset_port(s
, port
);
587 ide_bus_reset(&d
->port
);
588 ide_state
->ncq_queues
= AHCI_MAX_CMDS
;
594 pr
->sig
= 0xFFFFFFFF;
596 d
->init_d2h_sent
= false;
598 ide_state
= &s
->dev
[port
].port
.ifs
[0];
599 if (!ide_state
->blk
) {
603 /* reset ncq queue */
604 for (i
= 0; i
< AHCI_MAX_CMDS
; i
++) {
605 NCQTransferState
*ncq_tfs
= &s
->dev
[port
].ncq_tfs
[i
];
606 ncq_tfs
->halt
= false;
607 if (!ncq_tfs
->used
) {
611 if (ncq_tfs
->aiocb
) {
612 blk_aio_cancel(ncq_tfs
->aiocb
);
613 ncq_tfs
->aiocb
= NULL
;
616 /* Maybe we just finished the request thanks to blk_aio_cancel() */
617 if (!ncq_tfs
->used
) {
621 qemu_sglist_destroy(&ncq_tfs
->sglist
);
625 s
->dev
[port
].port_state
= STATE_RUN
;
626 if (ide_state
->drive_kind
== IDE_CD
) {
627 ahci_set_signature(d
, SATA_SIGNATURE_CDROM
);\
628 ide_state
->status
= SEEK_STAT
| WRERR_STAT
| READY_STAT
;
630 ahci_set_signature(d
, SATA_SIGNATURE_DISK
);
631 ide_state
->status
= SEEK_STAT
| WRERR_STAT
;
634 ide_state
->error
= 1;
638 /* Buffer pretty output based on a raw FIS structure. */
639 static char *ahci_pretty_buffer_fis(uint8_t *fis
, int cmd_len
)
642 GString
*s
= g_string_new("FIS:");
644 for (i
= 0; i
< cmd_len
; i
++) {
645 if ((i
& 0xf) == 0) {
646 g_string_append_printf(s
, "\n0x%02x: ", i
);
648 g_string_append_printf(s
, "%02x ", fis
[i
]);
650 g_string_append_c(s
, '\n');
652 return g_string_free(s
, FALSE
);
655 static bool ahci_map_fis_address(AHCIDevice
*ad
)
657 AHCIPortRegs
*pr
= &ad
->port_regs
;
658 map_page(ad
->hba
->as
, &ad
->res_fis
,
659 ((uint64_t)pr
->fis_addr_hi
<< 32) | pr
->fis_addr
, 256);
660 if (ad
->res_fis
!= NULL
) {
661 pr
->cmd
|= PORT_CMD_FIS_ON
;
665 pr
->cmd
&= ~PORT_CMD_FIS_ON
;
669 static void ahci_unmap_fis_address(AHCIDevice
*ad
)
671 if (ad
->res_fis
== NULL
) {
672 trace_ahci_unmap_fis_address_null(ad
->hba
, ad
->port_no
);
675 ad
->port_regs
.cmd
&= ~PORT_CMD_FIS_ON
;
676 dma_memory_unmap(ad
->hba
->as
, ad
->res_fis
, 256,
677 DMA_DIRECTION_FROM_DEVICE
, 256);
681 static bool ahci_map_clb_address(AHCIDevice
*ad
)
683 AHCIPortRegs
*pr
= &ad
->port_regs
;
685 map_page(ad
->hba
->as
, &ad
->lst
,
686 ((uint64_t)pr
->lst_addr_hi
<< 32) | pr
->lst_addr
, 1024);
687 if (ad
->lst
!= NULL
) {
688 pr
->cmd
|= PORT_CMD_LIST_ON
;
692 pr
->cmd
&= ~PORT_CMD_LIST_ON
;
696 static void ahci_unmap_clb_address(AHCIDevice
*ad
)
698 if (ad
->lst
== NULL
) {
699 trace_ahci_unmap_clb_address_null(ad
->hba
, ad
->port_no
);
702 ad
->port_regs
.cmd
&= ~PORT_CMD_LIST_ON
;
703 dma_memory_unmap(ad
->hba
->as
, ad
->lst
, 1024,
704 DMA_DIRECTION_FROM_DEVICE
, 1024);
708 static void ahci_write_fis_sdb(AHCIState
*s
, NCQTransferState
*ncq_tfs
)
710 AHCIDevice
*ad
= ncq_tfs
->drive
;
711 AHCIPortRegs
*pr
= &ad
->port_regs
;
716 !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
720 sdb_fis
= (SDBFIS
*)&ad
->res_fis
[RES_FIS_SDBFIS
];
721 ide_state
= &ad
->port
.ifs
[0];
723 sdb_fis
->type
= SATA_FIS_TYPE_SDB
;
724 /* Interrupt pending & Notification bit */
725 sdb_fis
->flags
= 0x40; /* Interrupt bit, always 1 for NCQ */
726 sdb_fis
->status
= ide_state
->status
& 0x77;
727 sdb_fis
->error
= ide_state
->error
;
728 /* update SAct field in SDB_FIS */
729 sdb_fis
->payload
= cpu_to_le32(ad
->finished
);
731 /* Update shadow registers (except BSY 0x80 and DRQ 0x08) */
732 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
733 (ad
->port
.ifs
[0].status
& 0x77) |
735 pr
->scr_act
&= ~ad
->finished
;
738 /* Trigger IRQ if interrupt bit is set (which currently, it always is) */
739 if (sdb_fis
->flags
& 0x40) {
740 ahci_trigger_irq(s
, ad
, AHCI_PORT_IRQ_BIT_SDBS
);
744 static void ahci_write_fis_pio(AHCIDevice
*ad
, uint16_t len
)
746 AHCIPortRegs
*pr
= &ad
->port_regs
;
748 IDEState
*s
= &ad
->port
.ifs
[0];
750 if (!ad
->res_fis
|| !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
754 pio_fis
= &ad
->res_fis
[RES_FIS_PSFIS
];
756 pio_fis
[0] = SATA_FIS_TYPE_PIO_SETUP
;
757 pio_fis
[1] = (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
758 pio_fis
[2] = s
->status
;
759 pio_fis
[3] = s
->error
;
761 pio_fis
[4] = s
->sector
;
762 pio_fis
[5] = s
->lcyl
;
763 pio_fis
[6] = s
->hcyl
;
764 pio_fis
[7] = s
->select
;
765 pio_fis
[8] = s
->hob_sector
;
766 pio_fis
[9] = s
->hob_lcyl
;
767 pio_fis
[10] = s
->hob_hcyl
;
769 pio_fis
[12] = s
->nsector
& 0xFF;
770 pio_fis
[13] = (s
->nsector
>> 8) & 0xFF;
772 pio_fis
[15] = s
->status
;
773 pio_fis
[16] = len
& 255;
774 pio_fis
[17] = len
>> 8;
778 /* Update shadow registers: */
779 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
780 ad
->port
.ifs
[0].status
;
782 if (pio_fis
[2] & ERR_STAT
) {
783 ahci_trigger_irq(ad
->hba
, ad
, AHCI_PORT_IRQ_BIT_TFES
);
786 ahci_trigger_irq(ad
->hba
, ad
, AHCI_PORT_IRQ_BIT_PSS
);
789 static bool ahci_write_fis_d2h(AHCIDevice
*ad
)
791 AHCIPortRegs
*pr
= &ad
->port_regs
;
794 IDEState
*s
= &ad
->port
.ifs
[0];
796 if (!ad
->res_fis
|| !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
800 d2h_fis
= &ad
->res_fis
[RES_FIS_RFIS
];
802 d2h_fis
[0] = SATA_FIS_TYPE_REGISTER_D2H
;
803 d2h_fis
[1] = (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
804 d2h_fis
[2] = s
->status
;
805 d2h_fis
[3] = s
->error
;
807 d2h_fis
[4] = s
->sector
;
808 d2h_fis
[5] = s
->lcyl
;
809 d2h_fis
[6] = s
->hcyl
;
810 d2h_fis
[7] = s
->select
;
811 d2h_fis
[8] = s
->hob_sector
;
812 d2h_fis
[9] = s
->hob_lcyl
;
813 d2h_fis
[10] = s
->hob_hcyl
;
815 d2h_fis
[12] = s
->nsector
& 0xFF;
816 d2h_fis
[13] = (s
->nsector
>> 8) & 0xFF;
817 for (i
= 14; i
< 20; i
++) {
821 /* Update shadow registers: */
822 pr
->tfdata
= (ad
->port
.ifs
[0].error
<< 8) |
823 ad
->port
.ifs
[0].status
;
825 if (d2h_fis
[2] & ERR_STAT
) {
826 ahci_trigger_irq(ad
->hba
, ad
, AHCI_PORT_IRQ_BIT_TFES
);
829 ahci_trigger_irq(ad
->hba
, ad
, AHCI_PORT_IRQ_BIT_DHRS
);
833 static int prdt_tbl_entry_size(const AHCI_SG
*tbl
)
835 /* flags_size is zero-based */
836 return (le32_to_cpu(tbl
->flags_size
) & AHCI_PRDT_SIZE_MASK
) + 1;
840 * Fetch entries in a guest-provided PRDT and convert it into a QEMU SGlist.
841 * @ad: The AHCIDevice for whom we are building the SGList.
842 * @sglist: The SGList target to add PRD entries to.
843 * @cmd: The AHCI Command Header that describes where the PRDT is.
844 * @limit: The remaining size of the S/ATA transaction, in bytes.
845 * @offset: The number of bytes already transferred, in bytes.
847 * The AHCI PRDT can describe up to 256GiB. S/ATA only support transactions of
848 * up to 32MiB as of ATA8-ACS3 rev 1b, assuming a 512 byte sector size. We stop
849 * building the sglist from the PRDT as soon as we hit @limit bytes,
850 * which is <= INT32_MAX/2GiB.
852 static int ahci_populate_sglist(AHCIDevice
*ad
, QEMUSGList
*sglist
,
853 AHCICmdHdr
*cmd
, int64_t limit
, uint64_t offset
)
855 uint16_t opts
= le16_to_cpu(cmd
->opts
);
856 uint16_t prdtl
= le16_to_cpu(cmd
->prdtl
);
857 uint64_t cfis_addr
= le64_to_cpu(cmd
->tbl_addr
);
858 uint64_t prdt_addr
= cfis_addr
+ 0x80;
859 dma_addr_t prdt_len
= (prdtl
* sizeof(AHCI_SG
));
860 dma_addr_t real_prdt_len
= prdt_len
;
866 int64_t off_pos
= -1;
868 IDEBus
*bus
= &ad
->port
;
869 BusState
*qbus
= BUS(bus
);
871 trace_ahci_populate_sglist(ad
->hba
, ad
->port_no
);
874 trace_ahci_populate_sglist_no_prdtl(ad
->hba
, ad
->port_no
, opts
);
879 if (!(prdt
= dma_memory_map(ad
->hba
->as
, prdt_addr
, &prdt_len
,
880 DMA_DIRECTION_TO_DEVICE
))){
881 trace_ahci_populate_sglist_no_map(ad
->hba
, ad
->port_no
);
885 if (prdt_len
< real_prdt_len
) {
886 trace_ahci_populate_sglist_short_map(ad
->hba
, ad
->port_no
);
891 /* Get entries in the PRDT, init a qemu sglist accordingly */
893 AHCI_SG
*tbl
= (AHCI_SG
*)prdt
;
895 for (i
= 0; i
< prdtl
; i
++) {
896 tbl_entry_size
= prdt_tbl_entry_size(&tbl
[i
]);
897 if (offset
< (sum
+ tbl_entry_size
)) {
899 off_pos
= offset
- sum
;
902 sum
+= tbl_entry_size
;
904 if ((off_idx
== -1) || (off_pos
< 0) || (off_pos
> tbl_entry_size
)) {
905 trace_ahci_populate_sglist_bad_offset(ad
->hba
, ad
->port_no
,
911 qemu_sglist_init(sglist
, qbus
->parent
, (prdtl
- off_idx
),
913 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[off_idx
].addr
) + off_pos
,
914 MIN(prdt_tbl_entry_size(&tbl
[off_idx
]) - off_pos
,
917 for (i
= off_idx
+ 1; i
< prdtl
&& sglist
->size
< limit
; i
++) {
918 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[i
].addr
),
919 MIN(prdt_tbl_entry_size(&tbl
[i
]),
920 limit
- sglist
->size
));
925 dma_memory_unmap(ad
->hba
->as
, prdt
, prdt_len
,
926 DMA_DIRECTION_TO_DEVICE
, prdt_len
);
930 static void ncq_err(NCQTransferState
*ncq_tfs
)
932 IDEState
*ide_state
= &ncq_tfs
->drive
->port
.ifs
[0];
934 ide_state
->error
= ABRT_ERR
;
935 ide_state
->status
= READY_STAT
| ERR_STAT
;
936 ncq_tfs
->drive
->port_regs
.scr_err
|= (1 << ncq_tfs
->tag
);
937 qemu_sglist_destroy(&ncq_tfs
->sglist
);
941 static void ncq_finish(NCQTransferState
*ncq_tfs
)
943 /* If we didn't error out, set our finished bit. Errored commands
944 * do not get a bit set for the SDB FIS ACT register, nor do they
945 * clear the outstanding bit in scr_act (PxSACT). */
946 if (!(ncq_tfs
->drive
->port_regs
.scr_err
& (1 << ncq_tfs
->tag
))) {
947 ncq_tfs
->drive
->finished
|= (1 << ncq_tfs
->tag
);
950 ahci_write_fis_sdb(ncq_tfs
->drive
->hba
, ncq_tfs
);
952 trace_ncq_finish(ncq_tfs
->drive
->hba
, ncq_tfs
->drive
->port_no
,
955 block_acct_done(blk_get_stats(ncq_tfs
->drive
->port
.ifs
[0].blk
),
957 qemu_sglist_destroy(&ncq_tfs
->sglist
);
961 static void ncq_cb(void *opaque
, int ret
)
963 NCQTransferState
*ncq_tfs
= (NCQTransferState
*)opaque
;
964 IDEState
*ide_state
= &ncq_tfs
->drive
->port
.ifs
[0];
966 ncq_tfs
->aiocb
= NULL
;
967 if (ret
== -ECANCELED
) {
972 bool is_read
= ncq_tfs
->cmd
== READ_FPDMA_QUEUED
;
973 BlockErrorAction action
= blk_get_error_action(ide_state
->blk
,
975 if (action
== BLOCK_ERROR_ACTION_STOP
) {
976 ncq_tfs
->halt
= true;
977 ide_state
->bus
->error_status
= IDE_RETRY_HBA
;
978 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
981 blk_error_action(ide_state
->blk
, action
, is_read
, -ret
);
983 ide_state
->status
= READY_STAT
| SEEK_STAT
;
986 if (!ncq_tfs
->halt
) {
991 static int is_ncq(uint8_t ata_cmd
)
993 /* Based on SATA 3.2 section 13.6.3.2 */
995 case READ_FPDMA_QUEUED
:
996 case WRITE_FPDMA_QUEUED
:
998 case RECEIVE_FPDMA_QUEUED
:
999 case SEND_FPDMA_QUEUED
:
1006 static void execute_ncq_command(NCQTransferState
*ncq_tfs
)
1008 AHCIDevice
*ad
= ncq_tfs
->drive
;
1009 IDEState
*ide_state
= &ad
->port
.ifs
[0];
1010 int port
= ad
->port_no
;
1012 g_assert(is_ncq(ncq_tfs
->cmd
));
1013 ncq_tfs
->halt
= false;
1015 switch (ncq_tfs
->cmd
) {
1016 case READ_FPDMA_QUEUED
:
1017 trace_execute_ncq_command_read(ad
->hba
, port
, ncq_tfs
->tag
,
1018 ncq_tfs
->sector_count
, ncq_tfs
->lba
);
1019 dma_acct_start(ide_state
->blk
, &ncq_tfs
->acct
,
1020 &ncq_tfs
->sglist
, BLOCK_ACCT_READ
);
1021 ncq_tfs
->aiocb
= dma_blk_read(ide_state
->blk
, &ncq_tfs
->sglist
,
1022 ncq_tfs
->lba
<< BDRV_SECTOR_BITS
,
1026 case WRITE_FPDMA_QUEUED
:
1027 trace_execute_ncq_command_read(ad
->hba
, port
, ncq_tfs
->tag
,
1028 ncq_tfs
->sector_count
, ncq_tfs
->lba
);
1029 dma_acct_start(ide_state
->blk
, &ncq_tfs
->acct
,
1030 &ncq_tfs
->sglist
, BLOCK_ACCT_WRITE
);
1031 ncq_tfs
->aiocb
= dma_blk_write(ide_state
->blk
, &ncq_tfs
->sglist
,
1032 ncq_tfs
->lba
<< BDRV_SECTOR_BITS
,
1037 trace_execute_ncq_command_unsup(ad
->hba
, port
,
1038 ncq_tfs
->tag
, ncq_tfs
->cmd
);
1044 static void process_ncq_command(AHCIState
*s
, int port
, uint8_t *cmd_fis
,
1047 AHCIDevice
*ad
= &s
->dev
[port
];
1048 NCQFrame
*ncq_fis
= (NCQFrame
*)cmd_fis
;
1049 uint8_t tag
= ncq_fis
->tag
>> 3;
1050 NCQTransferState
*ncq_tfs
= &ad
->ncq_tfs
[tag
];
1053 g_assert(is_ncq(ncq_fis
->command
));
1054 if (ncq_tfs
->used
) {
1055 /* error - already in use */
1056 fprintf(stderr
, "%s: tag %d already used\n", __FUNCTION__
, tag
);
1061 ncq_tfs
->drive
= ad
;
1062 ncq_tfs
->slot
= slot
;
1063 ncq_tfs
->cmdh
= &((AHCICmdHdr
*)ad
->lst
)[slot
];
1064 ncq_tfs
->cmd
= ncq_fis
->command
;
1065 ncq_tfs
->lba
= ((uint64_t)ncq_fis
->lba5
<< 40) |
1066 ((uint64_t)ncq_fis
->lba4
<< 32) |
1067 ((uint64_t)ncq_fis
->lba3
<< 24) |
1068 ((uint64_t)ncq_fis
->lba2
<< 16) |
1069 ((uint64_t)ncq_fis
->lba1
<< 8) |
1070 (uint64_t)ncq_fis
->lba0
;
1073 /* Sanity-check the NCQ packet */
1075 trace_process_ncq_command_mismatch(s
, port
, tag
, slot
);
1078 if (ncq_fis
->aux0
|| ncq_fis
->aux1
|| ncq_fis
->aux2
|| ncq_fis
->aux3
) {
1079 trace_process_ncq_command_aux(s
, port
, tag
);
1081 if (ncq_fis
->prio
|| ncq_fis
->icc
) {
1082 trace_process_ncq_command_prioicc(s
, port
, tag
);
1084 if (ncq_fis
->fua
& NCQ_FIS_FUA_MASK
) {
1085 trace_process_ncq_command_fua(s
, port
, tag
);
1087 if (ncq_fis
->tag
& NCQ_FIS_RARC_MASK
) {
1088 trace_process_ncq_command_rarc(s
, port
, tag
);
1091 ncq_tfs
->sector_count
= ((ncq_fis
->sector_count_high
<< 8) |
1092 ncq_fis
->sector_count_low
);
1093 if (!ncq_tfs
->sector_count
) {
1094 ncq_tfs
->sector_count
= 0x10000;
1096 size
= ncq_tfs
->sector_count
* 512;
1097 ahci_populate_sglist(ad
, &ncq_tfs
->sglist
, ncq_tfs
->cmdh
, size
, 0);
1099 if (ncq_tfs
->sglist
.size
< size
) {
1100 error_report("ahci: PRDT length for NCQ command (0x%zx) "
1101 "is smaller than the requested size (0x%zx)",
1102 ncq_tfs
->sglist
.size
, size
);
1104 ahci_trigger_irq(ad
->hba
, ad
, AHCI_PORT_IRQ_BIT_OFS
);
1106 } else if (ncq_tfs
->sglist
.size
!= size
) {
1107 trace_process_ncq_command_large(s
, port
, tag
,
1108 ncq_tfs
->sglist
.size
, size
);
1111 trace_process_ncq_command(s
, port
, tag
,
1114 ncq_tfs
->lba
+ ncq_tfs
->sector_count
- 1);
1115 execute_ncq_command(ncq_tfs
);
1118 static AHCICmdHdr
*get_cmd_header(AHCIState
*s
, uint8_t port
, uint8_t slot
)
1120 if (port
>= s
->ports
|| slot
>= AHCI_MAX_CMDS
) {
1124 return s
->dev
[port
].lst
? &((AHCICmdHdr
*)s
->dev
[port
].lst
)[slot
] : NULL
;
1127 static void handle_reg_h2d_fis(AHCIState
*s
, int port
,
1128 uint8_t slot
, uint8_t *cmd_fis
)
1130 IDEState
*ide_state
= &s
->dev
[port
].port
.ifs
[0];
1131 AHCICmdHdr
*cmd
= get_cmd_header(s
, port
, slot
);
1132 uint16_t opts
= le16_to_cpu(cmd
->opts
);
1134 if (cmd_fis
[1] & 0x0F) {
1135 trace_handle_reg_h2d_fis_pmp(s
, port
, cmd_fis
[1],
1136 cmd_fis
[2], cmd_fis
[3]);
1140 if (cmd_fis
[1] & 0x70) {
1141 trace_handle_reg_h2d_fis_res(s
, port
, cmd_fis
[1],
1142 cmd_fis
[2], cmd_fis
[3]);
1146 if (!(cmd_fis
[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER
)) {
1147 switch (s
->dev
[port
].port_state
) {
1149 if (cmd_fis
[15] & ATA_SRST
) {
1150 s
->dev
[port
].port_state
= STATE_RESET
;
1154 if (!(cmd_fis
[15] & ATA_SRST
)) {
1155 ahci_reset_port(s
, port
);
1162 /* Check for NCQ command */
1163 if (is_ncq(cmd_fis
[2])) {
1164 process_ncq_command(s
, port
, cmd_fis
, slot
);
1168 /* Decompose the FIS:
1169 * AHCI does not interpret FIS packets, it only forwards them.
1170 * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
1171 * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
1173 * ATA4 describes sector number for LBA28/CHS commands.
1174 * ATA6 describes sector number for LBA48 commands.
1175 * ATA8 deprecates CHS fully, describing only LBA28/48.
1177 * We dutifully convert the FIS into IDE registers, and allow the
1178 * core layer to interpret them as needed. */
1179 ide_state
->feature
= cmd_fis
[3];
1180 ide_state
->sector
= cmd_fis
[4]; /* LBA 7:0 */
1181 ide_state
->lcyl
= cmd_fis
[5]; /* LBA 15:8 */
1182 ide_state
->hcyl
= cmd_fis
[6]; /* LBA 23:16 */
1183 ide_state
->select
= cmd_fis
[7]; /* LBA 27:24 (LBA28) */
1184 ide_state
->hob_sector
= cmd_fis
[8]; /* LBA 31:24 */
1185 ide_state
->hob_lcyl
= cmd_fis
[9]; /* LBA 39:32 */
1186 ide_state
->hob_hcyl
= cmd_fis
[10]; /* LBA 47:40 */
1187 ide_state
->hob_feature
= cmd_fis
[11];
1188 ide_state
->nsector
= (int64_t)((cmd_fis
[13] << 8) | cmd_fis
[12]);
1189 /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
1190 /* 15: Only valid when UPDATE_COMMAND not set. */
1192 /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
1193 * table to ide_state->io_buffer */
1194 if (opts
& AHCI_CMD_ATAPI
) {
1195 memcpy(ide_state
->io_buffer
, &cmd_fis
[AHCI_COMMAND_TABLE_ACMD
], 0x10);
1196 if (trace_event_get_state_backends(TRACE_HANDLE_REG_H2D_FIS_DUMP
)) {
1197 char *pretty_fis
= ahci_pretty_buffer_fis(ide_state
->io_buffer
, 0x10);
1198 trace_handle_reg_h2d_fis_dump(s
, port
, pretty_fis
);
1201 s
->dev
[port
].done_atapi_packet
= false;
1202 /* XXX send PIO setup FIS */
1205 ide_state
->error
= 0;
1207 /* Reset transferred byte counter */
1210 /* We're ready to process the command in FIS byte 2. */
1211 ide_exec_cmd(&s
->dev
[port
].port
, cmd_fis
[2]);
1214 static int handle_cmd(AHCIState
*s
, int port
, uint8_t slot
)
1216 IDEState
*ide_state
;
1222 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
1223 /* Engine currently busy, try again later */
1224 trace_handle_cmd_busy(s
, port
);
1228 if (!s
->dev
[port
].lst
) {
1229 trace_handle_cmd_nolist(s
, port
);
1232 cmd
= get_cmd_header(s
, port
, slot
);
1233 /* remember current slot handle for later */
1234 s
->dev
[port
].cur_cmd
= cmd
;
1236 /* The device we are working for */
1237 ide_state
= &s
->dev
[port
].port
.ifs
[0];
1238 if (!ide_state
->blk
) {
1239 trace_handle_cmd_badport(s
, port
);
1243 tbl_addr
= le64_to_cpu(cmd
->tbl_addr
);
1245 cmd_fis
= dma_memory_map(s
->as
, tbl_addr
, &cmd_len
,
1246 DMA_DIRECTION_FROM_DEVICE
);
1248 trace_handle_cmd_badfis(s
, port
);
1250 } else if (cmd_len
!= 0x80) {
1251 ahci_trigger_irq(s
, &s
->dev
[port
], AHCI_PORT_IRQ_BIT_HBFS
);
1252 trace_handle_cmd_badmap(s
, port
, cmd_len
);
1255 if (trace_event_get_state_backends(TRACE_HANDLE_CMD_FIS_DUMP
)) {
1256 char *pretty_fis
= ahci_pretty_buffer_fis(cmd_fis
, 0x80);
1257 trace_handle_cmd_fis_dump(s
, port
, pretty_fis
);
1260 switch (cmd_fis
[0]) {
1261 case SATA_FIS_TYPE_REGISTER_H2D
:
1262 handle_reg_h2d_fis(s
, port
, slot
, cmd_fis
);
1265 trace_handle_cmd_unhandled_fis(s
, port
,
1266 cmd_fis
[0], cmd_fis
[1], cmd_fis
[2]);
1271 dma_memory_unmap(s
->as
, cmd_fis
, cmd_len
, DMA_DIRECTION_FROM_DEVICE
,
1274 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
1275 /* async command, complete later */
1276 s
->dev
[port
].busy_slot
= slot
;
1280 /* done handling the command */
1284 /* DMA dev <-> ram */
1285 static void ahci_start_transfer(IDEDMA
*dma
)
1287 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1288 IDEState
*s
= &ad
->port
.ifs
[0];
1289 uint32_t size
= (uint32_t)(s
->data_end
- s
->data_ptr
);
1290 /* write == ram -> device */
1291 uint16_t opts
= le16_to_cpu(ad
->cur_cmd
->opts
);
1292 int is_write
= opts
& AHCI_CMD_WRITE
;
1293 int is_atapi
= opts
& AHCI_CMD_ATAPI
;
1296 if (is_atapi
&& !ad
->done_atapi_packet
) {
1297 /* already prepopulated iobuffer */
1298 ad
->done_atapi_packet
= true;
1303 if (ahci_dma_prepare_buf(dma
, size
)) {
1307 trace_ahci_start_transfer(ad
->hba
, ad
->port_no
, is_write
? "writ" : "read",
1308 size
, is_atapi
? "atapi" : "ata",
1309 has_sglist
? "" : "o");
1311 if (has_sglist
&& size
) {
1313 dma_buf_write(s
->data_ptr
, size
, &s
->sg
);
1315 dma_buf_read(s
->data_ptr
, size
, &s
->sg
);
1320 /* declare that we processed everything */
1321 s
->data_ptr
= s
->data_end
;
1323 /* Update number of transferred bytes, destroy sglist */
1324 dma_buf_commit(s
, size
);
1326 s
->end_transfer_func(s
);
1328 if (!(s
->status
& DRQ_STAT
)) {
1329 /* done with PIO send/receive */
1330 ahci_write_fis_pio(ad
, le32_to_cpu(ad
->cur_cmd
->status
));
1334 static void ahci_start_dma(IDEDMA
*dma
, IDEState
*s
,
1335 BlockCompletionFunc
*dma_cb
)
1337 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1338 trace_ahci_start_dma(ad
->hba
, ad
->port_no
);
1339 s
->io_buffer_offset
= 0;
1343 static void ahci_restart_dma(IDEDMA
*dma
)
1345 /* Nothing to do, ahci_start_dma already resets s->io_buffer_offset. */
1349 * IDE/PIO restarts are handled by the core layer, but NCQ commands
1350 * need an extra kick from the AHCI HBA.
1352 static void ahci_restart(IDEDMA
*dma
)
1354 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1357 for (i
= 0; i
< AHCI_MAX_CMDS
; i
++) {
1358 NCQTransferState
*ncq_tfs
= &ad
->ncq_tfs
[i
];
1359 if (ncq_tfs
->halt
) {
1360 execute_ncq_command(ncq_tfs
);
1366 * Called in DMA and PIO R/W chains to read the PRDT.
1367 * Not shared with NCQ pathways.
1369 static int32_t ahci_dma_prepare_buf(IDEDMA
*dma
, int32_t limit
)
1371 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1372 IDEState
*s
= &ad
->port
.ifs
[0];
1374 if (ahci_populate_sglist(ad
, &s
->sg
, ad
->cur_cmd
,
1375 limit
, s
->io_buffer_offset
) == -1) {
1376 trace_ahci_dma_prepare_buf_fail(ad
->hba
, ad
->port_no
);
1379 s
->io_buffer_size
= s
->sg
.size
;
1381 trace_ahci_dma_prepare_buf(ad
->hba
, ad
->port_no
, limit
, s
->io_buffer_size
);
1382 return s
->io_buffer_size
;
1386 * Updates the command header with a bytes-read value.
1387 * Called via dma_buf_commit, for both DMA and PIO paths.
1388 * sglist destruction is handled within dma_buf_commit.
1390 static void ahci_commit_buf(IDEDMA
*dma
, uint32_t tx_bytes
)
1392 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1394 tx_bytes
+= le32_to_cpu(ad
->cur_cmd
->status
);
1395 ad
->cur_cmd
->status
= cpu_to_le32(tx_bytes
);
1398 static int ahci_dma_rw_buf(IDEDMA
*dma
, int is_write
)
1400 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1401 IDEState
*s
= &ad
->port
.ifs
[0];
1402 uint8_t *p
= s
->io_buffer
+ s
->io_buffer_index
;
1403 int l
= s
->io_buffer_size
- s
->io_buffer_index
;
1405 if (ahci_populate_sglist(ad
, &s
->sg
, ad
->cur_cmd
, l
, s
->io_buffer_offset
)) {
1410 dma_buf_read(p
, l
, &s
->sg
);
1412 dma_buf_write(p
, l
, &s
->sg
);
1415 /* free sglist, update byte count */
1416 dma_buf_commit(s
, l
);
1417 s
->io_buffer_index
+= l
;
1419 trace_ahci_dma_rw_buf(ad
->hba
, ad
->port_no
, l
);
1423 static void ahci_cmd_done(IDEDMA
*dma
)
1425 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1427 trace_ahci_cmd_done(ad
->hba
, ad
->port_no
);
1429 /* update d2h status */
1430 ahci_write_fis_d2h(ad
);
1432 if (!ad
->check_bh
) {
1433 /* maybe we still have something to process, check later */
1434 ad
->check_bh
= qemu_bh_new(ahci_check_cmd_bh
, ad
);
1435 qemu_bh_schedule(ad
->check_bh
);
1439 static void ahci_irq_set(void *opaque
, int n
, int level
)
1443 static const IDEDMAOps ahci_dma_ops
= {
1444 .start_dma
= ahci_start_dma
,
1445 .restart
= ahci_restart
,
1446 .restart_dma
= ahci_restart_dma
,
1447 .start_transfer
= ahci_start_transfer
,
1448 .prepare_buf
= ahci_dma_prepare_buf
,
1449 .commit_buf
= ahci_commit_buf
,
1450 .rw_buf
= ahci_dma_rw_buf
,
1451 .cmd_done
= ahci_cmd_done
,
1454 void ahci_init(AHCIState
*s
, DeviceState
*qdev
)
1456 s
->container
= qdev
;
1457 /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
1458 memory_region_init_io(&s
->mem
, OBJECT(qdev
), &ahci_mem_ops
, s
,
1459 "ahci", AHCI_MEM_BAR_SIZE
);
1460 memory_region_init_io(&s
->idp
, OBJECT(qdev
), &ahci_idp_ops
, s
,
1464 void ahci_realize(AHCIState
*s
, DeviceState
*qdev
, AddressSpace
*as
, int ports
)
1471 s
->dev
= g_new0(AHCIDevice
, ports
);
1473 irqs
= qemu_allocate_irqs(ahci_irq_set
, s
, s
->ports
);
1474 for (i
= 0; i
< s
->ports
; i
++) {
1475 AHCIDevice
*ad
= &s
->dev
[i
];
1477 ide_bus_new(&ad
->port
, sizeof(ad
->port
), qdev
, i
, 1);
1478 ide_init2(&ad
->port
, irqs
[i
]);
1482 ad
->port
.dma
= &ad
->dma
;
1483 ad
->port
.dma
->ops
= &ahci_dma_ops
;
1484 ide_register_restart_cb(&ad
->port
);
1489 void ahci_uninit(AHCIState
*s
)
1493 for (i
= 0; i
< s
->ports
; i
++) {
1494 AHCIDevice
*ad
= &s
->dev
[i
];
1496 for (j
= 0; j
< 2; j
++) {
1497 IDEState
*s
= &ad
->port
.ifs
[j
];
1501 object_unparent(OBJECT(&ad
->port
));
1507 void ahci_reset(AHCIState
*s
)
1512 trace_ahci_reset(s
);
1514 s
->control_regs
.irqstatus
= 0;
1516 * The implementation of this bit is dependent upon the value of the
1517 * CAP.SAM bit. If CAP.SAM is '0', then GHC.AE shall be read-write and
1518 * shall have a reset value of '0'. If CAP.SAM is '1', then AE shall be
1519 * read-only and shall have a reset value of '1'.
1521 * We set HOST_CAP_AHCI so we must enable AHCI at reset.
1523 s
->control_regs
.ghc
= HOST_CTL_AHCI_EN
;
1525 for (i
= 0; i
< s
->ports
; i
++) {
1526 pr
= &s
->dev
[i
].port_regs
;
1530 pr
->cmd
= PORT_CMD_SPIN_UP
| PORT_CMD_POWER_ON
;
1531 ahci_reset_port(s
, i
);
1535 static const VMStateDescription vmstate_ncq_tfs
= {
1536 .name
= "ncq state",
1538 .fields
= (VMStateField
[]) {
1539 VMSTATE_UINT32(sector_count
, NCQTransferState
),
1540 VMSTATE_UINT64(lba
, NCQTransferState
),
1541 VMSTATE_UINT8(tag
, NCQTransferState
),
1542 VMSTATE_UINT8(cmd
, NCQTransferState
),
1543 VMSTATE_UINT8(slot
, NCQTransferState
),
1544 VMSTATE_BOOL(used
, NCQTransferState
),
1545 VMSTATE_BOOL(halt
, NCQTransferState
),
1546 VMSTATE_END_OF_LIST()
1550 static const VMStateDescription vmstate_ahci_device
= {
1551 .name
= "ahci port",
1553 .fields
= (VMStateField
[]) {
1554 VMSTATE_IDE_BUS(port
, AHCIDevice
),
1555 VMSTATE_IDE_DRIVE(port
.ifs
[0], AHCIDevice
),
1556 VMSTATE_UINT32(port_state
, AHCIDevice
),
1557 VMSTATE_UINT32(finished
, AHCIDevice
),
1558 VMSTATE_UINT32(port_regs
.lst_addr
, AHCIDevice
),
1559 VMSTATE_UINT32(port_regs
.lst_addr_hi
, AHCIDevice
),
1560 VMSTATE_UINT32(port_regs
.fis_addr
, AHCIDevice
),
1561 VMSTATE_UINT32(port_regs
.fis_addr_hi
, AHCIDevice
),
1562 VMSTATE_UINT32(port_regs
.irq_stat
, AHCIDevice
),
1563 VMSTATE_UINT32(port_regs
.irq_mask
, AHCIDevice
),
1564 VMSTATE_UINT32(port_regs
.cmd
, AHCIDevice
),
1565 VMSTATE_UINT32(port_regs
.tfdata
, AHCIDevice
),
1566 VMSTATE_UINT32(port_regs
.sig
, AHCIDevice
),
1567 VMSTATE_UINT32(port_regs
.scr_stat
, AHCIDevice
),
1568 VMSTATE_UINT32(port_regs
.scr_ctl
, AHCIDevice
),
1569 VMSTATE_UINT32(port_regs
.scr_err
, AHCIDevice
),
1570 VMSTATE_UINT32(port_regs
.scr_act
, AHCIDevice
),
1571 VMSTATE_UINT32(port_regs
.cmd_issue
, AHCIDevice
),
1572 VMSTATE_BOOL(done_atapi_packet
, AHCIDevice
),
1573 VMSTATE_INT32(busy_slot
, AHCIDevice
),
1574 VMSTATE_BOOL(init_d2h_sent
, AHCIDevice
),
1575 VMSTATE_STRUCT_ARRAY(ncq_tfs
, AHCIDevice
, AHCI_MAX_CMDS
,
1576 1, vmstate_ncq_tfs
, NCQTransferState
),
1577 VMSTATE_END_OF_LIST()
1581 static int ahci_state_post_load(void *opaque
, int version_id
)
1584 struct AHCIDevice
*ad
;
1585 NCQTransferState
*ncq_tfs
;
1587 AHCIState
*s
= opaque
;
1589 for (i
= 0; i
< s
->ports
; i
++) {
1591 pr
= &ad
->port_regs
;
1593 if (!(pr
->cmd
& PORT_CMD_START
) && (pr
->cmd
& PORT_CMD_LIST_ON
)) {
1594 error_report("AHCI: DMA engine should be off, but status bit "
1595 "indicates it is still running.");
1598 if (!(pr
->cmd
& PORT_CMD_FIS_RX
) && (pr
->cmd
& PORT_CMD_FIS_ON
)) {
1599 error_report("AHCI: FIS RX engine should be off, but status bit "
1600 "indicates it is still running.");
1604 /* After a migrate, the DMA/FIS engines are "off" and
1605 * need to be conditionally restarted */
1606 pr
->cmd
&= ~(PORT_CMD_LIST_ON
| PORT_CMD_FIS_ON
);
1607 if (ahci_cond_start_engines(ad
) != 0) {
1611 for (j
= 0; j
< AHCI_MAX_CMDS
; j
++) {
1612 ncq_tfs
= &ad
->ncq_tfs
[j
];
1613 ncq_tfs
->drive
= ad
;
1615 if (ncq_tfs
->used
!= ncq_tfs
->halt
) {
1618 if (!ncq_tfs
->halt
) {
1621 if (!is_ncq(ncq_tfs
->cmd
)) {
1624 if (ncq_tfs
->slot
!= ncq_tfs
->tag
) {
1627 /* If ncq_tfs->halt is justly set, the engine should be engaged,
1628 * and the command list buffer should be mapped. */
1629 ncq_tfs
->cmdh
= get_cmd_header(s
, i
, ncq_tfs
->slot
);
1630 if (!ncq_tfs
->cmdh
) {
1633 ahci_populate_sglist(ncq_tfs
->drive
, &ncq_tfs
->sglist
,
1634 ncq_tfs
->cmdh
, ncq_tfs
->sector_count
* 512,
1636 if (ncq_tfs
->sector_count
!= ncq_tfs
->sglist
.size
>> 9) {
1643 * If an error is present, ad->busy_slot will be valid and not -1.
1644 * In this case, an operation is waiting to resume and will re-check
1645 * for additional AHCI commands to execute upon completion.
1647 * In the case where no error was present, busy_slot will be -1,
1648 * and we should check to see if there are additional commands waiting.
1650 if (ad
->busy_slot
== -1) {
1653 /* We are in the middle of a command, and may need to access
1654 * the command header in guest memory again. */
1655 if (ad
->busy_slot
< 0 || ad
->busy_slot
>= AHCI_MAX_CMDS
) {
1658 ad
->cur_cmd
= get_cmd_header(s
, i
, ad
->busy_slot
);
1665 const VMStateDescription vmstate_ahci
= {
1668 .post_load
= ahci_state_post_load
,
1669 .fields
= (VMStateField
[]) {
1670 VMSTATE_STRUCT_VARRAY_POINTER_INT32(dev
, AHCIState
, ports
,
1671 vmstate_ahci_device
, AHCIDevice
),
1672 VMSTATE_UINT32(control_regs
.cap
, AHCIState
),
1673 VMSTATE_UINT32(control_regs
.ghc
, AHCIState
),
1674 VMSTATE_UINT32(control_regs
.irqstatus
, AHCIState
),
1675 VMSTATE_UINT32(control_regs
.impl
, AHCIState
),
1676 VMSTATE_UINT32(control_regs
.version
, AHCIState
),
1677 VMSTATE_UINT32(idp_index
, AHCIState
),
1678 VMSTATE_INT32_EQUAL(ports
, AHCIState
, NULL
),
1679 VMSTATE_END_OF_LIST()
1683 static const VMStateDescription vmstate_sysbus_ahci
= {
1684 .name
= "sysbus-ahci",
1685 .fields
= (VMStateField
[]) {
1686 VMSTATE_AHCI(ahci
, SysbusAHCIState
),
1687 VMSTATE_END_OF_LIST()
1691 static void sysbus_ahci_reset(DeviceState
*dev
)
1693 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1695 ahci_reset(&s
->ahci
);
1698 static void sysbus_ahci_init(Object
*obj
)
1700 SysbusAHCIState
*s
= SYSBUS_AHCI(obj
);
1701 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1703 ahci_init(&s
->ahci
, DEVICE(obj
));
1705 sysbus_init_mmio(sbd
, &s
->ahci
.mem
);
1706 sysbus_init_irq(sbd
, &s
->ahci
.irq
);
1709 static void sysbus_ahci_realize(DeviceState
*dev
, Error
**errp
)
1711 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1713 ahci_realize(&s
->ahci
, dev
, &address_space_memory
, s
->num_ports
);
1716 static Property sysbus_ahci_properties
[] = {
1717 DEFINE_PROP_UINT32("num-ports", SysbusAHCIState
, num_ports
, 1),
1718 DEFINE_PROP_END_OF_LIST(),
1721 static void sysbus_ahci_class_init(ObjectClass
*klass
, void *data
)
1723 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1725 dc
->realize
= sysbus_ahci_realize
;
1726 dc
->vmsd
= &vmstate_sysbus_ahci
;
1727 dc
->props
= sysbus_ahci_properties
;
1728 dc
->reset
= sysbus_ahci_reset
;
1729 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1732 static const TypeInfo sysbus_ahci_info
= {
1733 .name
= TYPE_SYSBUS_AHCI
,
1734 .parent
= TYPE_SYS_BUS_DEVICE
,
1735 .instance_size
= sizeof(SysbusAHCIState
),
1736 .instance_init
= sysbus_ahci_init
,
1737 .class_init
= sysbus_ahci_class_init
,
1740 #define ALLWINNER_AHCI_BISTAFR ((0xa0 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1741 #define ALLWINNER_AHCI_BISTCR ((0xa4 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1742 #define ALLWINNER_AHCI_BISTFCTR ((0xa8 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1743 #define ALLWINNER_AHCI_BISTSR ((0xac - ALLWINNER_AHCI_MMIO_OFF) / 4)
1744 #define ALLWINNER_AHCI_BISTDECR ((0xb0 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1745 #define ALLWINNER_AHCI_DIAGNR0 ((0xb4 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1746 #define ALLWINNER_AHCI_DIAGNR1 ((0xb8 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1747 #define ALLWINNER_AHCI_OOBR ((0xbc - ALLWINNER_AHCI_MMIO_OFF) / 4)
1748 #define ALLWINNER_AHCI_PHYCS0R ((0xc0 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1749 #define ALLWINNER_AHCI_PHYCS1R ((0xc4 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1750 #define ALLWINNER_AHCI_PHYCS2R ((0xc8 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1751 #define ALLWINNER_AHCI_TIMER1MS ((0xe0 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1752 #define ALLWINNER_AHCI_GPARAM1R ((0xe8 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1753 #define ALLWINNER_AHCI_GPARAM2R ((0xec - ALLWINNER_AHCI_MMIO_OFF) / 4)
1754 #define ALLWINNER_AHCI_PPARAMR ((0xf0 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1755 #define ALLWINNER_AHCI_TESTR ((0xf4 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1756 #define ALLWINNER_AHCI_VERSIONR ((0xf8 - ALLWINNER_AHCI_MMIO_OFF) / 4)
1757 #define ALLWINNER_AHCI_IDR ((0xfc - ALLWINNER_AHCI_MMIO_OFF) / 4)
1758 #define ALLWINNER_AHCI_RWCR ((0xfc - ALLWINNER_AHCI_MMIO_OFF) / 4)
1760 static uint64_t allwinner_ahci_mem_read(void *opaque
, hwaddr addr
,
1763 AllwinnerAHCIState
*a
= opaque
;
1764 AHCIState
*s
= &(SYSBUS_AHCI(a
)->ahci
);
1765 uint64_t val
= a
->regs
[addr
/4];
1768 case ALLWINNER_AHCI_PHYCS0R
:
1771 case ALLWINNER_AHCI_PHYCS2R
:
1772 val
&= ~(0x1 << 24);
1775 trace_allwinner_ahci_mem_read(s
, a
, addr
, val
, size
);
1779 static void allwinner_ahci_mem_write(void *opaque
, hwaddr addr
,
1780 uint64_t val
, unsigned size
)
1782 AllwinnerAHCIState
*a
= opaque
;
1783 AHCIState
*s
= &(SYSBUS_AHCI(a
)->ahci
);
1785 trace_allwinner_ahci_mem_write(s
, a
, addr
, val
, size
);
1786 a
->regs
[addr
/4] = val
;
1789 static const MemoryRegionOps allwinner_ahci_mem_ops
= {
1790 .read
= allwinner_ahci_mem_read
,
1791 .write
= allwinner_ahci_mem_write
,
1792 .valid
.min_access_size
= 4,
1793 .valid
.max_access_size
= 4,
1794 .endianness
= DEVICE_LITTLE_ENDIAN
,
1797 static void allwinner_ahci_init(Object
*obj
)
1799 SysbusAHCIState
*s
= SYSBUS_AHCI(obj
);
1800 AllwinnerAHCIState
*a
= ALLWINNER_AHCI(obj
);
1802 memory_region_init_io(&a
->mmio
, OBJECT(obj
), &allwinner_ahci_mem_ops
, a
,
1803 "allwinner-ahci", ALLWINNER_AHCI_MMIO_SIZE
);
1804 memory_region_add_subregion(&s
->ahci
.mem
, ALLWINNER_AHCI_MMIO_OFF
,
1808 static const VMStateDescription vmstate_allwinner_ahci
= {
1809 .name
= "allwinner-ahci",
1811 .minimum_version_id
= 1,
1812 .fields
= (VMStateField
[]) {
1813 VMSTATE_UINT32_ARRAY(regs
, AllwinnerAHCIState
,
1814 ALLWINNER_AHCI_MMIO_SIZE
/4),
1815 VMSTATE_END_OF_LIST()
1819 static void allwinner_ahci_class_init(ObjectClass
*klass
, void *data
)
1821 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1823 dc
->vmsd
= &vmstate_allwinner_ahci
;
1826 static const TypeInfo allwinner_ahci_info
= {
1827 .name
= TYPE_ALLWINNER_AHCI
,
1828 .parent
= TYPE_SYSBUS_AHCI
,
1829 .instance_size
= sizeof(AllwinnerAHCIState
),
1830 .instance_init
= allwinner_ahci_init
,
1831 .class_init
= allwinner_ahci_class_init
,
1834 static void sysbus_ahci_register_types(void)
1836 type_register_static(&sysbus_ahci_info
);
1837 type_register_static(&allwinner_ahci_info
);
1840 type_init(sysbus_ahci_register_types
)
1842 int32_t ahci_get_num_ports(PCIDevice
*dev
)
1844 AHCIPCIState
*d
= ICH_AHCI(dev
);
1845 AHCIState
*ahci
= &d
->ahci
;
1850 void ahci_ide_create_devs(PCIDevice
*dev
, DriveInfo
**hd
)
1852 AHCIPCIState
*d
= ICH_AHCI(dev
);
1853 AHCIState
*ahci
= &d
->ahci
;
1856 for (i
= 0; i
< ahci
->ports
; i
++) {
1857 if (hd
[i
] == NULL
) {
1860 ide_create_drive(&ahci
->dev
[i
].port
, 0, hd
[i
]);