feat: ATAPI device capacity probing
authorMinep <zelong56@gmail.com>
Mon, 4 Jul 2022 23:11:16 +0000 (00:11 +0100)
committerMinep <zelong56@gmail.com>
Mon, 4 Jul 2022 23:11:16 +0000 (00:11 +0100)
feat: SCSI command set support
refactor: The driver code is very tricky! I have tried my best, however I mess it up :)

lunaix-os/hal/ahci/ahci.c
lunaix-os/hal/ahci/atapi.c [new file with mode: 0644]
lunaix-os/hal/ahci/utils.c
lunaix-os/includes/hal/ahci/ahci.h
lunaix-os/includes/hal/ahci/hba.h [new file with mode: 0644]
lunaix-os/includes/hal/ahci/sata.h [new file with mode: 0644]
lunaix-os/includes/hal/ahci/scsi.h [new file with mode: 0644]
lunaix-os/includes/hal/ahci/utils.h

index 57bedb6b1f93616589841684a3957e76f04a031c..6771bb690248412bfc1d9455551ed5d7af2e4898 100644 (file)
@@ -9,7 +9,11 @@
  *
  */
 #include <hal/ahci/ahci.h>
+#include <hal/ahci/hba.h>
+#include <hal/ahci/sata.h>
+#include <hal/ahci/scsi.h>
 #include <hal/ahci/utils.h>
+
 #include <hal/pci.h>
 #include <klibc/string.h>
 #include <lunaix/mm/mmio.h>
@@ -29,6 +33,9 @@ static struct ahci_hba hba;
 void
 __ahci_hba_isr(isr_param param);
 
+int
+ahci_init_device(struct hba_port* port);
+
 void
 ahci_init()
 {
@@ -76,8 +83,8 @@ ahci_init()
             continue;
         }
 
-        struct ahci_port* port =
-          (struct ahci_port*)valloc(sizeof(struct ahci_port));
+        struct hba_port* port =
+          (struct hba_port*)valloc(sizeof(struct hba_port));
         hba_reg_t* port_regs =
           (hba_reg_t*)(&hba.base[HBA_RPBASE + i * HBA_RPSIZE]);
 
@@ -98,10 +105,10 @@ ahci_init()
         port_regs[HBA_RPxCLB] = clb_pa + clbp * HBA_CLB_SIZE;
         port_regs[HBA_RPxFB] = fis_pa + fisp * HBA_FIS_SIZE;
 
-        *port = (struct ahci_port){ .regs = port_regs,
-                                    .ssts = port_regs[HBA_RPxSSTS],
-                                    .cmdlst = clb_pg_addr + clbp * HBA_CLB_SIZE,
-                                    .fis = fis_pg_addr + fisp * HBA_FIS_SIZE };
+        *port = (struct hba_port){ .regs = port_regs,
+                                   .ssts = port_regs[HBA_RPxSSTS],
+                                   .cmdlst = clb_pg_addr + clbp * HBA_CLB_SIZE,
+                                   .fis = fis_pg_addr + fisp * HBA_FIS_SIZE };
 
         /* 初始化端口,并置于就绪状态 */
         port_regs[HBA_RPxCI] = 0;
@@ -118,8 +125,9 @@ ahci_init()
             wait_until(!(port_regs[HBA_RPxCMD] & HBA_PxCMD_CR));
             port_regs[HBA_RPxCMD] |= HBA_PxCMD_FRE;
             port_regs[HBA_RPxCMD] |= HBA_PxCMD_ST;
-            if (!ahci_identify_device(port)) {
-                kprintf(KERROR "fail to probe device info");
+
+            if (!ahci_init_device(port)) {
+                kprintf(KERROR "fail to init device");
             }
         }
     }
@@ -144,11 +152,11 @@ ahci_list_device()
             hba.version,
             hba.ports_num,
             hba.cmd_slots);
-    struct ahci_port* port;
+    struct hba_port* port;
     for (size_t i = 0; i < 32; i++) {
         port = hba.ports[i];
 
-        // 愚蠢的gcc似乎认为 struct ahci_port* 不可能为空
+        // 愚蠢的gcc似乎认为 struct hba_port* 不可能为空
         //  所以将这个非常关键的if给优化掉了。
         //  这里将指针强制转换为整数,欺骗gcc :)
         if ((uintptr_t)port == 0) {
@@ -162,21 +170,20 @@ ahci_list_device()
                 &sata_ifs[device_state],
                 port->regs[HBA_RPxSIG]);
 
-        struct ahci_device_info* dev_info = port->device_info;
+        struct hba_device* dev_info = port->device;
         if (!device_state || !dev_info) {
             continue;
         }
-
         kprintf("\t\t capacity: %d KiB\n",
-                (dev_info->max_lba * dev_info->sector_size) >> 10);
-        kprintf("\t\t sector size: %dB\n", dev_info->sector_size);
+                (dev_info->max_lba * dev_info->block_size) >> 10);
+        kprintf("\t\t sector size: %dB\n", dev_info->block_size);
         kprintf("\t\t model: %s\n", &dev_info->model);
         kprintf("\t\t serial: %s\n", &dev_info->serial_num);
     }
 }
 
 int
-achi_alloc_slot(struct ahci_port* port)
+__get_free_slot(struct hba_port* port)
 {
     hba_reg_t pxsact = port->regs[HBA_RPxSACT];
     hba_reg_t pxci = port->regs[HBA_RPxCI];
@@ -188,11 +195,11 @@ achi_alloc_slot(struct ahci_port* port)
 }
 
 void
-__ahci_create_fis(struct sata_reg_fis* cmd_fis,
-                  uint8_t command,
-                  uint32_t lba_lo,
-                  uint32_t lba_hi,
-                  uint16_t sector_count)
+sata_create_fis(struct sata_reg_fis* cmd_fis,
+                uint8_t command,
+                uint32_t lba_lo,
+                uint32_t lba_hi,
+                uint16_t sector_count)
 {
     cmd_fis->head.type = SATA_REG_FIS_H2D;
     cmd_fis->head.options = SATA_REG_FIS_COMMAND;
@@ -211,48 +218,67 @@ __ahci_create_fis(struct sata_reg_fis* cmd_fis,
 }
 
 int
-ahci_identify_device(struct ahci_port* port)
+hba_alloc_slot(struct hba_port* port,
+               struct hba_cmdt** cmdt,
+               struct hba_cmdh** cmdh,
+               uint16_t header_options)
 {
-    int slot = achi_alloc_slot(port);
-    assert_msg(slot >= 0, "No free slot");
-
-    // 清空任何待响应的中断
-    port->regs[HBA_RPxIS] = 0;
-
-    /* 发送ATA命令,参考:SATA AHCI Spec Rev.1.3.1, section 5.5 */
+    int slot = __get_free_slot(port);
+    assert_msg(slot >= 0, "HBA: No free slot");
 
     // 构建命令头(Command Header)和命令表(Command Table)
-    struct ahci_hba_cmdh* cmd_header = &port->cmdlst[slot];
-    struct ahci_hba_cmdt* cmd_table = valloc_dma(sizeof(struct ahci_hba_cmdt));
+    struct hba_cmdh* cmd_header = &port->cmdlst[slot];
+    struct hba_cmdt* cmd_table = valloc_dma(sizeof(struct hba_cmdt));
 
     memset(cmd_header, 0, sizeof(*cmd_header));
     memset(cmd_table, 0, sizeof(*cmd_table));
 
+    // 将命令表挂到命令头上
+    cmd_header->prdt_len = 1;
+    cmd_header->cmd_table_base = vmm_v2p(cmd_table);
+    cmd_header->options = HBA_CMDH_FIS_LEN(sizeof(struct sata_reg_fis)) |
+                          HBA_CMDH_CLR_BUSY | (header_options & ~0x1f);
+
+    *cmdh = cmd_header;
+    *cmdt = cmd_table;
+
+    return slot;
+}
+
+int
+ahci_init_device(struct hba_port* port)
+{
+    /* 发送ATA命令,参考:SATA AHCI Spec Rev.1.3.1, section 5.5 */
+    struct hba_cmdt* cmd_table;
+    struct hba_cmdh* cmd_header;
+
+    int slot = hba_alloc_slot(port, &cmd_table, &cmd_header, 0);
+
+    // 清空任何待响应的中断
+    port->regs[HBA_RPxIS] = 0;
+    port->device = valloc(sizeof(struct hba_device));
+    port->device->signature = port->regs[HBA_RPxSIG];
+
     // 预备DMA接收缓存,用于存放HBA传回的数据
     uint16_t* data_in = (uint16_t*)valloc_dma(512);
 
     cmd_table->entries[0] =
-      (struct ahci_hba_prdte){ .data_base = vmm_v2p(data_in),
-                               .byte_count = 511 }; // byte_count是从0开始算的
+      (struct hba_prdte){ .data_base = vmm_v2p(data_in),
+                          .byte_count = 511 }; // byte_count是从0开始算的
+    cmd_header->prdt_len = 1;
 
     // 在命令表中构建命令FIS
     struct sata_reg_fis* cmd_fis = (struct sata_reg_fis*)cmd_table->command_fis;
 
     // 根据设备类型使用合适的命令
-    if (port->regs[HBA_RPxSIG] == HBA_DEV_SIG_ATA) {
+    if (port->device->signature == HBA_DEV_SIG_ATA) {
         // ATA 一般为硬盘
-        __ahci_create_fis(cmd_fis, ATA_IDENTIFY_DEVICE, 0, 0, 0);
+        sata_create_fis(cmd_fis, ATA_IDENTIFY_DEVICE, 0, 0, 0);
     } else {
         // ATAPI 一般为光驱,软驱,或者磁带机
-        __ahci_create_fis(cmd_fis, ATA_IDENTIFY_PAKCET_DEVICE, 0, 0, 0);
+        sata_create_fis(cmd_fis, ATA_IDENTIFY_PAKCET_DEVICE, 0, 0, 0);
     }
 
-    // 将命令表挂到命令头上
-    cmd_header->cmd_table_base = vmm_v2p(cmd_table);
-    cmd_header->prdt_len = 1;
-    cmd_header->options |=
-      HBA_CMDH_FIS_LEN(sizeof(*cmd_fis)) | HBA_CMDH_CLR_BUSY;
-
     // PxCI寄存器置位,告诉HBA这儿有个数据需要发送到SATA端口
     port->regs[HBA_RPxCI] = (1 << slot);
 
@@ -263,18 +289,49 @@ ahci_identify_device(struct ahci_port* port)
         解析IDENTIFY DEVICE传回来的数据。
           参考:
             * ATA/ATAPI Command Set - 3 (ACS-3), Section 7.12.7
+    */
+    ahci_parse_dev_info(port->device, data_in);
 
-        注意:ATAPI无法通过IDENTIFY PACKET DEVICE 获取容量信息。
-        这需要另外使用特殊的SCSI命令中的READ CAPACITY(16)
-        来获取,这种命令需要使用ATA的PACKET命令发出。
+    if (port->device->signature == HBA_DEV_SIG_ATA) {
+        goto done;
+    }
+
+    /*
+        注意:ATAPI设备是无法通过IDENTIFY PACKET DEVICE 获取容量信息的。
+        我们需要使用SCSI命令的READ_CAPACITY(16)进行获取。
+        步骤如下:
+            1. 因为ATAPI走的是SCSI,而AHCI对此专门进行了SATA的封装,
+               也就是通过SATA的PACKET命令对SCSI命令进行封装。所以我们
+               首先需要构建一个PACKET命令的FIS
+            2. 接着,在ACMD中构建命令READ_CAPACITY的CDB - 一种SCSI命令的封装
+            3. 然后把cmd_header->options的A位置位,表示这是一个送往ATAPI的命令。
+                一点细节:
+                    1. HBA往底层SATA控制器发送PACKET FIS
+                    2. SATA控制器回复PIO Setup FIS
+                    3. HBA读入ACMD中的CDB,打包成Data FIS进行答复
+                    4. SATA控制器解包,拿到CDB,通过SCSI协议转发往ATAPI设备。
+                    5. ATAPI设备回复Return Parameter,SATA通过DMA Setup FIS
+                       发起DMA请求,HBA介入,将Return Parameter写入我们在PRDT
+                       里设置的data_in位置。
+            4. 最后照常等待HBA把结果写入data_in,然后直接解析就好了。
           参考:
             * ATA/ATAPI Command Set - 3 (ACS-3), Section 7.18
             * SATA AHCI HBA Spec, Section 5.3.7
             * SCSI Command Reference Manual, Section 3.26
     */
-    port->device_info = valloc(sizeof(struct ahci_device_info));
-    ahci_parse_dev_info(port->device_info, data_in);
+    sata_create_fis(cmd_fis, ATA_PACKET, 512 << 8, 0, 0);
+    struct scsi_cdb16* cdb16 = (struct scsi_cdb16*)cmd_table->atapi_cmd;
+    scsi_create_packet16(cdb16, SCSI_READ_CAPACITY_16, 0, 0, 512);
+    cdb16->misc1 = 0x10; // service action
+    cmd_header->transferred_size = 0;
+    cmd_header->options |= HBA_CMDH_ATAPI;
+
+    port->regs[HBA_RPxCI] = (1 << slot);
+    wait_until(!(port->regs[HBA_RPxCI] & (1 << slot)));
+
+    scsi_parse_capacity(port->device, (uint32_t*)data_in);
 
+done:
     vfree_dma(data_in);
     vfree_dma(cmd_table);
 
diff --git a/lunaix-os/hal/ahci/atapi.c b/lunaix-os/hal/ahci/atapi.c
new file mode 100644 (file)
index 0000000..193f8f3
--- /dev/null
@@ -0,0 +1,39 @@
+#include <hal/ahci/hba.h>
+#include <hal/ahci/scsi.h>
+#include <hal/ahci/utils.h>
+
+#include <klibc/string.h>
+#include <lunaix/spike.h>
+
+void
+scsi_create_packet12(struct scsi_cdb12* cdb,
+                     uint8_t opcode,
+                     uint32_t lba,
+                     uint32_t alloc_size)
+{
+    memset(cdb, 0, sizeof(*cdb));
+    cdb->opcode = opcode;
+    cdb->lba_be = SCSI_FLIP(lba);
+    cdb->length = alloc_size;
+}
+
+void
+scsi_create_packet16(struct scsi_cdb16* cdb,
+                     uint8_t opcode,
+                     uint32_t lba_hi,
+                     uint32_t lba_lo,
+                     uint32_t alloc_size)
+{
+    memset(cdb, 0, sizeof(*cdb));
+    cdb->opcode = opcode;
+    cdb->lba_be_hi = SCSI_FLIP(lba_hi);
+    cdb->lba_be_lo = SCSI_FLIP(lba_lo);
+    cdb->length = alloc_size;
+}
+
+void
+scsi_parse_capacity(struct hba_device* device, uint32_t* parameter)
+{
+    device->max_lba = SCSI_FLIP(*(parameter + 1));
+    device->block_size = SCSI_FLIP(*(parameter + 2));
+}
\ No newline at end of file
index 279cd50ee0e900d26dd7ef7b9c845e24b9608403..9af9cfee25ada3189cbffb1b4a5463dc75c1e7e4 100644 (file)
@@ -1,3 +1,4 @@
+#include <hal/ahci/hba.h>
 #include <hal/ahci/utils.h>
 #include <klibc/string.h>
 
@@ -8,13 +9,14 @@
 #define IDDEV_MODELNUM_OFFSET 27
 
 void
-ahci_parse_dev_info(struct ahci_device_info* dev_info, uint16_t* data)
+ahci_parse_dev_info(struct hba_device* dev_info, uint16_t* data)
 {
     dev_info->max_lba = *((uint32_t*)(data + IDDEV_MAXLBA_OFFSET));
-    dev_info->sector_size = *((uint32_t*)(data + IDDEV_LSECSIZE_OFFSET));
+    dev_info->block_size = *((uint32_t*)(data + IDDEV_LSECSIZE_OFFSET));
+    dev_info->cbd_size = (*data & 0x3) ? 12 : 16;
     memcpy(&dev_info->wwn, (uint8_t*)(data + IDDEV_WWN_OFFSET), 8);
-    if (!dev_info->sector_size) {
-        dev_info->sector_size = 512;
+    if (!dev_info->block_size) {
+        dev_info->block_size = 512;
     }
     ahci_parsestr(&dev_info->serial_num, data + IDDEV_SERIALNUM_OFFSET, 10);
     ahci_parsestr(&dev_info->model, data + IDDEV_MODELNUM_OFFSET, 20);
@@ -23,9 +25,11 @@ ahci_parse_dev_info(struct ahci_device_info* dev_info, uint16_t* data)
 void
 ahci_parsestr(char* str, uint16_t* reg_start, int size_word)
 {
-    for (int i = 0, j = 0; i < size_word; i++, j += 2) {
+    int j = 0;
+    for (int i = 0; i < size_word; i++, j += 2) {
         uint16_t reg = *(reg_start + i);
         str[j] = (char)(reg >> 8);
         str[j + 1] = (char)(reg & 0xff);
     }
+    str[j - 1] = '\0';
 }
\ No newline at end of file
index c0c483aa1b285b8e26e60f690e684d42583ac265..0494181d03efc7ecda592091b63cb69a9eefbc89 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef __LUNAIX_AHCI_H
 #define __LUNAIX_AHCI_H
 
-#include <stdint.h>
-
 /*
  * Macro naming rule:
  *      HBA_R[xxx]
 
 #define AHCI_HBA_CLASS 0x10601
 
-#define HBA_RCAP 0
-#define HBA_RGHC 1
-#define HBA_RIS 2
-#define HBA_RPI 3
-#define HBA_RVER 4
-
-#define HBA_RPBASE (0x40)
-#define HBA_RPSIZE (0x80 >> 2)
-#define HBA_RPxCLB 0
-#define HBA_RPxFB 2
-#define HBA_RPxIS 4
-#define HBA_RPxIE 5
-#define HBA_RPxCMD 6
-#define HBA_RPxTFD 8
-#define HBA_RPxSIG 9
-#define HBA_RPxSSTS 10
-#define HBA_RPxSCTL 11
-#define HBA_RPxSERR 12
-#define HBA_RPxSACT 13
-#define HBA_RPxCI 14
-#define HBA_RPxSNTF 15
-#define HBA_RPxFBS 16
-
-#define HBA_PxCMD_FRE (1 << 4)
-#define HBA_PxCMD_CR (1 << 15)
-#define HBA_PxCMD_FR (1 << 14)
-#define HBA_PxCMD_ST (1)
-#define HBA_PxINTR_DMA (1 << 2)
-#define HBA_PxINTR_D2HR (1)
-
-#define HBA_RGHC_ACHI_ENABLE (1 << 31)
-#define HBA_RGHC_INTR_ENABLE (1 << 1)
-#define HBA_RGHC_RESET 1
-
-#define HBA_RPxSSTS_PWR(x) (((x) >> 8) & 0xf)
-#define HBA_RPxSSTS_IF(x) (((x) >> 4) & 0xf)
-#define HBA_RPxSSTS_PHYSTATE(x) ((x)&0xf)
-
-#define HBA_DEV_SIG_ATAPI 0xeb140101
-#define HBA_DEV_SIG_ATA 0x00000101
-
-#define __HBA_PACKED__ __attribute__((packed))
-
-typedef unsigned int hba_reg_t;
-
-#define HBA_CMDH_FIS_LEN(fis_bytes) (((fis_bytes) / 4) & 0x1f)
-#define HBA_CMDH_ATAPI (1 << 5)
-#define HBA_CMDH_WRITE (1 << 6)
-#define HBA_CMDH_PREFETCH (1 << 7)
-#define HBA_CMDH_R (1 << 8)
-#define HBA_CMDH_CLR_BUSY (1 << 10)
-#define HBA_CMDH_PRDT_LEN(entries) (((entries)&0xffff) << 16)
-
-struct ahci_hba_cmdh
-{
-    uint16_t options;
-    uint16_t prdt_len;
-    uint32_t transferred_size;
-    uint32_t cmd_table_base;
-    uint32_t reserved[5];
-} __HBA_PACKED__;
-
-#define HBA_PRDTE_BYTE_CNT(cnt) ((cnt & 0x3FFFFF) | 0x1)
-
-struct ahci_hba_prdte
-{
-    uint32_t data_base;
-    uint32_t reserved[2];
-    uint32_t byte_count;
-} __HBA_PACKED__;
-
-struct ahci_hba_cmdt
-{
-    uint8_t command_fis[64];
-    uint8_t atapi_cmd[16];
-    uint8_t reserved[0x30];
-    struct ahci_hba_prdte entries[3];
-} __HBA_PACKED__;
-
-#define SATA_REG_FIS_D2H 0x34
-#define SATA_REG_FIS_H2D 0x27
-#define SATA_REG_FIS_COMMAND 0x80
-#define SATA_LBA_COMPONENT(lba, offset) ((((lba_lo) >> (offset)) & 0xff))
-
-struct sata_fis_head
-{
-    uint8_t type;
-    uint8_t options;
-    uint8_t status_cmd;
-    uint8_t feat_err;
-} __HBA_PACKED__;
-
-struct sata_reg_fis
-{
-    struct sata_fis_head head;
-
-    uint8_t lba0, lba8, lba16;
-    uint8_t dev;
-    uint8_t lba24, lba32, lba40;
-    uint8_t reserved1;
-
-    uint16_t count;
-
-    uint8_t reserved[6];
-} __HBA_PACKED__;
-
-struct sata_data_fis
-{
-    struct sata_fis_head head;
-
-    uint8_t data[0];
-} __HBA_PACKED__;
-
-struct ahci_device_info
-{
-    char serial_num[20];
-    char model[40];
-    uint32_t max_lba;
-    uint32_t sector_size;
-    uint8_t wwn[8];
-};
-
-struct ahci_port
-{
-    volatile hba_reg_t* regs;
-    unsigned int ssts;
-    struct ahci_hba_cmdh* cmdlst;
-    struct sata_fis_head* fis;
-    struct ahci_device_info* device_info;
-};
-
-struct ahci_hba
-{
-    volatile hba_reg_t* base;
-    unsigned int ports_num;
-    unsigned int cmd_slots;
-    unsigned int version;
-    struct ahci_port* ports[32];
-};
-
 #define ATA_IDENTIFY_DEVICE 0xec
 #define ATA_IDENTIFY_PAKCET_DEVICE 0xa1
+#define ATA_PACKET 0xa0
 
 /**
  * @brief 初始化AHCI与HBA
@@ -167,7 +26,4 @@ ahci_init();
 void
 ahci_list_device();
 
-int
-ahci_identify_device(struct ahci_port* port);
-
 #endif /* __LUNAIX_AHCI_H */
diff --git a/lunaix-os/includes/hal/ahci/hba.h b/lunaix-os/includes/hal/ahci/hba.h
new file mode 100644 (file)
index 0000000..d836a78
--- /dev/null
@@ -0,0 +1,120 @@
+#ifndef __LUNAIX_HBA_H
+#define __LUNAIX_HBA_H
+
+#include <stdint.h>
+
+#define HBA_RCAP 0
+#define HBA_RGHC 1
+#define HBA_RIS 2
+#define HBA_RPI 3
+#define HBA_RVER 4
+
+#define HBA_RPBASE (0x40)
+#define HBA_RPSIZE (0x80 >> 2)
+#define HBA_RPxCLB 0
+#define HBA_RPxFB 2
+#define HBA_RPxIS 4
+#define HBA_RPxIE 5
+#define HBA_RPxCMD 6
+#define HBA_RPxTFD 8
+#define HBA_RPxSIG 9
+#define HBA_RPxSSTS 10
+#define HBA_RPxSCTL 11
+#define HBA_RPxSERR 12
+#define HBA_RPxSACT 13
+#define HBA_RPxCI 14
+#define HBA_RPxSNTF 15
+#define HBA_RPxFBS 16
+
+#define HBA_PxCMD_FRE (1 << 4)
+#define HBA_PxCMD_CR (1 << 15)
+#define HBA_PxCMD_FR (1 << 14)
+#define HBA_PxCMD_ST (1)
+#define HBA_PxINTR_DMA (1 << 2)
+#define HBA_PxINTR_D2HR (1)
+
+#define HBA_RGHC_ACHI_ENABLE (1 << 31)
+#define HBA_RGHC_INTR_ENABLE (1 << 1)
+#define HBA_RGHC_RESET 1
+
+#define HBA_RPxSSTS_PWR(x) (((x) >> 8) & 0xf)
+#define HBA_RPxSSTS_IF(x) (((x) >> 4) & 0xf)
+#define HBA_RPxSSTS_PHYSTATE(x) ((x)&0xf)
+
+#define HBA_DEV_SIG_ATAPI 0xeb140101
+#define HBA_DEV_SIG_ATA 0x00000101
+
+#define __HBA_PACKED__ __attribute__((packed))
+
+typedef unsigned int hba_reg_t;
+
+#define HBA_CMDH_FIS_LEN(fis_bytes) (((fis_bytes) / 4) & 0x1f)
+#define HBA_CMDH_ATAPI (1 << 5)
+#define HBA_CMDH_WRITE (1 << 6)
+#define HBA_CMDH_PREFETCH (1 << 7)
+#define HBA_CMDH_R (1 << 8)
+#define HBA_CMDH_CLR_BUSY (1 << 10)
+#define HBA_CMDH_PRDT_LEN(entries) (((entries)&0xffff) << 16)
+
+struct hba_cmdh
+{
+    uint16_t options;
+    uint16_t prdt_len;
+    uint32_t transferred_size;
+    uint32_t cmd_table_base;
+    uint32_t reserved[5];
+} __HBA_PACKED__;
+
+#define HBA_PRDTE_BYTE_CNT(cnt) ((cnt & 0x3FFFFF) | 0x1)
+
+struct hba_prdte
+{
+    uint32_t data_base;
+    uint32_t reserved[2];
+    uint32_t byte_count;
+} __HBA_PACKED__;
+
+struct hba_cmdt
+{
+    uint8_t command_fis[64];
+    uint8_t atapi_cmd[16];
+    uint8_t reserved[0x30];
+    struct hba_prdte entries[3];
+} __HBA_PACKED__;
+
+struct hba_device
+{
+    char serial_num[20];
+    char model[40];
+    uint32_t signature;
+    uint32_t max_lba;
+    uint32_t block_size;
+    uint8_t wwn[8];
+    uint8_t cbd_size;
+};
+
+struct hba_port
+{
+    volatile hba_reg_t* regs;
+    unsigned int ssts;
+    struct hba_cmdh* cmdlst;
+    struct sata_fis_head* fis;
+    struct hba_device* device;
+};
+
+struct ahci_hba
+{
+    volatile hba_reg_t* base;
+    unsigned int ports_num;
+    unsigned int cmd_slots;
+    unsigned int version;
+    struct hba_port* ports[32];
+};
+
+int
+hba_alloc_slot(struct hba_port* port,
+               struct hba_cmdt** cmdt,
+               struct hba_cmdh** cmdh,
+               uint16_t header_options);
+
+#endif /* __LUNAIX_HBA_H */
diff --git a/lunaix-os/includes/hal/ahci/sata.h b/lunaix-os/includes/hal/ahci/sata.h
new file mode 100644 (file)
index 0000000..135f3eb
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __LUNAIX_SATA_H
+#define __LUNAIX_SATA_H
+
+#include <stdint.h>
+
+#define SATA_REG_FIS_D2H 0x34
+#define SATA_REG_FIS_H2D 0x27
+#define SATA_REG_FIS_COMMAND 0x80
+#define SATA_LBA_COMPONENT(lba, offset) ((((lba_lo) >> (offset)) & 0xff))
+
+struct sata_fis_head
+{
+    uint8_t type;
+    uint8_t options;
+    uint8_t status_cmd;
+    uint8_t feat_err;
+} __HBA_PACKED__;
+
+struct sata_reg_fis
+{
+    struct sata_fis_head head;
+
+    uint8_t lba0, lba8, lba16;
+    uint8_t dev;
+    uint8_t lba24, lba32, lba40;
+    uint8_t reserved1;
+
+    uint16_t count;
+
+    uint8_t reserved[6];
+} __HBA_PACKED__;
+
+struct sata_data_fis
+{
+    struct sata_fis_head head;
+
+    uint8_t data[0];
+} __HBA_PACKED__;
+
+void
+sata_create_fis(struct sata_reg_fis* cmd_fis,
+                uint8_t command,
+                uint32_t lba_lo,
+                uint32_t lba_hi,
+                uint16_t sector_count);
+
+#endif /* __LUNAIX_SATA_H */
diff --git a/lunaix-os/includes/hal/ahci/scsi.h b/lunaix-os/includes/hal/ahci/scsi.h
new file mode 100644 (file)
index 0000000..5015641
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef __LUNAIX_SCSI_H
+#define __LUNAIX_SCSI_H
+
+#include <stdint.h>
+
+#define SCSI_FLIP(val)                                                         \
+    ((((val)&0x000000ff) << 24) | (((val)&0x0000ff00) << 8) |                  \
+     (((val)&0x00ff0000) >> 8) | (((val)&0xff000000) >> 24))
+
+#define SCSI_READ_CAPACITY_16 0x9e
+#define SCSI_READ_CAPACITY_10 0x25
+#define SCSI_READ_BLOCKS_16 0x88
+#define SCSI_READ_BLOCKS_12 0xa8
+#define SCSI_WRITE_BLOCKS_16 0x8a
+#define SCSI_WRITE_BLOCKS_12 0xaa
+
+struct scsi_cdb12
+{
+    uint8_t opcode;
+    uint8_t misc1;
+    uint32_t lba_be;
+    uint32_t length;
+    uint8_t misc2;
+    uint8_t ctrl;
+} __attribute__((packed));
+
+struct scsi_cdb16
+{
+    uint8_t opcode;
+    uint8_t misc1;
+    uint32_t lba_be_hi;
+    uint32_t lba_be_lo;
+    uint32_t length;
+    uint8_t misc2;
+    uint8_t ctrl;
+} __attribute__((packed));
+
+void
+scsi_create_packet12(struct scsi_cdb12* cdb,
+                     uint8_t opcode,
+                     uint32_t lba,
+                     uint32_t alloc_size);
+
+void
+scsi_create_packet16(struct scsi_cdb16* cdb,
+                     uint8_t opcode,
+                     uint32_t lba_hi,
+                     uint32_t lba_lo,
+                     uint32_t alloc_size);
+
+#endif /* __LUNAIX_ATAPI_H */
index 1ff5f32517c04c8f116b44cb36a6232910e52691..119ec3960eaf362944911d5d44522366326eb593 100644 (file)
@@ -1,13 +1,16 @@
 #ifndef __LUNAIX_AHCI_UTILS_H
 #define __LUNAIX_AHCI_UTILS_H
 
-#include <hal/ahci/ahci.h>
+#include <hal/ahci/hba.h>
 #include <stdint.h>
 
 void
-ahci_parse_dev_info(struct ahci_device_info* dev_info, uint16_t* data);
+ahci_parse_dev_info(struct hba_device* dev_info, uint16_t* data);
 
 void
 ahci_parsestr(char* str, uint16_t* reg_start, int size_word);
 
+void
+scsi_parse_capacity(struct hba_device* device, uint32_t* parameter);
+
 #endif /* __LUNAIX_UTILS_H */