refactor: send the command with retry and error detection
[lunaix-os.git] / lunaix-os / includes / hal / ahci / scsi.h
1 #ifndef __LUNAIX_SCSI_H
2 #define __LUNAIX_SCSI_H
3
4 #include <stdint.h>
5
6 #define SCSI_FLIP(val)                                                         \
7     ((((val)&0x000000ff) << 24) | (((val)&0x0000ff00) << 8) |                  \
8      (((val)&0x00ff0000) >> 8) | (((val)&0xff000000) >> 24))
9
10 #define SCSI_READ_CAPACITY_16 0x9e
11 #define SCSI_READ_CAPACITY_10 0x25
12 #define SCSI_READ_BLOCKS_16 0x88
13 #define SCSI_READ_BLOCKS_12 0xa8
14 #define SCSI_WRITE_BLOCKS_16 0x8a
15 #define SCSI_WRITE_BLOCKS_12 0xaa
16
17 #define SCSI_CDB16 16
18 #define SCSI_CDB12 12
19
20 struct scsi_cdb12
21 {
22     uint8_t opcode;
23     uint8_t misc1;
24     uint32_t lba_be;
25     uint32_t length;
26     uint8_t misc2;
27     uint8_t ctrl;
28 } __attribute__((packed));
29
30 struct scsi_cdb16
31 {
32     uint8_t opcode;
33     uint8_t misc1;
34     uint32_t lba_be_hi;
35     uint32_t lba_be_lo;
36     uint32_t length;
37     uint8_t misc2;
38     uint8_t ctrl;
39 } __attribute__((packed));
40
41 void
42 scsi_create_packet12(struct scsi_cdb12* cdb,
43                      uint8_t opcode,
44                      uint32_t lba,
45                      uint32_t alloc_size);
46
47 void
48 scsi_create_packet16(struct scsi_cdb16* cdb,
49                      uint8_t opcode,
50                      uint64_t lba,
51                      uint32_t alloc_size);
52
53 int
54 scsi_read_buffer(struct hba_device* dev,
55                  uint64_t lba,
56                  void* buffer,
57                  uint32_t size);
58
59 int
60 scsi_write_buffer(struct hba_device* dev,
61                   uint64_t lba,
62                   void* buffer,
63                   uint32_t size);
64
65 void
66 scsi_parse_capacity(struct hba_device* device, uint32_t* parameter);
67
68 #endif /* __LUNAIX_ATAPI_H */