refactor: change the disk io api to accept device instead of port struct
[lunaix-os.git] / lunaix-os / includes / hal / ahci / hba.h
1 #ifndef __LUNAIX_HBA_H
2 #define __LUNAIX_HBA_H
3
4 #include <stdint.h>
5
6 #define HBA_RCAP 0
7 #define HBA_RGHC 1
8 #define HBA_RIS 2
9 #define HBA_RPI 3
10 #define HBA_RVER 4
11
12 #define HBA_RPBASE (0x40)
13 #define HBA_RPSIZE (0x80 >> 2)
14 #define HBA_RPxCLB 0
15 #define HBA_RPxFB 2
16 #define HBA_RPxIS 4
17 #define HBA_RPxIE 5
18 #define HBA_RPxCMD 6
19 #define HBA_RPxTFD 8
20 #define HBA_RPxSIG 9
21 #define HBA_RPxSSTS 10
22 #define HBA_RPxSCTL 11
23 #define HBA_RPxSERR 12
24 #define HBA_RPxSACT 13
25 #define HBA_RPxCI 14
26 #define HBA_RPxSNTF 15
27 #define HBA_RPxFBS 16
28
29 #define HBA_PxCMD_FRE (1 << 4)
30 #define HBA_PxCMD_CR (1 << 15)
31 #define HBA_PxCMD_FR (1 << 14)
32 #define HBA_PxCMD_ST (1)
33 #define HBA_PxINTR_DMA (1 << 2)
34 #define HBA_PxINTR_D2HR (1)
35 #define HBA_PxINTR_DPE (1 << 5)
36 #define HBA_PxTFD_ERR (1)
37 #define HBA_PxTFD_BSY (1 << 7)
38 #define HBA_PxTFD_DRQ (1 << 3)
39
40 #define HBA_RGHC_ACHI_ENABLE (1 << 31)
41 #define HBA_RGHC_INTR_ENABLE (1 << 1)
42 #define HBA_RGHC_RESET 1
43
44 #define HBA_RPxSSTS_PWR(x) (((x) >> 8) & 0xf)
45 #define HBA_RPxSSTS_IF(x) (((x) >> 4) & 0xf)
46 #define HBA_RPxSSTS_PHYSTATE(x) ((x)&0xf)
47
48 #define HBA_DEV_SIG_ATAPI 0xeb140101
49 #define HBA_DEV_SIG_ATA 0x00000101
50
51 #define __HBA_PACKED__ __attribute__((packed))
52
53 typedef unsigned int hba_reg_t;
54
55 #define HBA_CMDH_FIS_LEN(fis_bytes) (((fis_bytes) / 4) & 0x1f)
56 #define HBA_CMDH_ATAPI (1 << 5)
57 #define HBA_CMDH_WRITE (1 << 6)
58 #define HBA_CMDH_PREFETCH (1 << 7)
59 #define HBA_CMDH_R (1 << 8)
60 #define HBA_CMDH_CLR_BUSY (1 << 10)
61 #define HBA_CMDH_PRDT_LEN(entries) (((entries)&0xffff) << 16)
62
63 struct hba_cmdh
64 {
65     uint16_t options;
66     uint16_t prdt_len;
67     uint32_t transferred_size;
68     uint32_t cmd_table_base;
69     uint32_t reserved[5];
70 } __HBA_PACKED__;
71
72 #define HBA_PRDTE_BYTE_CNT(cnt) ((cnt & 0x3FFFFF) | 0x1)
73
74 struct hba_prdte
75 {
76     uint32_t data_base;
77     uint32_t reserved[2];
78     uint32_t byte_count;
79 } __HBA_PACKED__;
80
81 struct hba_cmdt
82 {
83     uint8_t command_fis[64];
84     uint8_t atapi_cmd[16];
85     uint8_t reserved[0x30];
86     struct hba_prdte entries[3];
87 } __HBA_PACKED__;
88
89 #define HBA_DEV_FEXTLBA 1
90 #define HBA_DEV_FATAPI (1 << 1)
91
92 struct hba_port;
93
94 struct hba_device
95 {
96     char serial_num[20];
97     char model[40];
98     uint32_t flags;
99     uint64_t max_lba;
100     uint32_t block_size;
101     uint64_t wwn;
102     uint8_t cbd_size;
103     uint8_t last_error;
104     uint8_t last_status;
105     uint32_t alignment_offset;
106     uint32_t block_per_sec;
107     uint32_t capabilities;
108     struct hba_port* port;
109
110     struct
111     {
112         int (*identify)(struct hba_device* dev);
113         int (*read_buffer)(struct hba_device* dev,
114                            uint64_t lba,
115                            void* buffer,
116                            uint32_t size);
117         int (*write_buffer)(struct hba_device* dev,
118                             uint64_t lba,
119                             void* buffer,
120                             uint32_t size);
121     } ops;
122 };
123
124 struct hba_port
125 {
126     volatile hba_reg_t* regs;
127     unsigned int ssts;
128     struct hba_cmdh* cmdlst;
129     void* fis;
130     struct hba_device* device;
131 };
132
133 struct ahci_hba
134 {
135     volatile hba_reg_t* base;
136     unsigned int ports_num;
137     unsigned int ports_bmp;
138     unsigned int cmd_slots;
139     unsigned int version;
140     struct hba_port* ports[32];
141 };
142
143 int
144 hba_prepare_cmd(struct hba_port* port,
145                 struct hba_cmdt** cmdt,
146                 struct hba_cmdh** cmdh,
147                 void* buffer,
148                 unsigned int size);
149
150 #endif /* __LUNAIX_HBA_H */