feat: sector rwm ops test code
[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/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     device->max_lba = SCSI_FLIP(*(parameter + 1));
41     device->block_size = SCSI_FLIP(*(parameter + 2));
42 }
43
44 void
45 __scsi_buffer_io(struct hba_port* port,
46                  uint64_t lba,
47                  void* buffer,
48                  uint32_t size,
49                  int write)
50 {
51     assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment");
52
53     struct hba_cmdh* header;
54     struct hba_cmdt* table;
55     int slot = hba_alloc_slot(port, &table, &header, 0);
56     int bitmask = 1 << slot;
57
58     // 确保端口是空闲的
59     wait_until(!(port->regs[HBA_RPxTFD] & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)));
60
61     port->regs[HBA_RPxIS] = 0;
62
63     table->entries[0] = (struct hba_prdte){ .byte_count = size - 1,
64                                             .data_base = vmm_v2p(buffer) };
65     header->prdt_len = 1;
66     header->options |= (HBA_CMDH_WRITE * (write == 1)) | HBA_CMDH_ATAPI;
67
68     uint32_t count = ICEIL(size, port->device->block_size);
69
70     struct sata_reg_fis* fis = table->command_fis;
71     void* cdb = table->atapi_cmd;
72     sata_create_fis(fis, ATA_PACKET, (size << 8), 0);
73     fis->feature = 1 | ((!write) << 2);
74
75     if (port->device->cbd_size == 16) {
76         scsi_create_packet16((struct scsi_cdb16*)cdb,
77                              write ? SCSI_WRITE_BLOCKS_16 : SCSI_READ_BLOCKS_16,
78                              lba,
79                              count);
80         ((struct scsi_cdb16*)cdb)->misc1 = 3 << 5; // 禁用保护检查
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         ((struct scsi_cdb12*)cdb)->misc1 = 3 << 5; // 禁用保护检查
87     }
88
89     int retries = 0;
90
91     while (retries < MAX_RETRY) {
92         port->regs[HBA_RPxCI] = bitmask;
93
94         wait_until(!(port->regs[HBA_RPxCI] & bitmask));
95
96         if ((port->regs[HBA_RPxTFD] & HBA_PxTFD_ERR)) {
97             // 有错误
98             sata_read_error(port);
99             retries++;
100         } else {
101             vfree_dma(table);
102             return 1;
103         }
104     }
105
106 fail:
107     vfree_dma(table);
108     return 0;
109 }
110
111 void
112 scsi_read_buffer(struct hba_port* port,
113                  uint64_t lba,
114                  void* buffer,
115                  uint32_t size)
116 {
117     __scsi_buffer_io(port, lba, buffer, size, 0);
118 }
119
120 void
121 scsi_write_buffer(struct hba_port* port,
122                   uint64_t lba,
123                   void* buffer,
124                   uint32_t size)
125 {
126     __scsi_buffer_io(port, lba, buffer, size, 1);
127 }