|
|
@@ -33,21 +33,26 @@
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
|
-struct ahci_hba_mem *hba;
|
|
|
-static char buffer[512];
|
|
|
+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);
|
|
|
|
|
|
@@ -65,89 +70,115 @@ errno_t ahci_init(struct pci_header0 *hdr) {
|
|
|
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;
|
|
|
+ if (hba->global_ctl & HBA_MEM_GHC_AHCI_ENABLE)
|
|
|
+ ahci_info("AHCI is already in the AHCI mode. \n");
|
|
|
|
|
|
- while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--);
|
|
|
+ else
|
|
|
+ ahci_info("AHCI is in the IDE mode. \n");
|
|
|
|
|
|
- if (!timeout) {
|
|
|
- hba->bios_handoff = HBA_MEM_BHO_OWN_OS;
|
|
|
- hba->bios_handoff |= HBA_MEM_BHO_OWN_CHANGED;
|
|
|
- }
|
|
|
+ hba->global_ctl = 0x80000002;
|
|
|
|
|
|
- ahci_info("got the ownership. \n");
|
|
|
- ahci_info("current AHCI BIOS-handoff: %d\n", hba->bios_handoff);
|
|
|
- }
|
|
|
+ mfence();
|
|
|
|
|
|
- else
|
|
|
- ahci_info("AHCI is already in the OS mode. \n");
|
|
|
+ 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++) {
|
|
|
+ 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);
|
|
|
+ ahci_info("found ahci device %d on port %d. \n", nrdevs++, i);
|
|
|
|
|
|
- struct ahci_cmd_header *cmdlist = kmalloc(&devpool,
|
|
|
+ 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(cmdlist);
|
|
|
+ hba->ports[i].cmd_list = (__uint64_t) mm_vphy((void *) cmdlist);
|
|
|
|
|
|
- struct ahci_cmd_table *cmdtable = kmalloc(&devpool,
|
|
|
+ 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(cmdlist);
|
|
|
+ cmdlist->cmd_table = (__uint64_t) mm_vphy((void *) cmdlist);
|
|
|
|
|
|
}
|
|
|
|
|
|
- ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0);
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
|
|
|
+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);
|
|
|
|
|
|
- struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
|
|
|
+ port->sata_error = port->sata_error;
|
|
|
|
|
|
- ahci_info("ahci_rw: command list: %q. \n", cmdhdr);
|
|
|
+ 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) |
|
|
|
- ((atacmd == ATACMD_WR || atacmd == ATACMD_WR48) ?
|
|
|
- CMDHDR_CFG_WRITE : 0);
|
|
|
+ (1 << CMDHDR_CFG_PRDTLEN_SHIFT);
|
|
|
+
|
|
|
+ if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
|
|
|
+ cmdhdr->config |= CMDHDR_CFG_WRITE;
|
|
|
|
|
|
cmdhdr->transfered = 0;
|
|
|
|
|
|
- struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
|
|
|
+ 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(cmdtable);
|
|
|
+ cmdhdr->cmd_table = (__uint64_t) mm_vphy((void *) cmdtable);
|
|
|
+
|
|
|
+ volatile struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
|
|
|
|
|
|
- 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));
|
|
|
|
|
|
@@ -167,25 +198,66 @@ int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_
|
|
|
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].buf = (__uint64_t) mm_vphy((void *) devbuf);
|
|
|
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;
|
|
|
+ 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");
|
|
|
|
|
|
- while (!(port->int_status & (1 << 5)));
|
|
|
+ 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("AHCI request completed. \n");
|
|
|
+ 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;
|
|
|
}
|
|
|
|