feat: disk read/write support for both ATA and ATAPI device
[lunaix-os.git] / lunaix-os / includes / hal / ahci / sata.h
1 #ifndef __LUNAIX_SATA_H
2 #define __LUNAIX_SATA_H
3
4 #include <stdint.h>
5
6 #define SATA_REG_FIS_D2H 0x34
7 #define SATA_REG_FIS_H2D 0x27
8 #define SATA_REG_FIS_COMMAND 0x80
9 #define SATA_LBA_COMPONENT(lba, offset) ((((lba) >> (offset)) & 0xff))
10
11 #define ATA_IDENTIFY_DEVICE 0xec
12 #define ATA_IDENTIFY_PAKCET_DEVICE 0xa1
13 #define ATA_PACKET 0xa0
14 #define ATA_READ_DMA_EXT 0xc8
15 #define ATA_READ_DMA 0x25
16 #define ATA_WRITE_DMA_EXT 0xca
17 #define ATA_WRITE_DMA 0x35
18
19 struct sata_fis_head
20 {
21     uint8_t type;
22     uint8_t options;
23     uint8_t status_cmd;
24     uint8_t feat_err;
25 } __HBA_PACKED__;
26
27 struct sata_reg_fis
28 {
29     struct sata_fis_head head;
30
31     uint8_t lba0, lba8, lba16;
32     uint8_t dev;
33     uint8_t lba24, lba32, lba40;
34     uint8_t reserved1;
35
36     uint16_t count;
37
38     uint8_t reserved[6];
39 } __HBA_PACKED__;
40
41 struct sata_data_fis
42 {
43     struct sata_fis_head head;
44
45     uint8_t data[0];
46 } __HBA_PACKED__;
47
48 void
49 sata_create_fis(struct sata_reg_fis* cmd_fis,
50                 uint8_t command,
51                 uint64_t lba,
52                 uint16_t sector_count);
53
54 int
55 sata_read_buffer(struct hba_port* port,
56                  uint64_t lba,
57                  void* buffer,
58                  uint32_t size);
59
60 int
61 sata_write_buffer(struct hba_port* port,
62                   uint64_t lba,
63                   void* buffer,
64                   uint32_t size);
65
66 #endif /* __LUNAIX_SATA_H */