|
|
@@ -39,166 +39,159 @@
|
|
|
#include <lib/string.h>
|
|
|
|
|
|
|
|
|
-volatile struct ahci_hba *hba;
|
|
|
-__uint64_t ahci_buffer;
|
|
|
+struct ahci_hba_mem *hba;
|
|
|
+static char buffer[512];
|
|
|
|
|
|
-build_irq(0x2a);
|
|
|
|
|
|
+build_irq(0x2e);
|
|
|
+build_irq(0x2f);
|
|
|
|
|
|
-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);
|
|
|
+ register_irq(14, irq0x2e_interrupt, do_ahci_intr);
|
|
|
+ register_irq(15, irq0x2f_interrupt, do_ahci_intr);
|
|
|
|
|
|
- ahci_info("irq: %b\n", hdr->irqnr);
|
|
|
+ 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);
|
|
|
|
|
|
- /* map the bar5 to memory */
|
|
|
- hba = mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
|
|
|
+ hba = (struct ahci_hba_mem *) mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
|
|
|
|
|
|
- ahci_info("hba mem maps to the %q. \n", hba);
|
|
|
+ ahci_info("AHCI HBA MEM maps to %q. \n", hba);
|
|
|
|
|
|
- /* ahci enable, interrupt enable, hba reset */
|
|
|
- hba->ghc = 0x8000000;
|
|
|
+ ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr);
|
|
|
+ ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK);
|
|
|
|
|
|
- register_irq(0x20 + 0x0a, irq0x2a_interrupt, do_ahci_intr);
|
|
|
+ hba->global_ctl |= HBA_MEM_GHC_AHCI_ENABLE;
|
|
|
|
|
|
- struct cmd_header *cmdhdr = kmalloc(&devpool, sizeof(struct cmd_header));
|
|
|
+ ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
|
|
|
|
|
|
- if (!cmdhdr)
|
|
|
- panic("ahci_init: failed to allocate space for ahci command header. \n");
|
|
|
+ if (!(hba->bios_handoff & HBA_MEM_BHO_OWN_OS)) {
|
|
|
+ /* get the ownership */
|
|
|
|
|
|
- hba->portctl[0].cmd_listl = (__uint32_t) ((__uint64_t) cmdhdr) & 0xffffffff;
|
|
|
- hba->portctl[0].cmd_listh = (__uint32_t) ((__uint64_t) cmdhdr >> 32) & 0xffffffff;
|
|
|
+ hba->bios_handoff = (hba->bios_handoff & ~HBA_MEM_BHO_OWN_RWC) |
|
|
|
+ HBA_MEM_BHO_OWN_OS;
|
|
|
|
|
|
- ahci_info("current ahci list address: %q. \n", (__uint64_t) hba->portctl[0].cmd_listl);
|
|
|
+ int timeout = 4096;
|
|
|
|
|
|
- struct cmd_table *cmdtbl = alloc_page_att(PATT_DEVMM);
|
|
|
+ while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--);
|
|
|
|
|
|
- ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
- ->cmd_table_low = (__uint32_t) ((__uint64_t) cmdtbl) & 0xffffffff;
|
|
|
+ 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);
|
|
|
+ }
|
|
|
|
|
|
- ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
- ->cmd_table_high = (__uint32_t) ((__uint64_t) cmdtbl >> 32) & 0xffffffff;
|
|
|
+ else
|
|
|
+ ahci_info("AHCI is already in the OS mode. \n");
|
|
|
|
|
|
- ahci_info("current ahci command table address: %q. \n",
|
|
|
- (__uint64_t) ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
|
|
|
- ->cmd_table_low);
|
|
|
+ int i, nrdevs = 0;
|
|
|
|
|
|
- ahci_info("reading the sector 1 from the ahci disk...\n");
|
|
|
+ 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_read(hba->portctl, 0x0000000000000001, 1, buf);
|
|
|
+ ahci_info("found ahci device %d on port %d. ", nrdevs++, i);
|
|
|
|
|
|
- return 0;
|
|
|
-}
|
|
|
+ struct ahci_cmd_header *cmdlist = kmalloc(&devpool,
|
|
|
+ sizeof(struct ahci_cmd_header));
|
|
|
|
|
|
+ memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
|
|
|
|
|
|
-void do_ahci_intr(void) {
|
|
|
- ahci_info("irq 0x0e: sata master disk. \n");
|
|
|
+ /* well, that needs a physical address, I think... */
|
|
|
+ hba->ports[i].cmd_list = (__uint64_t) mm_vphy(cmdlist);
|
|
|
|
|
|
- ahci_info("disk data: \n");
|
|
|
+ struct ahci_cmd_table *cmdtable = kmalloc(&devpool,
|
|
|
+ sizeof(struct ahci_cmd_table));
|
|
|
|
|
|
- int i;
|
|
|
+ memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
|
|
|
|
|
|
- for (i = 0; i < 512 >> 3; i++)
|
|
|
- ahci_info("%q", ((__uint64_t *) buf)[i]);
|
|
|
+ cmdlist->cmd_table = (__uint64_t) mm_vphy(cmdlist);
|
|
|
|
|
|
- return;
|
|
|
-}
|
|
|
+ }
|
|
|
|
|
|
+ ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0);
|
|
|
|
|
|
-static int find_free_cmdslot(volatile struct ahci_hba_portctl *port) {
|
|
|
- int i;
|
|
|
- __uint32_t slot = (port->active | port->ci);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- for (i = 0; i < 32; i++) {
|
|
|
- if (!(slot & 1))
|
|
|
- return i;
|
|
|
|
|
|
- slot >>= 1;
|
|
|
- }
|
|
|
+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);
|
|
|
|
|
|
- ahci_info("find_free_cmdslot: warning: can't find a free slot");
|
|
|
+ struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
|
|
|
|
|
|
- return 0;
|
|
|
-}
|
|
|
+ ahci_info("ahci_rw: command list: %q. \n", cmdhdr);
|
|
|
|
|
|
+ memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
|
|
|
|
|
|
-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);
|
|
|
+ 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);
|
|
|
|
|
|
- port->int_status = -1;
|
|
|
+ cmdhdr->transfered = 0;
|
|
|
|
|
|
- struct cmd_header *cmdhdr = (struct cmd_header *) (__uint64_t) port->cmd_listl;
|
|
|
- ahci_info("ahci_read: command header address: %q. \n", cmdhdr);
|
|
|
+ struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
|
|
|
+ memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
|
|
|
|
|
|
- cmdhdr += slot;
|
|
|
+ cmdhdr->cmd_table = (__uint64_t) mm_vphy(cmdtable);
|
|
|
|
|
|
- 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 fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
|
|
|
|
|
|
- struct cmd_table *cmdtbl = (struct cmd_table *) (__uint64_t) cmdhdr->cmd_table_low;
|
|
|
- ahci_info("ahci_read: command table address: %q. \n", cmdtbl);
|
|
|
+ memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
|
|
|
|
|
|
- int i;
|
|
|
+ /* FIS_TYPE_H2D_FIS */
|
|
|
+ cmd_fis->fis_type = 0x27;
|
|
|
+ cmd_fis->config = 0 | FISH2D_CMD;
|
|
|
+ cmd_fis->command = atacmd;
|
|
|
|
|
|
- /* setup first 1 --- (last-1), and setup the last one later */
|
|
|
+ cmd_fis->lba0 = lba & 0xff;
|
|
|
+ cmd_fis->lba1 = (lba >> 8) & 0xff;
|
|
|
+ cmd_fis->lba2 = (lba >> 16) & 0xff;
|
|
|
|
|
|
- for (i = 0; i < cmdhdr->prdt_len - 1; i++) {
|
|
|
- memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
|
|
|
+ cmd_fis->lba3 = (lba >> 24) & 0xff;
|
|
|
+ cmd_fis->lba4 = (lba >> 32) & 0xff;
|
|
|
+ cmd_fis->lba5 = (lba >> 40) & 0xff;
|
|
|
|
|
|
- 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;
|
|
|
+ cmd_fis->dev = 0xe0;
|
|
|
+ cmd_fis->nsectors = 1;
|
|
|
|
|
|
- buf += 8192;
|
|
|
- }
|
|
|
|
|
|
- memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
|
|
|
+ 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;
|
|
|
|
|
|
- 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;
|
|
|
+ port->int_enable = 1 << 5;
|
|
|
+ port->int_status = 1 << 5;
|
|
|
+ port->cmd_issue |= 1;
|
|
|
+ port->cmd |= 1;
|
|
|
|
|
|
- struct fis_h2d *h2dfis = (struct fis_h2d *) &cmdtbl->cmd_fis;
|
|
|
+ /* wait it complete */
|
|
|
|
|
|
- h2dfis->fis_type = FIS_HOSTTODEV;
|
|
|
- h2dfis->cmd = 1;
|
|
|
- h2dfis->command = ATACMD_RDDMA;
|
|
|
+ ahci_info("waiting device complete...\n");
|
|
|
|
|
|
- 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;
|
|
|
+ while (!(port->int_status & (1 << 5)));
|
|
|
|
|
|
- h2dfis->count_low = nsectors & 0xff;
|
|
|
- h2dfis->count_high = (nsectors >> 8) & 0xff;
|
|
|
+ ahci_info("AHCI request completed. \n");
|
|
|
+ port->cmd &= ~1;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- h2dfis->dev = 0xe0;
|
|
|
|
|
|
- port->active = 1;
|
|
|
- port->cmd |= 0x10000003;
|
|
|
- port->ci = 1;
|
|
|
+void do_ahci_intr(void) {
|
|
|
+ ahci_info("AHCI interrupt. \n");
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
-
|