1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// TODO(eliza): write a `RwLock`...
use crate::drivers::pci::*;

pub fn init_pci() {
    let mut bad = 0;
    let mut devices = DeviceRegistry::default();

    let _span = tracing::info_span!("enumerating PCI devices").entered();
    for (addr, config) in config::enumerate_all() {
        let class = match config.header.classes() {
            Ok(class) => class,
            Err(error) => {
                tracing::error!(
                    target: "pci",
                    error = %error,
                    "[{addr}] bad class"
                );
                bad += 1;
                continue;
            }
        };

        let _ = tracing::info_span!(
            target: "pci",
            "pci",
            class = %class.class().name(),
            subclass = %class.subclass().name(),
            "[{addr}]"
        )
        .entered();

        let id = config.header.id();
        match id {
            device::Id::Known(ids) => {
                tracing::info!(
                    target: "  pci",
                    vendor = %ids.vendor().name(),
                    device = %ids.name(),
                );
            }
            device::Id::Unknown(ids) => {
                tracing::warn!(
                    target: "  pci",
                    vendor = ids.vendor_id,
                    device = ids.device_id,
                    "unrecognized vendor or device ID"
                );
            }
        };

        assert!(
            devices.insert(addr, class, id),
            "PCI device inserted twice! addr={addr:?}, id={id:?}, class={class:?}",
        );
    }

    tracing::info!("found {} PCI devices ({bad} bad)", devices.len());

    DEVICES.init(devices);
}