ahci.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. /* i tried to keep this as simple as i can... */
  23. /* pci-configure.bar5 points to this, and this points to another things */
  24. /*
  25. * PCI-config.bar5 -> AHCI HBA mem
  26. * AHCI HBA mem -> AHCI HBA ports (max 32)
  27. * AHCI_HBA ports -> command list (32 command headers)
  28. * command list [32] -> command headers
  29. * command headers -> command table
  30. * command table -> prdts
  31. * prdts [65536] (optional) -> data buffer
  32. * command table -> fis
  33. * fis -> lba addresses and others
  34. * */
  35. struct ahci_hba_port {
  36. __uint64_t cmd_list;
  37. /* received fis base */
  38. __uint64_t rfis_base;
  39. __uint32_t int_status;
  40. __uint32_t int_enable;
  41. __uint32_t cmd;
  42. __uint32_t rsvd0[3];
  43. __uint32_t sata_status;
  44. __uint32_t sata_ctl;
  45. __uint32_t sata_error;
  46. /* these look like bitmaps */
  47. __uint32_t sata_active;
  48. __uint32_t cmd_issue;
  49. __uint32_t rsvd[17];
  50. } __attribute__ ((packed));
  51. struct ahci_hba_mem {
  52. #define HBA_MEM_CAP_MASK 0x1f
  53. __uint32_t cap;
  54. #define HBA_MEM_GHC_INT_ENABLE 0x00000002
  55. #define HBA_MEM_GHC_AHCI_ENABLE 0x80000000
  56. __uint32_t global_ctl;
  57. __uint32_t int_status;
  58. /* ports implemented */
  59. __uint32_t ports_impled;
  60. __uint32_t rsvd0[6];
  61. /* ? */
  62. #define HBA_MEM_BHO_OWN_BIOS 1
  63. #define HBA_MEM_BHO_OWN_OS 2
  64. #define HBA_MEM_BHO_OWN_CHANGED 8
  65. #define HBA_MEM_BHO_OWN_RWC 8
  66. __uint32_t bios_handoff;
  67. __uint32_t rsvd1[53];
  68. struct ahci_hba_port ports[32];
  69. } __attribute__ ((packed));
  70. struct ahci_cmd_header {
  71. #define CMDHDR_CFG_CFISLEN_SHIFT 0
  72. #define CMDHDR_CFG_CFISLEN_MASK ((1 << 5) - 1)
  73. /* the mask for config */
  74. #define CMDHDR_CFG_ATAPI 0x20
  75. #define CMDHDR_CFG_WRITE 0x40
  76. #define CMDHDR_CFG_PREFETCHABLE 0x80
  77. #define CMDHDR_CFG_RESET 0x100
  78. #define CMDHDR_CFG_BIST 0x200
  79. #define CMDHDR_CFG_CLEAR_R_OK 0x400
  80. #define CMDHDR_CFG_PORTML_MASK 0xf000
  81. #define CMDHDR_CFG_PORTML_SHIFT 12
  82. #define CMDHDR_CFG_PRDTLEN_MASK 0xffff0000
  83. #define CMDHDR_CFG_PRDTLEN_SHIFT 16
  84. #define CMDHDR_CFG_INTR 0x800000000
  85. /* config word and prdt length (word) */
  86. __uint32_t config;
  87. /* total data length we transferred */
  88. __uint32_t transfered;
  89. __uint64_t cmd_table;
  90. __uint32_t rsvd[4];
  91. } __attribute__((packed));
  92. struct ahci_prdt_entry {
  93. __uint64_t buf;
  94. __uint32_t rsvd;
  95. /* size (of this.buf) */
  96. #define PRDT_SIZE_MASK ((1 << 22) - 1)
  97. /* interrupt on completion */
  98. #define PRDT_SIZE_INTR (0x80000000u)
  99. __uint32_t size;
  100. } __attribute__ ((packed));
  101. struct ahci_cmd_table {
  102. __uint8_t cmd_fis[64];
  103. __uint8_t atapi_cmd[16];
  104. __uint32_t rsvd[0x0c];
  105. /* max 65536 */
  106. /* TODO: support more prdt-entries */
  107. struct ahci_prdt_entry prdts[1];
  108. } __attribute__ ((packed));
  109. struct fis_h2d {
  110. __uint8_t fis_type;
  111. #define FISH2D_PORTMUL_MASK 15
  112. #define FISH2D_PORTMUL_SHIFT 0
  113. #define FISH2D_CMD 0x80
  114. __uint8_t config;
  115. __uint8_t command;
  116. __uint8_t lba0, lba1, lba2;
  117. __uint8_t dev;
  118. __uint8_t lba3, lba4, lba5;
  119. __uint8_t feature0;
  120. __uint16_t nsectors;
  121. __uint8_t icc;
  122. __uint8_t control;
  123. __uint32_t rsvd;
  124. } __attribute__ ((packed));
  125. #define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
  126. #define ATACMD_ID 0xec
  127. #define ATACMD_RD 0x20
  128. #define ATACMD_RD48 0x24
  129. #define ATACMD_WR 0x30
  130. #define ATACMD_WR48 0x34
  131. #define ATASTAT_READY 0x40
  132. #define ATASTAT_ERR 0x01
  133. #define ATASTAT_BUSY 0x80
  134. #define ATACTL_INTDIS 0x02
  135. #define ATACTL_RESET 0x04
  136. #define ATAERR_OTHER 0x01
  137. #define ATAERR_TRACK0 0x02
  138. #define ATAERR_ABORT 0x04
  139. #define ATAERR_NOTFOUND 0x10
  140. #define ATAERR_DATAERR 0x40
  141. #define ATAERR_BADSECT 0x80
  142. extern volatile struct ahci_hba_mem *hba;
  143. int ahci_rw_sect(volatile struct ahci_hba_port *, int, char *, __uint64_t);
  144. void do_ahci_intr(void);