xref: /qemu/hw/ide/ahci-internal.h (revision 4e6e1de4e05b01292027fdd9f36a507872c33b9d)
170e23370SJohn Snow /*
270e23370SJohn Snow  * QEMU AHCI Emulation
370e23370SJohn Snow  *
470e23370SJohn Snow  * Copyright (c) 2010 qiaochong@loongson.cn
570e23370SJohn Snow  * Copyright (c) 2010 Roland Elek <elek.roland@gmail.com>
670e23370SJohn Snow  * Copyright (c) 2010 Sebastian Herbszt <herbszt@gmx.de>
770e23370SJohn Snow  * Copyright (c) 2010 Alexander Graf <agraf@suse.de>
870e23370SJohn Snow  *
970e23370SJohn Snow  * This library is free software; you can redistribute it and/or
1070e23370SJohn Snow  * modify it under the terms of the GNU Lesser General Public
1170e23370SJohn Snow  * License as published by the Free Software Foundation; either
1270e23370SJohn Snow  * version 2 of the License, or (at your option) any later version.
1370e23370SJohn Snow  *
1470e23370SJohn Snow  * This library is distributed in the hope that it will be useful,
1570e23370SJohn Snow  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1670e23370SJohn Snow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1770e23370SJohn Snow  * Lesser General Public License for more details.
1870e23370SJohn Snow  *
1970e23370SJohn Snow  * You should have received a copy of the GNU Lesser General Public
2070e23370SJohn Snow  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
2170e23370SJohn Snow  *
2270e23370SJohn Snow  */
2370e23370SJohn Snow 
2470e23370SJohn Snow #ifndef HW_IDE_AHCI_INTERNAL_H
2570e23370SJohn Snow #define HW_IDE_AHCI_INTERNAL_H
2670e23370SJohn Snow 
2770e23370SJohn Snow #include "hw/ide/ahci.h"
2870e23370SJohn Snow #include "hw/sysbus.h"
2970e23370SJohn Snow 
3070e23370SJohn Snow #define AHCI_MEM_BAR_SIZE         0x1000
3170e23370SJohn Snow #define AHCI_MAX_PORTS            32
3270e23370SJohn Snow #define AHCI_MAX_SG               168 /* hardware max is 64K */
3370e23370SJohn Snow #define AHCI_DMA_BOUNDARY         0xffffffff
3470e23370SJohn Snow #define AHCI_USE_CLUSTERING       0
3570e23370SJohn Snow #define AHCI_MAX_CMDS             32
3670e23370SJohn Snow #define AHCI_CMD_SZ               32
3770e23370SJohn Snow #define AHCI_CMD_SLOT_SZ          (AHCI_MAX_CMDS * AHCI_CMD_SZ)
3870e23370SJohn Snow #define AHCI_RX_FIS_SZ            256
3970e23370SJohn Snow #define AHCI_CMD_TBL_CDB          0x40
4070e23370SJohn Snow #define AHCI_CMD_TBL_HDR_SZ       0x80
4170e23370SJohn Snow #define AHCI_CMD_TBL_SZ           (AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16))
4270e23370SJohn Snow #define AHCI_CMD_TBL_AR_SZ        (AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS)
4370e23370SJohn Snow #define AHCI_PORT_PRIV_DMA_SZ     (AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ + \
4470e23370SJohn Snow                                    AHCI_RX_FIS_SZ)
4570e23370SJohn Snow 
4670e23370SJohn Snow #define AHCI_IRQ_ON_SG            (1U << 31)
4770e23370SJohn Snow #define AHCI_CMD_ATAPI            (1 << 5)
4870e23370SJohn Snow #define AHCI_CMD_WRITE            (1 << 6)
4970e23370SJohn Snow #define AHCI_CMD_PREFETCH         (1 << 7)
5070e23370SJohn Snow #define AHCI_CMD_RESET            (1 << 8)
5170e23370SJohn Snow #define AHCI_CMD_CLR_BUSY         (1 << 10)
5270e23370SJohn Snow 
5370e23370SJohn Snow #define RX_FIS_D2H_REG            0x40 /* offset of D2H Register FIS data */
5470e23370SJohn Snow #define RX_FIS_SDB                0x58 /* offset of SDB FIS data */
5570e23370SJohn Snow #define RX_FIS_UNK                0x60 /* offset of Unknown FIS data */
5670e23370SJohn Snow 
5770e23370SJohn Snow /* global controller registers */
5870e23370SJohn Snow #define HOST_CAP                  0x00 /* host capabilities */
5970e23370SJohn Snow #define HOST_CTL                  0x04 /* global host control */
6070e23370SJohn Snow #define HOST_IRQ_STAT             0x08 /* interrupt status */
6170e23370SJohn Snow #define HOST_PORTS_IMPL           0x0c /* bitmap of implemented ports */
6270e23370SJohn Snow #define HOST_VERSION              0x10 /* AHCI spec. version compliancy */
6370e23370SJohn Snow 
6470e23370SJohn Snow /* HOST_CTL bits */
6570e23370SJohn Snow #define HOST_CTL_RESET            (1 << 0)  /* reset controller; self-clear */
6670e23370SJohn Snow #define HOST_CTL_IRQ_EN           (1 << 1)  /* global IRQ enable */
6770e23370SJohn Snow #define HOST_CTL_AHCI_EN          (1U << 31) /* AHCI enabled */
6870e23370SJohn Snow 
6970e23370SJohn Snow /* HOST_CAP bits */
7070e23370SJohn Snow #define HOST_CAP_SSC              (1 << 14) /* Slumber capable */
7170e23370SJohn Snow #define HOST_CAP_AHCI             (1 << 18) /* AHCI only */
7270e23370SJohn Snow #define HOST_CAP_CLO              (1 << 24) /* Command List Override support */
7370e23370SJohn Snow #define HOST_CAP_SSS              (1 << 27) /* Staggered Spin-up */
7470e23370SJohn Snow #define HOST_CAP_NCQ              (1 << 30) /* Native Command Queueing */
7570e23370SJohn Snow #define HOST_CAP_64               (1U << 31) /* PCI DAC (64-bit DMA) support */
7670e23370SJohn Snow 
7770e23370SJohn Snow /* registers for each SATA port */
78*4e6e1de4SJohn Snow enum AHCIPortReg {
79*4e6e1de4SJohn Snow     AHCI_PORT_REG_LST_ADDR    = 0, /* PxCLB: command list DMA addr */
80*4e6e1de4SJohn Snow     AHCI_PORT_REG_LST_ADDR_HI = 1, /* PxCLBU: command list DMA addr hi */
81*4e6e1de4SJohn Snow     AHCI_PORT_REG_FIS_ADDR    = 2, /* PxFB: FIS rx buf addr */
82*4e6e1de4SJohn Snow     AHCI_PORT_REG_FIS_ADDR_HI = 3, /* PxFBU: FIX rx buf addr hi */
83*4e6e1de4SJohn Snow     AHCI_PORT_REG_IRQ_STAT    = 4, /* PxIS: interrupt status */
84*4e6e1de4SJohn Snow     AHCI_PORT_REG_IRQ_MASK    = 5, /* PxIE: interrupt enable/disable mask */
85*4e6e1de4SJohn Snow     AHCI_PORT_REG_CMD         = 6, /* PxCMD: port command */
86*4e6e1de4SJohn Snow     /* RESERVED */
87*4e6e1de4SJohn Snow     AHCI_PORT_REG_TFDATA      = 8, /* PxTFD: taskfile data */
88*4e6e1de4SJohn Snow     AHCI_PORT_REG_SIG         = 9, /* PxSIG: device TF signature */
89*4e6e1de4SJohn Snow     AHCI_PORT_REG_SCR_STAT    = 10, /* PxSSTS: SATA phy register: SStatus */
90*4e6e1de4SJohn Snow     AHCI_PORT_REG_SCR_CTL     = 11, /* PxSCTL: SATA phy register: SControl */
91*4e6e1de4SJohn Snow     AHCI_PORT_REG_SCR_ERR     = 12, /* PxSERR: SATA phy register: SError */
92*4e6e1de4SJohn Snow     AHCI_PORT_REG_SCR_ACT     = 13, /* PxSACT: SATA phy register: SActive */
93*4e6e1de4SJohn Snow     AHCI_PORT_REG_CMD_ISSUE   = 14, /* PxCI: command issue */
94*4e6e1de4SJohn Snow     AHCI_PORT_REG_SCR_NOTIF   = 15, /* PxSNTF: SATA phy register: SNotification */
95*4e6e1de4SJohn Snow     AHCI_PORT_REG_FIS_CTL     = 16, /* PxFBS: Port multiplier switching ctl */
96*4e6e1de4SJohn Snow     AHCI_PORT_REG_DEV_SLEEP   = 17, /* PxDEVSLP: device sleep control */
97*4e6e1de4SJohn Snow     /* RESERVED */
98*4e6e1de4SJohn Snow     AHCI_PORT_REG_VENDOR_1    = 28, /* PxVS: Vendor Specific */
99*4e6e1de4SJohn Snow     AHCI_PORT_REG_VENDOR_2    = 29,
100*4e6e1de4SJohn Snow     AHCI_PORT_REG_VENDOR_3    = 30,
101*4e6e1de4SJohn Snow     AHCI_PORT_REG_VENDOR_4    = 31,
102*4e6e1de4SJohn Snow     AHCI_PORT_REG__COUNT      = 32
103*4e6e1de4SJohn Snow };
104*4e6e1de4SJohn Snow 
105*4e6e1de4SJohn Snow /* registers for each SATA port */
10670e23370SJohn Snow #define PORT_LST_ADDR             0x00 /* command list DMA addr */
10770e23370SJohn Snow #define PORT_LST_ADDR_HI          0x04 /* command list DMA addr hi */
10870e23370SJohn Snow #define PORT_FIS_ADDR             0x08 /* FIS rx buf addr */
10970e23370SJohn Snow #define PORT_FIS_ADDR_HI          0x0c /* FIS rx buf addr hi */
11070e23370SJohn Snow #define PORT_IRQ_STAT             0x10 /* interrupt status */
11170e23370SJohn Snow #define PORT_IRQ_MASK             0x14 /* interrupt enable/disable mask */
11270e23370SJohn Snow #define PORT_CMD                  0x18 /* port command */
113*4e6e1de4SJohn Snow 
11470e23370SJohn Snow #define PORT_TFDATA               0x20 /* taskfile data */
11570e23370SJohn Snow #define PORT_SIG                  0x24 /* device TF signature */
11670e23370SJohn Snow #define PORT_SCR_STAT             0x28 /* SATA phy register: SStatus */
11770e23370SJohn Snow #define PORT_SCR_CTL              0x2c /* SATA phy register: SControl */
11870e23370SJohn Snow #define PORT_SCR_ERR              0x30 /* SATA phy register: SError */
11970e23370SJohn Snow #define PORT_SCR_ACT              0x34 /* SATA phy register: SActive */
12070e23370SJohn Snow #define PORT_CMD_ISSUE            0x38 /* command issue */
12170e23370SJohn Snow #define PORT_RESERVED             0x3c /* reserved */
12270e23370SJohn Snow 
1235fa0feecSJohn Snow /* Port interrupt bit descriptors */
1245fa0feecSJohn Snow enum AHCIPortIRQ {
1255fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_DHRS = 0,
1265fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_PSS  = 1,
1275fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_DSS  = 2,
1285fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_SDBS = 3,
1295fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_UFS  = 4,
1305fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_DPS  = 5,
1315fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_PCS  = 6,
1325fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_DMPS = 7,
1335fa0feecSJohn Snow     /* RESERVED */
1345fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_PRCS = 22,
1355fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_IPMS = 23,
1365fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_OFS  = 24,
1375fa0feecSJohn Snow     /* RESERVED */
1385fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_INFS = 26,
1395fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_IFS  = 27,
1405fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_HBDS = 28,
1415fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_HBFS = 29,
1425fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_TFES = 30,
1435fa0feecSJohn Snow     AHCI_PORT_IRQ_BIT_CPDS = 31,
1445fa0feecSJohn Snow     AHCI_PORT_IRQ__COUNT   = 32
1455fa0feecSJohn Snow };
1465fa0feecSJohn Snow 
1475fa0feecSJohn Snow 
14870e23370SJohn Snow /* PORT_IRQ_{STAT,MASK} bits */
14970e23370SJohn Snow #define PORT_IRQ_COLD_PRES        (1U << 31) /* cold presence detect */
15070e23370SJohn Snow #define PORT_IRQ_TF_ERR           (1 << 30) /* task file error */
15170e23370SJohn Snow #define PORT_IRQ_HBUS_ERR         (1 << 29) /* host bus fatal error */
15270e23370SJohn Snow #define PORT_IRQ_HBUS_DATA_ERR    (1 << 28) /* host bus data error */
15370e23370SJohn Snow #define PORT_IRQ_IF_ERR           (1 << 27) /* interface fatal error */
15470e23370SJohn Snow #define PORT_IRQ_IF_NONFATAL      (1 << 26) /* interface non-fatal error */
1555fa0feecSJohn Snow                                             /* reserved */
15670e23370SJohn Snow #define PORT_IRQ_OVERFLOW         (1 << 24) /* xfer exhausted available S/G */
15770e23370SJohn Snow #define PORT_IRQ_BAD_PMP          (1 << 23) /* incorrect port multiplier */
15870e23370SJohn Snow #define PORT_IRQ_PHYRDY           (1 << 22) /* PhyRdy changed */
1595fa0feecSJohn Snow                                             /* reserved */
16070e23370SJohn Snow #define PORT_IRQ_DEV_ILCK         (1 << 7)  /* device interlock */
16170e23370SJohn Snow #define PORT_IRQ_CONNECT          (1 << 6)  /* port connect change status */
16270e23370SJohn Snow #define PORT_IRQ_SG_DONE          (1 << 5)  /* descriptor processed */
16370e23370SJohn Snow #define PORT_IRQ_UNK_FIS          (1 << 4)  /* unknown FIS rx'd */
16470e23370SJohn Snow #define PORT_IRQ_SDB_FIS          (1 << 3)  /* Set Device Bits FIS rx'd */
16570e23370SJohn Snow #define PORT_IRQ_DMAS_FIS         (1 << 2)  /* DMA Setup FIS rx'd */
16670e23370SJohn Snow #define PORT_IRQ_PIOS_FIS         (1 << 1)  /* PIO Setup FIS rx'd */
16770e23370SJohn Snow #define PORT_IRQ_D2H_REG_FIS      (1 << 0)  /* D2H Register FIS rx'd */
16870e23370SJohn Snow 
16970e23370SJohn Snow #define PORT_IRQ_FREEZE           (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR |   \
17070e23370SJohn Snow                                    PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY |    \
17170e23370SJohn Snow                                    PORT_IRQ_UNK_FIS)
17270e23370SJohn Snow #define PORT_IRQ_ERROR            (PORT_IRQ_FREEZE | PORT_IRQ_TF_ERR |     \
17370e23370SJohn Snow                                    PORT_IRQ_HBUS_DATA_ERR)
17470e23370SJohn Snow #define DEF_PORT_IRQ              (PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |     \
17570e23370SJohn Snow                                    PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |  \
17670e23370SJohn Snow                                    PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
17770e23370SJohn Snow 
17870e23370SJohn Snow /* PORT_CMD bits */
17970e23370SJohn Snow #define PORT_CMD_ATAPI            (1 << 24) /* Device is ATAPI */
18070e23370SJohn Snow #define PORT_CMD_LIST_ON          (1 << 15) /* cmd list DMA engine running */
18170e23370SJohn Snow #define PORT_CMD_FIS_ON           (1 << 14) /* FIS DMA engine running */
18270e23370SJohn Snow #define PORT_CMD_FIS_RX           (1 << 4) /* Enable FIS receive DMA engine */
18370e23370SJohn Snow #define PORT_CMD_CLO              (1 << 3) /* Command list override */
18470e23370SJohn Snow #define PORT_CMD_POWER_ON         (1 << 2) /* Power up device */
18570e23370SJohn Snow #define PORT_CMD_SPIN_UP          (1 << 1) /* Spin up device */
18670e23370SJohn Snow #define PORT_CMD_START            (1 << 0) /* Enable port DMA engine */
18770e23370SJohn Snow 
18870e23370SJohn Snow #define PORT_CMD_ICC_MASK        (0xfU << 28) /* i/f ICC state mask */
18970e23370SJohn Snow #define PORT_CMD_ICC_ACTIVE       (0x1 << 28) /* Put i/f in active state */
19070e23370SJohn Snow #define PORT_CMD_ICC_PARTIAL      (0x2 << 28) /* Put i/f in partial state */
19170e23370SJohn Snow #define PORT_CMD_ICC_SLUMBER      (0x6 << 28) /* Put i/f in slumber state */
19270e23370SJohn Snow 
19370e23370SJohn Snow #define PORT_CMD_RO_MASK          0x007dffe0 /* Which CMD bits are read only? */
19470e23370SJohn Snow 
19570e23370SJohn Snow /* ap->flags bits */
19670e23370SJohn Snow #define AHCI_FLAG_NO_NCQ                  (1 << 24)
19770e23370SJohn Snow #define AHCI_FLAG_IGN_IRQ_IF_ERR          (1 << 25) /* ignore IRQ_IF_ERR */
19870e23370SJohn Snow #define AHCI_FLAG_HONOR_PI                (1 << 26) /* honor PORTS_IMPL */
19970e23370SJohn Snow #define AHCI_FLAG_IGN_SERR_INTERNAL       (1 << 27) /* ignore SERR_INTERNAL */
20070e23370SJohn Snow #define AHCI_FLAG_32BIT_ONLY              (1 << 28) /* force 32bit */
20170e23370SJohn Snow 
20270e23370SJohn Snow #define ATA_SRST                          (1 << 2)  /* software reset */
20370e23370SJohn Snow 
20470e23370SJohn Snow #define STATE_RUN                         0
20570e23370SJohn Snow #define STATE_RESET                       1
20670e23370SJohn Snow 
20770e23370SJohn Snow #define SATA_SCR_SSTATUS_DET_NODEV        0x0
20870e23370SJohn Snow #define SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP 0x3
20970e23370SJohn Snow 
21070e23370SJohn Snow #define SATA_SCR_SSTATUS_SPD_NODEV        0x00
21170e23370SJohn Snow #define SATA_SCR_SSTATUS_SPD_GEN1         0x10
21270e23370SJohn Snow 
21370e23370SJohn Snow #define SATA_SCR_SSTATUS_IPM_NODEV        0x000
21470e23370SJohn Snow #define SATA_SCR_SSTATUS_IPM_ACTIVE       0X100
21570e23370SJohn Snow 
21670e23370SJohn Snow #define AHCI_SCR_SCTL_DET                 0xf
21770e23370SJohn Snow 
21870e23370SJohn Snow #define SATA_FIS_TYPE_REGISTER_H2D        0x27
21970e23370SJohn Snow #define   SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER 0x80
22070e23370SJohn Snow #define SATA_FIS_TYPE_REGISTER_D2H        0x34
22170e23370SJohn Snow #define SATA_FIS_TYPE_PIO_SETUP           0x5f
22270e23370SJohn Snow #define SATA_FIS_TYPE_SDB                 0xA1
22370e23370SJohn Snow 
22470e23370SJohn Snow #define AHCI_CMD_HDR_CMD_FIS_LEN           0x1f
22570e23370SJohn Snow #define AHCI_CMD_HDR_PRDT_LEN              16
22670e23370SJohn Snow 
22770e23370SJohn Snow #define SATA_SIGNATURE_CDROM               0xeb140101
22870e23370SJohn Snow #define SATA_SIGNATURE_DISK                0x00000101
22970e23370SJohn Snow 
23070e23370SJohn Snow #define AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR 0x20
23170e23370SJohn Snow                                             /* Shouldn't this be 0x2c? */
23270e23370SJohn Snow 
23370e23370SJohn Snow #define AHCI_PORT_REGS_START_ADDR          0x100
23470e23370SJohn Snow #define AHCI_PORT_ADDR_OFFSET_MASK         0x7f
23570e23370SJohn Snow #define AHCI_PORT_ADDR_OFFSET_LEN          0x80
23670e23370SJohn Snow 
23770e23370SJohn Snow #define AHCI_NUM_COMMAND_SLOTS             31
23870e23370SJohn Snow #define AHCI_SUPPORTED_SPEED               20
23970e23370SJohn Snow #define AHCI_SUPPORTED_SPEED_GEN1          1
24070e23370SJohn Snow #define AHCI_VERSION_1_0                   0x10000
24170e23370SJohn Snow 
24270e23370SJohn Snow #define AHCI_PROGMODE_MAJOR_REV_1          1
24370e23370SJohn Snow 
24470e23370SJohn Snow #define AHCI_COMMAND_TABLE_ACMD            0x40
24570e23370SJohn Snow 
24670e23370SJohn Snow #define AHCI_PRDT_SIZE_MASK                0x3fffff
24770e23370SJohn Snow 
24870e23370SJohn Snow #define IDE_FEATURE_DMA                    1
24970e23370SJohn Snow 
25070e23370SJohn Snow #define READ_FPDMA_QUEUED                  0x60
25170e23370SJohn Snow #define WRITE_FPDMA_QUEUED                 0x61
25270e23370SJohn Snow #define NCQ_NON_DATA                       0x63
25370e23370SJohn Snow #define RECEIVE_FPDMA_QUEUED               0x65
25470e23370SJohn Snow #define SEND_FPDMA_QUEUED                  0x64
25570e23370SJohn Snow 
25670e23370SJohn Snow #define NCQ_FIS_FUA_MASK                   0x80
25770e23370SJohn Snow #define NCQ_FIS_RARC_MASK                  0x01
25870e23370SJohn Snow 
25970e23370SJohn Snow #define RES_FIS_DSFIS                      0x00
26070e23370SJohn Snow #define RES_FIS_PSFIS                      0x20
26170e23370SJohn Snow #define RES_FIS_RFIS                       0x40
26270e23370SJohn Snow #define RES_FIS_SDBFIS                     0x58
26370e23370SJohn Snow #define RES_FIS_UFIS                       0x60
26470e23370SJohn Snow 
26570e23370SJohn Snow #define SATA_CAP_SIZE           0x8
26670e23370SJohn Snow #define SATA_CAP_REV            0x2
26770e23370SJohn Snow #define SATA_CAP_BAR            0x4
26870e23370SJohn Snow 
26970e23370SJohn Snow typedef struct AHCIPortRegs {
27070e23370SJohn Snow     uint32_t    lst_addr;
27170e23370SJohn Snow     uint32_t    lst_addr_hi;
27270e23370SJohn Snow     uint32_t    fis_addr;
27370e23370SJohn Snow     uint32_t    fis_addr_hi;
27470e23370SJohn Snow     uint32_t    irq_stat;
27570e23370SJohn Snow     uint32_t    irq_mask;
27670e23370SJohn Snow     uint32_t    cmd;
27770e23370SJohn Snow     uint32_t    unused0;
27870e23370SJohn Snow     uint32_t    tfdata;
27970e23370SJohn Snow     uint32_t    sig;
28070e23370SJohn Snow     uint32_t    scr_stat;
28170e23370SJohn Snow     uint32_t    scr_ctl;
28270e23370SJohn Snow     uint32_t    scr_err;
28370e23370SJohn Snow     uint32_t    scr_act;
28470e23370SJohn Snow     uint32_t    cmd_issue;
28570e23370SJohn Snow     uint32_t    reserved;
28670e23370SJohn Snow } AHCIPortRegs;
28770e23370SJohn Snow 
28870e23370SJohn Snow typedef struct AHCICmdHdr {
28970e23370SJohn Snow     uint16_t    opts;
29070e23370SJohn Snow     uint16_t    prdtl;
29170e23370SJohn Snow     uint32_t    status;
29270e23370SJohn Snow     uint64_t    tbl_addr;
29370e23370SJohn Snow     uint32_t    reserved[4];
29470e23370SJohn Snow } QEMU_PACKED AHCICmdHdr;
29570e23370SJohn Snow 
29670e23370SJohn Snow typedef struct AHCI_SG {
29770e23370SJohn Snow     uint64_t    addr;
29870e23370SJohn Snow     uint32_t    reserved;
29970e23370SJohn Snow     uint32_t    flags_size;
30070e23370SJohn Snow } QEMU_PACKED AHCI_SG;
30170e23370SJohn Snow 
30270e23370SJohn Snow typedef struct NCQTransferState {
30370e23370SJohn Snow     AHCIDevice *drive;
30470e23370SJohn Snow     BlockAIOCB *aiocb;
30570e23370SJohn Snow     AHCICmdHdr *cmdh;
30670e23370SJohn Snow     QEMUSGList sglist;
30770e23370SJohn Snow     BlockAcctCookie acct;
30870e23370SJohn Snow     uint32_t sector_count;
30970e23370SJohn Snow     uint64_t lba;
31070e23370SJohn Snow     uint8_t tag;
31170e23370SJohn Snow     uint8_t cmd;
31270e23370SJohn Snow     uint8_t slot;
31370e23370SJohn Snow     bool used;
31470e23370SJohn Snow     bool halt;
31570e23370SJohn Snow } NCQTransferState;
31670e23370SJohn Snow 
31770e23370SJohn Snow struct AHCIDevice {
31870e23370SJohn Snow     IDEDMA dma;
31970e23370SJohn Snow     IDEBus port;
32070e23370SJohn Snow     int port_no;
32170e23370SJohn Snow     uint32_t port_state;
32270e23370SJohn Snow     uint32_t finished;
32370e23370SJohn Snow     AHCIPortRegs port_regs;
32470e23370SJohn Snow     struct AHCIState *hba;
32570e23370SJohn Snow     QEMUBH *check_bh;
32670e23370SJohn Snow     uint8_t *lst;
32770e23370SJohn Snow     uint8_t *res_fis;
32870e23370SJohn Snow     bool done_atapi_packet;
32970e23370SJohn Snow     int32_t busy_slot;
33070e23370SJohn Snow     bool init_d2h_sent;
33170e23370SJohn Snow     AHCICmdHdr *cur_cmd;
33270e23370SJohn Snow     NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
33370e23370SJohn Snow };
33470e23370SJohn Snow 
33570e23370SJohn Snow struct AHCIPCIState {
33670e23370SJohn Snow     /*< private >*/
33770e23370SJohn Snow     PCIDevice parent_obj;
33870e23370SJohn Snow     /*< public >*/
33970e23370SJohn Snow 
34070e23370SJohn Snow     AHCIState ahci;
34170e23370SJohn Snow };
34270e23370SJohn Snow 
34370e23370SJohn Snow #define ICH_AHCI(obj) \
34470e23370SJohn Snow     OBJECT_CHECK(AHCIPCIState, (obj), TYPE_ICH9_AHCI)
34570e23370SJohn Snow 
34670e23370SJohn Snow extern const VMStateDescription vmstate_ahci;
34770e23370SJohn Snow 
34870e23370SJohn Snow #define VMSTATE_AHCI(_field, _state) {                               \
34970e23370SJohn Snow     .name       = (stringify(_field)),                               \
35070e23370SJohn Snow     .size       = sizeof(AHCIState),                                 \
35170e23370SJohn Snow     .vmsd       = &vmstate_ahci,                                     \
35270e23370SJohn Snow     .flags      = VMS_STRUCT,                                        \
35370e23370SJohn Snow     .offset     = vmstate_offset_value(_state, _field, AHCIState),   \
35470e23370SJohn Snow }
35570e23370SJohn Snow 
35670e23370SJohn Snow /**
35770e23370SJohn Snow  * NCQFrame is the same as a Register H2D FIS (described in SATA 3.2),
35870e23370SJohn Snow  * but some fields have been re-mapped and re-purposed, as seen in
35970e23370SJohn Snow  * SATA 3.2 section 13.6.4.1 ("READ FPDMA QUEUED")
36070e23370SJohn Snow  *
36170e23370SJohn Snow  * cmd_fis[3], feature 7:0, becomes sector count 7:0.
36270e23370SJohn Snow  * cmd_fis[7], device 7:0, uses bit 7 as the Force Unit Access bit.
36370e23370SJohn Snow  * cmd_fis[11], feature 15:8, becomes sector count 15:8.
36470e23370SJohn Snow  * cmd_fis[12], count 7:0, becomes the NCQ TAG (7:3) and RARC bit (0)
36570e23370SJohn Snow  * cmd_fis[13], count 15:8, becomes the priority value (7:6)
36670e23370SJohn Snow  * bytes 16-19 become an le32 "auxiliary" field.
36770e23370SJohn Snow  */
36870e23370SJohn Snow typedef struct NCQFrame {
36970e23370SJohn Snow     uint8_t fis_type;
37070e23370SJohn Snow     uint8_t c;
37170e23370SJohn Snow     uint8_t command;
37270e23370SJohn Snow     uint8_t sector_count_low;  /* (feature 7:0) */
37370e23370SJohn Snow     uint8_t lba0;
37470e23370SJohn Snow     uint8_t lba1;
37570e23370SJohn Snow     uint8_t lba2;
37670e23370SJohn Snow     uint8_t fua;               /* (device 7:0) */
37770e23370SJohn Snow     uint8_t lba3;
37870e23370SJohn Snow     uint8_t lba4;
37970e23370SJohn Snow     uint8_t lba5;
38070e23370SJohn Snow     uint8_t sector_count_high; /* (feature 15:8) */
38170e23370SJohn Snow     uint8_t tag;               /* (count 0:7) */
38270e23370SJohn Snow     uint8_t prio;              /* (count 15:8) */
38370e23370SJohn Snow     uint8_t icc;
38470e23370SJohn Snow     uint8_t control;
38570e23370SJohn Snow     uint8_t aux0;
38670e23370SJohn Snow     uint8_t aux1;
38770e23370SJohn Snow     uint8_t aux2;
38870e23370SJohn Snow     uint8_t aux3;
38970e23370SJohn Snow } QEMU_PACKED NCQFrame;
39070e23370SJohn Snow 
39170e23370SJohn Snow typedef struct SDBFIS {
39270e23370SJohn Snow     uint8_t type;
39370e23370SJohn Snow     uint8_t flags;
39470e23370SJohn Snow     uint8_t status;
39570e23370SJohn Snow     uint8_t error;
39670e23370SJohn Snow     uint32_t payload;
39770e23370SJohn Snow } QEMU_PACKED SDBFIS;
39870e23370SJohn Snow 
39970e23370SJohn Snow void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports);
40070e23370SJohn Snow void ahci_init(AHCIState *s, DeviceState *qdev);
40170e23370SJohn Snow void ahci_uninit(AHCIState *s);
40270e23370SJohn Snow 
40370e23370SJohn Snow void ahci_reset(AHCIState *s);
40470e23370SJohn Snow 
40570e23370SJohn Snow #define SYSBUS_AHCI(obj) OBJECT_CHECK(SysbusAHCIState, (obj), TYPE_SYSBUS_AHCI)
40670e23370SJohn Snow 
40770e23370SJohn Snow #endif /* HW_IDE_AHCI_H */
408