feat: (ahci) support multiple AHCI controller
[lunaix-os.git] / lunaix-os / includes / hal / ahci / hba.h
1 #ifndef __LUNAIX_HBA_H
2 #define __LUNAIX_HBA_H
3
4 #include <lunaix/blkio.h>
5 #include <lunaix/buffer.h>
6 #include <lunaix/types.h>
7
8 #define HBA_RCAP 0
9 #define HBA_RGHC 1
10 #define HBA_RIS 2
11 #define HBA_RPI 3
12 #define HBA_RVER 4
13
14 #define HBA_RPBASE (0x40)
15 #define HBA_RPSIZE (0x80 >> 2)
16 #define HBA_RPxCLB 0
17 #define HBA_RPxFB 2
18 #define HBA_RPxIS 4
19 #define HBA_RPxIE 5
20 #define HBA_RPxCMD 6
21 #define HBA_RPxTFD 8
22 #define HBA_RPxSIG 9
23 #define HBA_RPxSSTS 10
24 #define HBA_RPxSCTL 11
25 #define HBA_RPxSERR 12
26 #define HBA_RPxSACT 13
27 #define HBA_RPxCI 14
28 #define HBA_RPxSNTF 15
29 #define HBA_RPxFBS 16
30
31 #define HBA_PxCMD_FRE (1 << 4)
32 #define HBA_PxCMD_CR (1 << 15)
33 #define HBA_PxCMD_FR (1 << 14)
34 #define HBA_PxCMD_ST (1)
35 #define HBA_PxINTR_DMA (1 << 2)
36 #define HBA_PxINTR_DHR (1)
37 #define HBA_PxINTR_DPS (1 << 5)
38 #define HBA_PxINTR_TFEE (1 << 30)
39 #define HBA_PxINTR_OFE (1 << 24)
40 #define HBA_PxINTR_IFE (1 << 27)
41 #define HBA_PxTFD_ERR (1)
42 #define HBA_PxTFD_BSY (1 << 7)
43 #define HBA_PxTFD_DRQ (1 << 3)
44
45 #define HBA_RGHC_ACHI_ENABLE (1 << 31)
46 #define HBA_RGHC_INTR_ENABLE (1 << 1)
47 #define HBA_RGHC_RESET 1
48
49 #define HBA_RPxSSTS_PWR(x) (((x) >> 8) & 0xf)
50 #define HBA_RPxSSTS_IF(x) (((x) >> 4) & 0xf)
51 #define HBA_RPxSSTS_PHYSTATE(x) ((x)&0xf)
52
53 #define hba_clear_reg(reg) (reg) = -1
54
55 #define HBA_DEV_SIG_ATAPI 0xeb140101
56 #define HBA_DEV_SIG_ATA 0x00000101
57
58 #define __HBA_PACKED__ __attribute__((packed))
59
60 typedef unsigned int hba_reg_t;
61
62 #define HBA_CMDH_FIS_LEN(fis_bytes) (((fis_bytes) / 4) & 0x1f)
63 #define HBA_CMDH_ATAPI (1 << 5)
64 #define HBA_CMDH_WRITE (1 << 6)
65 #define HBA_CMDH_PREFETCH (1 << 7)
66 #define HBA_CMDH_R (1 << 8)
67 #define HBA_CMDH_CLR_BUSY (1 << 10)
68 #define HBA_CMDH_PRDT_LEN(entries) (((entries)&0xffff) << 16)
69
70 #define HBA_MAX_PRDTE 4
71
72 struct hba_cmdh
73 {
74     uint16_t options;
75     uint16_t prdt_len;
76     uint32_t transferred_size;
77     uint32_t cmd_table_base;
78     uint32_t reserved[5];
79 } __HBA_PACKED__;
80
81 #define HBA_PRDTE_BYTE_CNT(cnt) ((cnt & 0x3FFFFF) | 0x1)
82
83 struct hba_prdte
84 {
85     uint32_t data_base;
86     uint32_t reserved[2];
87     uint32_t byte_count;
88 } __HBA_PACKED__;
89
90 struct hba_cmdt
91 {
92     uint8_t command_fis[64];
93     uint8_t atapi_cmd[16];
94     uint8_t reserved[0x30];
95     struct hba_prdte entries[HBA_MAX_PRDTE];
96 } __HBA_PACKED__;
97
98 #define HBA_DEV_FEXTLBA 1
99 #define HBA_DEV_FATAPI (1 << 1)
100
101 struct hba_port;
102 struct ahci_hba;
103
104 struct hba_device
105 {
106     char serial_num[20];
107     char model[40];
108     uint32_t flags;
109     uint64_t max_lba;
110     uint32_t block_size;
111     uint64_t wwn;
112     uint8_t cbd_size;
113     struct
114     {
115         uint8_t sense_key;
116         uint8_t error;
117         uint8_t status;
118         uint8_t reserve;
119     } last_result;
120     uint32_t alignment_offset;
121     uint32_t block_per_sec;
122     uint32_t capabilities;
123     struct hba_port* port;
124     struct ahci_hba* hba;
125
126     struct
127     {
128         int (*identify)(struct hba_device* dev);
129         void (*submit)(struct hba_device* dev, struct blkio_req* io_req);
130     } ops;
131 };
132
133 struct hba_cmd_state
134 {
135     struct hba_cmdt* cmd_table;
136     void* state_ctx;
137 };
138
139 struct hba_cmd_context
140 {
141     struct hba_cmd_state* issued[32];
142     u32_t tracked_ci;
143 };
144
145 struct hba_port
146 {
147     volatile hba_reg_t* regs;
148     unsigned int ssts;
149     struct hba_cmdh* cmdlst;
150     struct hba_cmd_context cmdctx;
151     void* fis;
152     struct hba_device* device;
153     struct ahci_hba* hba;
154 };
155
156 struct ahci_hba
157 {
158     volatile hba_reg_t* base;
159     unsigned int ports_num;
160     unsigned int ports_bmp;
161     unsigned int cmd_slots;
162     unsigned int version;
163     struct hba_port* ports[32];
164 };
165
166 int
167 hba_prepare_cmd(struct hba_port* port,
168                 struct hba_cmdt** cmdt,
169                 struct hba_cmdh** cmdh);
170
171 int
172 hba_bind_vbuf(struct hba_cmdh* cmdh,
173               struct hba_cmdt* cmdt,
174               struct vecbuf* vbuf);
175
176 int
177 hba_bind_sbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct membuf mbuf);
178
179 #endif /* __LUNAIX_HBA_H */