X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/3b6a05fc894d0c1a3d431045ee5a53955ba093a0..1e54f1850eda97e3b41c302b72c07a1f9cc4d637:/lunaix-os/hal/ahci/ahci.c diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index c9f8297..e07200e 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -27,11 +27,13 @@ #define HBA_FIS_SIZE 256 #define HBA_CLB_SIZE 1024 +#define HBA_MY_IE (HBA_PxINTR_DHR | HBA_PxINTR_TFE | HBA_PxINTR_OF) + // #define DO_HBA_FULL_RESET LOG_MODULE("AHCI") -struct ahci_hba hba; +struct llist_header ahcis; static char sata_ifs[][20] = { "Not detected", "SATA I (1.5Gbps)", @@ -56,20 +58,8 @@ achi_register_ops(struct hba_port* port); void ahci_register_device(struct hba_device* hbadev); -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* +ahci_driver_init(struct pci_device* ahci_dev); void __hba_reset_port(hba_reg_t* port_reg) @@ -90,9 +80,13 @@ __hba_reset_port(hba_reg_t* port_reg) void ahci_init() { - struct pci_device* ahci_dev = pci_get_device_by_class(AHCI_HBA_CLASS); - assert_msg(ahci_dev, "AHCI: Not found."); + llist_init_head(&ahcis); + pci_add_driver("Serial ATA AHCI", AHCI_HBA_CLASS, 0, 0, ahci_driver_init); +} +void* +ahci_driver_init(struct pci_device* ahci_dev) +{ struct pci_base_addr* bar6 = &ahci_dev->bar[5]; assert_msg(bar6->type & BAR_TYPE_MMIO, "AHCI: BAR#6 is not MMIO."); @@ -103,30 +97,35 @@ ahci_init() pci_write_cspace(ahci_dev->cspace_base, PCI_REG_STATUS_CMD, cmd); - pci_setup_msi(ahci_dev, isrm_ivexalloc(__ahci_hba_isr)); + int iv = isrm_ivexalloc(__ahci_hba_isr); + pci_setup_msi(ahci_dev, iv); + + struct ahci_driver* ahci_drv = vzalloc(sizeof(*ahci_drv)); + struct ahci_hba* hba = &ahci_drv->hba; + ahci_drv->id = iv; - memset(&hba, 0, sizeof(hba)); + llist_append(&ahcis, &ahci_drv->ahci_drvs); - hba.base = (hba_reg_t*)ioremap(bar6->start, bar6->size); + hba->base = (hba_reg_t*)ioremap(bar6->start, bar6->size); #ifdef DO_HBA_FULL_RESET // 重置HBA - hba.base[HBA_RGHC] |= HBA_RGHC_RESET; - wait_until(!(hba.base[HBA_RGHC] & HBA_RGHC_RESET)); + 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; - hba.base[HBA_RGHC] |= HBA_RGHC_INTR_ENABLE; + hba->base[HBA_RGHC] |= HBA_RGHC_ACHI_ENABLE; + hba->base[HBA_RGHC] |= HBA_RGHC_INTR_ENABLE; // 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_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->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端口配置 ------ */ uintptr_t clb_pg_addr, fis_pg_addr, clb_pa, fis_pa; @@ -139,7 +138,7 @@ ahci_init() 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]); + (hba_reg_t*)(&hba->base[HBA_RPBASE + i * HBA_RPSIZE]); #ifndef DO_HBA_FULL_RESET __hba_reset_port(port_regs); @@ -165,14 +164,15 @@ ahci_init() *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 }; + .fis = fis_pg_addr + fisp * HBA_FIS_SIZE, + .hba = hba }; /* 初始化端口,并置于就绪状态 */ port_regs[HBA_RPxCI] = 0; hba_clear_reg(port_regs[HBA_RPxSERR]); - hba.ports[i] = port; + hba->ports[i] = port; if (!HBA_RPxSSTS_IF(port->ssts)) { continue; @@ -188,7 +188,7 @@ ahci_init() } struct hba_device* hbadev = port->device; - kprintf(KINFO "sata%d: %s, sector_size=%dB, sector=%d\n", + kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d\n", i, hbadev->model, hbadev->block_size, @@ -196,6 +196,8 @@ ahci_init() ahci_register_device(hbadev); } + + return ahci_drv; } void @@ -210,46 +212,6 @@ ahci_register_device(struct hba_device* hbadev) block_mount(bdev, ahci_fsexport); } -void -ahci_list_device() -{ - kprintf(KINFO "Version: %x; Ports: %d; Slot: %d\n", - hba.version, - hba.ports_num, - hba.cmd_slots); - struct hba_port* port; - for (size_t i = 0; i < 32; i++) { - port = hba.ports[i]; - - // 愚蠢的gcc似乎认为 struct hba_port* 不可能为空 - // 所以将这个非常关键的if给优化掉了。 - // 这里将指针强制转换为整数,欺骗gcc :) - if ((uintptr_t)port == 0) { - continue; - } - - int device_state = HBA_RPxSSTS_IF(port->ssts); - - kprintf("\t Port %d: %s (%x)\n", - i, - &sata_ifs[device_state], - port->device->flags); - - 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->block_size) >> 10); - 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); - } -} - int __get_free_slot(struct hba_port* port) { @@ -257,9 +219,9 @@ __get_free_slot(struct hba_port* port) hba_reg_t pxci = port->regs[HBA_RPxCI]; hba_reg_t free_bmp = pxsact | pxci; uint32_t i = 0; - for (; i <= hba.cmd_slots && (free_bmp & 0x1); i++, free_bmp >>= 1) + for (; i <= port->hba->cmd_slots && (free_bmp & 0x1); i++, free_bmp >>= 1) ; - return i | -(i > hba.cmd_slots); + return i | -(i > port->hba->cmd_slots); } void @@ -287,7 +249,7 @@ sata_create_fis(struct sata_reg_fis* cmd_fis, int hba_bind_sbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct membuf mbuf) { - assert_msg(mbuf.buffer <= 0x400000, "HBA: Buffer too big"); + assert_msg(mbuf.size <= 0x400000, "HBA: Buffer too big"); cmdh->prdt_len = 1; cmdt->entries[0] = (struct hba_prdte){ .data_base = vmm_v2p(mbuf.buffer), .byte_count = mbuf.size - 1 }; @@ -297,17 +259,17 @@ int hba_bind_vbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct vecbuf* vbuf) { size_t i = 0; - struct vecbuf *pos, *n; + struct vecbuf* pos = vbuf; - llist_for_each(pos, n, &vbuf->components, components) - { + do { assert_msg(i < HBA_MAX_PRDTE, "HBA: Too many PRDTEs"); - assert_msg(pos->buf.buffer <= 0x400000, "HBA: Buffer too big"); + assert_msg(pos->buf.size <= 0x400000, "HBA: Buffer too big"); cmdt->entries[i++] = (struct hba_prdte){ .data_base = vmm_v2p(pos->buf.buffer), .byte_count = pos->buf.size - 1 }; - } + pos = list_entry(pos->components.next, struct vecbuf, components); + } while (pos != vbuf); cmdh->prdt_len = i + 1; } @@ -345,7 +307,7 @@ ahci_init_device(struct hba_port* port) struct hba_cmdh* cmd_header; // mask DHR interrupt - port->regs[HBA_RPxIE] &= ~HBA_PxINTR_DHR; + port->regs[HBA_RPxIE] &= ~HBA_MY_IE; // 预备DMA接收缓存,用于存放HBA传回的数据 uint16_t* data_in = (uint16_t*)valloc_dma(512); @@ -356,6 +318,7 @@ ahci_init_device(struct hba_port* port) port->device = vzalloc(sizeof(struct hba_device)); port->device->port = port; + port->device->hba = port->hba; // 在命令表中构建命令FIS struct sata_reg_fis* cmd_fis = (struct sata_reg_fis*)cmd_table->command_fis; @@ -434,7 +397,7 @@ ahci_init_device(struct hba_port* port) done: // reset interrupt status and unmask D2HR interrupt - port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR; + port->regs[HBA_RPxIE] |= HBA_MY_IE; achi_register_ops(port); vfree_dma(data_in); @@ -443,7 +406,7 @@ done: return 1; fail: - port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR; + port->regs[HBA_RPxIE] |= HBA_MY_IE; vfree_dma(data_in); vfree_dma(cmd_table);