ahci: complete read/write sectors function

This commit is contained in:
Michael Shaw 2023-05-07 15:21:04 -04:00
commit 69ee1fd79c
6 changed files with 75 additions and 11 deletions

View file

@ -1,7 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include "stdio.h" #include "stdio.h"
#include <kernel/mem.h> #include <kernel/mem.h>
#include <kernel/slab.h>
#include <kernel/irq.h> #include <kernel/irq.h>
#include <kernel/pio.h>
#include <dev/ahci.h> #include <dev/ahci.h>
#include <dev/pci.h> #include <dev/pci.h>
@ -15,6 +17,7 @@ static paddr_t ahci_pbase = 0;
static void ahci_irq_handler() static void ahci_irq_handler()
{ {
printf(LOG_ERROR "AHCI INTERRUPT\n");
} }
static int ahci_port_type(hbaport_t *port) static int ahci_port_type(hbaport_t *port)
@ -110,14 +113,16 @@ static int cmdslot(hbaport_t *port)
{ {
uint32_t slots = (port->sact | port->ci); uint32_t slots = (port->sact | port->ci);
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
if ((slots & 1) == 0) if ((slots & 1))
slots >>= 1;
else
return i; return i;
slots >>= 1;
} }
return -1; return -1;
} }
bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf) bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{ {
hbaport_t *port = &dev->abar->ports[dev->port]; hbaport_t *port = &dev->abar->ports[dev->port];
@ -154,7 +159,7 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis); struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
cmdfis->fis_type = FIS_REG_H2D; cmdfis->fis_type = FIS_REG_H2D;
cmdfis->cmd_mode = 1; cmdfis->cmd_mode = 1;
cmdfis->command = ATA_CMD_READ_DMA_EX; cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX;
cmdfis->lba0 = (uint8_t)lba; cmdfis->lba0 = (uint8_t)lba;
cmdfis->lba1 = (uint8_t)(lba >> 8); cmdfis->lba1 = (uint8_t)(lba >> 8);
@ -177,8 +182,10 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad
return false; return false;
} }
/* issue command */
port->ci = 1 << slot; port->ci = 1 << slot;
/* wait for completion */
while (1) { while (1) {
if ((port->ci & (1 << slot)) == 0) if ((port->ci & (1 << slot)) == 0)
break; break;
@ -188,7 +195,8 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad
} }
} }
if(port->is & HBA_PORT_IS_TFES) { /* check again */
if (port->is & HBA_PORT_IS_TFES) {
printf(LOG_ERROR "AHCI: Read disk error\n"); printf(LOG_ERROR "AHCI: Read disk error\n");
return false; return false;
} }
@ -201,18 +209,35 @@ void ahci_init()
struct pci_device *dev = pci_find_device(0x01, 0x06); struct pci_device *dev = pci_find_device(0x01, 0x06);
/* check interrupt number */ /* check interrupt number */
if (dev == NULL) { if (dev == NULL) {
printf(LOG_ERROR "Failed to find AHCI device\n"); printf(LOG_WARN "No SATA controllers found\n");
return; return;
} }
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start); abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
if (!(abar->ghc & (1 << 31))) { if (!(abar->ghc & (1 << 31))) {
printf(LOG_ERROR "AHCI controller not enabled\n"); printf(LOG_WARN "AHCI controller not enabled\n");
return; return;
} }
/* enable AHCI interrupts */
abar->ghc |= AHCI_GHC_IE;
abar->ghc |= AHCI_GHC_AE;
size_t ahci_zone_size = 0x100000;
ahci_pbase = (paddr_t)buddy_alloc(ahci_zone_size) & (~hhdm_start); /* 1 MiB */
ahci_probe_ports(dev, abar); ahci_probe_ports(dev, abar);
ahci_pbase = (paddr_t)buddy_alloc(0x100000); /* 1 MiB */
/* map ahci zone with cache disabled (pcd=1) */
struct page attrs;
attrs.val = 1;
attrs.rw = 1;
attrs.pcd = 1;
kmap(ahci_pbase, ahci_pbase | hhdm_start, ahci_zone_size, attrs.val);
if (sata_device_count == 0) {
printf(LOG_WARN "No SATA devices found\n");
return;
}
/* setup device irq number */ /* setup device irq number */
uint16_t irq = irq_highest_free(); uint16_t irq = irq_highest_free();
@ -222,7 +247,13 @@ void ahci_init()
pci_config_write_long(dev->bus, dev->slot, dev->func, 0x3C, tmp); pci_config_write_long(dev->bus, dev->slot, dev->func, 0x3C, tmp);
irq_map(irq, ahci_irq_handler); irq_map(irq, ahci_irq_handler);
tmp = pci_config_read_long(dev->bus, dev->slot, dev->func, 0x3C); char *buf = kmalloc(512);
ahci_access_sectors(&sata_devices[1], 0, 1, (paddr_t)buf & (~hhdm_start), false);
buf[511] = 0;
printf("AHCI: %s\n", buf);
memcpy(buf, "Hello, World!", 13);
ahci_access_sectors(&sata_devices[1], 0, 1, (paddr_t)buf & (~hhdm_start), true);
printf(LOG_SUCCESS "AHCI controller ready\n"); printf(LOG_SUCCESS "AHCI controller ready\n");
} }

View file

@ -19,6 +19,7 @@
#define ATA_DEV_DRQ 0x08 #define ATA_DEV_DRQ 0x08
#define ATA_CMD_READ_DMA_EX 0x25 #define ATA_CMD_READ_DMA_EX 0x25
#define ATA_CMD_WRITE_DMA_EX 0x35
#define ATA_CMD_IDENT 0xEC #define ATA_CMD_IDENT 0xEC
#define SATA_SIG_ATA 0x00000101 #define SATA_SIG_ATA 0x00000101
@ -32,6 +33,10 @@
#define AHCI_DEV_PM 3 #define AHCI_DEV_PM 3
#define AHCI_DEV_SATAPI 4 #define AHCI_DEV_SATAPI 4
#define AHCI_GHC_ENABLE 0x1
#define AHCI_GHC_IE 0x2
#define AHCI_GHC_AE 0x80000000
#define HBA_PORT_IPM_ACTIVE 0x1 #define HBA_PORT_IPM_ACTIVE 0x1
#define HBA_PORT_DET_PRESENT 0x3 #define HBA_PORT_DET_PRESENT 0x3

View file

@ -80,6 +80,7 @@ extern size_t num_regions;
extern size_t max_regions; extern size_t max_regions;
extern struct page *pml4; extern struct page *pml4;
extern paddr_t kcr3;
uint64_t cr3_read(); uint64_t cr3_read();
void cr3_write(uint64_t cr3); void cr3_write(uint64_t cr3);

View file

@ -104,7 +104,7 @@ void irq_unmap(uint8_t irq)
int irq_highest_free() int irq_highest_free()
{ {
for (int i = 15; i >= 0; i--) { for (int i = 14; i >= 0; i--) {
if (!irq_mapped[i]) if (!irq_mapped[i])
return i; return i;
} }

View file

@ -13,6 +13,7 @@
#include <dev/pci.h> #include <dev/pci.h>
#include <dev/ahci.h> #include <dev/ahci.h>
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
#include <limine/limine.h> #include <limine/limine.h>
#define KSTACK_SIZE 0x4000 #define KSTACK_SIZE 0x4000
@ -20,6 +21,12 @@
void stack_init(uintptr_t rsp, uintptr_t rbp); void stack_init(uintptr_t rsp, uintptr_t rbp);
struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 }; struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 };
struct limine_kernel_file_request module_req = { .id = LIMINE_KERNEL_FILE_REQUEST, .revision = 0 };
static char *kcmdline;
static char *kpath;
static struct limine_uuid kroot_disk;
static struct limine_uuid kroot_part;
uintptr_t hhdm_start; uintptr_t hhdm_start;
uintptr_t data_end; uintptr_t data_end;
@ -43,7 +50,6 @@ static inline void _pic_init()
while (!console_ready()) while (!console_ready())
; ;
iowait();
} }
void kmain(void) void kmain(void)
@ -63,6 +69,24 @@ void kmain(void)
mem_early_init(kmem, one_gb); mem_early_init(kmem, one_gb);
kmalloc_init(); kmalloc_init();
struct limine_kernel_file_response *resp = module_req.response;
struct limine_file *kfile = (struct limine_file *)((paddr_t)resp->kernel_file | hhdm_start);
kcmdline = kzalloc(strlen(kfile->cmdline) + 1);
strcpy(kcmdline, kfile->cmdline);
kpath = kzalloc(strlen(kfile->path) + 1);
strcpy(kpath, kfile->path);
kroot_disk = kfile->gpt_disk_uuid;
kroot_part = kfile->gpt_part_uuid;
#ifdef KDEBUG
printf(LOG_DEBUG "Kernel file: path='%s' cmdline='%s'\n", kpath, kcmdline);
printf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b);
#endif /* KDEBUG */
console_init(); console_init();
acpi_init(); acpi_init();

View file

@ -21,6 +21,8 @@ struct page *pml4;
struct mem_region *mem_regions; struct mem_region *mem_regions;
size_t num_regions; size_t num_regions;
paddr_t kcr3 = 0;
static void *(*alloc_page)(void); static void *(*alloc_page)(void);
static struct slab *kmap_slab; static struct slab *kmap_slab;
@ -431,6 +433,7 @@ void mem_early_init(char *mem, size_t len)
pml4_paddr = (uintptr_t)pml4; pml4_paddr = (uintptr_t)pml4;
pml4_paddr &= (~hhdm_start); pml4_paddr &= (~hhdm_start);
cr3.pml4_addr = pml4_paddr >> 12; cr3.pml4_addr = pml4_paddr >> 12;
kcr3 = cr3.val;
cr3_write(cr3.val); cr3_write(cr3.val);
/* done */ /* done */