f75c1bec6cb59ed463907c8c1a104d2e2c2ba946
[lunaix-os.git] / lunaix-os / hal / ahci / atapi.c
1 #include <hal/ahci/ahci.h>
2 #include <hal/ahci/hba.h>
3 #include <hal/ahci/sata.h>
4 #include <hal/ahci/scsi.h>
5
6 #include <klibc/string.h>
7 #include <lunaix/mm/valloc.h>
8 #include <lunaix/mm/vmm.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/syslog.h>
11
12 void
13 scsi_create_packet12(struct scsi_cdb12* cdb,
14                      uint8_t opcode,
15                      uint32_t lba,
16                      uint32_t alloc_size)
17 {
18     memset(cdb, 0, sizeof(*cdb));
19     cdb->opcode = opcode;
20     cdb->lba_be = SCSI_FLIP(lba);
21     cdb->length = SCSI_FLIP(alloc_size);
22 }
23
24 void
25 scsi_create_packet16(struct scsi_cdb16* cdb,
26                      uint8_t opcode,
27                      uint64_t lba,
28                      uint32_t alloc_size)
29 {
30     memset(cdb, 0, sizeof(*cdb));
31     cdb->opcode = opcode;
32     cdb->lba_be_hi = SCSI_FLIP((uint32_t)(lba >> 32));
33     cdb->lba_be_lo = SCSI_FLIP((uint32_t)lba);
34     cdb->length = SCSI_FLIP(alloc_size);
35 }
36
37 void
38 scsi_parse_capacity(struct hba_device* device, uint32_t* parameter)
39 {
40     if (device->cbd_size == SCSI_CDB16) {
41         device->max_lba =
42           SCSI_FLIP(*(parameter + 1)) | (SCSI_FLIP(*parameter) << 32);
43         device->block_size = SCSI_FLIP(*(parameter + 2));
44     } else {
45         // for READ_CAPACITY(10)
46         device->max_lba = SCSI_FLIP(*(parameter));
47         device->block_size = SCSI_FLIP(*(parameter + 1));
48     }
49 }
50
51 int
52 __scsi_buffer_io(struct hba_device* dev,
53                  uint64_t lba,
54                  void* buffer,
55                  uint32_t size,
56                  int write)
57 {
58     assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment");
59
60     struct hba_port* port = dev->port;
61     struct hba_cmdh* header;
62     struct hba_cmdt* table;
63     int slot = hba_prepare_cmd(port, &table, &header, buffer, size);
64
65     header->options |= (HBA_CMDH_WRITE * (write == 1)) | HBA_CMDH_ATAPI;
66
67     uint32_t count = ICEIL(size, port->device->block_size);
68
69     struct sata_reg_fis* fis = (struct sata_reg_fis*)table->command_fis;
70     void* cdb = table->atapi_cmd;
71     sata_create_fis(fis, ATA_PACKET, (size << 8), 0);
72     fis->feature = 1 | ((!write) << 2);
73
74     if (port->device->cbd_size == SCSI_CDB16) {
75         scsi_create_packet16((struct scsi_cdb16*)cdb,
76                              write ? SCSI_WRITE_BLOCKS_16 : SCSI_READ_BLOCKS_16,
77                              lba,
78                              count);
79     } else {
80         scsi_create_packet12((struct scsi_cdb12*)cdb,
81                              write ? SCSI_WRITE_BLOCKS_12 : SCSI_READ_BLOCKS_12,
82                              lba,
83                              count);
84     }
85
86     // field: cdb->misc1
87     *((uint8_t*)cdb + 1) = 3 << 5; // RPROTECT=011b 禁用保护检查
88
89     int is_ok = ahci_try_send(port, slot);
90     vfree_dma(table);
91
92     return is_ok;
93 }
94
95 int
96 scsi_read_buffer(struct hba_device* dev,
97                  uint64_t lba,
98                  void* buffer,
99                  uint32_t size)
100 {
101     return __scsi_buffer_io(dev, lba, buffer, size, 0);
102 }
103
104 int
105 scsi_write_buffer(struct hba_device* dev,
106                   uint64_t lba,
107                   void* buffer,
108                   uint32_t size)
109 {
110     return __scsi_buffer_io(dev, lba, buffer, size, 1);
111 }