|
|
@@ -0,0 +1,204 @@
|
|
|
+/* 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 <arch/x86/paging.h>
|
|
|
+#include <modules/ahci.h>
|
|
|
+#include <lib/string.h>
|
|
|
+
|
|
|
+
|
|
|
+volatile struct ahci_hba *hba;
|
|
|
+__uint64_t ahci_buffer;
|
|
|
+
|
|
|
+build_irq(0x2a);
|
|
|
+
|
|
|
+
|
|
|
+static char buf[512] = {0};
|
|
|
+
|
|
|
+/* calls from pci_init() */
|
|
|
+
|
|
|
+errno_t ahci_init(struct pci_header0 *hdr) {
|
|
|
+ ahci_info("bar0: %q\n", hdr->bar0);
|
|
|
+ ahci_info("bar1: %q\n", hdr->bar1);
|
|
|
+ ahci_info("bar2: %q\n", hdr->bar2);
|
|
|
+ ahci_info("bar3: %q\n", hdr->bar3);
|
|
|
+ ahci_info("bar4: %q\n", hdr->bar4);
|
|
|
+ ahci_info("bar5: %q\n", hdr->bar5);
|
|
|
+
|
|
|
+ ahci_info("irq: %b\n", hdr->irqnr);
|
|
|
+
|
|
|
+ /* map the bar5 to memory */
|
|
|
+ hba = mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
|
|
|
+
|
|
|
+ ahci_info("hba mem maps to the %q. \n", hba);
|
|
|
+
|
|
|
+ /* ahci enable, interrupt enable, hba reset */
|
|
|
+ hba->ghc = 0x8000000;
|
|
|
+
|
|
|
+ register_irq(0x20 + 0x0a, irq0x2a_interrupt, do_ahci_intr);
|
|
|
+
|
|
|
+ struct cmd_header *cmdhdr = kmalloc(&devpool, sizeof(struct cmd_header));
|
|
|
+
|
|
|
+ if (!cmdhdr)
|
|
|
+ panic("ahci_init: failed to allocate space for ahci command header. \n");
|
|
|
+
|
|
|
+ hba->portctl[0].cmd_listl = (__uint32_t) ((__uint64_t) cmdhdr) & 0xffffffff;
|
|
|
+ hba->portctl[0].cmd_listh = (__uint32_t) ((__uint64_t) cmdhdr >> 32) & 0xffffffff;
|
|
|
+
|
|
|
+ ahci_info("current ahci list address: %q. \n", (__uint64_t) hba->portctl[0].cmd_listl);
|
|
|
+
|
|
|
+ struct cmd_table *cmdtbl = alloc_page_att(PATT_DEVMM);
|
|
|
+
|
|
|
+ ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
+ ->cmd_table_low = (__uint32_t) ((__uint64_t) cmdtbl) & 0xffffffff;
|
|
|
+
|
|
|
+
|
|
|
+ ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
+ ->cmd_table_high = (__uint32_t) ((__uint64_t) cmdtbl >> 32) & 0xffffffff;
|
|
|
+
|
|
|
+ ahci_info("current ahci command table address: %q. \n",
|
|
|
+ (__uint64_t) ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
+ ->cmd_table_low);
|
|
|
+
|
|
|
+ ahci_info("reading the sector 1 from the ahci disk...\n");
|
|
|
+
|
|
|
+ ahci_read(hba->portctl, 0x0000000000000001, 1, buf);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void do_ahci_intr(void) {
|
|
|
+ ahci_info("irq 0x0e: sata master disk. \n");
|
|
|
+
|
|
|
+ ahci_info("disk data: \n");
|
|
|
+
|
|
|
+ int i;
|
|
|
+
|
|
|
+ for (i = 0; i < 512 >> 3; i++)
|
|
|
+ ahci_info("%q", ((__uint64_t *) buf)[i]);
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int find_free_cmdslot(volatile struct ahci_hba_portctl *port) {
|
|
|
+ int i;
|
|
|
+ __uint32_t slot = (port->active | port->ci);
|
|
|
+
|
|
|
+ for (i = 0; i < 32; i++) {
|
|
|
+ if (!(slot & 1))
|
|
|
+ return i;
|
|
|
+
|
|
|
+ slot >>= 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ ahci_info("find_free_cmdslot: warning: can't find a free slot");
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ahci_read(volatile struct ahci_hba_portctl *port, __uint64_t lba, __uint16_t nsectors, char *buf) {
|
|
|
+ int slot = find_free_cmdslot(port);
|
|
|
+
|
|
|
+ ahci_info("ahci_read: free cmd slot: %d. \n", slot);
|
|
|
+
|
|
|
+ port->int_status = -1;
|
|
|
+
|
|
|
+ struct cmd_header *cmdhdr = (struct cmd_header *) (__uint64_t) port->cmd_listl;
|
|
|
+ ahci_info("ahci_read: command header address: %q. \n", cmdhdr);
|
|
|
+
|
|
|
+ cmdhdr += slot;
|
|
|
+
|
|
|
+ cmdhdr->fis_len = sizeof(struct fis_h2d) / sizeof(int);
|
|
|
+ cmdhdr->wr = 0;
|
|
|
+ cmdhdr->atapi = 0;
|
|
|
+ cmdhdr->clear = 1;
|
|
|
+ cmdhdr->pre = 1;
|
|
|
+ /* the max size of a prdt is 8kb (16 sectors, >> 4) */
|
|
|
+ cmdhdr->prdt_len = ((nsectors - 1) >> 4) + 1;
|
|
|
+
|
|
|
+ struct cmd_table *cmdtbl = (struct cmd_table *) (__uint64_t) cmdhdr->cmd_table_low;
|
|
|
+ ahci_info("ahci_read: command table address: %q. \n", cmdtbl);
|
|
|
+
|
|
|
+ int i;
|
|
|
+
|
|
|
+ /* setup first 1 --- (last-1), and setup the last one later */
|
|
|
+
|
|
|
+ for (i = 0; i < cmdhdr->prdt_len - 1; i++) {
|
|
|
+ memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
|
|
|
+
|
|
|
+ cmdtbl->prdts[i].buf_low = (__uint32_t) (__uint64_t) buf & 0xffffffff;
|
|
|
+ cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
|
|
|
+ /* max 8kb */
|
|
|
+ cmdtbl->prdts[i].nbytes = 8191;
|
|
|
+ cmdtbl->prdts[i].irq = 1;
|
|
|
+
|
|
|
+ buf += 8192;
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
|
|
|
+
|
|
|
+ cmdtbl->prdts[i].buf_low = (__uint32_t) ((__uint64_t) buf) & 0xffffffff;
|
|
|
+ cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
|
|
|
+ cmdtbl->prdts[i].nbytes = (nsectors << 9) % 8192 - 1;
|
|
|
+ cmdtbl->prdts[i].irq = 1;
|
|
|
+
|
|
|
+ struct fis_h2d *h2dfis = (struct fis_h2d *) &cmdtbl->cmd_fis;
|
|
|
+
|
|
|
+ h2dfis->fis_type = FIS_HOSTTODEV;
|
|
|
+ h2dfis->cmd = 1;
|
|
|
+ h2dfis->command = ATACMD_RDDMA;
|
|
|
+
|
|
|
+ h2dfis->lba0 = lba & 0xff;
|
|
|
+ h2dfis->lba1 = (lba >> 8) & 0xff;
|
|
|
+ h2dfis->lba2 = (lba >> 16) & 0xff;
|
|
|
+ h2dfis->lba3 = (lba >> 24) & 0xff;
|
|
|
+ h2dfis->lba4 = (lba >> 32) & 0xff;
|
|
|
+ h2dfis->lba5 = (lba >> 40) & 0xff;
|
|
|
+
|
|
|
+ h2dfis->count_low = nsectors & 0xff;
|
|
|
+ h2dfis->count_high = (nsectors >> 8) & 0xff;
|
|
|
+
|
|
|
+ h2dfis->dev = 0xe0;
|
|
|
+
|
|
|
+ port->active = 1;
|
|
|
+ port->cmd |= 0x10000003;
|
|
|
+ port->ci = 1;
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|