feat: disk read/write support for both ATA and ATAPI device
[lunaix-os.git] / lunaix-os / hal / ahci / atapi.c
1 #include <hal/ahci/hba.h>
2 #include <hal/ahci/sata.h>
3 #include <hal/ahci/scsi.h>
4 #include <hal/ahci/utils.h>
5
6 #include <klibc/string.h>
7 #include <lunaix/spike.h>
8
9 void
10 scsi_create_packet12(struct scsi_cdb12* cdb,
11                      uint8_t opcode,
12                      uint32_t lba,
13                      uint32_t alloc_size)
14 {
15     memset(cdb, 0, sizeof(*cdb));
16     cdb->opcode = opcode;
17     cdb->lba_be = SCSI_FLIP(lba);
18     cdb->length = alloc_size;
19 }
20
21 void
22 scsi_create_packet16(struct scsi_cdb16* cdb,
23                      uint8_t opcode,
24                      uint64_t lba,
25                      uint32_t alloc_size)
26 {
27     memset(cdb, 0, sizeof(*cdb));
28     cdb->opcode = opcode;
29     cdb->lba_be_hi = SCSI_FLIP(lba >> 32);
30     cdb->lba_be_lo = SCSI_FLIP((uint32_t)lba);
31     cdb->length = alloc_size;
32 }
33
34 void
35 scsi_parse_capacity(struct hba_device* device, uint32_t* parameter)
36 {
37     device->max_lba = SCSI_FLIP(*(parameter + 1));
38     device->block_size = SCSI_FLIP(*(parameter + 2));
39 }
40
41 void
42 __scsi_buffer_io(struct hba_port* port,
43                  uint64_t lba,
44                  void* buffer,
45                  uint32_t size,
46                  int write)
47 {
48     assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment");
49
50     struct hba_cmdh* header;
51     struct hba_cmdt* table;
52     int slot = hba_alloc_slot(port, &table, &header, 0);
53     int bitmask = 1 << slot;
54
55     // 确保端口是空闲的
56     wait_until(!(port->regs[HBA_RPxTFD] & (HBA_PxTFD_BSY)));
57
58     port->regs[HBA_RPxIS] = 0;
59
60     table->entries[0] =
61       (struct hba_prdte){ .byte_count = size - 1, .data_base = buffer };
62     header->prdt_len = 1;
63     header->options |= (HBA_CMDH_WRITE * (write == 1)) | HBA_CMDH_ATAPI;
64
65     uint32_t count = size / port->device->block_size;
66     if (count == 0) {
67         // 对ATAPI设备来说,READ/WRITE (16/12) 没有这个count=0的 special case
68         //  但是对于READ/WRITE (6),就存在这个special case
69         goto fail;
70     }
71
72     struct sata_reg_fis* fis = table->command_fis;
73     void* cdb = table->atapi_cmd;
74     sata_create_fis(fis, ATA_PACKET, (size << 8) & 0xffffff, 0);
75
76     if (port->device->cbd_size == 16) {
77         scsi_create_packet16((struct scsi_cdb16*)cdb,
78                              write ? SCSI_WRITE_BLOCKS_16 : SCSI_READ_BLOCKS_16,
79                              lba,
80                              count);
81     } else {
82         scsi_create_packet12((struct scsi_cdb12*)cdb,
83                              write ? SCSI_WRITE_BLOCKS_12 : SCSI_READ_BLOCKS_12,
84                              lba,
85                              count);
86     }
87
88     port->regs[HBA_RPxCI] = bitmask;
89
90     wait_until(!(port->regs[HBA_RPxCI] & bitmask));
91
92     if ((port->regs[HBA_RPxTFD] & HBA_PxTFD_ERR)) {
93         // 有错误
94         sata_read_error(port);
95         goto fail;
96     }
97
98     vfree_dma(table);
99     return 1;
100
101 fail:
102     vfree_dma(table);
103     return 0;
104 }
105
106 void
107 scsi_read_buffer(struct hba_port* port,
108                  uint64_t lba,
109                  void* buffer,
110                  uint32_t size)
111 {
112     __scsi_buffer_io(port, lba, buffer, size, 0);
113 }
114
115 void
116 scsi_write_buffer(struct hba_port* port,
117                   uint64_t lba,
118                   void* buffer,
119                   uint32_t size)
120 {
121     __scsi_buffer_io(port, lba, buffer, size, 1);
122 }