From 970b34de9aa7e840f2b515bc2820d5ae83c65240 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 6 May 2023 20:48:23 -0400 Subject: [PATCH] ahci: enumerate ahci bus --- dev/ahci.c | 43 +++++++++++++++++++++++++++++++++++++++++++ dev/pci.c | 4 ---- include/dev/ahci.h | 26 ++++++++++++++++++-------- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/dev/ahci.c b/dev/ahci.c index 680c691..5fdd606 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -1,4 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include "stdio.h" #include #include @@ -11,6 +12,46 @@ static void ahci_irq_handler() { } +static int ahci_port_type(hbaport_t *port) +{ + uint32_t ssts = port->ssts; + uint8_t det = ssts & 0x0F; + + if (det != HBA_PORT_DET_PRESENT) + return AHCI_DEV_NULL; + + switch (port->sig) { + case SATA_SIG_ATAPI: + return AHCI_DEV_SATAPI; + case SATA_SIG_SEMB: + return AHCI_DEV_SEMB; + case SATA_SIG_PM: + return AHCI_DEV_PM; + default: + return AHCI_DEV_SATA; + } +} + +static void ahci_probe_ports(hbamem_t *abar) +{ + uint32_t pi = abar->pi; + for (int i = 0; i < 32; i++) { + if (pi & 1) { + uint32_t type = ahci_port_type(&abar->ports[i]); + if (type == AHCI_DEV_SATA) { + printf(LOG_INFO "SATA drive found at port %d\n", i); + } else if (type == AHCI_DEV_SATAPI) { + printf(LOG_INFO "SATAPI drive found at port %d\n", i); + } else if (type == AHCI_DEV_SEMB) { + printf(LOG_INFO "SEMB drive found at port %d\n", i); + } else if (type == AHCI_DEV_PM) { + printf(LOG_INFO "PM drive found at port %d\n", i); + } + } + pi >>= 1; + } +} + void ahci_init() { struct pci_device *dev = pci_find_device(0x01, 0x06); @@ -26,6 +67,8 @@ void ahci_init() return; } + ahci_probe_ports(abar); + /* setup device irq number */ uint16_t irq = irq_highest_free(); uint32_t tmp = pci_config_read_long(dev->bus, dev->slot, 0, 0x3C); diff --git a/dev/pci.c b/dev/pci.c index c330237..91b39e9 100644 --- a/dev/pci.c +++ b/dev/pci.c @@ -56,7 +56,6 @@ static void pci_scan_bus(uint8_t bus) pci_devices[pci_device_count].subclass = subclass; pci_devices[pci_device_count].bar0 = pci_config_read_long(bus, slot, 0, 0x10); - pci_devices[pci_device_count].bar1 = pci_config_read_long(bus, slot, 0, 0x14); uint8_t header_type = (pci_config_read_long(bus, slot, 0, 0xC) >> 24) & 0xFF; @@ -64,11 +63,8 @@ static void pci_scan_bus(uint8_t bus) if (header_type == 0) { pci_devices[pci_device_count].bar2 = pci_config_read_long(bus, slot, 0, 0x18); - pci_devices[pci_device_count].bar3 = pci_config_read_long(bus, slot, 0, 0x1C); - pci_devices[pci_device_count].bar4 = pci_config_read_long(bus, slot, 0, 0x20); - pci_devices[pci_device_count].bar5 = pci_config_read_long(bus, slot, 0, 0x24); } diff --git a/include/dev/ahci.h b/include/dev/ahci.h index d1824ba..ecffea1 100644 --- a/include/dev/ahci.h +++ b/include/dev/ahci.h @@ -17,6 +17,20 @@ #define ATA_CMD_IDENT 0xEC +#define SATA_SIG_ATA 0x00000101 +#define SATA_SIG_ATAPI 0xEB140101 +#define SATA_SIG_SEMB 0xC33C0101 +#define SATA_SIG_PM 0x96690101 + +#define AHCI_DEV_NULL 0 +#define AHCI_DEV_SATA 1 +#define AHCI_DEV_SEMB 2 +#define AHCI_DEV_PM 3 +#define AHCI_DEV_SATAPI 4 + +#define HBA_PORT_IPM_ACTIVE 1 +#define HBA_PORT_DET_PRESENT 3 + struct fis_reg_h2d { /* 0x00 */ uint8_t fis_type; @@ -195,15 +209,13 @@ struct hba_cmd_header { volatile uint32_t prdb_count; - uint32_t cmd_table_base_low; - uint32_t cmd_table_base_high; + paddr_t cmd_table_base; uint32_t reserved2[4]; } PACKED; struct hba_prdt_entry { - uint32_t data_base_low; - uint32_t data_base_high; + paddr_t data_base; uint32_t reserved; @@ -224,10 +236,8 @@ struct hba_cmd_tbl { typedef volatile struct { /* 0x00 */ - uint32_t clb; - uint32_t clbu; - uint32_t fb; - uint32_t fbu; + paddr_t clb; + paddr_t fb; /* 0x10 */ uint32_t is;