/* Copyright (C) 2021 Rain */ /* This file is part of XNIX. */ /* XNIX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) and later version. */ /* XNIX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ /* You should have received a copy of the GNU General Public License along with XNIX. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include struct ahci_hba_mem *hba; static char buffer[512]; build_irq(0x2e); build_irq(0x2f); errno_t ahci_init(struct pci_header0 *hdr) { register_irq(14, irq0x2e_interrupt, do_ahci_intr); register_irq(15, irq0x2f_interrupt, do_ahci_intr); ahci_info("PCI BAR0: \t%q\n", hdr->bar0); ahci_info("PCI BAR1: \t%q\n", hdr->bar1); ahci_info("PCI BAR2: \t%q\n", hdr->bar2); ahci_info("PCI BAR3: \t%q\n", hdr->bar3); ahci_info("PCI BAR4: \t%q\n", hdr->bar4); ahci_info("PCI BAR5: \t%q\n", hdr->bar5); hba = (struct ahci_hba_mem *) mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM); ahci_info("AHCI HBA MEM maps to %q. \n", hba); ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr); ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK); hba->global_ctl |= HBA_MEM_GHC_AHCI_ENABLE; ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff); if (!(hba->bios_handoff & HBA_MEM_BHO_OWN_OS)) { /* get the ownership */ hba->bios_handoff = (hba->bios_handoff & ~HBA_MEM_BHO_OWN_RWC) | HBA_MEM_BHO_OWN_OS; int timeout = 4096; while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--); if (!timeout) { hba->bios_handoff = HBA_MEM_BHO_OWN_OS; hba->bios_handoff |= HBA_MEM_BHO_OWN_CHANGED; } ahci_info("got the ownership. \n"); ahci_info("current AHCI BIOS-handoff: %d\n", hba->bios_handoff); } else ahci_info("AHCI is already in the OS mode. \n"); int i, nrdevs = 0; for (i = 0; i < (hba->cap & HBA_MEM_CAP_MASK); i++) { /* if (bitmap_read(ports_impled, i) == 0) return; */ if (!(hba->ports_impled & (1 << i))) continue; ahci_info("found ahci device %d on port %d. ", nrdevs++, i); struct ahci_cmd_header *cmdlist = kmalloc(&devpool, sizeof(struct ahci_cmd_header)); memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header)); /* well, that needs a physical address, I think... */ hba->ports[i].cmd_list = (__uint64_t) mm_vphy(cmdlist); struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table)); memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table)); cmdlist->cmd_table = (__uint64_t) mm_vphy(cmdlist); } ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0); return 0; } int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) { ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n", port, lba, buffer, atacmd); struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list; ahci_info("ahci_rw: command list: %q. \n", cmdhdr); memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header)); cmdhdr->config = (4 << CMDHDR_CFG_CFISLEN_SHIFT) | CMDHDR_CFG_CLEAR_R_OK | (0 << CMDHDR_CFG_PORTML_SHIFT) | (1 << CMDHDR_CFG_PRDTLEN_SHIFT) | ((atacmd == ATACMD_WR || atacmd == ATACMD_WR48) ? CMDHDR_CFG_WRITE : 0); cmdhdr->transfered = 0; struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table)); memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table)); cmdhdr->cmd_table = (__uint64_t) mm_vphy(cmdtable); struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d)); memset(cmd_fis, 0x00, sizeof(struct fis_h2d)); /* FIS_TYPE_H2D_FIS */ cmd_fis->fis_type = 0x27; cmd_fis->config = 0 | FISH2D_CMD; cmd_fis->command = atacmd; cmd_fis->lba0 = lba & 0xff; cmd_fis->lba1 = (lba >> 8) & 0xff; cmd_fis->lba2 = (lba >> 16) & 0xff; cmd_fis->lba3 = (lba >> 24) & 0xff; cmd_fis->lba4 = (lba >> 32) & 0xff; cmd_fis->lba5 = (lba >> 40) & 0xff; cmd_fis->dev = 0xe0; cmd_fis->nsectors = 1; memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d)); cmdtable->prdts[0].buf = (__uint64_t) buffer; cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR; port->int_enable = 1 << 5; port->int_status = 1 << 5; port->cmd_issue |= 1; port->cmd |= 1; /* wait it complete */ ahci_info("waiting device complete...\n"); while (!(port->int_status & (1 << 5))); ahci_info("AHCI request completed. \n"); port->cmd &= ~1; return 0; } void do_ahci_intr(void) { ahci_info("AHCI interrupt. \n"); return; }