ahci: add ahci_read_sectors function
This commit is contained in:
parent
0c7c542eb5
commit
7db20603f6
3 changed files with 163 additions and 8 deletions
139
dev/ahci.c
139
dev/ahci.c
|
|
@ -9,7 +9,9 @@
|
|||
static hbamem_t *abar = NULL;
|
||||
|
||||
static uint8_t sata_device_count = 0;
|
||||
static struct sata_device sata_devices[32] = {0};
|
||||
static struct sata_device sata_devices[32] = { 0 };
|
||||
|
||||
static paddr_t ahci_pbase = 0;
|
||||
|
||||
static void ahci_irq_handler()
|
||||
{
|
||||
|
|
@ -23,7 +25,7 @@ static int ahci_port_type(hbaport_t *port)
|
|||
if (det != HBA_PORT_DET_PRESENT)
|
||||
return AHCI_DEV_NULL;
|
||||
|
||||
if(port->clb_low == 0 && port->clb_high == 0)
|
||||
if (port->clb == 0)
|
||||
return AHCI_DEV_NULL;
|
||||
|
||||
switch (port->sig) {
|
||||
|
|
@ -38,6 +40,46 @@ static int ahci_port_type(hbaport_t *port)
|
|||
}
|
||||
}
|
||||
|
||||
static void halt_cmd(hbaport_t *port)
|
||||
{
|
||||
port->cmd &= ~HBA_PORT_CMD_ST;
|
||||
port->cmd &= ~HBA_PORT_CMD_FRE;
|
||||
|
||||
while (port->cmd & (HBA_PORT_CMD_FR | HBA_PORT_CMD_CR))
|
||||
;
|
||||
}
|
||||
|
||||
static void start_cmd(hbaport_t *port)
|
||||
{
|
||||
while (port->cmd & HBA_PORT_CMD_CR)
|
||||
;
|
||||
|
||||
port->cmd |= HBA_PORT_CMD_FRE;
|
||||
port->cmd |= HBA_PORT_CMD_ST;
|
||||
}
|
||||
|
||||
static void ahci_rebase(hbaport_t *port, int n)
|
||||
{
|
||||
halt_cmd(port);
|
||||
|
||||
port->clb = (ahci_pbase) + (n << 10);
|
||||
memset((void *)(port->clb | hhdm_start), 0, 1024);
|
||||
|
||||
port->fb = ahci_pbase + (32 << 10) + (n << 8);
|
||||
memset((void *)(port->fb | hhdm_start), 0, 256);
|
||||
|
||||
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
cmdheader[i].prdt_len = 8;
|
||||
cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8);
|
||||
|
||||
void *ctba = (void *)((uintptr_t)cmdheader[i].cmd_table_base | hhdm_start);
|
||||
memset(ctba, 0, 256);
|
||||
}
|
||||
|
||||
start_cmd(port);
|
||||
}
|
||||
|
||||
static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
||||
{
|
||||
uint32_t pi = abar->pi;
|
||||
|
|
@ -49,6 +91,8 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
|||
sata_devices[sata_device_count].port = i;
|
||||
sata_devices[sata_device_count].controller = dev;
|
||||
sata_devices[sata_device_count].abar = abar;
|
||||
|
||||
ahci_rebase(&abar->ports[i], i);
|
||||
sata_device_count++;
|
||||
} else if (type == AHCI_DEV_SATAPI) {
|
||||
printf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
||||
|
|
@ -62,6 +106,96 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
|||
}
|
||||
}
|
||||
|
||||
static int cmdslot(hbaport_t *port)
|
||||
{
|
||||
uint32_t slots = (port->sact | port->ci);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if ((slots & 1) == 0)
|
||||
return i;
|
||||
slots >>= 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf)
|
||||
{
|
||||
hbaport_t *port = &dev->abar->ports[dev->port];
|
||||
|
||||
/* clear pending interrupt */
|
||||
port->is = (uint32_t)-1;
|
||||
|
||||
int spin = 0;
|
||||
int slot = cmdslot(port);
|
||||
if (slot == -1)
|
||||
return false;
|
||||
|
||||
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
||||
cmdheader += slot;
|
||||
cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
|
||||
cmdheader->write = 0;
|
||||
cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1;
|
||||
|
||||
struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start);
|
||||
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
|
||||
|
||||
int i;
|
||||
for (i = 0; i < cmdheader->prdt_len - 1; i++) {
|
||||
cmdtbl->prdt_entry[i].data_base = buf;
|
||||
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
|
||||
cmdtbl->prdt_entry[i].interrupt = 1;
|
||||
buf += 4 * 1024;
|
||||
count -= 16;
|
||||
}
|
||||
|
||||
cmdtbl->prdt_entry[i].data_base = buf;
|
||||
cmdtbl->prdt_entry[i].byte_count = (count << 9) - 1;
|
||||
cmdtbl->prdt_entry[i].interrupt = 1;
|
||||
|
||||
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
|
||||
cmdfis->fis_type = FIS_REG_H2D;
|
||||
cmdfis->cmd_mode = 1;
|
||||
cmdfis->command = ATA_CMD_READ_DMA_EX;
|
||||
|
||||
cmdfis->lba0 = (uint8_t)lba;
|
||||
cmdfis->lba1 = (uint8_t)(lba >> 8);
|
||||
cmdfis->lba2 = (uint8_t)(lba >> 16);
|
||||
cmdfis->device = 1 << 6; /* LBA mode */
|
||||
cmdfis->lba3 = (uint8_t)(lba >> 24);
|
||||
cmdfis->lba4 = (uint8_t)(lba >> 32);
|
||||
cmdfis->lba5 = (uint8_t)(lba >> 40);
|
||||
|
||||
cmdfis->count_low = count & 0xff;
|
||||
cmdfis->count_high = (count >> 8) & 0xff;
|
||||
|
||||
/* wait until port is free */
|
||||
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) {
|
||||
spin++;
|
||||
}
|
||||
|
||||
if (spin == 1000000) {
|
||||
printf(LOG_ERROR "AHCI: Port is hung\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
port->ci = 1 << slot;
|
||||
|
||||
while (1) {
|
||||
if ((port->ci & (1 << slot)) == 0)
|
||||
break;
|
||||
if (port->is & HBA_PORT_IS_TFES) {
|
||||
printf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(port->is & HBA_PORT_IS_TFES) {
|
||||
printf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ahci_init()
|
||||
{
|
||||
struct pci_device *dev = pci_find_device(0x01, 0x06);
|
||||
|
|
@ -78,6 +212,7 @@ void ahci_init()
|
|||
}
|
||||
|
||||
ahci_probe_ports(dev, abar);
|
||||
ahci_pbase = (paddr_t)buddy_alloc(0x100000); /* 1 MiB */
|
||||
|
||||
/* setup device irq number */
|
||||
uint16_t irq = irq_highest_free();
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@
|
|||
#define FIS_PIO_SETUP 0x5F
|
||||
#define FIS_DEV_BITS 0xA1
|
||||
|
||||
#define ATA_DEV_BUSY 0x80
|
||||
#define ATA_DEV_DRQ 0x08
|
||||
|
||||
#define ATA_CMD_READ_DMA_EX 0x25
|
||||
#define ATA_CMD_IDENT 0xEC
|
||||
|
||||
#define SATA_SIG_ATA 0x00000101
|
||||
|
|
@ -28,8 +32,14 @@
|
|||
#define AHCI_DEV_PM 3
|
||||
#define AHCI_DEV_SATAPI 4
|
||||
|
||||
#define HBA_PORT_IPM_ACTIVE 1
|
||||
#define HBA_PORT_DET_PRESENT 3
|
||||
#define HBA_PORT_IPM_ACTIVE 0x1
|
||||
#define HBA_PORT_DET_PRESENT 0x3
|
||||
|
||||
#define HBA_PORT_CMD_ST 0x1
|
||||
#define HBA_PORT_CMD_FRE 0x10
|
||||
#define HBA_PORT_CMD_FR 0x4000
|
||||
#define HBA_PORT_CMD_CR 0x8000
|
||||
#define HBA_PORT_IS_TFES 0x40000000
|
||||
|
||||
struct fis_reg_h2d {
|
||||
/* 0x00 */
|
||||
|
|
@ -236,11 +246,9 @@ struct hba_cmd_tbl {
|
|||
|
||||
typedef volatile struct {
|
||||
/* 0x00 */
|
||||
paddr32_t clb_low;
|
||||
paddr32_t clb_high;
|
||||
paddr_t clb;
|
||||
|
||||
paddr32_t fb_low;
|
||||
paddr32_t fb_high;
|
||||
paddr_t fb;
|
||||
|
||||
/* 0x10 */
|
||||
uint32_t is;
|
||||
|
|
|
|||
12
mem/buddy.c
12
mem/buddy.c
|
|
@ -8,6 +8,11 @@
|
|||
struct limine_memmap_request map_req = { .id = LIMINE_MEMMAP_REQUEST, .revision = 0 };
|
||||
struct limine_kernel_address_request kern_req = { .id = LIMINE_KERNEL_ADDRESS_REQUEST, .revision = 0 };
|
||||
|
||||
#ifdef KDEBUG
|
||||
static const char *limine_types[] = { "USABLE", "RESERVED", "ACPI_RECLAIMABLE", "ACPI_NVS", "BAD_MEMORY", "BOOTLOADER_RECLAIMABLE",
|
||||
"KERN", "FRAMEBUFFER" };
|
||||
#endif
|
||||
|
||||
static char *kmem;
|
||||
static size_t kmem_len;
|
||||
|
||||
|
|
@ -297,6 +302,13 @@ static size_t limine_parse_memregions()
|
|||
for (unsigned int i = 0; i < res->entry_count; i++) {
|
||||
struct limine_memmap_entry *entry = res->entries[i];
|
||||
|
||||
#ifdef KDEBUG
|
||||
const char *type_str = "base=%X, len=%x %s\n";
|
||||
printf(LOG_DEBUG "Memmap entry: ");
|
||||
printf(type_str, entry->base, entry->length, limine_types[entry->type]);
|
||||
|
||||
#endif
|
||||
|
||||
switch (entry->type) {
|
||||
case LIMINE_MEMMAP_KERNEL_AND_MODULES:
|
||||
ret = entry->length;
|
||||
|
|
|
|||
Loading…
Reference in a new issue