diff options
| -rw-r--r-- | dev/Kernel/HALKit/AMD64/Storage/AHCI.cc | 149 | ||||
| -rw-r--r-- | dev/Kernel/NewKit/Crc32.h | 2 | ||||
| -rw-r--r-- | dev/Kernel/src/Crc32.cc | 11 |
3 files changed, 83 insertions, 79 deletions
diff --git a/dev/Kernel/HALKit/AMD64/Storage/AHCI.cc b/dev/Kernel/HALKit/AMD64/Storage/AHCI.cc index 5bc960e0..9e9e5f7c 100644 --- a/dev/Kernel/HALKit/AMD64/Storage/AHCI.cc +++ b/dev/Kernel/HALKit/AMD64/Storage/AHCI.cc @@ -15,6 +15,7 @@ * */ +#include "NewKit/Macros.h" #include <KernelKit/UserProcessScheduler.h> #include <KernelKit/LPC.h> @@ -27,17 +28,19 @@ #ifdef __AHCI__ #define kHBAErrTaskFile (1 << 30) -#define kHBAPxCmdST 0x0001 -#define kHBAPxCmdFre 0x0010 -#define kHBAPxCmdFR 0x4000 -#define kHBAPxCmdCR 0x8000 +#define kHBAPxCmdST (0x0001) +#define kHBAPxCmdFre (0x0010) +#define kHBAPxCmdFR (0x4000) +#define kHBAPxCmdCR (0x8000) #define kSataLBAMode (1 << 6) #define kAhciSRBsy (0x80) #define kAhciSRDrq (0x08) -#define kAhciPortCnt 32 +#define kAhciPortCnt (0x20) + +using namespace NeOS; enum { @@ -46,39 +49,39 @@ enum kSATABar5 = 0x24, }; -STATIC NeOS::PCI::Device kPCIDevice; +STATIC PCI::Device kPCIDevice; STATIC HbaMem* kSATA = nullptr; -STATIC NeOS::SizeT kSATAPortIdx = 0UL; -STATIC NeOS::Lba kCurrentDiskSectorCount = 0UL; +STATIC SizeT kSATAIndex = 0UL; +STATIC Lba kCurrentDiskSectorCount = 0UL; template <BOOL Write, BOOL CommandOrCTRL, BOOL Identify> -static NeOS::Void drv_std_input_output(NeOS::UInt64 lba, NeOS::UInt8* buffer, NeOS::SizeT sector_sz, NeOS::SizeT size_buffer) noexcept; +STATIC Void drv_std_input_output(UInt64 lba, UInt8* buffer, SizeT sector_sz, SizeT size_buffer) noexcept; -static NeOS::Int32 drv_find_cmd_slot(HbaPort* port) noexcept; +STATIC Int32 drv_find_cmd_slot(HbaPort* port) noexcept; -static NeOS::Void drv_calculate_disk_geometry() noexcept; +STATIC Void drv_calculate_disk_geometry() noexcept; -static NeOS::Void drv_calculate_disk_geometry() noexcept +STATIC Void drv_calculate_disk_geometry() noexcept { kCurrentDiskSectorCount = 0UL; - NeOS::UInt8 identify_data[kib_cast(4)] = {0}; + UInt8 identify_data[kib_cast(8)] = {0}; + + rt_set_memory(identify_data, 0, kib_cast(8)); drv_std_input_output<NO, YES, YES>(0, identify_data, 0, kib_cast(8)); kCurrentDiskSectorCount = (identify_data[61] << 16) | identify_data[60]; - kout << "Disk Size: " << NeOS::number(drv_get_size()) << endl; - kout << "Highest LBA: " << NeOS::number(kCurrentDiskSectorCount) << endl; + kout << "Disk Size: " << number(drv_get_size()) << endl; + kout << "Highest LBA: " << number(kCurrentDiskSectorCount) << endl; } /// @brief Initializes an AHCI disk. -/// @param PortsImplemented the amount of kSATA that have been detected. +/// @param pi the amount of kSATA that have been detected. /// @return if the disk was successfully initialized or not. -NeOS::Boolean drv_std_init(NeOS::UInt16& PortsImplemented) +Boolean drv_std_init(UInt16& pi) { - using namespace NeOS; - PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController); for (SizeT device_index = 0; device_index < NE_BUS_COUNT; ++device_index) @@ -89,39 +92,41 @@ NeOS::Boolean drv_std_init(NeOS::UInt16& PortsImplemented) if (kPCIDevice.Subclass() == kSATASubClass && kPCIDevice.ProgIf() == kSATAProgIfAHCI) { - kPCIDevice.EnableMmio(0x24); // Enable the memory index_byte/o for this ahci device. - kPCIDevice.BecomeBusMaster(0x24); // Become bus master for this ahci device, so that we can control it. + HbaMem* mem_ahci = (HbaMem*)kPCIDevice.Bar(kSATABar5); - HbaMem* mem_ahci = (HbaMem*)kPCIDevice.Bar(0x24); + kPCIDevice.EnableMmio(kPCIDevice.Bar(kSATABar5)); // Enable the memory index_byte/o for this ahci device. + kPCIDevice.BecomeBusMaster(kPCIDevice.Bar(kSATABar5)); // Become bus master for this ahci device, so that we can control it. - NeOS::UInt32 ports_implemented = mem_ahci->Pi; - NeOS::UInt16 ahci_index = 0; + UInt32 ports_implemented = mem_ahci->Pi; + UInt16 ahci_index = 0; - const NeOS::UInt16 kMaxPortsImplemented = kAhciPortCnt; - const NeOS::UInt32 kSATASignature = 0x00000101; - const NeOS::UInt8 kAhciPresent = 0x03; - const NeOS::UInt8 kAhciIPMActive = 0x01; + const UInt16 kMaxPortsImplemented = kAhciPortCnt; + const UInt32 kSATASignature = 0x00000101; + const UInt8 kAhciPresent = 0x03; + const UInt8 kAhciIPMActive = 0x01; - NeOS::Boolean detected = false; + Boolean detected = false; while (ahci_index < kMaxPortsImplemented) { if (ports_implemented) { - NeOS::UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F; - NeOS::UInt8 det = mem_ahci->Ports[ahci_index].Ssts & 0x0F; + UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F; + UInt8 det = mem_ahci->Ports[ahci_index].Ssts & 0x0F; if (mem_ahci->Ports[ahci_index].Sig == kSATASignature && det == kAhciPresent && ipm == kAhciIPMActive) { kout << "SATA port found.\r"; - kSATAPortIdx = ahci_index; + kSATAIndex = ahci_index; kSATA = mem_ahci; drv_calculate_disk_geometry(); detected = YES; + pi = ports_implemented; + break; } } @@ -137,29 +142,29 @@ NeOS::Boolean drv_std_init(NeOS::UInt16& PortsImplemented) return No; } -NeOS::Boolean drv_std_detected(NeOS::Void) +Boolean drv_std_detected(Void) { - return kPCIDevice.DeviceId() != (NeOS::UShort)NeOS::PCI::PciConfigKind::Invalid; + return kPCIDevice.DeviceId() != (UShort)PCI::PciConfigKind::Invalid; } -NeOS::Void drv_std_write(NeOS::UInt64 lba, NeOS::Char* buffer, NeOS::SizeT sector_sz, NeOS::SizeT size_buffer) +Void drv_std_write(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) { - drv_std_input_output<YES, YES, NO>(lba, (NeOS::UInt8*)buffer, sector_sz, size_buffer); + drv_std_input_output<YES, YES, NO>(lba, (UInt8*)buffer, sector_sz, size_buffer); } -NeOS::Void drv_std_read(NeOS::UInt64 lba, NeOS::Char* buffer, NeOS::SizeT sector_sz, NeOS::SizeT size_buffer) +Void drv_std_read(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) { - drv_std_input_output<NO, YES, NO>(lba, (NeOS::UInt8*)buffer, sector_sz, size_buffer); + drv_std_input_output<NO, YES, NO>(lba, (UInt8*)buffer, sector_sz, size_buffer); } -static NeOS::Int32 drv_find_cmd_slot(HbaPort* port) noexcept +STATIC Int32 drv_find_cmd_slot(HbaPort* port) noexcept { if (port == nullptr) - return -1; + return ~0; - NeOS::UInt32 slots = (kSATA->Ports[kSATAPortIdx].Sact | kSATA->Ports[kSATAPortIdx].Ci); + UInt32 slots = (kSATA->Ports[kSATAIndex].Sact | kSATA->Ports[kSATAIndex].Ci); - for (NeOS::Int32 i = 0; i < kAhciPortCnt; ++i) + for (Int32 i = 0; i < kAhciPortCnt; ++i) { if ((slots & 1) == 0) return i; @@ -167,51 +172,51 @@ static NeOS::Int32 drv_find_cmd_slot(HbaPort* port) noexcept slots >>= 1; } - return -1; + return ~0; } BOOL kAHCICommandIssued = NO; template <BOOL Write, BOOL CommandOrCTRL, BOOL Identify> -static NeOS::Void drv_std_input_output(NeOS::UInt64 lba, NeOS::UInt8* buffer, NeOS::SizeT sector_sz, NeOS::SizeT size_buffer) noexcept +STATIC Void drv_std_input_output(UInt64 lba, UInt8* buffer, SizeT sector_sz, SizeT size_buffer) noexcept { - kSATA->Ports[kSATAPortIdx].Cmd |= kHBAPxCmdFre; - kSATA->Ports[kSATAPortIdx].Cmd |= kHBAPxCmdST; + kSATA->Ports[kSATAIndex].Cmd |= kHBAPxCmdFre; + kSATA->Ports[kSATAIndex].Cmd |= kHBAPxCmdST; auto slot = 0L; - slot = drv_find_cmd_slot(&kSATA->Ports[kSATAPortIdx]); + slot = drv_find_cmd_slot(&kSATA->Ports[kSATAIndex]); - if (slot == -1) + if (slot == ~0) return; - volatile HbaCmdHeader* command_header = ((volatile HbaCmdHeader*)((NeOS::UInt64)kSATA->Ports[kSATAPortIdx].Clb)); + volatile HbaCmdHeader* command_header = ((volatile HbaCmdHeader*)((UInt64)kSATA->Ports[kSATAIndex].Clb)); command_header += slot; MUST_PASS(command_header); - command_header->Cfl = sizeof(FisRegH2D) / sizeof(NeOS::UInt32); + command_header->Cfl = sizeof(FisRegH2D) / sizeof(UInt32); command_header->Write = Write; command_header->Prdtl = 2; - volatile HbaCmdTbl* command_table = (volatile HbaCmdTbl*)((NeOS::UInt64)command_header->Ctba); + volatile HbaCmdTbl* command_table = (volatile HbaCmdTbl*)((UInt64)command_header->Ctba); MUST_PASS(command_table); - auto buffer_phys = NeOS::HAL::hal_get_phys_address(buffer); + auto buffer_phys = HAL::hal_get_phys_address(buffer); - command_table->Prdt[0].Dba = ((NeOS::UInt32)(NeOS::UInt64)buffer_phys); - command_table->Prdt[0].Dbau = (((NeOS::UInt64)(buffer_phys) >> 32)); + command_table->Prdt[0].Dba = ((UInt32)(UInt64)buffer_phys); + command_table->Prdt[0].Dbau = (((UInt64)(buffer_phys) >> 32)); command_table->Prdt[0].Dbc = ((size_buffer / 2) - 1); command_table->Prdt[0].Ie = YES; - command_table->Prdt[1].Dba = ((NeOS::UInt32)(NeOS::UInt64)(buffer_phys + ((size_buffer / 2) - 1))); - command_table->Prdt[1].Dbau = (((NeOS::UInt64)(buffer_phys + ((size_buffer / 2) - 1)) >> 32)); + command_table->Prdt[1].Dba = ((UInt32)(UInt64)(buffer_phys + ((size_buffer / 2) - 1))); + command_table->Prdt[1].Dbau = (((UInt64)(buffer_phys + ((size_buffer / 2) - 1)) >> 32)); command_table->Prdt[1].Dbc = ((size_buffer / 2) - 1); command_table->Prdt[1].Ie = YES; - volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)((NeOS::UInt64)&command_table->Cfis); + volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)((UInt64)&command_table->Cfis); h2d_fis->FisType = kFISTypeRegH2D; h2d_fis->CmdOrCtrl = CommandOrCTRL; @@ -220,41 +225,41 @@ static NeOS::Void drv_std_input_output(NeOS::UInt64 lba, NeOS::UInt8* buffer, Ne if (Identify) h2d_fis->Command = kAHCICmdIdentify; - h2d_fis->Lba0 = (lba); - h2d_fis->Lba1 = (lba >> 8); - h2d_fis->Lba2 = (lba >> 16); + h2d_fis->Lba0 = (lba) & 0xFF; + h2d_fis->Lba1 = (lba >> 8) & 0xFF; + h2d_fis->Lba2 = (lba >> 16) & 0xFF; h2d_fis->Device = kSataLBAMode; - h2d_fis->Lba3 = (lba >> 24); - h2d_fis->Lba4 = (lba >> 32); - h2d_fis->Lba5 = (lba >> 40); + h2d_fis->Lba3 = (lba >> 24) & 0xFF; + h2d_fis->Lba4 = (lba >> 32) & 0xFF; + h2d_fis->Lba5 = (lba >> 40) & 0xFF; h2d_fis->CountLow = (size_buffer) & 0xFF; h2d_fis->CountHigh = ((size_buffer) >> 8) & 0xFF; - while ((kSATA->Ports[kSATAPortIdx].Tfd & (kAhciSRBsy | kAhciSRDrq))) + while ((kSATA->Ports[kSATAIndex].Tfd & (kAhciSRBsy | kAhciSRDrq))) { kout << "Waiting for slot to be ready\r"; } - kSATA->Ports[kSATAPortIdx].Ci = 1 << slot; + kSATA->Ports[kSATAIndex].Ci = (1 << slot); kAHCICommandIssued = YES; - while (kSATA->Ports[kSATAPortIdx].Ci & (1 << slot)) + while (kSATA->Ports[kSATAIndex].Ci & (1 << slot)) { if (kSATA->Is & kHBAErrTaskFile) // check for task file error. { - NeOS::ke_panic(RUNTIME_CHECK_BAD_BEHAVIOR, "AHCI Read disk failure, faulty component."); + ke_panic(RUNTIME_CHECK_BAD_BEHAVIOR, "AHCI Read disk failure, faulty component."); } } - kSATA->Ports[kSATAPortIdx].Cmd &= ~kHBAPxCmdFre; - kSATA->Ports[kSATAPortIdx].Cmd &= ~kHBAPxCmdST; + kSATA->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdFre; + kSATA->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdST; if (kSATA->Is & kHBAErrTaskFile) // check for task file error. { - NeOS::ke_panic(RUNTIME_CHECK_BAD_BEHAVIOR, "AHCI Read disk failure, faulty component."); + ke_panic(RUNTIME_CHECK_BAD_BEHAVIOR, "AHCI Read disk failure, faulty component."); } } @@ -262,14 +267,14 @@ static NeOS::Void drv_std_input_output(NeOS::UInt64 lba, NeOS::UInt8* buffer, Ne @brief Gets the number of sectors inside the drive. @return Sector size in bytes. */ -NeOS::SizeT drv_get_sector_count() +SizeT drv_get_sector_count() { return kCurrentDiskSectorCount; } /// @brief Get the drive size. /// @return Disk size in bytes. -NeOS::SizeT drv_get_size() +SizeT drv_get_size() { return drv_get_sector_count() * kAHCISectorSize; } diff --git a/dev/Kernel/NewKit/Crc32.h b/dev/Kernel/NewKit/Crc32.h index 38d2f8fa..1b5b2ac1 100644 --- a/dev/Kernel/NewKit/Crc32.h +++ b/dev/Kernel/NewKit/Crc32.h @@ -17,7 +17,7 @@ namespace NeOS { - UInt ke_calculate_crc32(const Char* crc, UInt len) noexcept; + UInt32 ke_calculate_crc32(const Char* crc, Int32 len) noexcept; } // namespace NeOS #endif // !CRC32_H diff --git a/dev/Kernel/src/Crc32.cc b/dev/Kernel/src/Crc32.cc index bbc0f9bf..98821602 100644 --- a/dev/Kernel/src/Crc32.cc +++ b/dev/Kernel/src/Crc32.cc @@ -69,15 +69,14 @@ namespace NeOS /// @brief Calculate CRC32 of p /// @param p the data to compute. /// @param len the length of the data. - /// @return the CRC32. - UInt ke_calculate_crc32(const Char* p, UInt len) noexcept + /// @return CRC32 of **p**. + UInt32 ke_calculate_crc32(const Char* p, Int32 len) noexcept { - UInt crc = 0xffffffff; + UInt32 crc = 0xffffffff; - while (len-- != 0) + while (--len > 0) crc = kCrcTbl[((UInt8)crc ^ *(p++))] ^ (crc >> 8); - // return (~crc); also works, does the same thing. - return (crc ^ 0xffffffff); + return ~crc; } } // namespace NeOS |
