X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/af8e873ae38b72a56a89485c62bb5ccd22a9f8a7..ebb55b7e5f0b8f31328950ec383b77b208ffbb64:/lunaix-os/hal/ahci/ahci.c diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index a04bca7..82375e0 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -12,29 +12,43 @@ #include #include #include -#include - #include + +#include + #include #include +#include #include -#include #include -#include +#include #include #include #define HBA_FIS_SIZE 256 #define HBA_CLB_SIZE 1024 +#define HBA_MY_IE (HBA_PxINTR_DHR | HBA_PxINTR_TFE | HBA_PxINTR_OF) +#define AHCI_DEVCLASS DEVCLASS(DEVIF_PCI, DEVFN_STORAGE, DEV_SATA) + // #define DO_HBA_FULL_RESET LOG_MODULE("AHCI") -static struct ahci_hba hba; +DEFINE_LLIST(ahcis); -void -__ahci_hba_isr(const isr_param* param); +static char sata_ifs[][20] = { "Not detected", + "SATA I (1.5Gbps)", + "SATA II (3.0Gbps)", + "SATA III (6.0Gbps)" }; + +static struct devclass ahci_class = AHCI_DEVCLASS; + +extern void +ahci_fsexport(struct block_dev* bdev, void* fs_node); + +extern void +__ahci_blkio_handler(struct blkio_req* req); int ahci_init_device(struct hba_port* port); @@ -42,20 +56,8 @@ 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 +ahci_register_device(struct hba_device* hbadev); void __hba_reset_port(hba_reg_t* port_reg) @@ -69,55 +71,46 @@ __hba_reset_port(hba_reg_t* port_reg) } // 如果port未响应,则继续执行重置 port_reg[HBA_RPxSCTL] = (port_reg[HBA_RPxSCTL] & ~0xf) | 1; - io_delay(100000); //等待至少一毫秒,差不多就行了 + port_delay(100000); // 等待至少一毫秒,差不多就行了 port_reg[HBA_RPxSCTL] &= ~0xf; } -void -ahci_init() +struct ahci_driver* +ahci_driver_init(struct ahci_driver_param* param) { - struct pci_device* ahci_dev = pci_get_device_by_class(AHCI_HBA_CLASS); - assert_msg(ahci_dev, "AHCI: Not found."); - - uintptr_t bar6, size; - size = pci_bar_sizing(ahci_dev, &bar6, 6); - assert_msg(bar6 && PCI_BAR_MMIO(bar6), "AHCI: BAR#6 is not MMIO."); - - pci_reg_t cmd = pci_read_cspace(ahci_dev->cspace_base, PCI_REG_STATUS_CMD); - - // 禁用传统中断(因为我们使用MSI),启用MMIO访问,允许PCI设备间访问 - cmd |= (PCI_RCMD_MM_ACCESS | PCI_RCMD_DISABLE_INTR | PCI_RCMD_BUS_MASTER); + struct ahci_driver* ahci_drv = vzalloc(sizeof(*ahci_drv)); + struct ahci_hba* hba = &ahci_drv->hba; + ahci_drv->id = param->ahci_iv; - pci_write_cspace(ahci_dev->cspace_base, PCI_REG_STATUS_CMD, cmd); + isrm_set_payload(param->ahci_iv, (ptr_t)&ahcis); - pci_setup_msi(ahci_dev, AHCI_HBA_IV); - intr_subscribe(AHCI_HBA_IV, __ahci_hba_isr); + llist_append(&ahcis, &ahci_drv->ahci_drvs); - memset(&hba, 0, sizeof(hba)); - - hba.base = (hba_reg_t*)ioremap(PCI_BAR_ADDR_MM(bar6), size); + hba->base = (hba_reg_t*)ioremap(param->mmio_base, param->mmio_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; + ptr_t clb_pg_addr = 0, fis_pg_addr = 0; + ptr_t clb_pa = 0, fis_pa = 0; + for (size_t i = 0, fisp = 0, clbp = 0; i < 32; i++, pmap >>= 1, fisp = (fisp + 1) % 16, clbp = (clbp + 1) % 4) { if (!(pmap & 0x1)) { @@ -127,41 +120,46 @@ 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); #endif + struct leaflet* leaflet; if (!clbp) { // 每页最多4个命令队列 - clb_pa = pmm_alloc_page(KERNEL_PID, PP_FGLOCKED); - clb_pg_addr = ioremap(clb_pa, 0x1000); - memset(clb_pg_addr, 0, 0x1000); + leaflet = alloc_leaflet(0); + clb_pa = leaflet_addr(leaflet); + clb_pg_addr = vmap(leaflet, KERNEL_DATA); + memset((void*)clb_pg_addr, 0, 0x1000); } if (!fisp) { // 每页最多16个FIS - fis_pa = pmm_alloc_page(KERNEL_PID, PP_FGLOCKED); - fis_pg_addr = ioremap(fis_pa, 0x1000); - memset(fis_pg_addr, 0, 0x1000); + leaflet = alloc_leaflet(0); + fis_pa = leaflet_addr(leaflet); + fis_pg_addr = vmap(leaflet, KERNEL_DATA); + memset((void*)fis_pg_addr, 0, 0x1000); } /* 重定向CLB与FIS */ port_regs[HBA_RPxCLB] = clb_pa + clbp * HBA_CLB_SIZE; port_regs[HBA_RPxFB] = fis_pa + 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 = (struct hba_port){ + .regs = port_regs, + .ssts = port_regs[HBA_RPxSSTS], + .cmdlst = (struct hba_cmdh*)(clb_pg_addr + clbp * HBA_CLB_SIZE), + .fis = (void*)(fis_pg_addr + fisp * HBA_FIS_SIZE), + .hba = hba + }; /* 初始化端口,并置于就绪状态 */ port_regs[HBA_RPxCI] = 0; - // 需要通过全部置位去清空这些寄存器(相当的奇怪……) - port_regs[HBA_RPxSERR] = -1; + hba_clear_reg(port_regs[HBA_RPxSERR]); - hba.ports[i] = port; + hba->ports[i] = port; if (!HBA_RPxSSTS_IF(port->ssts)) { continue; @@ -172,64 +170,34 @@ ahci_init() port_regs[HBA_RPxCMD] |= HBA_PxCMD_ST; if (!ahci_init_device(port)) { - kprintf(KERROR "fail to init device"); + ERROR("init fail: 0x%x@p%d", port->regs[HBA_RPxSIG], i); + continue; } - block_mount_disk(port->device); - } -} + struct hba_device* hbadev = port->device; + kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d", + i, + hbadev->model, + hbadev->block_size, + (u32_t)hbadev->max_lba); -char sata_ifs[][20] = { "Not detected", - "SATA I (1.5Gbps)", - "SATA II (3.0Gbps)", - "SATA III (6.0Gbps)" }; + ahci_register_device(hbadev); + } -void -__ahci_hba_isr(const isr_param* param) -{ - // TODO: clear the interrupt status - // TODO: I/O-operation scheduler should be here - kprintf(KDEBUG "HBA INTR\n"); + return ahci_drv; } void -ahci_list_device() +ahci_register_device(struct hba_device* hbadev) { - 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; - } + struct block_dev* bdev = + block_alloc_dev(hbadev->model, hbadev, __ahci_blkio_handler); - int device_state = HBA_RPxSSTS_IF(port->ssts); + bdev->end_lba = hbadev->max_lba; + bdev->blk_size = hbadev->block_size; + bdev->class = &ahci_class; - 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); - } + block_mount(bdev, ahci_fsexport); } int @@ -238,17 +206,17 @@ __get_free_slot(struct hba_port* port) hba_reg_t pxsact = port->regs[HBA_RPxSACT]; 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) + u32_t i = 0; + 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 sata_create_fis(struct sata_reg_fis* cmd_fis, - uint8_t command, - uint64_t lba, - uint16_t sector_count) + u8_t command, + lba_t lba, + u16_t sector_count) { cmd_fis->head.type = SATA_REG_FIS_H2D; cmd_fis->head.options = SATA_REG_FIS_COMMAND; @@ -266,16 +234,46 @@ sata_create_fis(struct sata_reg_fis* cmd_fis, cmd_fis->count = sector_count; } +int +hba_bind_sbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct membuf mbuf) +{ + assert_msg(mbuf.size <= 0x400000U, "HBA: Buffer too big"); + cmdh->prdt_len = 1; + cmdt->entries[0] = + (struct hba_prdte){ .data_base = vmm_v2p((ptr_t)mbuf.buffer), + .byte_count = mbuf.size - 1 }; + + return 0; +} + +int +hba_bind_vbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct vecbuf* vbuf) +{ + size_t i = 0; + struct vecbuf* pos = vbuf; + + do { + assert_msg(i < HBA_MAX_PRDTE, "HBA: Too many PRDTEs"); + assert_msg(pos->buf.size <= 0x400000U, "HBA: Buffer too big"); + + cmdt->entries[i++] = + (struct hba_prdte){ .data_base = vmm_v2p((ptr_t)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; + + return 0; +} + int hba_prepare_cmd(struct hba_port* port, struct hba_cmdt** cmdt, - struct hba_cmdh** cmdh, - void* buffer, - unsigned int size) + struct hba_cmdh** cmdh) { 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]; @@ -284,17 +282,10 @@ hba_prepare_cmd(struct hba_port* port, memset(cmd_header, 0, sizeof(*cmd_header)); // 将命令表挂到命令头上 - cmd_header->cmd_table_base = vmm_v2p(cmd_table); + cmd_header->cmd_table_base = vmm_v2p((ptr_t)cmd_table); 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; @@ -309,20 +300,18 @@ ahci_init_device(struct hba_port* port) struct hba_cmdh* cmd_header; // mask DHR interrupt - port->regs[HBA_RPxIE] &= ~HBA_PxINTR_DHR; - - // 确保端口是空闲的 - wait_until(!(port->regs[HBA_RPxTFD] & (HBA_PxTFD_BSY))); + port->regs[HBA_RPxIE] &= ~HBA_MY_IE; // 预备DMA接收缓存,用于存放HBA传回的数据 - uint16_t* data_in = (uint16_t*)valloc_dma(512); + u16_t* data_in = (u16_t*)valloc_dma(512); - int slot = hba_prepare_cmd(port, &cmd_table, &cmd_header, data_in, 512); + int slot = hba_prepare_cmd(port, &cmd_table, &cmd_header); + hba_bind_sbuf( + cmd_header, cmd_table, (struct membuf){ .buffer = data_in, .size = 512 }); - // 清空任何待响应的中断 - port->regs[HBA_RPxIS] = 0; 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; @@ -337,14 +326,7 @@ ahci_init_device(struct hba_port* port) 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 ((port->regs[HBA_RPxTFD] & HBA_PxTFD_ERR)) { - // 有错误 - sata_read_error(port); + if (!ahci_try_send(port, slot)) { goto fail; } @@ -383,30 +365,32 @@ ahci_init_device(struct hba_port* port) * SATA AHCI HBA Spec, Section 5.3.7 * SCSI Command Reference Manual, Section 3.26 */ - struct scsi_cdb16* cdb16 = (struct scsi_cdb16*)cmd_table->atapi_cmd; sata_create_fis(cmd_fis, ATA_PACKET, 512 << 8, 0); - scsi_create_packet16(cdb16, SCSI_READ_CAPACITY_16, 0, 512); - cdb16->misc1 = 0x10; // service action + // 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 ((port->regs[HBA_RPxTFD] & HBA_PxTFD_ERR)) { - // 有错误 - sata_read_error(port); + if (!ahci_try_send(port, slot)) { goto fail; } - scsi_parse_capacity(port->device, (uint32_t*)data_in); + scsi_parse_capacity(port->device, (u32_t*)data_in); done: // reset interrupt status and unmask D2HR interrupt - port->regs[HBA_RPxIS] = -1; - port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR; + port->regs[HBA_RPxIE] |= HBA_MY_IE; achi_register_ops(port); vfree_dma(data_in); @@ -415,8 +399,7 @@ done: return 1; fail: - port->regs[HBA_RPxIS] = -1; - port->regs[HBA_RPxIE] |= HBA_PxINTR_DHR; + port->regs[HBA_RPxIE] |= HBA_MY_IE; vfree_dma(data_in); vfree_dma(cmd_table); @@ -436,10 +419,8 @@ 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; + port->device->ops.submit = sata_submit; } else { - port->device->ops.read_buffer = scsi_read_buffer; - port->device->ops.write_buffer = scsi_write_buffer; + port->device->ops.submit = scsi_submit; } -} \ No newline at end of file +}