Code-base clean-up and refactoring (#47)
[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                      u8_t opcode,
15                      u32_t lba,
16                      u32_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                      u8_t opcode,
27                      lba_t lba,
28                      u32_t alloc_size)
29 {
30     memset(cdb, 0, sizeof(*cdb));
31     cdb->opcode = opcode;
32     cdb->lba_be_hi = SCSI_FLIP((u32_t)(lba >> 32));
33     cdb->lba_be_lo = SCSI_FLIP((u32_t)lba);
34     cdb->length = SCSI_FLIP(alloc_size);
35 }
36
37 void
38 scsi_parse_capacity(struct hba_device* device, u32_t* parameter)
39 {
40     if (device->cbd_size == SCSI_CDB16) {
41         device->max_lba = (lba_t)SCSI_FLIP(*(parameter + 1)) |
42                           ((lba_t)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 void
52 scsi_submit(struct hba_device* dev, struct blkio_req* io_req)
53 {
54     struct hba_port* port = dev->port;
55     struct hba_cmdh* header;
56     struct hba_cmdt* table;
57
58     int write = !!(io_req->flags & BLKIO_WRITE);
59     int slot = hba_prepare_cmd(port, &table, &header);
60     hba_bind_vbuf(header, table, io_req->vbuf);
61
62     header->options |= (HBA_CMDH_WRITE * (write == 1)) | HBA_CMDH_ATAPI;
63
64     size_t size = vbuf_size(io_req->vbuf);
65     u32_t count = ICEIL(size, port->device->block_size);
66
67     struct sata_reg_fis* fis = (struct sata_reg_fis*)table->command_fis;
68     void* cdb = table->atapi_cmd;
69     sata_create_fis(fis, ATA_PACKET, (size << 8), 0);
70     fis->feature = 1 | ((!write) << 2);
71
72     if (port->device->cbd_size == SCSI_CDB16) {
73         scsi_create_packet16((struct scsi_cdb16*)cdb,
74                              write ? SCSI_WRITE_BLOCKS_16 : SCSI_READ_BLOCKS_16,
75                              io_req->blk_addr,
76                              count);
77     } else {
78         scsi_create_packet12((struct scsi_cdb12*)cdb,
79                              write ? SCSI_WRITE_BLOCKS_12 : SCSI_READ_BLOCKS_12,
80                              io_req->blk_addr & (u32_t)-1,
81                              count);
82     }
83
84     // field: cdb->misc1
85     *((u8_t*)cdb + 1) = 3 << 5; // RPROTECT=011b 禁用保护检查
86
87     // The async way...
88     struct hba_cmd_state* cmds = valloc(sizeof(struct hba_cmd_state));
89     *cmds = (struct hba_cmd_state){ .cmd_table = table, .state_ctx = io_req };
90     ahci_post(port, cmds, slot);
91 }