ahci.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #pragma once
  2. #include "../block_device.h"
  3. #include "../../pci/pci.h"
  4. #include "../../../mm/mm.h"
  5. #define AHCI_MAPPING_BASE SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE+AHCI_MAPPING_OFFSET
  6. #define MAX_AHCI_DEVICES 100
  7. #define HBA_PxCMD_ST 0x0001
  8. #define HBA_PxCMD_FRE 0x0010
  9. #define HBA_PxCMD_FR 0x4000
  10. #define HBA_PxCMD_CR 0x8000
  11. #define ATA_DEV_BUSY 0x80
  12. #define ATA_DEV_DRQ 0x08
  13. #define ATA_CMD_READ_DMA_EXT 0x25
  14. #define ATA_CMD_WRITE_DMA_EXT 0x30
  15. #define HBA_PxIS_TFES (1 << 30) /* TFES - Task File Error Status */
  16. /**
  17. * @brief 在SATA3.0规范中定义的Frame Information Structure类型
  18. *
  19. */
  20. typedef enum
  21. {
  22. FIS_TYPE_REG_H2D = 0x27, // Register FIS - host to device
  23. FIS_TYPE_REG_D2H = 0x34, // Register FIS - device to host
  24. FIS_TYPE_DMA_ACT = 0x39, // DMA activate FIS - device to host
  25. FIS_TYPE_DMA_SETUP = 0x41, // DMA setup FIS - bidirectional
  26. FIS_TYPE_DATA = 0x46, // Data FIS - bidirectional
  27. FIS_TYPE_BIST = 0x58, // BIST activate FIS - bidirectional
  28. FIS_TYPE_PIO_SETUP = 0x5F, // PIO setup FIS - device to host
  29. FIS_TYPE_DEV_BITS = 0xA1, // Set device bits FIS - device to host
  30. } FIS_TYPE;
  31. /**
  32. * @brief FIS_REG_H2D 被用于从主机向设备发送控制命令
  33. * 注意:reserved bit应当被清零
  34. */
  35. typedef struct tagFIS_REG_H2D
  36. {
  37. // DWORD 0
  38. uint8_t fis_type; // FIS_TYPE_REG_H2D
  39. uint8_t pmport : 4; // Port multiplier
  40. uint8_t rsv0 : 3; // Reserved
  41. uint8_t c : 1; // 1: Command, 0: Control
  42. uint8_t command; // Command register
  43. uint8_t featurel; // Feature register, 7:0
  44. // DWORD 1
  45. uint8_t lba0; // LBA low register, 7:0
  46. uint8_t lba1; // LBA mid register, 15:8
  47. uint8_t lba2; // LBA high register, 23:16
  48. uint8_t device; // Device register
  49. // DWORD 2
  50. uint8_t lba3; // LBA register, 31:24
  51. uint8_t lba4; // LBA register, 39:32
  52. uint8_t lba5; // LBA register, 47:40
  53. uint8_t featureh; // Feature register, 15:8
  54. // DWORD 3
  55. uint8_t countl; // Count register, 7:0
  56. uint8_t counth; // Count register, 15:8
  57. uint8_t icc; // Isochronous command completion
  58. uint8_t control; // Control register
  59. // DWORD 4
  60. uint8_t rsv1[4]; // Reserved
  61. } FIS_REG_H2D;
  62. // A device to host register FIS is used by the device to notify the host that some ATA register has changed.
  63. // It contains the updated task files such as status, error and other registers.
  64. typedef struct tagFIS_REG_D2H
  65. {
  66. // DWORD 0
  67. uint8_t fis_type; // FIS_TYPE_REG_D2H
  68. uint8_t pmport : 4; // Port multiplier
  69. uint8_t rsv0 : 2; // Reserved
  70. uint8_t i : 1; // Interrupt bit
  71. uint8_t rsv1 : 1; // Reserved
  72. uint8_t status; // Status register
  73. uint8_t error; // Error register
  74. // DWORD 1
  75. uint8_t lba0; // LBA low register, 7:0
  76. uint8_t lba1; // LBA mid register, 15:8
  77. uint8_t lba2; // LBA high register, 23:16
  78. uint8_t device; // Device register
  79. // DWORD 2
  80. uint8_t lba3; // LBA register, 31:24
  81. uint8_t lba4; // LBA register, 39:32
  82. uint8_t lba5; // LBA register, 47:40
  83. uint8_t rsv2; // Reserved
  84. // DWORD 3
  85. uint8_t countl; // Count register, 7:0
  86. uint8_t counth; // Count register, 15:8
  87. uint8_t rsv3[2]; // Reserved
  88. // DWORD 4
  89. uint8_t rsv4[4]; // Reserved
  90. } FIS_REG_D2H;
  91. // This FIS is used by the host or device to send data payload. The data size can be varied.
  92. typedef struct tagFIS_DATA
  93. {
  94. // DWORD 0
  95. uint8_t fis_type; // FIS_TYPE_DATA
  96. uint8_t pmport : 4; // Port multiplier
  97. uint8_t rsv0 : 4; // Reserved
  98. uint8_t rsv1[2]; // Reserved
  99. // DWORD 1 ~ N
  100. uint32_t data[1]; // Payload
  101. } FIS_DATA;
  102. // 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.
  103. typedef struct tagFIS_PIO_SETUP
  104. {
  105. // DWORD 0
  106. uint8_t fis_type; // FIS_TYPE_PIO_SETUP
  107. uint8_t pmport : 4; // Port multiplier
  108. uint8_t rsv0 : 1; // Reserved
  109. uint8_t d : 1; // Data transfer direction, 1 - device to host
  110. uint8_t i : 1; // Interrupt bit
  111. uint8_t rsv1 : 1;
  112. uint8_t status; // Status register
  113. uint8_t error; // Error register
  114. // DWORD 1
  115. uint8_t lba0; // LBA low register, 7:0
  116. uint8_t lba1; // LBA mid register, 15:8
  117. uint8_t lba2; // LBA high register, 23:16
  118. uint8_t device; // Device register
  119. // DWORD 2
  120. uint8_t lba3; // LBA register, 31:24
  121. uint8_t lba4; // LBA register, 39:32
  122. uint8_t lba5; // LBA register, 47:40
  123. uint8_t rsv2; // Reserved
  124. // DWORD 3
  125. uint8_t countl; // Count register, 7:0
  126. uint8_t counth; // Count register, 15:8
  127. uint8_t rsv3; // Reserved
  128. uint8_t e_status; // New value of status register
  129. // DWORD 4
  130. uint16_t tc; // Transfer count
  131. uint8_t rsv4[2]; // Reserved
  132. } FIS_PIO_SETUP;
  133. typedef struct tagFIS_DMA_SETUP
  134. {
  135. // DWORD 0
  136. uint8_t fis_type; // FIS_TYPE_DMA_SETUP
  137. uint8_t pmport : 4; // Port multiplier
  138. uint8_t rsv0 : 1; // Reserved
  139. uint8_t d : 1; // Data transfer direction, 1 - device to host
  140. uint8_t i : 1; // Interrupt bit
  141. uint8_t a : 1; // Auto-activate. Specifies if DMA Activate FIS is needed
  142. uint8_t rsved[2]; // Reserved
  143. // DWORD 1&2
  144. uint64_t DMAbufferID; // DMA Buffer Identifier. Used to Identify DMA buffer in host memory.
  145. // SATA Spec says host specific and not in Spec. Trying AHCI spec might work.
  146. // DWORD 3
  147. uint32_t rsvd; // More reserved
  148. // DWORD 4
  149. uint32_t DMAbufOffset; // Byte offset into buffer. First 2 bits must be 0
  150. // DWORD 5
  151. uint32_t TransferCount; // Number of bytes to transfer. Bit 0 must be 0
  152. // DWORD 6
  153. uint32_t resvd; // Reserved
  154. } FIS_DMA_SETUP;
  155. typedef volatile struct tagHBA_PORT
  156. {
  157. uint64_t clb; // 0x00, command list base address, 1K-byte aligned
  158. uint64_t fb; // 0x08, FIS base address, 256-byte aligned
  159. uint32_t is; // 0x10, interrupt status
  160. uint32_t ie; // 0x14, interrupt enable
  161. uint32_t cmd; // 0x18, command and status
  162. uint32_t rsv0; // 0x1C, Reserved
  163. uint32_t tfd; // 0x20, task file data
  164. uint32_t sig; // 0x24, signature
  165. uint32_t ssts; // 0x28, SATA status (SCR0:SStatus)
  166. uint32_t sctl; // 0x2C, SATA control (SCR2:SControl)
  167. uint32_t serr; // 0x30, SATA error (SCR1:SError)
  168. uint32_t sact; // 0x34, SATA active (SCR3:SActive)
  169. uint32_t ci; // 0x38, command issue
  170. uint32_t sntf; // 0x3C, SATA notification (SCR4:SNotification)
  171. uint32_t fbs; // 0x40, FIS-based switch control
  172. uint32_t rsv1[11]; // 0x44 ~ 0x6F, Reserved
  173. uint32_t vendor[4]; // 0x70 ~ 0x7F, vendor specific
  174. } HBA_PORT;
  175. typedef volatile struct tagHBA_MEM
  176. {
  177. // 0x00 - 0x2B, Generic Host Control
  178. uint32_t cap; // 0x00, Host capability
  179. uint32_t ghc; // 0x04, Global host control
  180. uint32_t is; // 0x08, Interrupt status
  181. uint32_t pi; // 0x0C, Port implemented
  182. uint32_t vs; // 0x10, Version
  183. uint32_t ccc_ctl; // 0x14, Command completion coalescing control
  184. uint32_t ccc_pts; // 0x18, Command completion coalescing ports
  185. uint32_t em_loc; // 0x1C, Enclosure management location
  186. uint32_t em_ctl; // 0x20, Enclosure management control
  187. uint32_t cap2; // 0x24, Host capabilities extended
  188. uint32_t bohc; // 0x28, BIOS/OS handoff control and status
  189. // 0x2C - 0x9F, Reserved
  190. uint8_t rsv[0xA0-0x2C];
  191. // 0xA0 - 0xFF, Vendor specific registers
  192. uint8_t vendor[0x100-0xA0];
  193. // 0x100 - 0x10FF, Port control registers
  194. HBA_PORT ports[32]; // 1 ~ 32
  195. } HBA_MEM;
  196. // There are four kinds of FIS which may be sent to the host by the device as indicated in the following structure declaration.
  197. //
  198. typedef volatile struct tagHBA_FIS
  199. {
  200. // 0x00
  201. FIS_DMA_SETUP dsfis; // DMA Setup FIS
  202. uint8_t pad0[4];
  203. // 0x20
  204. FIS_PIO_SETUP psfis; // PIO Setup FIS
  205. uint8_t pad1[12];
  206. // 0x40
  207. FIS_REG_D2H rfis; // Register – Device to Host FIS
  208. uint8_t pad2[4];
  209. // 0x58
  210. //FIS_DEV_BITS sdbfis; // Set Device Bit FIS
  211. // 0x60
  212. uint8_t ufis[64];
  213. // 0xA0
  214. uint8_t rsv[0x100-0xA0];
  215. } HBA_FIS;
  216. typedef struct tagHBA_CMD_HEADER
  217. {
  218. // DW0
  219. uint8_t cfl:5; // Command FIS length in DWORDS, 2 ~ 16
  220. uint8_t a:1; // ATAPI
  221. uint8_t w:1; // Write, 1: H2D, 0: D2H
  222. uint8_t p:1; // Prefetchable
  223. uint8_t r:1; // Reset
  224. uint8_t b:1; // BIST
  225. uint8_t c:1; // Clear busy upon R_OK
  226. uint8_t rsv0:1; // Reserved
  227. uint8_t pmp:4; // Port multiplier port
  228. uint16_t prdtl; // Physical region descriptor table length in entries
  229. // DW1
  230. volatile
  231. uint32_t prdbc; // Physical region descriptor byte count transferred
  232. // DW2, 3
  233. uint64_t ctba; // Command table descriptor base address
  234. // DW4 - 7
  235. uint32_t rsv1[4]; // Reserved
  236. } HBA_CMD_HEADER;
  237. typedef struct tagHBA_PRDT_ENTRY
  238. {
  239. uint64_t dba; // Data base address
  240. uint32_t rsv0; // Reserved
  241. // DW3
  242. uint32_t dbc:22; // Byte count, 4M max
  243. uint32_t rsv1:9; // Reserved
  244. uint32_t i:1; // Interrupt on completion
  245. } HBA_PRDT_ENTRY;
  246. typedef struct tagHBA_CMD_TBL
  247. {
  248. // 0x00
  249. uint8_t cfis[64]; // Command FIS
  250. // 0x40
  251. uint8_t acmd[16]; // ATAPI command, 12 or 16 bytes
  252. // 0x50
  253. uint8_t rsv[48]; // Reserved
  254. // 0x80
  255. HBA_PRDT_ENTRY prdt_entry[1]; // Physical region descriptor table entries, 0 ~ 65535
  256. } HBA_CMD_TBL;
  257. struct ahci_device_t
  258. {
  259. uint32_t type; // 设备类型
  260. struct pci_device_structure_header_t * dev_struct;
  261. HBA_MEM * hba_mem;
  262. }ahci_devices[MAX_AHCI_DEVICES];
  263. #define SATA_SIG_ATA 0x00000101 // SATA drive
  264. #define SATA_SIG_ATAPI 0xEB140101 // SATAPI drive
  265. #define SATA_SIG_SEMB 0xC33C0101 // Enclosure management bridge
  266. #define SATA_SIG_PM 0x96690101 // Port multiplier
  267. #define AHCI_DEV_NULL 0
  268. #define AHCI_DEV_SATA 1
  269. #define AHCI_DEV_SEMB 2
  270. #define AHCI_DEV_PM 3
  271. #define AHCI_DEV_SATAPI 4
  272. #define HBA_PORT_IPM_ACTIVE 1
  273. #define HBA_PORT_DET_PRESENT 3
  274. struct block_device_request_queue ahci_req_queue;
  275. /*
  276. struct block_device_operation ahci_operation =
  277. {
  278. .open = ahci_open,
  279. .close = ahci_close,
  280. .ioctl = ahci_ioctl,
  281. .transfer = ahci_transfer,
  282. };
  283. */
  284. /**
  285. * @brief 初始化ahci模块
  286. *
  287. */
  288. void ahci_init();
  289. /**
  290. * @brief 检测端口连接的设备的类型
  291. *
  292. * @param device_num ahci设备号
  293. */
  294. void ahci_probe_port(const uint32_t device_num);
  295. /**
  296. * @brief read data from SATA device using 48bit LBA address
  297. *
  298. * @param port HBA PORT
  299. * @param startl low 32bits of start addr
  300. * @param starth high 32bits of start addr
  301. * @param count total sectors to read
  302. * @param buf buffer
  303. * @return true done
  304. * @return false failed
  305. */
  306. bool ahci_read(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t count, uint64_t buf);
  307. /**
  308. * @brief write data to SATA device using 48bit LBA address
  309. *
  310. * @param port HBA PORT
  311. * @param startl low 32bits of start addr
  312. * @param starth high 32bits of start addr
  313. * @param count total sectors to read
  314. * @param buf buffer
  315. * @return true done
  316. * @return false failed
  317. */
  318. bool ahci_write(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t count,
  319. uint64_t buf);