1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 21:55:07 +00:00

Kernel/Net: Introduce a new mechanism to initialize a PCI device

Instead of using a clunky if-statement paradigm, we now have all drivers
being declaring two methods for their adapter class - create and probe.
These methods are linked in each PCINetworkDriverInitializer structure,
in a new s_initializers static list of them.
Then, when we probe for a PCI device, we use each probe method and if
there's a match, then the corresponding create method is called. After
the adapter instance is created, we call the virtual initialize method
on it, because many drivers actually require a sort of post-construction
initialization sequence to ensure the network adapter can properly
function.

As a result of this change, it's much more easy to add more drivers and
the initialization code is more readable and it's easier to understand
when and where things could fail in the whole initialization sequence.
This commit is contained in:
Liav A 2022-12-17 22:11:44 +02:00 committed by Jelle Raaijmakers
parent 90ac9d7253
commit 0cede94c39
13 changed files with 131 additions and 86 deletions

View file

@ -93,18 +93,34 @@ ErrorOr<NonnullOwnPtr<KString>> NetworkingManagement::generate_interface_name_fr
return name;
}
struct PCINetworkDriverInitializer {
ErrorOr<bool> (*probe)(PCI::DeviceIdentifier const&) = nullptr;
ErrorOr<NonnullLockRefPtr<NetworkAdapter>> (*create)(PCI::DeviceIdentifier const&) = nullptr;
};
static constexpr PCINetworkDriverInitializer s_initializers[] = {
{ RTL8168NetworkAdapter::probe, RTL8168NetworkAdapter::create },
{ RTL8139NetworkAdapter::probe, RTL8139NetworkAdapter::create },
{ NE2000NetworkAdapter::probe, NE2000NetworkAdapter::create },
{ E1000NetworkAdapter::probe, E1000NetworkAdapter::create },
{ E1000ENetworkAdapter::probe, E1000ENetworkAdapter::create },
};
UNMAP_AFTER_INIT ErrorOr<NonnullLockRefPtr<NetworkAdapter>> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
{
if (auto candidate = TRY(E1000NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(E1000ENetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(RTL8139NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(RTL8168NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(NE2000NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
for (auto& initializer : s_initializers) {
auto initializer_probe_found_driver_match_or_error = initializer.probe(device_identifier);
if (initializer_probe_found_driver_match_or_error.is_error()) {
dmesgln("Networking: Failed to probe device {}, due to {}", device_identifier.address(), initializer_probe_found_driver_match_or_error.error());
continue;
}
auto initializer_probe_found_driver_match = initializer_probe_found_driver_match_or_error.release_value();
if (initializer_probe_found_driver_match) {
auto adapter = TRY(initializer.create(device_identifier));
TRY(adapter->initialize({}));
return adapter;
}
}
return Error::from_string_literal("Unsupported network adapter");
}