Merge branch 'master' into vfs-dev
[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_device* dev,
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_port* port = dev->port;
54     struct hba_cmdh* header;
55     struct hba_cmdt* table;
56     int slot = hba_prepare_cmd(port, &table, &header, buffer, size);
57     int bitmask = 1 << slot;
58
59     // 确保端口是空闲的
60     wait_until(!(port->regs[HBA_RPxTFD] & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)));
61
62     port->regs[HBA_RPxIS] = 0;
63
64     header->options |= (HBA_CMDH_WRITE * (write == 1)) | HBA_CMDH_ATAPI;
65
66     uint32_t count = ICEIL(size, port->device->block_size);
67
68     struct sata_reg_fis* fis = table->command_fis;
69     void* cdb = table->atapi_cmd;
70     sata_create_fis(fis, ATA_PACKET, (size << 8), 0);
71     fis->feature = 1 | ((!write) << 2);
72
73     if (port->device->cbd_size == 16) {
74         scsi_create_packet16((struct scsi_cdb16*)cdb,
75                              write ? SCSI_WRITE_BLOCKS_16 : SCSI_READ_BLOCKS_16,
76                              lba,
77                              count);
78     } else {
79         scsi_create_packet12((struct scsi_cdb12*)cdb,
80                              write ? SCSI_WRITE_BLOCKS_12 : SCSI_READ_BLOCKS_12,
81                              lba,
82                              count);
83     }
84
85     // field: cdb->misc1
86     *((uint8_t*)cdb + 1) = 3 << 5; // RPROTECT=011b 禁用保护检查
87
88     int retries = 0;
89
90     while (retries < MAX_RETRY) {
91         port->regs[HBA_RPxCI] = bitmask;
92
93         wait_until(!(port->regs[HBA_RPxCI] & bitmask));
94
95         if ((port->regs[HBA_RPxTFD] & HBA_PxTFD_ERR)) {
96             // 有错误
97             sata_read_error(port);
98             retries++;
99         } else {
100             vfree_dma(table);
101             return 1;
102         }
103     }
104
105 fail:
106     vfree_dma(table);
107     return 0;
108 }
109
110 void
111 scsi_read_buffer(struct hba_device* dev,
112                  uint64_t lba,
113                  void* buffer,
114                  uint32_t size)
115 {
116     __scsi_buffer_io(dev, lba, buffer, size, 0);
117 }
118
119 void
120 scsi_write_buffer(struct hba_device* dev,
121                   uint64_t lba,
122                   void* buffer,
123                   uint32_t size)
124 {
125     __scsi_buffer_io(dev, lba, buffer, size, 1);
126 }