Compare commits

..

2 Commits

6 changed files with 52 additions and 48 deletions

View File

@ -96,7 +96,7 @@ struct CommandTable {
uint8_t command_fis[64]; uint8_t command_fis[64];
uint8_t atapi_command[16]; uint8_t atapi_command[16];
uint8_t reserved[48]; uint8_t reserved[48];
PhysicalRegionDescriptor prdt[65535]; PhysicalRegionDescriptor prdt[8];
} __attribute__((packed)); } __attribute__((packed));
typedef enum { typedef enum {

View File

@ -9,35 +9,38 @@ AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
if ((port_struct_->sata_status & 0x103) != 0x103) { if ((port_struct_->sata_status & 0x103) != 0x103) {
return; 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) { // 0x0-0x400 -> Command List
crash("Non adjacent cl & fis", glcr::UNIMPLEMENTED); // 0x400-0x500 -> Received FIS
} // 0x500-0x2500 -> Command Tables (0x100 each) (Max PRDT Length is 8 for now)
command_structures_ = MappedMemoryRegion::ContiguousPhysical(0x2500);
uint64_t paddr = command_structures_.paddr();
command_structures_ = MappedMemoryRegion::DirectPhysical(cl_page, 0x1000); command_list_ = reinterpret_cast<CommandList*>(command_structures_.vaddr());
port_struct_->command_list_base = paddr;
uint64_t cl_off = port_struct_->command_list_base & 0xFFF;
command_list_ =
reinterpret_cast<CommandList*>(command_structures_.vaddr() + cl_off);
uint64_t fis_off = port_struct_->fis_base & 0xFFF;
received_fis_ = received_fis_ =
reinterpret_cast<ReceivedFis*>(command_structures_.vaddr() + fis_off); reinterpret_cast<ReceivedFis*>(command_structures_.vaddr() + 0x400);
port_struct_->fis_base = paddr + 0x400;
// FIXME: Hacky command_tables_ =
uint64_t ct_off = reinterpret_cast<CommandTable*>(command_structures_.vaddr() + 0x500);
command_list_->command_headers[0].command_table_base_addr & 0xFFF;
command_table_ =
reinterpret_cast<CommandTable*>(command_structures_.vaddr() + ct_off);
for (uint64_t i = 0; i < 32; i++) {
command_list_->command_headers[i].command_table_base_addr =
(paddr + 0x500) + (0x100 * i);
}
port_struct_->interrupt_enable = 0xFFFFFFFF; port_struct_->interrupt_enable = 0xFFFFFFFF;
// Reset the CMD and FRE bits since we move these structures.
// FIXME: I think we need to poll these bits to make sure they become
// 0 before setting them back to one.
port_struct_->command &= ~(0x00000011);
port_struct_->command |= 0x00000011;
} }
glcr::ErrorCode AhciDevice::IssueCommand(Command* command) { glcr::ErrorCode AhciDevice::IssueCommand(Command* command) {
command->PopulateFis(command_table_->command_fis); command->PopulateFis(command_tables_->command_fis);
command->PopulatePrdt(command_table_->prdt); command->PopulatePrdt(command_tables_->prdt);
command_list_->command_headers[0].command = command_list_->command_headers[0].command =
(sizeof(HostToDeviceRegisterFis) / 2) & 0x1F; (sizeof(HostToDeviceRegisterFis) / 2) & 0x1F;
@ -62,7 +65,7 @@ void AhciDevice::DumpInfo() {
dbgln("Int enable: %x", port_struct_->interrupt_enable); dbgln("Int enable: %x", port_struct_->interrupt_enable);
// Just dump one command info for now. // Just dump one command info for now.
for (uint64_t i = 0; i < 1; i++) { for (uint64_t i = 0; i < 32; i++) {
dbgln("Command Header: %u", i); dbgln("Command Header: %u", i);
dbgln("Command %x", command_list_->command_headers[i].command); dbgln("Command %x", command_list_->command_headers[i].command);
dbgln("PRD Len: %x", command_list_->command_headers[i].prd_table_length); dbgln("PRD Len: %x", command_list_->command_headers[i].prd_table_length);
@ -72,7 +75,7 @@ void AhciDevice::DumpInfo() {
} }
void AhciDevice::HandleIrq() { void AhciDevice::HandleIrq() {
uint64_t int_status = port_struct_->interrupt_status; uint32_t int_status = port_struct_->interrupt_status;
// FIXME: Probably only clear the interrupts we know how to handle. // FIXME: Probably only clear the interrupts we know how to handle.
port_struct_->interrupt_status = int_status; port_struct_->interrupt_status = int_status;

View File

@ -30,7 +30,7 @@ class AhciDevice {
CommandList* command_list_ = nullptr; CommandList* command_list_ = nullptr;
ReceivedFis* received_fis_ = nullptr; ReceivedFis* received_fis_ = nullptr;
CommandTable* command_table_ = nullptr; CommandTable* command_tables_ = nullptr;
Command* commands_[32]; Command* commands_[32];
volatile uint32_t commands_issued_ = 0; volatile uint32_t commands_issued_ = 0;

View File

@ -20,18 +20,25 @@ void interrupt_thread(void* void_driver) {
crash("Driver returned from interrupt loop", glcr::INTERNAL); crash("Driver returned from interrupt loop", glcr::INTERNAL);
} }
PciDeviceHeader* LoadPciDeviceHeader(uint64_t ahci_phys) {
MappedMemoryRegion pci_region =
MappedMemoryRegion::DirectPhysical(ahci_phys, kPciSize);
return reinterpret_cast<PciDeviceHeader*>(pci_region.vaddr());
}
} // namespace } // namespace
glcr::ErrorCode AhciDriver::Init() { glcr::ErrorOr<glcr::UniquePtr<AhciDriver>> AhciDriver::Init(
RET_ERR(LoadPciDeviceHeader()); uint64_t ahci_phys) {
// RET_ERR(LoadCapabilities()); PciDeviceHeader* header = LoadPciDeviceHeader(ahci_phys);
RET_ERR(RegisterIrq()); glcr::UniquePtr<AhciDriver> driver(new AhciDriver(header));
RET_ERR(LoadHbaRegisters()); // RET_ERR(driver->LoadCapabilities());
ahci_hba_->global_host_control |= kGhc_InteruptEnable; RET_ERR(driver->LoadHbaRegisters());
RET_ERR(LoadDevices()); RET_ERR(driver->LoadDevices());
// DumpCapabilities(); RET_ERR(driver->RegisterIrq());
// DumpPorts(); // driver->DumpCapabilities();
return glcr::OK; // driver->DumpPorts();
return driver;
} }
glcr::ErrorOr<AhciDevice*> AhciDriver::GetDevice(uint64_t id) { glcr::ErrorOr<AhciDevice*> AhciDriver::GetDevice(uint64_t id) {
@ -153,12 +160,6 @@ void AhciDriver::InterruptLoop() {
} }
} }
glcr::ErrorCode AhciDriver::LoadPciDeviceHeader() {
pci_region_ = MappedMemoryRegion::DirectPhysical(ahci_phys_, kPciSize);
pci_device_header_ = reinterpret_cast<PciDeviceHeader*>(pci_region_.vaddr());
return glcr::OK;
}
glcr::ErrorCode AhciDriver::LoadCapabilities() { glcr::ErrorCode AhciDriver::LoadCapabilities() {
if (!(pci_device_header_->status_reg & 0x10)) { if (!(pci_device_header_->status_reg & 0x10)) {
dbgln("No caps!"); dbgln("No caps!");
@ -195,6 +196,7 @@ glcr::ErrorCode AhciDriver::RegisterIrq() {
uint64_t irq_num = Z_IRQ_PCI_BASE + pci_device_header_->interrupt_pin - 1; uint64_t irq_num = Z_IRQ_PCI_BASE + pci_device_header_->interrupt_pin - 1;
RET_ERR(ZIrqRegister(irq_num, &irq_port_cap_)); RET_ERR(ZIrqRegister(irq_num, &irq_port_cap_));
irq_thread_ = Thread(interrupt_thread, this); irq_thread_ = Thread(interrupt_thread, this);
ahci_hba_->global_host_control |= kGhc_InteruptEnable;
return glcr::OK; return glcr::OK;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h> #include <glacier/status/error_or.h>
#include <mammoth/thread.h> #include <mammoth/thread.h>
#include <ztypes.h> #include <ztypes.h>
@ -9,8 +10,8 @@
class AhciDriver { class AhciDriver {
public: public:
AhciDriver(uint64_t ahci_phys) : ahci_phys_(ahci_phys) {} static glcr::ErrorOr<glcr::UniquePtr<AhciDriver>> Init(uint64_t ahci_phys);
glcr::ErrorCode Init(); glcr::ErrorCode RegisterIrq();
void InterruptLoop(); void InterruptLoop();
@ -20,8 +21,6 @@ class AhciDriver {
void DumpPorts(); void DumpPorts();
private: private:
uint64_t ahci_phys_;
MappedMemoryRegion pci_region_;
PciDeviceHeader* pci_device_header_ = nullptr; PciDeviceHeader* pci_device_header_ = nullptr;
MappedMemoryRegion ahci_region_; MappedMemoryRegion ahci_region_;
AhciHba* ahci_hba_ = nullptr; AhciHba* ahci_hba_ = nullptr;
@ -35,9 +34,10 @@ class AhciDriver {
uint64_t num_ports_; uint64_t num_ports_;
uint64_t num_commands_; uint64_t num_commands_;
glcr::ErrorCode LoadPciDeviceHeader();
glcr::ErrorCode LoadCapabilities(); glcr::ErrorCode LoadCapabilities();
glcr::ErrorCode RegisterIrq();
glcr::ErrorCode LoadHbaRegisters(); glcr::ErrorCode LoadHbaRegisters();
glcr::ErrorCode LoadDevices(); glcr::ErrorCode LoadDevices();
AhciDriver(PciDeviceHeader* device_header)
: pci_device_header_(device_header) {}
}; };

View File

@ -24,8 +24,7 @@ uint64_t main(uint64_t init_port_cap) {
} }
uint64_t ahci_addr = resp_or.value().ahci_phys_offset; uint64_t ahci_addr = resp_or.value().ahci_phys_offset;
AhciDriver driver(ahci_addr); ASSIGN_OR_RETURN(auto driver, AhciDriver::Init(ahci_addr));
RET_ERR(driver.Init());
YellowstoneGetReq req{ YellowstoneGetReq req{
.type = kYellowstoneGetRegistration, .type = kYellowstoneGetRegistration,
@ -46,7 +45,7 @@ uint64_t main(uint64_t init_port_cap) {
endpoint->CreateClient()); endpoint->CreateClient());
notify.WriteMessage("denali", client->GetCap()); notify.WriteMessage("denali", client->GetCap());
DenaliServer server(glcr::Move(endpoint), driver); DenaliServer server(glcr::Move(endpoint), *driver);
RET_ERR(server.RunServer()); RET_ERR(server.RunServer());
// FIXME: Add thread join. // FIXME: Add thread join.
return 0; return 0;