ahci.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #pragma once
  2. #include "../block_device.h"
  3. /**
  4. * @brief 在SATA3.0规范中定义的Frame Information Structure类型
  5. *
  6. */
  7. typedef enum
  8. {
  9. FIS_TYPE_REG_H2D = 0x27, // Register FIS - host to device
  10. FIS_TYPE_REG_D2H = 0x34, // Register FIS - device to host
  11. FIS_TYPE_DMA_ACT = 0x39, // DMA activate FIS - device to host
  12. FIS_TYPE_DMA_SETUP = 0x41, // DMA setup FIS - bidirectional
  13. FIS_TYPE_DATA = 0x46, // Data FIS - bidirectional
  14. FIS_TYPE_BIST = 0x58, // BIST activate FIS - bidirectional
  15. FIS_TYPE_PIO_SETUP = 0x5F, // PIO setup FIS - device to host
  16. FIS_TYPE_DEV_BITS = 0xA1, // Set device bits FIS - device to host
  17. } FIS_TYPE;
  18. /**
  19. * @brief FIS_REG_H2D 被用于从主机向设备发送控制命令
  20. * 注意:reserved bit应当被清零
  21. */
  22. typedef struct tagFIS_REG_H2D
  23. {
  24. // DWORD 0
  25. uint8_t fis_type; // FIS_TYPE_REG_H2D
  26. uint8_t pmport : 4; // Port multiplier
  27. uint8_t rsv0 : 3; // Reserved
  28. uint8_t c : 1; // 1: Command, 0: Control
  29. uint8_t command; // Command register
  30. uint8_t featurel; // Feature register, 7:0
  31. // DWORD 1
  32. uint8_t lba0; // LBA low register, 7:0
  33. uint8_t lba1; // LBA mid register, 15:8
  34. uint8_t lba2; // LBA high register, 23:16
  35. uint8_t device; // Device register
  36. // DWORD 2
  37. uint8_t lba3; // LBA register, 31:24
  38. uint8_t lba4; // LBA register, 39:32
  39. uint8_t lba5; // LBA register, 47:40
  40. uint8_t featureh; // Feature register, 15:8
  41. // DWORD 3
  42. uint8_t countl; // Count register, 7:0
  43. uint8_t counth; // Count register, 15:8
  44. uint8_t icc; // Isochronous command completion
  45. uint8_t control; // Control register
  46. // DWORD 4
  47. uint8_t rsv1[4]; // Reserved
  48. } FIS_REG_H2D;
  49. // A device to host register FIS is used by the device to notify the host that some ATA register has changed.
  50. // It contains the updated task files such as status, error and other registers.
  51. typedef struct tagFIS_REG_D2H
  52. {
  53. // DWORD 0
  54. uint8_t fis_type; // FIS_TYPE_REG_D2H
  55. uint8_t pmport : 4; // Port multiplier
  56. uint8_t rsv0 : 2; // Reserved
  57. uint8_t i : 1; // Interrupt bit
  58. uint8_t rsv1 : 1; // Reserved
  59. uint8_t status; // Status register
  60. uint8_t error; // Error register
  61. // DWORD 1
  62. uint8_t lba0; // LBA low register, 7:0
  63. uint8_t lba1; // LBA mid register, 15:8
  64. uint8_t lba2; // LBA high register, 23:16
  65. uint8_t device; // Device register
  66. // DWORD 2
  67. uint8_t lba3; // LBA register, 31:24
  68. uint8_t lba4; // LBA register, 39:32
  69. uint8_t lba5; // LBA register, 47:40
  70. uint8_t rsv2; // Reserved
  71. // DWORD 3
  72. uint8_t countl; // Count register, 7:0
  73. uint8_t counth; // Count register, 15:8
  74. uint8_t rsv3[2]; // Reserved
  75. // DWORD 4
  76. uint8_t rsv4[4]; // Reserved
  77. } FIS_REG_D2H;
  78. // This FIS is used by the host or device to send data payload. The data size can be varied.
  79. typedef struct tagFIS_DATA
  80. {
  81. // DWORD 0
  82. uint8_t fis_type; // FIS_TYPE_DATA
  83. uint8_t pmport : 4; // Port multiplier
  84. uint8_t rsv0 : 4; // Reserved
  85. uint8_t rsv1[2]; // Reserved
  86. // DWORD 1 ~ N
  87. uint32_t data[1]; // Payload
  88. } FIS_DATA;
  89. // This FIS is used by the device to tell the host that it’s about to send or ready to receive a PIO data payload.
  90. typedef struct tagFIS_PIO_SETUP
  91. {
  92. // DWORD 0
  93. uint8_t fis_type; // FIS_TYPE_PIO_SETUP
  94. uint8_t pmport : 4; // Port multiplier
  95. uint8_t rsv0 : 1; // Reserved
  96. uint8_t d : 1; // Data transfer direction, 1 - device to host
  97. uint8_t i : 1; // Interrupt bit
  98. uint8_t rsv1 : 1;
  99. uint8_t status; // Status register
  100. uint8_t error; // Error register
  101. // DWORD 1
  102. uint8_t lba0; // LBA low register, 7:0
  103. uint8_t lba1; // LBA mid register, 15:8
  104. uint8_t lba2; // LBA high register, 23:16
  105. uint8_t device; // Device register
  106. // DWORD 2
  107. uint8_t lba3; // LBA register, 31:24
  108. uint8_t lba4; // LBA register, 39:32
  109. uint8_t lba5; // LBA register, 47:40
  110. uint8_t rsv2; // Reserved
  111. // DWORD 3
  112. uint8_t countl; // Count register, 7:0
  113. uint8_t counth; // Count register, 15:8
  114. uint8_t rsv3; // Reserved
  115. uint8_t e_status; // New value of status register
  116. // DWORD 4
  117. uint16_t tc; // Transfer count
  118. uint8_t rsv4[2]; // Reserved
  119. } FIS_PIO_SETUP;
  120. typedef struct tagFIS_DMA_SETUP
  121. {
  122. // DWORD 0
  123. uint8_t fis_type; // FIS_TYPE_DMA_SETUP
  124. uint8_t pmport : 4; // Port multiplier
  125. uint8_t rsv0 : 1; // Reserved
  126. uint8_t d : 1; // Data transfer direction, 1 - device to host
  127. uint8_t i : 1; // Interrupt bit
  128. uint8_t a : 1; // Auto-activate. Specifies if DMA Activate FIS is needed
  129. uint8_t rsved[2]; // Reserved
  130. // DWORD 1&2
  131. uint64_t DMAbufferID; // DMA Buffer Identifier. Used to Identify DMA buffer in host memory.
  132. // SATA Spec says host specific and not in Spec. Trying AHCI spec might work.
  133. // DWORD 3
  134. uint32_t rsvd; // More reserved
  135. // DWORD 4
  136. uint32_t DMAbufOffset; // Byte offset into buffer. First 2 bits must be 0
  137. // DWORD 5
  138. uint32_t TransferCount; // Number of bytes to transfer. Bit 0 must be 0
  139. // DWORD 6
  140. uint32_t resvd; // Reserved
  141. } FIS_DMA_SETUP;
  142. typedef volatile struct tagHBA_MEM
  143. {
  144. // 0x00 - 0x2B, Generic Host Control
  145. uint32_t cap; // 0x00, Host capability
  146. uint32_t ghc; // 0x04, Global host control
  147. uint32_t is; // 0x08, Interrupt status
  148. uint32_t pi; // 0x0C, Port implemented
  149. uint32_t vs; // 0x10, Version
  150. uint32_t ccc_ctl; // 0x14, Command completion coalescing control
  151. uint32_t ccc_pts; // 0x18, Command completion coalescing ports
  152. uint32_t em_loc; // 0x1C, Enclosure management location
  153. uint32_t em_ctl; // 0x20, Enclosure management control
  154. uint32_t cap2; // 0x24, Host capabilities extended
  155. uint32_t bohc; // 0x28, BIOS/OS handoff control and status
  156. // 0x2C - 0x9F, Reserved
  157. uint8_t rsv[0xA0-0x2C];
  158. // 0xA0 - 0xFF, Vendor specific registers
  159. uint8_t vendor[0x100-0xA0];
  160. // 0x100 - 0x10FF, Port control registers
  161. HBA_PORT ports[1]; // 1 ~ 32
  162. } HBA_MEM;
  163. typedef volatile struct tagHBA_PORT
  164. {
  165. uint32_t clb; // 0x00, command list base address, 1K-byte aligned
  166. uint32_t clbu; // 0x04, command list base address upper 32 bits
  167. uint32_t fb; // 0x08, FIS base address, 256-byte aligned
  168. uint32_t fbu; // 0x0C, FIS base address upper 32 bits
  169. uint32_t is; // 0x10, interrupt status
  170. uint32_t ie; // 0x14, interrupt enable
  171. uint32_t cmd; // 0x18, command and status
  172. uint32_t rsv0; // 0x1C, Reserved
  173. uint32_t tfd; // 0x20, task file data
  174. uint32_t sig; // 0x24, signature
  175. uint32_t ssts; // 0x28, SATA status (SCR0:SStatus)
  176. uint32_t sctl; // 0x2C, SATA control (SCR2:SControl)
  177. uint32_t serr; // 0x30, SATA error (SCR1:SError)
  178. uint32_t sact; // 0x34, SATA active (SCR3:SActive)
  179. uint32_t ci; // 0x38, command issue
  180. uint32_t sntf; // 0x3C, SATA notification (SCR4:SNotification)
  181. uint32_t fbs; // 0x40, FIS-based switch control
  182. uint32_t rsv1[11]; // 0x44 ~ 0x6F, Reserved
  183. uint32_t vendor[4]; // 0x70 ~ 0x7F, vendor specific
  184. } HBA_PORT;
  185. // There are four kinds of FIS which may be sent to the host by the device as indicated in the following structure declaration.
  186. //
  187. typedef volatile struct tagHBA_FIS
  188. {
  189. // 0x00
  190. FIS_DMA_SETUP dsfis; // DMA Setup FIS
  191. uint8_t pad0[4];
  192. // 0x20
  193. FIS_PIO_SETUP psfis; // PIO Setup FIS
  194. uint8_t pad1[12];
  195. // 0x40
  196. FIS_REG_D2H rfis; // Register – Device to Host FIS
  197. uint8_t pad2[4];
  198. // 0x58
  199. //FIS_DEV_BITS sdbfis; // Set Device Bit FIS
  200. // 0x60
  201. uint8_t ufis[64];
  202. // 0xA0
  203. uint8_t rsv[0x100-0xA0];
  204. } HBA_FIS;
  205. typedef struct tagHBA_CMD_HEADER
  206. {
  207. // DW0
  208. uint8_t cfl:5; // Command FIS length in DWORDS, 2 ~ 16
  209. uint8_t a:1; // ATAPI
  210. uint8_t w:1; // Write, 1: H2D, 0: D2H
  211. uint8_t p:1; // Prefetchable
  212. uint8_t r:1; // Reset
  213. uint8_t b:1; // BIST
  214. uint8_t c:1; // Clear busy upon R_OK
  215. uint8_t rsv0:1; // Reserved
  216. uint8_t pmp:4; // Port multiplier port
  217. uint16_t prdtl; // Physical region descriptor table length in entries
  218. // DW1
  219. volatile
  220. uint32_t prdbc; // Physical region descriptor byte count transferred
  221. // DW2, 3
  222. uint32_t ctba; // Command table descriptor base address
  223. uint32_t ctbau; // Command table descriptor base address upper 32 bits
  224. // DW4 - 7
  225. uint32_t rsv1[4]; // Reserved
  226. } HBA_CMD_HEADER;
  227. typedef struct tagHBA_CMD_TBL
  228. {
  229. // 0x00
  230. uint8_t cfis[64]; // Command FIS
  231. // 0x40
  232. uint8_t acmd[16]; // ATAPI command, 12 or 16 bytes
  233. // 0x50
  234. uint8_t rsv[48]; // Reserved
  235. // 0x80
  236. HBA_PRDT_ENTRY prdt_entry[1]; // Physical region descriptor table entries, 0 ~ 65535
  237. } HBA_CMD_TBL;
  238. typedef struct tagHBA_PRDT_ENTRY
  239. {
  240. uint32_t dba; // Data base address
  241. uint32_t dbau; // Data base address upper 32 bits
  242. uint32_t rsv0; // Reserved
  243. // DW3
  244. uint32_t dbc:22; // Byte count, 4M max
  245. uint32_t rsv1:9; // Reserved
  246. uint32_t i:1; // Interrupt on completion
  247. } HBA_PRDT_ENTRY;
  248. struct block_device_request_queue ahci_req_queue;
  249. struct block_device_operation ahci_operation =
  250. {
  251. .open = ahci_open,
  252. .close = ahci_close,
  253. .ioctl = ahci_ioctl,
  254. .transfer = ahci_transfer,
  255. };