ahci.h 11 KB

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