Skip to content

Commit e7ababb

Browse files
committed
refactor(virtio): convert VirtioPciCfg to const u8
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent b4fb02b commit e7ababb

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

alioth/src/virtio/pci.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::utils::{get_atomic_high32, get_atomic_low32, set_atomic_high32, set_a
4242
use crate::virtio::dev::{Register, StartParam, VirtioDevice, WakeEvent};
4343
use crate::virtio::queue::QueueReg;
4444
use crate::virtio::{DevStatus, DeviceId, IrqSender, Result, error};
45-
use crate::{impl_mmio_for_zerocopy, mem};
45+
use crate::{consts, impl_mmio_for_zerocopy, mem};
4646

4747
const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff;
4848

@@ -584,23 +584,25 @@ fn get_class(id: DeviceId) -> (u8, u8) {
584584
}
585585
}
586586

587-
#[repr(u8)]
588-
pub enum VirtioPciCfg {
589-
Common = 1,
590-
Notify = 2,
591-
Isr = 3,
592-
Device = 4,
593-
Pci = 5,
594-
SharedMemory = 8,
595-
Vendor = 9,
587+
consts! {
588+
#[derive(Default, FromZeros, Immutable, IntoBytes)]
589+
pub struct VirtioPciCfg(u8) {
590+
COMMON = 1;
591+
NOTIFY = 2;
592+
ISR = 3;
593+
DEVICE = 4;
594+
PCI = 5;
595+
SHARED_MEMORY = 8;
596+
VENDOR = 9;
597+
}
596598
}
597599

598600
#[repr(C, align(4))]
599601
#[derive(Debug, Default, FromZeros, Immutable, IntoBytes)]
600602
pub struct VirtioPciCap {
601603
header: PciCapHdr,
602604
cap_len: u8,
603-
cfg_type: u8,
605+
cfg_type: VirtioPciCfg,
604606
bar: u8,
605607
id: u8,
606608
padding: [u8; 2],
@@ -730,7 +732,7 @@ where
730732
..Default::default()
731733
},
732734
cap_len: size_of::<VirtioPciCap>() as u8,
733-
cfg_type: VirtioPciCfg::Common as u8,
735+
cfg_type: VirtioPciCfg::COMMON,
734736
bar: 0,
735737
id: 0,
736738
offset: (virtio_register_offset + VirtioPciRegister::OFFSET_COMMON) as u32,
@@ -743,7 +745,7 @@ where
743745
..Default::default()
744746
},
745747
cap_len: size_of::<VirtioPciCap>() as u8,
746-
cfg_type: VirtioPciCfg::Isr as u8,
748+
cfg_type: VirtioPciCfg::ISR,
747749
bar: 0,
748750
id: 0,
749751
offset: (virtio_register_offset + VirtioPciRegister::OFFSET_ISR_STATUS) as u32,
@@ -757,7 +759,7 @@ where
757759
..Default::default()
758760
},
759761
cap_len: size_of::<VirtioPciNotifyCap>() as u8,
760-
cfg_type: VirtioPciCfg::Notify as u8,
762+
cfg_type: VirtioPciCfg::NOTIFY,
761763
bar: 0,
762764
id: 0,
763765
offset: (virtio_register_offset + VirtioPciRegister::OFFSET_QUEUE_NOTIFY) as u32,
@@ -772,7 +774,7 @@ where
772774
..Default::default()
773775
},
774776
cap_len: size_of::<VirtioPciCap>() as u8,
775-
cfg_type: VirtioPciCfg::Device as u8,
777+
cfg_type: VirtioPciCfg::DEVICE,
776778
bar: 0,
777779
id: 0,
778780
offset: device_config_offset as u32,
@@ -814,7 +816,7 @@ where
814816
..Default::default()
815817
},
816818
cap_len: size_of::<VirtioPciCap64>() as u8,
817-
cfg_type: VirtioPciCfg::SharedMemory as u8,
819+
cfg_type: VirtioPciCfg::SHARED_MEMORY,
818820
bar: 2,
819821
id: index as u8,
820822
offset: offset as u32,

0 commit comments

Comments
 (0)