Skip to content

Commit

Permalink
Kernel/AHCI: Dont assume ports start at 0
Browse files Browse the repository at this point in the history
This fixes a bug that occurs when the controller's ports are not
(internally) numbered sequentially.
This is done by checking the bits set in PI.
This bug was found on bare-metal, on a laptop with 1 Port that
was reported as port 4.
  • Loading branch information
ElectrodeYT authored and awesomekling committed Jun 25, 2021
1 parent e9b7d58 commit 2b4cab2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Kernel/Storage/AHCIController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,18 @@ RefPtr<StorageDevice> AHCIController::device_by_port(u32 port_index) const
RefPtr<StorageDevice> AHCIController::device(u32 index) const
{
NonnullRefPtrVector<StorageDevice> connected_devices;
for (size_t index = 0; index < capabilities().ports_count; index++) {
auto checked_device = device_by_port(index);
u32 pi = hba().control_regs.pi;
u32 bit = __builtin_ffsl(pi);
while (bit) {
dbgln_if(AHCI_DEBUG, "Checking implemented port {}, pi {:b}", bit - 1, pi);
pi &= ~(1u << (bit - 1));
auto checked_device = device_by_port(bit - 1);
bit = __builtin_ffsl(pi);
if (checked_device.is_null())
continue;
connected_devices.append(checked_device.release_nonnull());
}
dbgln_if(AHCI_DEBUG, "Connected device count: {}, Index: {}", connected_devices.size(), index);
if (index >= connected_devices.size())
return nullptr;
return connected_devices[index];
Expand Down

0 comments on commit 2b4cab2

Please sign in to comment.