aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/kernel/bus/pci.cpp58
-rw-r--r--kernel/kernel/bus/pci.hpp164
-rw-r--r--kernel/kernel/bus/pci.tests.cpp622
4 files changed, 845 insertions, 0 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 7f418c61..e8ba16eb 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -51,6 +51,7 @@ target_sources("kernel_lib" PRIVATE
# Bus Sumbsystem
"kernel/bus/boot_modules.cpp"
+ "kernel/bus/pci.cpp"
"kernel/bus/pseudo.cpp"
"kernel/bus/root.cpp"
diff --git a/kernel/kernel/bus/pci.cpp b/kernel/kernel/bus/pci.cpp
new file mode 100644
index 00000000..16953910
--- /dev/null
+++ b/kernel/kernel/bus/pci.cpp
@@ -0,0 +1,58 @@
+#include <kernel/bus/pci.hpp>
+
+#include <kernel/devices/pci/function_info.hpp>
+
+#include <kapi/devices.hpp>
+
+#include <kstd/result.hpp>
+
+#include <algorithm>
+#include <cstdint>
+#include <ranges>
+
+namespace kernel::bus
+{
+
+ namespace
+ {
+
+ //! Create a callable object that matches a PCI ID against the given PCI info
+ //!
+ //! @param info The PCI info to match against
+ //! @return A callable object that takes a PCI ID and returns the match score
+ constexpr auto matcher_for(devices::pci::function_info const & info)
+ {
+ return [&info](auto const & id) {
+ return id.match(info).value_or(0);
+ };
+ }
+
+ } // namespace
+
+ auto pci_protocol::match(kapi::devices::device const & device, kapi::devices::driver const & driver) const
+ -> kstd::result<std::uint32_t>
+ {
+ auto signature = device.facet<pci_signature>();
+ auto claim = driver.facet<pci_claim>();
+
+ if (!signature || !claim)
+ {
+ return kstd::failure(kapi::devices::driver_match_errc::no_match);
+ }
+
+ auto supported_ids = claim->supported_ids();
+ if (std::ranges::empty(supported_ids))
+ {
+ return kstd::failure(kapi::devices::driver_match_errc::no_match);
+ }
+
+ auto score = std::ranges::max(supported_ids | std::views::transform(matcher_for(signature->info())));
+ if (score == 0)
+ {
+ return kstd::failure(kapi::devices::driver_match_errc::no_match);
+ }
+
+ return kstd::success(score);
+ }
+
+} // namespace kernel::bus
diff --git a/kernel/kernel/bus/pci.hpp b/kernel/kernel/bus/pci.hpp
new file mode 100644
index 00000000..782bd8b7
--- /dev/null
+++ b/kernel/kernel/bus/pci.hpp
@@ -0,0 +1,164 @@
+#ifndef TEACHOS_KERNEL_BUS_PCI_HPP
+#define TEACHOS_KERNEL_BUS_PCI_HPP
+
+#include <kernel/devices/pci/function_info.hpp>
+
+#include <kapi/capabilities/facet_id.hpp>
+#include <kapi/devices.hpp>
+
+#include <kstd/result.hpp>
+
+#include <cstdint>
+#include <optional>
+#include <span>
+
+namespace kernel::bus
+{
+
+ //! An ID of a PCI function used to match devices against drivers.
+ struct pci_id
+ {
+ constexpr auto static exact_id_score = 0x10u;
+
+ //! Calculate the prefix chain match score for a given function information object against this ID.
+ //!
+ //! The match score is calculated based on the following rules:
+ //! - If both vendor_id and device_id of this ID object are set, a match of both fields adds 0x10 to the score.
+ //! - A programming_interface match requires a subclass_code match, which in turn requires a class_code match.
+ //! - Each successful match of class_code, subclass_code, and programming_interface adds 1 to the score.
+ //!
+ //! If neither vendor_id nor device_id are set, the match will rely solely on class_code, subclass_code, and
+ //! programming_interface. Any violations of the rules above will result in a non-match.
+ //!
+ //! @param info The PCI function information to match against.
+ //! @return The match score if applicable, otherwise std::nullopt.
+ [[nodiscard]] constexpr auto match(devices::pci::function_info const & info) const -> std::optional<std::uint32_t>
+ {
+ if (vendor_id.has_value() != device_id.has_value())
+ {
+ return std::nullopt;
+ }
+
+ if (subclass_code && !class_code)
+ {
+ return std::nullopt;
+ }
+
+ if (programming_interface && !subclass_code)
+ {
+ return std::nullopt;
+ }
+
+ if (!vendor_id && !class_code)
+ {
+ return std::nullopt;
+ }
+
+ auto score = std::uint32_t{};
+
+ if (vendor_id)
+ {
+ if (*vendor_id != info.vendor_id || *device_id != info.device_id)
+ {
+ return std::nullopt;
+ }
+ score += exact_id_score;
+ }
+
+ if (class_code)
+ {
+ if (*class_code != info.class_code)
+ {
+ return std::nullopt;
+ }
+ score += 1;
+ }
+
+ if (subclass_code)
+ {
+ if (*subclass_code != info.subclass_code)
+ {
+ return std::nullopt;
+ }
+ score += 1;
+ }
+
+ if (programming_interface)
+ {
+ if (*programming_interface != info.programming_interface)
+ {
+ return std::nullopt;
+ }
+ score += 1;
+ }
+
+ return score;
+ }
+
+ //! The vendor ID of the PCI device.
+ std::optional<std::uint16_t> vendor_id{};
+ //! The device ID of the PCI device.
+ std::optional<std::uint16_t> device_id{};
+ //! The class code of the PCI device.
+ std::optional<std::uint8_t> class_code{};
+ //! The subclass code of the PCI device.
+ std::optional<std::uint8_t> subclass_code{};
+ //! The programming interface of the PCI device.
+ std::optional<std::uint8_t> programming_interface{};
+ };
+
+ //! The signature of a PCI device.
+ struct pci_signature
+ {
+ //! The unique ID of this facet.
+ constexpr auto static id = kapi::capabilities::facet_id{"sig.dev.pci"};
+
+ //! Virtual destructor to allow for safe deletion of derived objects through base pointers.
+ virtual ~pci_signature() = default;
+
+ //! Retrieve the PCI function information associated with the device providing this facet.
+ //!
+ //! @return The PCI function information associated with the device.
+ [[nodiscard]] virtual auto info() const noexcept -> devices::pci::function_info const & = 0;
+ };
+
+ //! The claim a PCI driver makes on a device.
+ //!
+ //! This facet represents the set of PCI devices a driver can handle, as identified by their PCI IDs.
+ struct pci_claim
+ {
+ //! The unique ID of this facet.
+ constexpr auto static id = kapi::capabilities::facet_id{"clm.drv.pci"};
+
+ //! Virtual destructor to allow for safe deletion of derived objects through base pointers.
+ virtual ~pci_claim() = default;
+
+ //! Get the set of PCI IDs supported by the driver.
+ //!
+ //! @return A span of PCI IDs that the driver can handle.
+ [[nodiscard]] virtual auto supported_ids() const noexcept -> std::span<pci_id const> = 0;
+ };
+
+ //! The base class for all PCI protocols.
+ struct pci_protocol : kapi::devices::bus_protocol
+ {
+ //! Enumerate the PCI devices on a given bus.
+ //!
+ //! Concrete subclasses of pci_protocol must implement this function.
+ //!
+ //! @param bus The bus to enumerate.
+ //! @return void.
+ auto enumerate(kapi::devices::bus & bus) -> void override = 0;
+
+ //! Match a device against a driver to determine compatibility.
+ //!
+ //! @param device The device to match.
+ //! @param driver The driver to match against.
+ //! @return The match score on success, an error otherwise.
+ [[nodiscard]] auto match(kapi::devices::device const & device, kapi::devices::driver const & driver) const
+ -> kstd::result<std::uint32_t> final;
+ };
+
+} // namespace kernel::bus
+
+#endif // TEACHOS_KERNEL_BUS_PCI_HPP
diff --git a/kernel/kernel/bus/pci.tests.cpp b/kernel/kernel/bus/pci.tests.cpp
new file mode 100644
index 00000000..f44f3f2a
--- /dev/null
+++ b/kernel/kernel/bus/pci.tests.cpp
@@ -0,0 +1,622 @@
+#include <kernel/bus/pci.hpp>
+
+#include <kernel/devices/pci/function_info.hpp>
+
+#include <kapi/capabilities/facet_id.hpp>
+#include <kapi/devices.hpp>
+#include <kapi/devices/driver_registry.hpp>
+
+#include <kstd/memory.hpp>
+#include <kstd/result.hpp>
+#include <kstd/string.hpp>
+#include <kstd/test_support/result.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+#include <initializer_list>
+#include <optional>
+#include <span>
+#include <string_view>
+#include <utility>
+#include <vector>
+
+// NOLINTBEGIN(readability-magic-numbers)
+
+using kernel::bus::pci_claim;
+using kernel::bus::pci_id;
+using kernel::bus::pci_protocol;
+using kernel::bus::pci_signature;
+using kernel::devices::pci::function_info;
+using kstd::tests::require_ok;
+
+namespace
+{
+ constexpr auto vendor_intel = 0x8086;
+ constexpr auto device_ich9_ahci = 0x2922;
+ constexpr auto class_mass_storage = 0x01;
+ constexpr auto subclass_sata = 0x06;
+ constexpr auto programming_interface_ahci = 0x01;
+
+ constexpr auto vendor_red_hat = 0x1af4;
+ constexpr auto device_virtio_blk = 0x1001;
+
+ auto ahci_controller() -> function_info
+ {
+ auto info = function_info{};
+ info.vendor_id = vendor_intel;
+ info.device_id = device_ich9_ahci;
+ info.class_code = class_mass_storage;
+ info.subclass_code = subclass_sata;
+ info.programming_interface = programming_interface_ahci;
+ return info;
+ }
+
+ struct test_signature final : pci_signature
+ {
+ explicit test_signature(function_info info)
+ : m_info{info}
+ {}
+
+ [[nodiscard]] auto info() const noexcept -> function_info const & override
+ {
+ return m_info;
+ }
+
+ private:
+ function_info m_info;
+ };
+
+ //! A device that carries a PCI signature.
+ struct test_device final : kapi::devices::device
+ {
+ test_device(kstd::string name, function_info info)
+ : device{name}
+ , m_signature{info}
+ {}
+
+ protected:
+ auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override
+ {
+ if (facet == pci_signature::id)
+ {
+ return kstd::make_observer<pci_signature>(&m_signature);
+ }
+
+ return device::query_facet(facet);
+ }
+
+ private:
+ test_signature m_signature;
+ };
+
+ //! A device without any PCI signature.
+ struct bare_device final : kapi::devices::device
+ {
+ using device::device;
+ };
+
+ //! A driver that claims a table of PCI IDs and records how often it was probed.
+ struct test_driver final : kapi::devices::driver, pci_claim
+ {
+ explicit test_driver(std::vector<pci_id> ids, bool probe_succeeds = true)
+ : m_ids{std::move(ids)}
+ , m_probe_succeeds{probe_succeeds}
+ {}
+
+ [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override
+ {
+ ++probe_calls;
+
+ if (!m_probe_succeeds)
+ {
+ return kstd::failure(kapi::devices::driver_match_errc::no_match);
+ }
+
+ return kstd::success();
+ }
+
+ auto unbind(kapi::devices::device &) -> void override {}
+
+ [[nodiscard]] auto name() const noexcept -> std::string_view override
+ {
+ return "PCI Test Driver";
+ }
+
+ [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci_id const> override
+ {
+ return m_ids;
+ }
+
+ unsigned probe_calls{};
+
+ protected:
+ auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override
+ {
+ if (facet == pci_claim::id)
+ {
+ return kstd::make_observer<pci_claim>(this);
+ }
+
+ return driver::query_facet(facet);
+ }
+
+ private:
+ std::vector<pci_id> m_ids;
+ bool m_probe_succeeds;
+ };
+
+ //! A driver without any PCI claim.
+ struct bare_driver final : kapi::devices::driver
+ {
+ [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override
+ {
+ return kstd::success();
+ }
+
+ auto unbind(kapi::devices::device &) -> void override {}
+
+ [[nodiscard]] auto name() const noexcept -> std::string_view override
+ {
+ return "Bare Test Driver";
+ }
+ };
+
+ struct test_protocol final : pci_protocol
+ {
+ auto enumerate(kapi::devices::bus &) -> void override {}
+ } constinit protocol_instance;
+
+ struct test_bus final : kapi::devices::bus
+ {
+ explicit test_bus(kstd::string name)
+ : bus{name, protocol_instance}
+ {}
+ };
+
+ auto is_no_match(kstd::result<std::uint32_t> const & result) -> bool
+ {
+ return !result && result.error() == kapi::devices::make_error_code(kapi::devices::driver_match_errc::no_match);
+ }
+
+ auto table(std::initializer_list<pci_id> ids) -> std::vector<pci_id>
+ {
+ return ids;
+ }
+
+ auto exact_id() -> pci_id
+ {
+ return {.vendor_id = vendor_intel, .device_id = device_ich9_ahci};
+ }
+
+ auto other_exact_id() -> pci_id
+ {
+ return {.vendor_id = vendor_red_hat, .device_id = device_virtio_blk};
+ }
+
+ auto class_id() -> pci_id
+ {
+ return {.class_code = class_mass_storage};
+ }
+
+ auto subclass_id() -> pci_id
+ {
+ return {.class_code = class_mass_storage, .subclass_code = subclass_sata};
+ }
+
+ auto full_class_id() -> pci_id
+ {
+ return {
+ .class_code = class_mass_storage,
+ .subclass_code = subclass_sata,
+ .programming_interface = programming_interface_ahci,
+ };
+ }
+} // namespace
+
+SCENARIO("PCI IDs are scored against a function", "[bus][pci]")
+{
+ GIVEN("an AHCI controller function")
+ {
+ auto const info = ahci_controller();
+
+ WHEN("an ID names the vendor and device exactly")
+ {
+ THEN("it matches with the exact ID score")
+ {
+ REQUIRE(exact_id().match(info) == pci_id::exact_id_score);
+ }
+ }
+
+ WHEN("an ID names the right vendor but the wrong device")
+ {
+ auto const id = pci_id{.vendor_id = vendor_intel, .device_id = device_virtio_blk};
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names the right device but the wrong vendor")
+ {
+ auto const id = pci_id{.vendor_id = vendor_red_hat, .device_id = device_ich9_ahci};
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names only the class")
+ {
+ THEN("it matches with a score of 1")
+ {
+ REQUIRE(class_id().match(info) == 1u);
+ }
+ }
+
+ WHEN("an ID names the class and subclass")
+ {
+ THEN("it matches with a score of 2")
+ {
+ REQUIRE(subclass_id().match(info) == 2u);
+ }
+ }
+
+ WHEN("an ID names the class, subclass and programming interface")
+ {
+ THEN("it matches with a score of 3")
+ {
+ REQUIRE(full_class_id().match(info) == 3u);
+ }
+ }
+
+ WHEN("an ID names the wrong class")
+ {
+ auto const id = pci_id{.class_code = 0x02};
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names the right class but the wrong subclass")
+ {
+ auto const id = pci_id{.class_code = class_mass_storage, .subclass_code = 0x07};
+
+ THEN("it does not match, not even partially")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names the right class and subclass but the wrong programming interface")
+ {
+ auto const id = pci_id{
+ .class_code = class_mass_storage,
+ .subclass_code = subclass_sata,
+ .programming_interface = 0x02,
+ };
+
+ THEN("it does not match, not even partially")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names the exact device and the class")
+ {
+ auto const id = pci_id{
+ .vendor_id = vendor_intel,
+ .device_id = device_ich9_ahci,
+ .class_code = class_mass_storage,
+ };
+
+ THEN("the scores add up")
+ {
+ REQUIRE(id.match(info) == pci_id::exact_id_score + 1u);
+ }
+ }
+
+ WHEN("an ID names the exact device but the wrong class")
+ {
+ auto const id = pci_id{
+ .vendor_id = vendor_intel,
+ .device_id = device_ich9_ahci,
+ .class_code = 0x02,
+ };
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+ }
+}
+
+SCENARIO("Malformed PCI IDs never match", "[bus][pci]")
+{
+ GIVEN("an AHCI controller function")
+ {
+ auto const info = ahci_controller();
+
+ WHEN("an ID names nothing at all")
+ {
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(pci_id{}.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names only a vendor")
+ {
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(pci_id{.vendor_id = vendor_intel}.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names only a device")
+ {
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(pci_id{.device_id = device_ich9_ahci}.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names a vendor without a device, but a correct class")
+ {
+ auto const id = pci_id{.vendor_id = vendor_intel, .class_code = class_mass_storage};
+
+ THEN("it does not match on the class alone")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names a subclass without a class")
+ {
+ auto const id = pci_id{.subclass_code = subclass_sata};
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names a programming interface without a subclass or class")
+ {
+ auto const id = pci_id{.programming_interface = programming_interface_ahci};
+
+ THEN("it does not match")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+
+ WHEN("an ID names a class and programming interface but no subclass")
+ {
+ auto const id = pci_id{.class_code = class_mass_storage, .programming_interface = programming_interface_ahci};
+
+ THEN("it does not match on the class alone")
+ {
+ REQUIRE_FALSE(id.match(info).has_value());
+ }
+ }
+ }
+}
+
+SCENARIO("More specific PCI IDs score higher", "[bus][pci]")
+{
+ GIVEN("an AHCI controller function")
+ {
+ auto const info = ahci_controller();
+
+ THEN("an exact ID outranks every class-based ID, and each additional class field raises the score")
+ {
+ auto const exact = *exact_id().match(info);
+ auto const full = *full_class_id().match(info);
+ auto const sub = *subclass_id().match(info);
+ auto const cls = *class_id().match(info);
+
+ REQUIRE(exact > full);
+ REQUIRE(full > sub);
+ REQUIRE(sub > cls);
+ }
+ }
+}
+
+SCENARIO("The PCI protocol rejects pairings that cannot match", "[bus][pci]")
+{
+ GIVEN("an AHCI controller device")
+ {
+ auto const device = test_device{"pci_protocol_ahci_device", ahci_controller()};
+
+ WHEN("the driver has no PCI claim")
+ {
+ auto const driver = bare_driver{};
+
+ THEN("there is no match")
+ {
+ REQUIRE(is_no_match(protocol_instance.match(device, driver)));
+ }
+ }
+
+ WHEN("the driver claims an empty table")
+ {
+ auto const driver = test_driver{table({})};
+
+ THEN("there is no match")
+ {
+ REQUIRE(is_no_match(protocol_instance.match(device, driver)));
+ }
+ }
+
+ WHEN("none of the driver's entries match")
+ {
+ auto const driver = test_driver{table({other_exact_id(), pci_id{.class_code = 0x02}})};
+
+ THEN("there is no match")
+ {
+ REQUIRE(is_no_match(protocol_instance.match(device, driver)));
+ }
+ }
+ }
+
+ GIVEN("a device without a PCI signature")
+ {
+ auto const device = bare_device{"pci_protocol_bare_device"};
+
+ WHEN("a driver that would match an AHCI controller is tested against it")
+ {
+ auto const driver = test_driver{table({exact_id(), class_id()})};
+
+ THEN("there is no match")
+ {
+ REQUIRE(is_no_match(protocol_instance.match(device, driver)));
+ }
+ }
+ }
+}
+
+SCENARIO("The PCI protocol scores a driver by its best entry", "[bus][pci]")
+{
+ GIVEN("an AHCI controller device")
+ {
+ auto const device = test_device{"pci_protocol_best_entry_device", ahci_controller()};
+
+ WHEN("a driver claims only a class")
+ {
+ auto const driver = test_driver{table({class_id()})};
+
+ THEN("the score is that of the class entry")
+ {
+ REQUIRE(require_ok(protocol_instance.match(device, driver)) == 1u);
+ }
+ }
+
+ WHEN("a driver claims the exact device")
+ {
+ auto const driver = test_driver{table({exact_id()})};
+
+ THEN("the score is the exact ID score")
+ {
+ REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci_id::exact_id_score);
+ }
+ }
+
+ WHEN("a driver claims several entries that all match")
+ {
+ auto const driver = test_driver{table({class_id(), exact_id(), subclass_id()})};
+
+ THEN("the score is the best single entry, not the sum")
+ {
+ REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci_id::exact_id_score);
+ }
+ }
+
+ WHEN("a driver lists a non-matching entry before a matching one")
+ {
+ auto const driver = test_driver{table({other_exact_id(), subclass_id()})};
+
+ THEN("the matching entry is still found")
+ {
+ REQUIRE(require_ok(protocol_instance.match(device, driver)) == 2u);
+ }
+ }
+
+ WHEN("the same entries are listed in the opposite order")
+ {
+ auto const forward = test_driver{table({class_id(), subclass_id(), full_class_id()})};
+ auto const backward = test_driver{table({full_class_id(), subclass_id(), class_id()})};
+
+ THEN("the score does not change")
+ {
+ REQUIRE(require_ok(protocol_instance.match(device, forward)) ==
+ require_ok(protocol_instance.match(device, backward)));
+ }
+ }
+ }
+}
+
+SCENARIO("The PCI protocol lets a more specific driver win regardless of registration order", "[bus][pci]")
+{
+ GIVEN("a class driver that is registered before an exact-ID driver")
+ {
+ auto bus = kstd::make_shared<test_bus>("pci_selection_bus");
+ auto class_driver = kstd::make_shared<test_driver>(std::vector{class_id()});
+ auto exact_driver = kstd::make_shared<test_driver>(std::vector{exact_id()});
+
+ kapi::devices::driver_registry::get().add(class_driver);
+ kapi::devices::driver_registry::get().add(exact_driver);
+
+ WHEN("an AHCI controller arrives")
+ {
+ auto device = kstd::make_shared<test_device>("pci_selection_device", ahci_controller());
+ bus->add_child(device);
+
+ THEN("the exact-ID driver is bound and the class driver is never probed")
+ {
+ REQUIRE(device->state() == kapi::devices::state::bound);
+ REQUIRE(device->bound_driver() == exact_driver);
+ REQUIRE(exact_driver->probe_calls == 1);
+ REQUIRE(class_driver->probe_calls == 0);
+ }
+ }
+ }
+}
+
+SCENARIO("A PCI device falls back to the next best driver when the best one fails to probe", "[bus][pci]")
+{
+ GIVEN("an exact-ID driver that fails to probe and a class driver that succeeds")
+ {
+ auto bus = kstd::make_shared<test_bus>("pci_fallback_bus");
+ auto exact_driver = kstd::make_shared<test_driver>(std::vector{exact_id()}, false);
+ auto class_driver = kstd::make_shared<test_driver>(std::vector{class_id()});
+
+ kapi::devices::driver_registry::get().add(exact_driver);
+ kapi::devices::driver_registry::get().add(class_driver);
+
+ WHEN("an AHCI controller arrives")
+ {
+ auto device = kstd::make_shared<test_device>("pci_fallback_device", ahci_controller());
+ bus->add_child(device);
+
+ THEN("the better driver is tried first and the class driver ends up bound")
+ {
+ REQUIRE(exact_driver->probe_calls == 1);
+ REQUIRE(class_driver->probe_calls == 1);
+ REQUIRE(device->state() == kapi::devices::state::bound);
+ REQUIRE(device->bound_driver() == class_driver);
+ }
+ }
+ }
+}
+
+SCENARIO("A PCI device nobody claims stays present", "[bus][pci]")
+{
+ GIVEN("a driver that only claims a different device")
+ {
+ auto bus = kstd::make_shared<test_bus>("pci_unclaimed_bus");
+ auto driver = kstd::make_shared<test_driver>(std::vector{other_exact_id()});
+
+ kapi::devices::driver_registry::get().add(driver);
+
+ WHEN("an AHCI controller arrives")
+ {
+ auto device = kstd::make_shared<test_device>("pci_unclaimed_device", ahci_controller());
+ bus->add_child(device);
+
+ THEN("the device is neither bound nor failed, and the driver is never probed")
+ {
+ REQUIRE(device->state() == kapi::devices::state::present);
+ REQUIRE(device->bound_driver() == nullptr);
+ REQUIRE(driver->probe_calls == 0);
+ }
+ }
+ }
+}
+
+// NOLINTEND(readability-magic-numbers)