| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /* 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 <https://www.gnu.org/licenses/>.
- */
- #include <kernel/init.h>
- #include <kernel/errno.h>
- #include <kernel/types.h>
- #include <kernel/console.h>
- #include <kernel/pci.h>
- #include <kernel/mm.h>
- #include <kernel/int.h>
- #include <kernel/panic.h>
- #include <kernel/schk.h>
- #include <arch/x86/paging.h>
- #include <modules/ahci.h>
- #include <lib/string.h>
- volatile struct ahci_hba_mem *hba;
- static char buffer[512] = {0};
- build_irq(0x2a);
- build_irq(0x2e);
- build_irq(0x2f);
- errno_t ahci_init(struct pci_header0 *hdr) {
- schk_init();
- register_irq(10, irq0x2a_interrupt, do_ahci_intr);
- 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);
- if (hba->global_ctl & HBA_MEM_GHC_AHCI_ENABLE)
- ahci_info("AHCI is already in the AHCI mode. \n");
- else
- ahci_info("AHCI is in the IDE mode. \n");
- hba->global_ctl = 0x80000002;
- mfence();
- ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
- 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. \n", nrdevs++, i);
- volatile 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((void *) cmdlist);
- volatile 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((void *) cmdlist);
- }
- hba->ports[0].cmd = hba->ports[0].cmd | 0x10000003;
- hba->ports[0].sata_ctl = 0x04;
- mfence();
- ahci_info("SATA error: %x. \n", hba->ports[0].sata_error);
- ahci_info("interrupt status: %x\n", hba->ports[0].int_status);
- ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 2);
- schk_leave();
- return 0;
- }
- static void wait_controller_ready(volatile struct ahci_hba_port *port) {
- schk_init();
- while (port->sata_status & ATASTAT_BUSY && !(port->sata_status & ATASTAT_ERR));
- ahci_info("wait_controller_ready: controller is now ready. \n");
- schk_leave();
- return;
- }
- int ahci_rw_sect(volatile struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
- schk_init();
- volatile char *devbuf = kmalloc(&devpool, 512);
- wait_controller_ready(port);
- if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
- memcpy(devbuf, buffer, 512);
- ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n",
- port, lba, buffer, atacmd);
- port->sata_error = port->sata_error;
- volatile struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
- ahci_info("ahci_rw: command list: %q. \n", mm_vphy((void *) 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);
- if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
- cmdhdr->config |= CMDHDR_CFG_WRITE;
- cmdhdr->transfered = 0;
- volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
- ahci_info("ahci_rw: command table: %q. \n", mm_vphy((void *) cmdtable));
- memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
- cmdhdr->cmd_table = (__uint64_t) mm_vphy((void *) cmdtable);
- volatile struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
- ahci_info("ahci_rw: command fis: %q. \n", mm_vphy((void *) cmd_fis));
- 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) mm_vphy((void *) devbuf);
- cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
- port->int_enable = 0xffffffff;
- port->int_status = 0xffffffff;
- port->cmd_issue = 1;
- port->sata_active = 1;
- /* interrupt enable */
- port->sata_ctl = 0x00;
-
- mfence();
- /* wait it complete */
- ahci_info("waiting device complete...\n");
- ahci_info("sata error: %q. \n", port->sata_error);
- ahci_info("sata status: %q. \n", port->sata_status);
- for (;;) {
- /* task file error */
- if (port->cmd_issue & (1 << 30)) {
- ahci_info("ahci_rw_sect: controller error. \n");
- goto panic;
- }
- if (!(port->cmd_issue & 1))
- /* complete */
- break;
- }
- if (port->sata_status & ATASTAT_ERR) {
- ahci_info("ahci_rw_sect: controller error. \n");
- ahci_info("sata error: %q\n", port->sata_status);
- goto panic;
- }
- int i;
- for (i = 0; i < 512 / 8; i++)
- printk(yellow, "%x", devbuf[i]);
- ahci_info("\nAHCI request completed. \n");
- port->cmd &= ~1;
- if (atacmd == ATACMD_RD || atacmd == ATACMD_RD48)
- memcpy(buffer, devbuf, 512);
- panic:
- kfree(&devpool, (void *) cmd_fis, sizeof(struct fis_h2d));
- kfree(&devpool, (void *) cmdtable, sizeof(struct ahci_cmd_table));
- kfree(&devpool, (void *) devbuf, 512);
- schk_leave();
- return 0;
- }
- void do_ahci_intr(void) {
- ahci_info("AHCI interrupt. \n");
- return;
- }
|