fix: symlink resolve.
[lunaix-os.git] / lunaix-os / hal / ahci / ahci.c
index 6771bb690248412bfc1d9455551ed5d7af2e4898..9a7073a6c0c17c58363eb8091434e37d15d96d26 100644 (file)
 #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/block.h>
 #include <lunaix/mm/mmio.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/valloc.h>
 #define HBA_FIS_SIZE 256
 #define HBA_CLB_SIZE 1024
 
+// #define DO_HBA_FULL_RESET
+
 LOG_MODULE("AHCI")
 
 static struct ahci_hba hba;
 
 void
-__ahci_hba_isr(isr_param param);
+__ahci_hba_isr(const isr_param* param);
 
 int
 ahci_init_device(struct hba_port* port);
 
+void
+achi_register_ops(struct hba_port* port);
+
+unsigned int
+ahci_get_port_usage()
+{
+    return hba.ports_bmp;
+}
+
+struct hba_port*
+ahci_get_port(unsigned int index)
+{
+    if (index >= 32) {
+        return 0;
+    }
+    return hba.ports[index];
+}
+
+void
+__hba_reset_port(hba_reg_t* port_reg)
+{
+    // 根据:SATA-AHCI spec section 10.4.2 描述的端口重置流程
+    port_reg[HBA_RPxCMD] &= ~HBA_PxCMD_ST;
+    port_reg[HBA_RPxCMD] &= ~HBA_PxCMD_FRE;
+    int cnt = wait_until_expire(!(port_reg[HBA_RPxCMD] & HBA_PxCMD_CR), 500000);
+    if (cnt) {
+        return;
+    }
+    // 如果port未响应,则继续执行重置
+    port_reg[HBA_RPxSCTL] = (port_reg[HBA_RPxSCTL] & ~0xf) | 1;
+    io_delay(100000); //等待至少一毫秒,差不多就行了
+    port_reg[HBA_RPxSCTL] &= ~0xf;
+}
+
 void
 ahci_init()
 {
@@ -60,9 +96,11 @@ ahci_init()
 
     hba.base = (hba_reg_t*)ioremap(PCI_BAR_ADDR_MM(bar6), size);
 
+#ifdef DO_HBA_FULL_RESET
     // 重置HBA
     hba.base[HBA_RGHC] |= HBA_RGHC_RESET;
     wait_until(!(hba.base[HBA_RGHC] & HBA_RGHC_RESET));
+#endif
 
     // 启用AHCI工作模式,启用中断
     hba.base[HBA_RGHC] |= HBA_RGHC_ACHI_ENABLE;
@@ -70,12 +108,14 @@ ahci_init()
 
     // As per section 3.1.1, this is 0 based value.
     hba_reg_t cap = hba.base[HBA_RCAP];
+    hba_reg_t pmap = hba.base[HBA_RPI];
+
     hba.ports_num = (cap & 0x1f) + 1;  // CAP.PI
     hba.cmd_slots = (cap >> 8) & 0x1f; // CAP.NCS
     hba.version = hba.base[HBA_RVER];
+    hba.ports_bmp = pmap;
 
     /* ------ HBA端口配置 ------ */
-    hba_reg_t pmap = hba.base[HBA_RPI];
     uintptr_t clb_pg_addr, fis_pg_addr, clb_pa, fis_pa;
     for (size_t i = 0, fisp = 0, clbp = 0; i < 32;
          i++, pmap >>= 1, fisp = (fisp + 1) % 16, clbp = (clbp + 1) % 4) {
@@ -88,6 +128,10 @@ ahci_init()
         hba_reg_t* port_regs =
           (hba_reg_t*)(&hba.base[HBA_RPBASE + i * HBA_RPSIZE]);
 
+#ifndef DO_HBA_FULL_RESET
+        __hba_reset_port(port_regs);
+#endif
+
         if (!clbp) {
             // 每页最多4个命令队列
             clb_pa = pmm_alloc_page(KERNEL_PID, PP_FGLOCKED);
@@ -113,23 +157,30 @@ ahci_init()
         /* 初始化端口,并置于就绪状态 */
         port_regs[HBA_RPxCI] = 0;
 
-        // 需要通过全部置位去清空这些寄存器(相当的奇怪……)
-        port_regs[HBA_RPxSERR] = -1;
-
-        port_regs[HBA_RPxIE] |= (HBA_PxINTR_DMA);
-        port_regs[HBA_RPxIE] |= (HBA_PxINTR_D2HR);
+        hba_clear_reg(port_regs[HBA_RPxSERR]);
 
         hba.ports[i] = port;
 
-        if (HBA_RPxSSTS_IF(port->ssts)) {
-            wait_until(!(port_regs[HBA_RPxCMD] & HBA_PxCMD_CR));
-            port_regs[HBA_RPxCMD] |= HBA_PxCMD_FRE;
-            port_regs[HBA_RPxCMD] |= HBA_PxCMD_ST;
+        if (!HBA_RPxSSTS_IF(port->ssts)) {
+            continue;
+        }
+
+        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_init_device(port)) {
-                kprintf(KERROR "fail to init device");
-            }
+        if (!ahci_init_device(port)) {
+            kprintf(KERROR "init fail: 0x%x@p%d\n", port->regs[HBA_RPxSIG], i);
+            continue;
         }
+
+        kprintf(KINFO "sata%d: %s, sector_size=%dB, sector=%d\n",
+                i,
+                port->device->model,
+                port->device->block_size,
+                (uint32_t)port->device->max_lba);
+
+        block_mount_disk(port->device);
     }
 }
 
@@ -139,9 +190,10 @@ char sata_ifs[][20] = { "Not detected",
                         "SATA III (6.0Gbps)" };
 
 void
-__ahci_hba_isr(isr_param param)
+__ahci_hba_isr(const isr_param* param)
 {
-    // TODO: hba interrupt
+    // TODO: clear the interrupt status
+    // TODO: I/O-operation scheduler should be here
     kprintf(KDEBUG "HBA INTR\n");
 }
 
@@ -168,7 +220,7 @@ ahci_list_device()
         kprintf("\t Port %d: %s (%x)\n",
                 i,
                 &sata_ifs[device_state],
-                port->regs[HBA_RPxSIG]);
+                port->device->flags);
 
         struct hba_device* dev_info = port->device;
         if (!device_state || !dev_info) {
@@ -176,7 +228,10 @@ ahci_list_device()
         }
         kprintf("\t\t capacity: %d KiB\n",
                 (dev_info->max_lba * dev_info->block_size) >> 10);
-        kprintf("\t\t sector size: %dB\n", dev_info->block_size);
+        kprintf("\t\t block size: %dB\n", dev_info->block_size);
+        kprintf("\t\t block/sector: %d\n", dev_info->block_per_sec);
+        kprintf("\t\t alignment: %dB\n", dev_info->alignment_offset);
+        kprintf("\t\t capabilities: %x\n", dev_info->capabilities);
         kprintf("\t\t model: %s\n", &dev_info->model);
         kprintf("\t\t serial: %s\n", &dev_info->serial_num);
     }
@@ -197,8 +252,7 @@ __get_free_slot(struct hba_port* port)
 void
 sata_create_fis(struct sata_reg_fis* cmd_fis,
                 uint8_t command,
-                uint32_t lba_lo,
-                uint32_t lba_hi,
+                uint64_t lba,
                 uint16_t sector_count)
 {
     cmd_fis->head.type = SATA_REG_FIS_H2D;
@@ -206,38 +260,45 @@ sata_create_fis(struct sata_reg_fis* cmd_fis,
     cmd_fis->head.status_cmd = command;
     cmd_fis->dev = 0;
 
-    cmd_fis->lba0 = SATA_LBA_COMPONENT(lba_lo, 0);
-    cmd_fis->lba8 = SATA_LBA_COMPONENT(lba_lo, 8);
-    cmd_fis->lba16 = SATA_LBA_COMPONENT(lba_lo, 16);
-    cmd_fis->lba24 = SATA_LBA_COMPONENT(lba_lo, 24);
+    cmd_fis->lba0 = SATA_LBA_COMPONENT(lba, 0);
+    cmd_fis->lba8 = SATA_LBA_COMPONENT(lba, 8);
+    cmd_fis->lba16 = SATA_LBA_COMPONENT(lba, 16);
+    cmd_fis->lba24 = SATA_LBA_COMPONENT(lba, 24);
 
-    cmd_fis->lba32 = SATA_LBA_COMPONENT(lba_hi, 0);
-    cmd_fis->lba40 = SATA_LBA_COMPONENT(lba_hi, 8);
+    cmd_fis->lba32 = SATA_LBA_COMPONENT(lba, 32);
+    cmd_fis->lba40 = SATA_LBA_COMPONENT(lba, 40);
 
     cmd_fis->count = sector_count;
 }
 
 int
-hba_alloc_slot(struct hba_port* port,
-               struct hba_cmdt** cmdt,
-               struct hba_cmdh** cmdh,
-               uint16_t header_options)
+hba_prepare_cmd(struct hba_port* port,
+                struct hba_cmdt** cmdt,
+                struct hba_cmdh** cmdh,
+                void* buffer,
+                unsigned int size)
 {
     int slot = __get_free_slot(port);
     assert_msg(slot >= 0, "HBA: No free slot");
+    assert_msg(size <= 0x400000, "HBA: buffer too big");
 
     // 构建命令头(Command Header)和命令表(Command Table)
     struct hba_cmdh* cmd_header = &port->cmdlst[slot];
-    struct hba_cmdt* cmd_table = valloc_dma(sizeof(struct hba_cmdt));
+    struct hba_cmdt* cmd_table = vzalloc_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);
+    cmd_header->options =
+      HBA_CMDH_FIS_LEN(sizeof(struct sata_reg_fis)) | HBA_CMDH_CLR_BUSY;
+
+    if (buffer) {
+        cmd_header->prdt_len = 1;
+        cmd_table->entries[0] =
+          (struct hba_prdte){ .data_base = vmm_v2p(buffer),
+                              .byte_count = size - 1 };
+    }
 
     *cmdh = cmd_header;
     *cmdt = cmd_table;
@@ -252,37 +313,33 @@ ahci_init_device(struct hba_port* port)
     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];
+    // mask DHR interrupt
+    port->regs[HBA_RPxIE] &= ~HBA_PxINTR_DHR;
 
     // 预备DMA接收缓存,用于存放HBA传回的数据
     uint16_t* data_in = (uint16_t*)valloc_dma(512);
 
-    cmd_table->entries[0] =
-      (struct hba_prdte){ .data_base = vmm_v2p(data_in),
-                          .byte_count = 511 }; // byte_count是从0开始算的
-    cmd_header->prdt_len = 1;
+    int slot = hba_prepare_cmd(port, &cmd_table, &cmd_header, data_in, 512);
+
+    port->device = vzalloc(sizeof(struct hba_device));
+    port->device->port = port;
 
     // 在命令表中构建命令FIS
     struct sata_reg_fis* cmd_fis = (struct sata_reg_fis*)cmd_table->command_fis;
 
     // 根据设备类型使用合适的命令
-    if (port->device->signature == HBA_DEV_SIG_ATA) {
+    if (port->regs[HBA_RPxSIG] == HBA_DEV_SIG_ATA) {
         // ATA 一般为硬盘
-        sata_create_fis(cmd_fis, ATA_IDENTIFY_DEVICE, 0, 0, 0);
+        sata_create_fis(cmd_fis, ATA_IDENTIFY_DEVICE, 0, 0);
     } else {
         // ATAPI 一般为光驱,软驱,或者磁带机
-        sata_create_fis(cmd_fis, ATA_IDENTIFY_PAKCET_DEVICE, 0, 0, 0);
+        port->device->flags |= HBA_DEV_FATAPI;
+        sata_create_fis(cmd_fis, ATA_IDENTIFY_PAKCET_DEVICE, 0, 0);
     }
 
-    // PxCI寄存器置位,告诉HBA这儿有个数据需要发送到SATA端口
-    port->regs[HBA_RPxCI] = (1 << slot);
-
-    wait_until(!(port->regs[HBA_RPxCI] & (1 << slot)));
+    if (!ahci_try_send(port, slot)) {
+        goto fail;
+    }
 
     /*
         等待数据到达内存
@@ -292,7 +349,7 @@ ahci_init_device(struct hba_port* port)
     */
     ahci_parse_dev_info(port->device, data_in);
 
-    if (port->device->signature == HBA_DEV_SIG_ATA) {
+    if (!(port->device->flags & HBA_DEV_FATAPI)) {
         goto done;
     }
 
@@ -319,23 +376,64 @@ ahci_init_device(struct hba_port* port)
             * SATA AHCI HBA Spec, Section 5.3.7
             * SCSI Command Reference Manual, Section 3.26
     */
-    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
+
+    sata_create_fis(cmd_fis, ATA_PACKET, 512 << 8, 0);
+
+    // for dev use 12 bytes cdb, READ_CAPACITY must use the 10 bytes variation.
+    if (port->device->cbd_size == SCSI_CDB12) {
+        struct scsi_cdb12* cdb12 = (struct scsi_cdb12*)cmd_table->atapi_cmd;
+        // ugly tricks to construct 10 byte cdb from 12 byte cdb
+        scsi_create_packet12(cdb12, SCSI_READ_CAPACITY_10, 0, 512 << 8);
+    } else {
+        struct scsi_cdb16* cdb16 = (struct scsi_cdb16*)cmd_table->atapi_cmd;
+        scsi_create_packet16(cdb16, SCSI_READ_CAPACITY_16, 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)));
+    if (!ahci_try_send(port, slot)) {
+        goto fail;
+    }
 
     scsi_parse_capacity(port->device, (uint32_t*)data_in);
 
 done:
+    // reset interrupt status and unmask D2HR interrupt
+    port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR;
+    achi_register_ops(port);
+
     vfree_dma(data_in);
     vfree_dma(cmd_table);
 
     return 1;
+
+fail:
+    port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR;
+    vfree_dma(data_in);
+    vfree_dma(cmd_table);
+
+    return 0;
+}
+
+int
+ahci_identify_device(struct hba_device* device)
+{
+    // 用于重新识别设备(比如在热插拔的情况下)
+    vfree(device);
+    return ahci_init_device(device->port);
 }
 
-// TODO: Support ATAPI Device.
\ No newline at end of file
+void
+achi_register_ops(struct hba_port* port)
+{
+    port->device->ops.identify = ahci_identify_device;
+    if (!(port->device->flags & HBA_DEV_FATAPI)) {
+        port->device->ops.read_buffer = sata_read_buffer;
+        port->device->ops.write_buffer = sata_write_buffer;
+    } else {
+        port->device->ops.read_buffer = scsi_read_buffer;
+        port->device->ops.write_buffer = scsi_write_buffer;
+    }
+}
\ No newline at end of file