diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-01 10:40:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-01 10:40:19 +0200 |
| commit | 48d0df78c872f7b843f2444a6bcfa41438dc736e (patch) | |
| tree | 34c23cc3508114f3985289f6cb4e915454604426 /dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | |
| parent | f88f6074479c627529559f690bf836960d5a6378 (diff) | |
| parent | 1c3b37123cef3fa9c69099bc6228d7b4a082ee90 (diff) | |
Merge pull request #5 from amlel-el-mahrouss/dev
pr/ahci: important patches.
Diffstat (limited to 'dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc')
| -rw-r--r-- | dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index e5f10db3..2a360e60 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -7,7 +7,7 @@ /** * @file AHCI+Generic.cc * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief AHCI driver. + * @brief AHCI Generic driver. * @version 0.1 * @date 2024-02-02 * @@ -15,6 +15,7 @@ * */ +#include "NewKit/Defines.h" #include <KernelKit/DeviceMgr.h> #include <KernelKit/DriveMgr.h> #include <KernelKit/ProcessScheduler.h> @@ -56,7 +57,7 @@ STATIC PCI::Device kSATADev; STATIC HbaMemRef kSATAHba; STATIC Lba kSATASectorCount = 0UL; STATIC UInt16 kSATAIndex = 0U; -STATIC Char kCurrentDiskModel[50] = {"UNKNOWN AHCI DRIVE"}; +STATIC Char kCurrentDiskModel[50] = {"GENERIC SATA"}; STATIC UInt16 kSATAPortsImplemented = 0U; template <BOOL Write, BOOL CommandOrCTRL, BOOL Identify> @@ -74,11 +75,7 @@ STATIC Void drv_compute_disk_ahci() noexcept const UInt16 kSzIdent = kib_cast(1); /// Push it to the stack - static UInt8 identify_data[kSzIdent] ATTRIBUTE(aligned(4096)) = {0}; - - HAL::mm_map_page((void*)mib_cast(1), (void*)mib_cast(1), HAL::kMMFlagsWr); - - rt_set_memory(identify_data, 0, kSzIdent); + UInt8* identify_data ATTRIBUTE(aligned(4096)) = new UInt8[kSzIdent]; /// Send AHCI command for identification. drv_std_input_output_ahci<NO, YES, YES>(0, identify_data, kAHCISectorSize, kSzIdent); @@ -87,19 +84,13 @@ STATIC Void drv_compute_disk_ahci() noexcept UInt64 lba48_sectors = 0; lba48_sectors |= (UInt64)identify_data[100]; - lba48_sectors |= (UInt64)identify_data[101] << 16; - lba48_sectors |= (UInt64)identify_data[102] << 32; + lba48_sectors |= (UInt64)identify_data[101] << 16; + lba48_sectors |= (UInt64)identify_data[102] << 32; if (lba48_sectors == 0) kSATASectorCount = (identify_data[61] << 16) | identify_data[60]; else kSATASectorCount = lba48_sectors; - - /// Show what we got. - - kout << "Disk Model: " << kCurrentDiskModel << kendl; - kout << "Disk Size: " << number(drv_get_size()) << kendl; - kout << "Disk Sector Count: " << number(kSATASectorCount) << kendl; } /// @brief Finds a command slot for a HBA port. @@ -152,7 +143,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz MUST_PASS(command_header); - constexpr const UInt32 kMaxPRDSize = mib_cast(4); + constexpr const UInt32 kMaxPRDSize = kib_cast(4); command_header->Cfl = sizeof(FisRegH2D) / sizeof(UInt32); command_header->Write = Write; @@ -172,6 +163,9 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz { UInt32 chunk = (bytes_remaining > kMaxPRDSize) ? kMaxPRDSize : bytes_remaining; + if (chunk == 0) + break; + command_table->Prdt[i].Dba = (UInt32)(buffer_phys & 0xFFFFFFFF); command_table->Prdt[i].Dbau = (UInt32)(buffer_phys >> 32); command_table->Prdt[i].Dbc = chunk - 1; @@ -181,7 +175,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz bytes_remaining -= chunk; } - volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)(&command_table->Cfis); + volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)(&command_table->Cfis[0]); rt_set_memory((FisRegH2D*)h2d_fis, 0, sizeof(FisRegH2D)); @@ -275,7 +269,8 @@ STATIC Void ahci_enable_and_probe() // Relocate Command List Base. - constexpr UIntPtr const kAHCIBaseAddress = mib_cast(4); + auto const addr = mm_new_heap(kib_cast(64), YES, NO, 0); + auto kAHCIBaseAddress = (UIntPtr)addr; port->Clb = kAHCIBaseAddress + (kSATAIndex << 10); port->Clbu = 0; @@ -302,6 +297,12 @@ STATIC Void ahci_enable_and_probe() rt_set_memory(reinterpret_cast<VoidPtr>(cmd_hdr[i].Ctba), 0, 256); } + for (UIntPtr offset = 0; offset < kib_cast(64); offset += kib_cast(1)) + { + VoidPtr addr = reinterpret_cast<VoidPtr>(kAHCIBaseAddress + offset); + HAL::mm_map_page(addr, addr, HAL::kMMFlagsWr); + } + // Now we are ready. kSATAHba->Ports[kSATAIndex].Cmd |= kHBAPxCmdFre; @@ -337,8 +338,8 @@ STATIC Bool drv_std_init_ahci(UInt16& pi, BOOL& atapi) { HbaMem* mem_ahci = (HbaMem*)kSATADev.Bar(kSATABar5); - kSATADev.EnableMmio((UIntPtr)mem_ahci); - kSATADev.BecomeBusMaster((UIntPtr)mem_ahci); + kSATADev.EnableMmio(); + kSATADev.BecomeBusMaster(); UInt32 ports_implemented = mem_ahci->Pi; UInt16 ahci_index = 0; @@ -368,7 +369,9 @@ STATIC Bool drv_std_init_ahci(UInt16& pi, BOOL& atapi) ahci_enable_and_probe(); - break; + err_global_get() = kErrorSuccess; + + return YES; } else if (atapi && kSATAPISignature == mem_ahci->Ports[ahci_index].Sig) { @@ -377,16 +380,14 @@ STATIC Bool drv_std_init_ahci(UInt16& pi, BOOL& atapi) ahci_enable_and_probe(); - break; + err_global_get() = kErrorSuccess; + + return YES; } ports_implemented >>= 1; ++ahci_index; } - - err_global_get() = kErrorSuccess; - - return YES; } } @@ -473,7 +474,7 @@ namespace Kernel UInt16 sk_init_ahci_device(BOOL atapi) { UInt16 pi = 0; - return drv_std_init_ahci(pi, atapi); + drv_std_init_ahci(pi, atapi); kSATAPortsImplemented = pi; |
