Further parse AHCI information.
Send an IDENTIFY command to each drive and set up a hook to handle interrupts.
This commit is contained in:
parent
4e1888bd24
commit
0f0e39d1e9
25 changed files with 721 additions and 90 deletions
157
sys/denali/ahci/ahci.h
Normal file
157
sys/denali/ahci/ahci.h
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct PciDeviceHeader {
|
||||
uint16_t vendor_id;
|
||||
uint16_t device_id;
|
||||
uint16_t command_reg;
|
||||
uint16_t status_reg;
|
||||
uint8_t revision;
|
||||
uint8_t prog_interface;
|
||||
uint8_t subclass;
|
||||
uint8_t class_code;
|
||||
uint8_t cache_line_size;
|
||||
uint8_t latency_timer;
|
||||
uint8_t header_type;
|
||||
uint8_t bist;
|
||||
uint32_t bars[5];
|
||||
uint32_t abar;
|
||||
uint32_t reserved0;
|
||||
uint32_t subsystem_id;
|
||||
uint32_t expansion_rom;
|
||||
uint8_t cap_ptr;
|
||||
uint8_t reserved1[7];
|
||||
uint8_t interrupt_line;
|
||||
uint8_t interrupt_pin;
|
||||
uint8_t min_grant;
|
||||
uint8_t max_latency;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PciMsiCap {
|
||||
uint8_t cap_id;
|
||||
uint8_t next_offset;
|
||||
uint8_t message_control;
|
||||
uint8_t reserved;
|
||||
uint64_t message_address;
|
||||
uint16_t message_data;
|
||||
};
|
||||
|
||||
struct AhciHba {
|
||||
uint32_t capabilities;
|
||||
uint32_t global_host_control;
|
||||
uint32_t interrupt_status;
|
||||
uint32_t port_implemented;
|
||||
uint32_t version;
|
||||
uint32_t ccc_ctl; // 0x14, Command completion coalescing control
|
||||
uint32_t ccc_pts; // 0x18, Command completion coalescing ports
|
||||
uint32_t em_loc; // 0x1C, Enclosure management location
|
||||
uint32_t em_ctl; // 0x20, Enclosure management control
|
||||
uint32_t capabilities_ext;
|
||||
uint32_t bohc; // 0x28, BIOS/OS handoff control and status
|
||||
} __attribute__((packed));
|
||||
|
||||
struct AhciPort {
|
||||
uint64_t command_list_base;
|
||||
uint64_t fis_base;
|
||||
uint32_t interrupt_status;
|
||||
uint32_t interrupt_enable;
|
||||
uint32_t command;
|
||||
uint32_t reserved;
|
||||
uint32_t task_file_data;
|
||||
uint32_t signature;
|
||||
uint32_t sata_status;
|
||||
uint32_t sata_control;
|
||||
uint32_t sata_error;
|
||||
uint32_t sata_active;
|
||||
uint32_t command_issue;
|
||||
uint32_t sata_notification;
|
||||
uint32_t fis_based_switching_ctl;
|
||||
uint32_t device_sleep;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct CommandHeader {
|
||||
uint16_t command;
|
||||
uint16_t prd_table_length;
|
||||
uint32_t prd_byte_count;
|
||||
uint64_t command_table_base_addr;
|
||||
uint64_t reserved1;
|
||||
uint64_t reserved2;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct CommandList {
|
||||
CommandHeader command_headers[32];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PhysicalRegionDescriptor {
|
||||
uint64_t region_address;
|
||||
uint32_t reserved;
|
||||
// bit 0 must be one.
|
||||
// 21:0 is byte count
|
||||
// 31 is Interrupt on Completion
|
||||
uint32_t byte_count;
|
||||
};
|
||||
|
||||
struct CommandTable {
|
||||
uint8_t command_fis[64];
|
||||
uint8_t atapi_command[16];
|
||||
uint8_t reserved[48];
|
||||
PhysicalRegionDescriptor prds[65535];
|
||||
} __attribute__((packed));
|
||||
|
||||
typedef enum {
|
||||
FIS_TYPE_REG_H2D = 0x27, // Register FIS - host to device
|
||||
FIS_TYPE_REG_D2H = 0x34, // Register FIS - device to host
|
||||
FIS_TYPE_DMA_ACT = 0x39, // DMA activate FIS - device to host
|
||||
FIS_TYPE_DMA_SETUP = 0x41, // DMA setup FIS - bidirectional
|
||||
FIS_TYPE_DATA = 0x46, // Data FIS - bidirectional
|
||||
FIS_TYPE_BIST = 0x58, // BIST activate FIS - bidirectional
|
||||
FIS_TYPE_PIO_SETUP = 0x5F, // PIO setup FIS - device to host
|
||||
FIS_TYPE_DEV_BITS = 0xA1, // Set device bits FIS - device to host
|
||||
} FIS_TYPE;
|
||||
|
||||
struct DmaFis {};
|
||||
|
||||
struct PioSetupFis {};
|
||||
struct HostToDeviceRegisterFis {
|
||||
uint8_t fis_type; // FIS_TYPE_REG_H2D
|
||||
uint8_t pmp_and_c;
|
||||
uint8_t command; // Command register
|
||||
uint8_t featurel; // Feature register, 7:0
|
||||
|
||||
// DWORD 1
|
||||
uint8_t lba0; // LBA low register, 7:0
|
||||
uint8_t lba1; // LBA mid register, 15:8
|
||||
uint8_t lba2; // LBA high register, 23:16
|
||||
uint8_t device; // Device register
|
||||
|
||||
// DWORD 2
|
||||
uint8_t lba3; // LBA register, 31:24
|
||||
uint8_t lba4; // LBA register, 39:32
|
||||
uint8_t lba5; // LBA register, 47:40
|
||||
uint8_t featureh; // Feature register, 15:8
|
||||
|
||||
// DWORD 3
|
||||
uint16_t count;
|
||||
uint8_t icc; // Isochronous command completion
|
||||
uint8_t control; // Control register
|
||||
|
||||
// DWORD 4
|
||||
uint32_t reserved; // Reserved
|
||||
};
|
||||
struct DeviceToHostRegisterFis {};
|
||||
struct SetDeviceBitsFis {};
|
||||
|
||||
struct ReceivedFis {
|
||||
DmaFis dma_fis;
|
||||
uint32_t reserved0;
|
||||
|
||||
PioSetupFis pio_set_fis;
|
||||
uint32_t reserved1[3];
|
||||
|
||||
DeviceToHostRegisterFis device_to_host_register_fis;
|
||||
uint32_t reserved2;
|
||||
|
||||
SetDeviceBitsFis set_device_bits_fis;
|
||||
uint8_t unknown_fis[64];
|
||||
} __attribute__((packed));
|
||||
108
sys/denali/ahci/ahci_device.cpp
Normal file
108
sys/denali/ahci/ahci_device.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
#include "ahci/ahci_device.h"
|
||||
|
||||
#include <mammoth/debug.h>
|
||||
#include <string.h>
|
||||
#include <zcall.h>
|
||||
|
||||
AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
|
||||
if ((port_struct_->sata_status & 0x103) != 0x103) {
|
||||
return;
|
||||
}
|
||||
uint64_t cl_page = port_struct_->command_list_base & (~0xFFF);
|
||||
uint64_t fis_page = port_struct_->fis_base & (~0xFFF);
|
||||
|
||||
if (cl_page != fis_page) {
|
||||
crash("Non adjacent cl & fis", Z_ERR_UNIMPLEMENTED);
|
||||
}
|
||||
|
||||
check(ZMemoryObjectCreatePhysical(cl_page, 0x1000, &vmmo_cap_));
|
||||
|
||||
uint64_t vaddr;
|
||||
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap_, &vaddr));
|
||||
|
||||
uint64_t cl_off = port_struct_->command_list_base & 0xFFF;
|
||||
command_list_ = reinterpret_cast<CommandList*>(vaddr + cl_off);
|
||||
|
||||
uint64_t fis_off = port_struct_->fis_base & 0xFFF;
|
||||
received_fis_ = reinterpret_cast<ReceivedFis*>(vaddr + fis_off);
|
||||
|
||||
// FIXME: Hacky
|
||||
uint64_t ct_off =
|
||||
command_list_->command_headers[0].command_table_base_addr & 0xFFF;
|
||||
command_table_ = reinterpret_cast<CommandTable*>(vaddr + ct_off);
|
||||
|
||||
port_struct_->interrupt_enable = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
z_err_t AhciDevice::SendIdentify(uint16_t** result) {
|
||||
HostToDeviceRegisterFis fis{
|
||||
.fis_type = FIS_TYPE_REG_H2D,
|
||||
.pmp_and_c = 0x80,
|
||||
.command = 0xEC,
|
||||
.featurel = 0,
|
||||
|
||||
.lba0 = 0,
|
||||
.lba1 = 0,
|
||||
.lba2 = 0,
|
||||
.device = 0,
|
||||
|
||||
.lba3 = 0,
|
||||
.lba4 = 0,
|
||||
.lba5 = 0,
|
||||
.featureh = 0,
|
||||
|
||||
.count = 0,
|
||||
.icc = 0,
|
||||
.control = 0,
|
||||
|
||||
.reserved = 0,
|
||||
};
|
||||
|
||||
command_list_->command_headers[0].command = (sizeof(fis) / 2) & 0x1F;
|
||||
command_list_->command_headers[0].prd_table_length = 1;
|
||||
|
||||
memcpy(command_table_->command_fis, &fis, sizeof(fis));
|
||||
|
||||
port_struct_->command_issue |= 1;
|
||||
|
||||
uint64_t vmmo_cap, paddr;
|
||||
RET_ERR(ZMemoryObjectCreateContiguous(512, &vmmo_cap, &paddr));
|
||||
|
||||
command_table_->prds[0].region_address = paddr;
|
||||
command_table_->prds[0].byte_count = 512;
|
||||
|
||||
uint64_t vaddr;
|
||||
RET_ERR(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &vaddr));
|
||||
|
||||
*result = reinterpret_cast<uint16_t*>(vaddr);
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
void AhciDevice::DumpInfo() {
|
||||
dbgln("Comlist: %lx", port_struct_->command_list_base);
|
||||
dbgln("FIS: %lx", port_struct_->fis_base);
|
||||
dbgln("Command: %x", port_struct_->command);
|
||||
dbgln("Signature: %x", port_struct_->signature);
|
||||
dbgln("SATA status: %x", port_struct_->sata_status);
|
||||
dbgln("Int status: %x", port_struct_->interrupt_status);
|
||||
dbgln("Int enable: %x", port_struct_->interrupt_enable);
|
||||
dbgln("Int enable: %x", port_struct_->interrupt_enable);
|
||||
|
||||
// Just dump one command info for now.
|
||||
for (uint64_t i = 0; i < 1; i++) {
|
||||
dbgln("Command Header: %u", i);
|
||||
dbgln("Command %x", command_list_->command_headers[i].command);
|
||||
dbgln("PRD Len: %x", command_list_->command_headers[i].prd_table_length);
|
||||
dbgln("Command Table %lx",
|
||||
command_list_->command_headers[i].command_table_base_addr);
|
||||
}
|
||||
}
|
||||
|
||||
void AhciDevice::HandleIrq() {
|
||||
uint64_t int_status = port_struct_->interrupt_status;
|
||||
// FIXME: Probably only clear the interrupts we know how to handle.
|
||||
port_struct_->interrupt_status = int_status;
|
||||
|
||||
dbgln("int receieved: %x", int_status);
|
||||
}
|
||||
29
sys/denali/ahci/ahci_device.h
Normal file
29
sys/denali/ahci/ahci_device.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <zerrors.h>
|
||||
|
||||
#include "ahci/ahci.h"
|
||||
|
||||
class AhciDevice {
|
||||
public:
|
||||
AhciDevice() {}
|
||||
// Caller retains ownership of the pointer.
|
||||
AhciDevice(AhciPort* port_struct);
|
||||
|
||||
void DumpInfo();
|
||||
|
||||
bool IsInit() { return port_struct_ != nullptr && vmmo_cap_ != 0; }
|
||||
|
||||
// Result will point to a 512 byte (256 word array).
|
||||
z_err_t SendIdentify(uint16_t** result);
|
||||
|
||||
void HandleIrq();
|
||||
|
||||
private:
|
||||
AhciPort* port_struct_ = nullptr;
|
||||
uint64_t vmmo_cap_ = 0;
|
||||
|
||||
CommandList* command_list_ = nullptr;
|
||||
ReceivedFis* received_fis_ = nullptr;
|
||||
CommandTable* command_table_ = nullptr;
|
||||
};
|
||||
|
|
@ -9,13 +9,30 @@ namespace {
|
|||
const uint64_t kSataPciPhys = 0xB00FA000;
|
||||
const uint64_t kPciSize = 0x1000;
|
||||
|
||||
const uint64_t kGhc_InteruptEnable = 0x2;
|
||||
|
||||
void interrupt_thread(void* void_driver) {
|
||||
AhciDriver* driver = static_cast<AhciDriver*>(void_driver);
|
||||
dbgln("this %lx", driver);
|
||||
|
||||
driver->InterruptLoop();
|
||||
|
||||
crash("Driver returned from interrupt loop", Z_ERR_UNIMPLEMENTED);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
z_err_t AhciDriver::Init() {
|
||||
RET_ERR(LoadPciDeviceHeader());
|
||||
RET_ERR(LoadCapabilities());
|
||||
dbgln("ABAR: %x", pci_device_header_->abar);
|
||||
dbgln("Interrupt line: %x", pci_device_header_->interrupt_line);
|
||||
dbgln("Interrupt pin: %x", pci_device_header_->interrupt_pin);
|
||||
RET_ERR(RegisterIrq());
|
||||
RET_ERR(LoadHbaRegisters());
|
||||
dbgln("Version: %x", ahci_hba_->version);
|
||||
ahci_hba_->global_host_control |= kGhc_InteruptEnable;
|
||||
RET_ERR(LoadDevices());
|
||||
DumpCapabilities();
|
||||
DumpPorts();
|
||||
return Z_OK;
|
||||
|
|
@ -26,7 +43,7 @@ void AhciDriver::DumpCapabilities() {
|
|||
uint32_t caps = ahci_hba_->capabilities;
|
||||
|
||||
dbgln("Num Ports: %u", (caps & 0x1F) + 1);
|
||||
dbgln("Num Command Slots: %u", (caps & 0x1F00) >> 8);
|
||||
dbgln("Num Command Slots: %u", ((caps & 0x1F00) >> 8) + 1);
|
||||
if (caps & 0x20) {
|
||||
dbgln("External SATA");
|
||||
}
|
||||
|
|
@ -97,33 +114,33 @@ void AhciDriver::DumpCapabilities() {
|
|||
if (caps & 0x10) {
|
||||
dbgln("Aggressive device sleep management");
|
||||
}
|
||||
|
||||
dbgln("Control %x", ahci_hba_->global_host_control);
|
||||
}
|
||||
|
||||
void AhciDriver::DumpPorts() {
|
||||
dbgln("Ports implemented %x", ahci_hba_->port_implemented);
|
||||
|
||||
uint64_t port_index = 0;
|
||||
uint32_t ports_implemented = ahci_hba_->port_implemented;
|
||||
while (ports_implemented) {
|
||||
if (!(ports_implemented & 0x1)) {
|
||||
ports_implemented >>= 1;
|
||||
port_index++;
|
||||
for (uint64_t i = 0; i < 6; i++) {
|
||||
AhciDevice& dev = devices_[i];
|
||||
if (!dev.IsInit()) {
|
||||
continue;
|
||||
}
|
||||
uint64_t port_addr =
|
||||
reinterpret_cast<uint64_t>(ahci_hba_) + 0x100 + (0x80 * port_index);
|
||||
AhciPort* port = reinterpret_cast<AhciPort*>(port_addr);
|
||||
|
||||
dbgln("");
|
||||
dbgln("Port %u:", port_index);
|
||||
dbgln("Comlist: %lx", port->command_list_base);
|
||||
dbgln("FIS: %lx", port->fis_base);
|
||||
dbgln("Command: %x", port->command);
|
||||
dbgln("Signature: %x", port->signature);
|
||||
dbgln("SATA status: %x", port->sata_status);
|
||||
dbgln("Port %u:", i);
|
||||
dev.DumpInfo();
|
||||
}
|
||||
}
|
||||
|
||||
ports_implemented >>= 1;
|
||||
port_index++;
|
||||
void AhciDriver::InterruptLoop() {
|
||||
dbgln("this %lx", this);
|
||||
while (true) {
|
||||
uint64_t type, bytes, caps;
|
||||
check(ZPortRecv(irq_port_cap_, 0, 0, 0, 0, &type, &bytes, &caps));
|
||||
for (uint64_t i = 0; i < 6; i++) {
|
||||
if (devices_[i].IsInit()) {
|
||||
devices_[i].HandleIrq();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -137,6 +154,46 @@ z_err_t AhciDriver::LoadPciDeviceHeader() {
|
|||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t AhciDriver::LoadCapabilities() {
|
||||
if (!(pci_device_header_->status_reg & 0x10)) {
|
||||
dbgln("No caps!");
|
||||
return Z_ERR_INVALID;
|
||||
}
|
||||
uint8_t* base = reinterpret_cast<uint8_t*>(pci_device_header_);
|
||||
uint16_t offset = pci_device_header_->cap_ptr;
|
||||
do {
|
||||
uint16_t* cap = reinterpret_cast<uint16_t*>(base + offset);
|
||||
switch (*cap & 0xFF) {
|
||||
case 0x01:
|
||||
dbgln("Power Management");
|
||||
break;
|
||||
case 0x05:
|
||||
dbgln("MSI");
|
||||
break;
|
||||
case 0x12:
|
||||
dbgln("SATA");
|
||||
break;
|
||||
default:
|
||||
dbgln("Unrecognized cap");
|
||||
break;
|
||||
}
|
||||
|
||||
offset = (*cap & 0xFF00) >> 8;
|
||||
} while (offset);
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t AhciDriver::RegisterIrq() {
|
||||
if (pci_device_header_->interrupt_pin == 0) {
|
||||
crash("Can't register IRQ without a pin num", Z_INVALID);
|
||||
}
|
||||
uint64_t irq_num = Z_IRQ_PCI_BASE + pci_device_header_->interrupt_pin - 1;
|
||||
RET_ERR(ZIrqRegister(irq_num, &irq_port_cap_));
|
||||
dbgln("this %lx", this);
|
||||
irq_thread_ = Thread(interrupt_thread, this);
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t AhciDriver::LoadHbaRegisters() {
|
||||
uint64_t vmmo_cap;
|
||||
RET_ERR(
|
||||
|
|
@ -147,3 +204,22 @@ z_err_t AhciDriver::LoadHbaRegisters() {
|
|||
ahci_hba_ = reinterpret_cast<AhciHba*>(vaddr);
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t AhciDriver::LoadDevices() {
|
||||
// FIXME: Don't set this up so we hardcode 6 devices.
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
if (!(ahci_hba_->port_implemented & (1 << i))) {
|
||||
continue;
|
||||
}
|
||||
uint64_t port_addr =
|
||||
reinterpret_cast<uint64_t>(ahci_hba_) + 0x100 + (0x80 * i);
|
||||
devices_[i] = AhciDevice(reinterpret_cast<AhciPort*>(port_addr));
|
||||
if (!devices_[i].IsInit()) {
|
||||
continue;
|
||||
}
|
||||
dbgln("Identify %u", i);
|
||||
uint16_t* identify;
|
||||
devices_[i].SendIdentify(&identify);
|
||||
}
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <mammoth/thread.h>
|
||||
#include <zerrors.h>
|
||||
|
||||
struct PciDeviceHeader {
|
||||
uint16_t vendor_id;
|
||||
uint16_t device_id;
|
||||
uint16_t command_reg;
|
||||
uint16_t status_reg;
|
||||
uint8_t revision;
|
||||
uint8_t prog_interface;
|
||||
uint8_t subclass;
|
||||
uint8_t class_code;
|
||||
uint8_t cache_line_size;
|
||||
uint8_t latency_timer;
|
||||
uint8_t header_type;
|
||||
uint8_t bist;
|
||||
uint32_t bars[5];
|
||||
uint32_t abar;
|
||||
uint32_t subsystem_id;
|
||||
uint32_t expansion_rom;
|
||||
uint8_t cap_ptr;
|
||||
uint8_t reserved[7];
|
||||
uint8_t interrupt_line;
|
||||
uint8_t interrupt_pin;
|
||||
uint8_t min_grant;
|
||||
uint8_t max_latency;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct AhciHba {
|
||||
uint32_t capabilities;
|
||||
uint32_t global_host_control;
|
||||
uint32_t interrupt_status;
|
||||
uint32_t port_implemented;
|
||||
uint32_t version;
|
||||
uint32_t ccc_ctl; // 0x14, Command completion coalescing control
|
||||
uint32_t ccc_pts; // 0x18, Command completion coalescing ports
|
||||
uint32_t em_loc; // 0x1C, Enclosure management location
|
||||
uint32_t em_ctl; // 0x20, Enclosure management control
|
||||
uint32_t capabilities_ext;
|
||||
uint32_t bohc; // 0x28, BIOS/OS handoff control and status
|
||||
};
|
||||
|
||||
struct AhciPort {
|
||||
uint64_t command_list_base;
|
||||
uint64_t fis_base;
|
||||
uint32_t interrupt_status;
|
||||
uint32_t interrupt_enable;
|
||||
uint32_t command;
|
||||
uint32_t reserved;
|
||||
uint32_t task_file_data;
|
||||
uint32_t signature;
|
||||
uint32_t sata_status;
|
||||
uint32_t sata_control;
|
||||
uint32_t sata_error;
|
||||
uint32_t sata_active;
|
||||
uint32_t command_issue;
|
||||
uint32_t sata_notification;
|
||||
uint32_t fis_based_switching_ctl;
|
||||
uint32_t device_sleep;
|
||||
};
|
||||
#include "ahci/ahci.h"
|
||||
#include "ahci/ahci_device.h"
|
||||
|
||||
class AhciDriver {
|
||||
public:
|
||||
z_err_t Init();
|
||||
|
||||
void InterruptLoop();
|
||||
|
||||
void DumpCapabilities();
|
||||
void DumpPorts();
|
||||
|
||||
|
|
@ -71,6 +19,14 @@ class AhciDriver {
|
|||
PciDeviceHeader* pci_device_header_ = nullptr;
|
||||
AhciHba* ahci_hba_ = nullptr;
|
||||
|
||||
AhciDevice devices_[6];
|
||||
|
||||
Thread irq_thread_;
|
||||
uint64_t irq_port_cap_ = 0;
|
||||
|
||||
z_err_t LoadPciDeviceHeader();
|
||||
z_err_t LoadCapabilities();
|
||||
z_err_t RegisterIrq();
|
||||
z_err_t LoadHbaRegisters();
|
||||
z_err_t LoadDevices();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue