2023-06-08 02:36:59 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-26 17:35:51 -07:00
|
|
|
#include <glacier/memory/unique_ptr.h>
|
2023-06-21 18:38:11 -07:00
|
|
|
#include <glacier/status/error_or.h>
|
2023-11-22 14:10:10 -08:00
|
|
|
#include <mammoth/proc/thread.h>
|
2023-06-17 01:45:53 -07:00
|
|
|
#include <ztypes.h>
|
2023-06-08 02:36:59 -07:00
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
#include "ahci/ahci.h"
|
|
|
|
#include "ahci/ahci_device.h"
|
2023-06-08 02:36:59 -07:00
|
|
|
|
|
|
|
class AhciDriver {
|
|
|
|
public:
|
2023-07-05 14:51:24 -07:00
|
|
|
static glcr::ErrorOr<glcr::UniquePtr<AhciDriver>> Init(
|
2023-11-22 14:59:41 -08:00
|
|
|
mmth::OwnedMemoryRegion&& ahci_phys);
|
2023-06-26 17:35:51 -07:00
|
|
|
glcr::ErrorCode RegisterIrq();
|
2023-06-08 02:36:59 -07:00
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
void InterruptLoop();
|
|
|
|
|
2023-06-21 18:38:11 -07:00
|
|
|
glcr::ErrorOr<AhciDevice*> GetDevice(uint64_t id);
|
2023-06-15 16:20:29 -07:00
|
|
|
|
2023-06-08 02:36:59 -07:00
|
|
|
void DumpCapabilities();
|
|
|
|
void DumpPorts();
|
|
|
|
|
|
|
|
private:
|
2023-11-22 14:59:41 -08:00
|
|
|
mmth::OwnedMemoryRegion pci_region_;
|
2023-06-08 02:36:59 -07:00
|
|
|
PciDeviceHeader* pci_device_header_ = nullptr;
|
2023-11-22 14:59:41 -08:00
|
|
|
mmth::OwnedMemoryRegion ahci_region_;
|
2023-06-08 02:36:59 -07:00
|
|
|
AhciHba* ahci_hba_ = nullptr;
|
|
|
|
|
2023-06-12 23:32:24 -07:00
|
|
|
// TODO: Allocate these dynamically.
|
2023-06-16 01:31:23 -07:00
|
|
|
AhciDevice* devices_[32];
|
2023-06-12 19:20:51 -07:00
|
|
|
|
|
|
|
Thread irq_thread_;
|
|
|
|
uint64_t irq_port_cap_ = 0;
|
|
|
|
|
2023-06-12 23:32:24 -07:00
|
|
|
uint64_t num_ports_;
|
|
|
|
uint64_t num_commands_;
|
|
|
|
|
2023-06-21 18:38:11 -07:00
|
|
|
glcr::ErrorCode LoadCapabilities();
|
|
|
|
glcr::ErrorCode LoadHbaRegisters();
|
|
|
|
glcr::ErrorCode LoadDevices();
|
2023-06-26 17:35:51 -07:00
|
|
|
|
2023-11-22 14:59:41 -08:00
|
|
|
AhciDriver(mmth::OwnedMemoryRegion&& pci_region)
|
2023-11-19 17:54:28 -08:00
|
|
|
: pci_region_(glcr::Move(pci_region)),
|
2023-07-05 14:51:24 -07:00
|
|
|
pci_device_header_(
|
|
|
|
reinterpret_cast<PciDeviceHeader*>(pci_region_.vaddr())) {}
|
2023-06-08 02:36:59 -07:00
|
|
|
};
|