ahci-internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * QEMU AHCI Emulation
  3. *
  4. * Copyright (c) 2010 qiaochong@loongson.cn
  5. * Copyright (c) 2010 Roland Elek <elek.roland@gmail.com>
  6. * Copyright (c) 2010 Sebastian Herbszt <herbszt@gmx.de>
  7. * Copyright (c) 2010 Alexander Graf <agraf@suse.de>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #ifndef HW_IDE_AHCI_INTERNAL_H
  24. #define HW_IDE_AHCI_INTERNAL_H
  25. #include "hw/ide/ahci.h"
  26. #include "ide-internal.h"
  27. #define AHCI_MEM_BAR_SIZE 0x1000
  28. #define AHCI_MAX_PORTS 32
  29. #define AHCI_MAX_SG 168 /* hardware max is 64K */
  30. #define AHCI_DMA_BOUNDARY 0xffffffff
  31. #define AHCI_USE_CLUSTERING 0
  32. #define AHCI_MAX_CMDS 32
  33. #define AHCI_CMD_SZ 32
  34. #define AHCI_CMD_SLOT_SZ (AHCI_MAX_CMDS * AHCI_CMD_SZ)
  35. #define AHCI_RX_FIS_SZ 256
  36. #define AHCI_CMD_TBL_CDB 0x40
  37. #define AHCI_CMD_TBL_HDR_SZ 0x80
  38. #define AHCI_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16))
  39. #define AHCI_CMD_TBL_AR_SZ (AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS)
  40. #define AHCI_PORT_PRIV_DMA_SZ (AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ + \
  41. AHCI_RX_FIS_SZ)
  42. #define AHCI_IRQ_ON_SG (1U << 31)
  43. #define AHCI_CMD_ATAPI (1 << 5)
  44. #define AHCI_CMD_WRITE (1 << 6)
  45. #define AHCI_CMD_PREFETCH (1 << 7)
  46. #define AHCI_CMD_RESET (1 << 8)
  47. #define AHCI_CMD_CLR_BUSY (1 << 10)
  48. #define RX_FIS_D2H_REG 0x40 /* offset of D2H Register FIS data */
  49. #define RX_FIS_SDB 0x58 /* offset of SDB FIS data */
  50. #define RX_FIS_UNK 0x60 /* offset of Unknown FIS data */
  51. /* global controller registers */
  52. enum AHCIHostReg {
  53. AHCI_HOST_REG_CAP = 0, /* CAP: host capabilities */
  54. AHCI_HOST_REG_CTL = 1, /* GHC: global host control */
  55. AHCI_HOST_REG_IRQ_STAT = 2, /* IS: interrupt status */
  56. AHCI_HOST_REG_PORTS_IMPL = 3, /* PI: bitmap of implemented ports */
  57. AHCI_HOST_REG_VERSION = 4, /* VS: AHCI spec. version compliance */
  58. AHCI_HOST_REG_CCC_CTL = 5, /* CCC_CTL: CCC Control */
  59. AHCI_HOST_REG_CCC_PORTS = 6, /* CCC_PORTS: CCC Ports */
  60. AHCI_HOST_REG_EM_LOC = 7, /* EM_LOC: Enclosure Mgmt Location */
  61. AHCI_HOST_REG_EM_CTL = 8, /* EM_CTL: Enclosure Mgmt Control */
  62. AHCI_HOST_REG_CAP2 = 9, /* CAP2: host capabilities, extended */
  63. AHCI_HOST_REG_BOHC = 10, /* BOHC: firmware/os handoff ctrl & status */
  64. AHCI_HOST_REG__COUNT = 11
  65. };
  66. /* HOST_CTL bits */
  67. #define HOST_CTL_RESET (1 << 0) /* reset controller; self-clear */
  68. #define HOST_CTL_IRQ_EN (1 << 1) /* global IRQ enable */
  69. #define HOST_CTL_AHCI_EN (1U << 31) /* AHCI enabled */
  70. /* HOST_CAP bits */
  71. #define HOST_CAP_SSC (1 << 14) /* Slumber capable */
  72. #define HOST_CAP_AHCI (1 << 18) /* AHCI only */
  73. #define HOST_CAP_CLO (1 << 24) /* Command List Override support */
  74. #define HOST_CAP_SSS (1 << 27) /* Staggered Spin-up */
  75. #define HOST_CAP_NCQ (1 << 30) /* Native Command Queueing */
  76. #define HOST_CAP_64 (1U << 31) /* PCI DAC (64-bit DMA) support */
  77. /* registers for each SATA port */
  78. enum AHCIPortReg {
  79. AHCI_PORT_REG_LST_ADDR = 0, /* PxCLB: command list DMA addr */
  80. AHCI_PORT_REG_LST_ADDR_HI = 1, /* PxCLBU: command list DMA addr hi */
  81. AHCI_PORT_REG_FIS_ADDR = 2, /* PxFB: FIS rx buf addr */
  82. AHCI_PORT_REG_FIS_ADDR_HI = 3, /* PxFBU: FIX rx buf addr hi */
  83. AHCI_PORT_REG_IRQ_STAT = 4, /* PxIS: interrupt status */
  84. AHCI_PORT_REG_IRQ_MASK = 5, /* PxIE: interrupt enable/disable mask */
  85. AHCI_PORT_REG_CMD = 6, /* PxCMD: port command */
  86. /* RESERVED */
  87. AHCI_PORT_REG_TFDATA = 8, /* PxTFD: taskfile data */
  88. AHCI_PORT_REG_SIG = 9, /* PxSIG: device TF signature */
  89. AHCI_PORT_REG_SCR_STAT = 10, /* PxSSTS: SATA phy register: SStatus */
  90. AHCI_PORT_REG_SCR_CTL = 11, /* PxSCTL: SATA phy register: SControl */
  91. AHCI_PORT_REG_SCR_ERR = 12, /* PxSERR: SATA phy register: SError */
  92. AHCI_PORT_REG_SCR_ACT = 13, /* PxSACT: SATA phy register: SActive */
  93. AHCI_PORT_REG_CMD_ISSUE = 14, /* PxCI: command issue */
  94. AHCI_PORT_REG_SCR_NOTIF = 15, /* PxSNTF: SATA phy register: SNotification */
  95. AHCI_PORT_REG_FIS_CTL = 16, /* PxFBS: Port multiplier switching ctl */
  96. AHCI_PORT_REG_DEV_SLEEP = 17, /* PxDEVSLP: device sleep control */
  97. /* RESERVED */
  98. AHCI_PORT_REG_VENDOR_1 = 28, /* PxVS: Vendor Specific */
  99. AHCI_PORT_REG_VENDOR_2 = 29,
  100. AHCI_PORT_REG_VENDOR_3 = 30,
  101. AHCI_PORT_REG_VENDOR_4 = 31,
  102. AHCI_PORT_REG__COUNT = 32
  103. };
  104. /* Port interrupt bit descriptors */
  105. enum AHCIPortIRQ {
  106. AHCI_PORT_IRQ_BIT_DHRS = 0,
  107. AHCI_PORT_IRQ_BIT_PSS = 1,
  108. AHCI_PORT_IRQ_BIT_DSS = 2,
  109. AHCI_PORT_IRQ_BIT_SDBS = 3,
  110. AHCI_PORT_IRQ_BIT_UFS = 4,
  111. AHCI_PORT_IRQ_BIT_DPS = 5,
  112. AHCI_PORT_IRQ_BIT_PCS = 6,
  113. AHCI_PORT_IRQ_BIT_DMPS = 7,
  114. /* RESERVED */
  115. AHCI_PORT_IRQ_BIT_PRCS = 22,
  116. AHCI_PORT_IRQ_BIT_IPMS = 23,
  117. AHCI_PORT_IRQ_BIT_OFS = 24,
  118. /* RESERVED */
  119. AHCI_PORT_IRQ_BIT_INFS = 26,
  120. AHCI_PORT_IRQ_BIT_IFS = 27,
  121. AHCI_PORT_IRQ_BIT_HBDS = 28,
  122. AHCI_PORT_IRQ_BIT_HBFS = 29,
  123. AHCI_PORT_IRQ_BIT_TFES = 30,
  124. AHCI_PORT_IRQ_BIT_CPDS = 31,
  125. AHCI_PORT_IRQ__COUNT = 32
  126. };
  127. /* PORT_IRQ_{STAT,MASK} bits */
  128. #define PORT_IRQ_COLD_PRES (1U << 31) /* cold presence detect */
  129. #define PORT_IRQ_TF_ERR (1 << 30) /* task file error */
  130. #define PORT_IRQ_HBUS_ERR (1 << 29) /* host bus fatal error */
  131. #define PORT_IRQ_HBUS_DATA_ERR (1 << 28) /* host bus data error */
  132. #define PORT_IRQ_IF_ERR (1 << 27) /* interface fatal error */
  133. #define PORT_IRQ_IF_NONFATAL (1 << 26) /* interface non-fatal error */
  134. /* reserved */
  135. #define PORT_IRQ_OVERFLOW (1 << 24) /* xfer exhausted available S/G */
  136. #define PORT_IRQ_BAD_PMP (1 << 23) /* incorrect port multiplier */
  137. #define PORT_IRQ_PHYRDY (1 << 22) /* PhyRdy changed */
  138. /* reserved */
  139. #define PORT_IRQ_DEV_ILCK (1 << 7) /* device interlock */
  140. #define PORT_IRQ_CONNECT (1 << 6) /* port connect change status */
  141. #define PORT_IRQ_SG_DONE (1 << 5) /* descriptor processed */
  142. #define PORT_IRQ_UNK_FIS (1 << 4) /* unknown FIS rx'd */
  143. #define PORT_IRQ_SDB_FIS (1 << 3) /* Set Device Bits FIS rx'd */
  144. #define PORT_IRQ_DMAS_FIS (1 << 2) /* DMA Setup FIS rx'd */
  145. #define PORT_IRQ_PIOS_FIS (1 << 1) /* PIO Setup FIS rx'd */
  146. #define PORT_IRQ_D2H_REG_FIS (1 << 0) /* D2H Register FIS rx'd */
  147. #define PORT_IRQ_FREEZE (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | \
  148. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY | \
  149. PORT_IRQ_UNK_FIS)
  150. #define PORT_IRQ_ERROR (PORT_IRQ_FREEZE | PORT_IRQ_TF_ERR | \
  151. PORT_IRQ_HBUS_DATA_ERR)
  152. #define DEF_PORT_IRQ (PORT_IRQ_ERROR | PORT_IRQ_SG_DONE | \
  153. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS | \
  154. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  155. /* PORT_CMD bits */
  156. #define PORT_CMD_ATAPI (1 << 24) /* Device is ATAPI */
  157. #define PORT_CMD_LIST_ON (1 << 15) /* cmd list DMA engine running */
  158. #define PORT_CMD_FIS_ON (1 << 14) /* FIS DMA engine running */
  159. #define PORT_CMD_FIS_RX (1 << 4) /* Enable FIS receive DMA engine */
  160. #define PORT_CMD_CLO (1 << 3) /* Command list override */
  161. #define PORT_CMD_POWER_ON (1 << 2) /* Power up device */
  162. #define PORT_CMD_SPIN_UP (1 << 1) /* Spin up device */
  163. #define PORT_CMD_START (1 << 0) /* Enable port DMA engine */
  164. #define PORT_CMD_ICC_MASK (0xfU << 28) /* i/f ICC state mask */
  165. #define PORT_CMD_ICC_ACTIVE (0x1 << 28) /* Put i/f in active state */
  166. #define PORT_CMD_ICC_PARTIAL (0x2 << 28) /* Put i/f in partial state */
  167. #define PORT_CMD_ICC_SLUMBER (0x6 << 28) /* Put i/f in slumber state */
  168. #define PORT_CMD_RO_MASK 0x007dffe0 /* Which CMD bits are read only? */
  169. /* ap->flags bits */
  170. #define AHCI_FLAG_NO_NCQ (1 << 24)
  171. #define AHCI_FLAG_IGN_IRQ_IF_ERR (1 << 25) /* ignore IRQ_IF_ERR */
  172. #define AHCI_FLAG_HONOR_PI (1 << 26) /* honor PORTS_IMPL */
  173. #define AHCI_FLAG_IGN_SERR_INTERNAL (1 << 27) /* ignore SERR_INTERNAL */
  174. #define AHCI_FLAG_32BIT_ONLY (1 << 28) /* force 32bit */
  175. #define ATA_SRST (1 << 2) /* software reset */
  176. #define STATE_RUN 0
  177. #define STATE_RESET 1
  178. #define SATA_SCR_SSTATUS_DET_NODEV 0x0
  179. #define SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP 0x3
  180. #define SATA_SCR_SSTATUS_SPD_NODEV 0x00
  181. #define SATA_SCR_SSTATUS_SPD_GEN1 0x10
  182. #define SATA_SCR_SSTATUS_IPM_NODEV 0x000
  183. #define SATA_SCR_SSTATUS_IPM_ACTIVE 0X100
  184. #define AHCI_SCR_SCTL_DET 0xf
  185. #define SATA_FIS_TYPE_REGISTER_H2D 0x27
  186. #define SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER 0x80
  187. #define SATA_FIS_TYPE_REGISTER_D2H 0x34
  188. #define SATA_FIS_TYPE_PIO_SETUP 0x5f
  189. #define SATA_FIS_TYPE_SDB 0xA1
  190. #define AHCI_CMD_HDR_CMD_FIS_LEN 0x1f
  191. #define AHCI_CMD_HDR_PRDT_LEN 16
  192. #define SATA_SIGNATURE_CDROM 0xeb140101
  193. #define SATA_SIGNATURE_DISK 0x00000101
  194. #define AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR 0x2c
  195. #define AHCI_PORT_REGS_START_ADDR 0x100
  196. #define AHCI_PORT_ADDR_OFFSET_MASK 0x7f
  197. #define AHCI_PORT_ADDR_OFFSET_LEN 0x80
  198. #define AHCI_NUM_COMMAND_SLOTS 31
  199. #define AHCI_SUPPORTED_SPEED 20
  200. #define AHCI_SUPPORTED_SPEED_GEN1 1
  201. #define AHCI_VERSION_1_0 0x10000
  202. #define AHCI_PROGMODE_MAJOR_REV_1 1
  203. #define AHCI_COMMAND_TABLE_ACMD 0x40
  204. #define AHCI_PRDT_SIZE_MASK 0x3fffff
  205. #define IDE_FEATURE_DMA 1
  206. #define READ_FPDMA_QUEUED 0x60
  207. #define WRITE_FPDMA_QUEUED 0x61
  208. #define NCQ_NON_DATA 0x63
  209. #define RECEIVE_FPDMA_QUEUED 0x65
  210. #define SEND_FPDMA_QUEUED 0x64
  211. #define NCQ_FIS_FUA_MASK 0x80
  212. #define NCQ_FIS_RARC_MASK 0x01
  213. #define RES_FIS_DSFIS 0x00
  214. #define RES_FIS_PSFIS 0x20
  215. #define RES_FIS_RFIS 0x40
  216. #define RES_FIS_SDBFIS 0x58
  217. #define RES_FIS_UFIS 0x60
  218. #define SATA_CAP_SIZE 0x8
  219. #define SATA_CAP_REV 0x2
  220. #define SATA_CAP_BAR 0x4
  221. typedef struct AHCIPortRegs {
  222. uint32_t lst_addr;
  223. uint32_t lst_addr_hi;
  224. uint32_t fis_addr;
  225. uint32_t fis_addr_hi;
  226. uint32_t irq_stat;
  227. uint32_t irq_mask;
  228. uint32_t cmd;
  229. uint32_t unused0;
  230. uint32_t tfdata;
  231. uint32_t sig;
  232. uint32_t scr_stat;
  233. uint32_t scr_ctl;
  234. uint32_t scr_err;
  235. uint32_t scr_act;
  236. uint32_t cmd_issue;
  237. uint32_t reserved;
  238. } AHCIPortRegs;
  239. typedef struct AHCICmdHdr {
  240. uint16_t opts;
  241. uint16_t prdtl;
  242. uint32_t status;
  243. uint64_t tbl_addr;
  244. uint32_t reserved[4];
  245. } QEMU_PACKED AHCICmdHdr;
  246. typedef struct AHCI_SG {
  247. uint64_t addr;
  248. uint32_t reserved;
  249. uint32_t flags_size;
  250. } QEMU_PACKED AHCI_SG;
  251. typedef struct NCQTransferState {
  252. AHCIDevice *drive;
  253. BlockAIOCB *aiocb;
  254. AHCICmdHdr *cmdh;
  255. QEMUSGList sglist;
  256. BlockAcctCookie acct;
  257. uint32_t sector_count;
  258. uint64_t lba;
  259. uint8_t tag;
  260. uint8_t cmd;
  261. uint8_t slot;
  262. bool used;
  263. bool halt;
  264. } NCQTransferState;
  265. struct AHCIDevice {
  266. IDEDMA dma;
  267. IDEBus port;
  268. int port_no;
  269. uint32_t port_state;
  270. uint32_t finished;
  271. AHCIPortRegs port_regs;
  272. struct AHCIState *hba;
  273. QEMUBH *check_bh;
  274. uint8_t *lst;
  275. uint8_t *res_fis;
  276. bool done_first_drq;
  277. int32_t busy_slot;
  278. bool init_d2h_sent;
  279. AHCICmdHdr *cur_cmd;
  280. NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
  281. MemReentrancyGuard mem_reentrancy_guard;
  282. };
  283. extern const VMStateDescription vmstate_ahci;
  284. #define VMSTATE_AHCI(_field, _state) { \
  285. .name = (stringify(_field)), \
  286. .size = sizeof(AHCIState), \
  287. .vmsd = &vmstate_ahci, \
  288. .flags = VMS_STRUCT, \
  289. .offset = vmstate_offset_value(_state, _field, AHCIState), \
  290. }
  291. /**
  292. * NCQFrame is the same as a Register H2D FIS (described in SATA 3.2),
  293. * but some fields have been re-mapped and re-purposed, as seen in
  294. * SATA 3.2 section 13.6.4.1 ("READ FPDMA QUEUED")
  295. *
  296. * cmd_fis[3], feature 7:0, becomes sector count 7:0.
  297. * cmd_fis[7], device 7:0, uses bit 7 as the Force Unit Access bit.
  298. * cmd_fis[11], feature 15:8, becomes sector count 15:8.
  299. * cmd_fis[12], count 7:0, becomes the NCQ TAG (7:3) and RARC bit (0)
  300. * cmd_fis[13], count 15:8, becomes the priority value (7:6)
  301. * bytes 16-19 become an le32 "auxiliary" field.
  302. */
  303. typedef struct NCQFrame {
  304. uint8_t fis_type;
  305. uint8_t c;
  306. uint8_t command;
  307. uint8_t sector_count_low; /* (feature 7:0) */
  308. uint8_t lba0;
  309. uint8_t lba1;
  310. uint8_t lba2;
  311. uint8_t fua; /* (device 7:0) */
  312. uint8_t lba3;
  313. uint8_t lba4;
  314. uint8_t lba5;
  315. uint8_t sector_count_high; /* (feature 15:8) */
  316. uint8_t tag; /* (count 0:7) */
  317. uint8_t prio; /* (count 15:8) */
  318. uint8_t icc;
  319. uint8_t control;
  320. uint8_t aux0;
  321. uint8_t aux1;
  322. uint8_t aux2;
  323. uint8_t aux3;
  324. } QEMU_PACKED NCQFrame;
  325. typedef struct SDBFIS {
  326. uint8_t type;
  327. uint8_t flags;
  328. uint8_t status;
  329. uint8_t error;
  330. uint32_t payload;
  331. } QEMU_PACKED SDBFIS;
  332. void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as);
  333. void ahci_init(AHCIState *s, DeviceState *qdev);
  334. void ahci_uninit(AHCIState *s);
  335. void ahci_reset(AHCIState *s);
  336. #endif /* HW_IDE_AHCI_INTERNAL_H */