Skip to content

Commit 2295ebd

Browse files
committed
pci: refactor: make BAR0_REG_IDX u16
This constant is only used as u16, so change it's type from u8 to u16 to avoid unnecessary conversions. Also rename with `_IDX` to have similarity with `reg_idx` we use in PCI code. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent 6643c3e commit 2295ebd

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/vmm/src/devices/virtio/transport/pci/device.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::devices::virtio::transport::pci::device_status::*;
3434
use crate::devices::virtio::transport::{VirtioInterrupt, VirtioInterruptType};
3535
use crate::logger::{debug, error, warn};
3636
use crate::pci::configuration::{
37-
BAR0_REG, Bars, NUM_BAR_REGS, PciCapability, PciConfiguration, PciConfigurationError,
37+
BAR0_REG_IDX, Bars, NUM_BAR_REGS, PciCapability, PciConfiguration, PciConfigurationError,
3838
PciConfigurationState,
3939
};
4040
use crate::pci::msix::{MsixCap, MsixConfig, MsixConfigState};
@@ -736,10 +736,10 @@ impl PciDevice for VirtioPciDevice {
736736
offset: u8,
737737
data: &[u8],
738738
) -> Option<Arc<Barrier>> {
739-
if u16::from(BAR0_REG) <= reg_idx && reg_idx < u16::from(BAR0_REG + NUM_BAR_REGS) {
740-
// reg_idx is in [BAR0_REG, BAR0_REG+NUM_BAR_REGS), so the difference is 0..5.
739+
if BAR0_REG_IDX <= reg_idx && reg_idx < BAR0_REG_IDX + u16::from(NUM_BAR_REGS) {
740+
// reg_idx is in [BAR0_REG_IDX, BAR0_REG_IDX+NUM_BAR_REGS), so the difference is 0..5.
741741
#[allow(clippy::cast_possible_truncation)]
742-
let bar_idx = (reg_idx - u16::from(BAR0_REG)) as u8;
742+
let bar_idx = (reg_idx - BAR0_REG_IDX) as u8;
743743
self.bars.write(bar_idx, offset, data);
744744
None
745745
} else {
@@ -768,10 +768,10 @@ impl PciDevice for VirtioPciDevice {
768768
}
769769

770770
fn read_config_register(&mut self, reg_idx: u16) -> u32 {
771-
if u16::from(BAR0_REG) <= reg_idx && reg_idx < u16::from(BAR0_REG + NUM_BAR_REGS) {
772-
// reg_idx is in [BAR0_REG, BAR0_REG+NUM_BAR_REGS), so the difference is 0..5.
771+
if BAR0_REG_IDX <= reg_idx && reg_idx < BAR0_REG_IDX + u16::from(NUM_BAR_REGS) {
772+
// reg_idx is in [BAR0_REG_IDX, BAR0_REG_IDX+NUM_BAR_REGS), so the difference is 0..5.
773773
#[allow(clippy::cast_possible_truncation)]
774-
let bar_idx = (reg_idx - u16::from(BAR0_REG)) as u8;
774+
let bar_idx = (reg_idx - BAR0_REG_IDX) as u8;
775775
let mut value: u32 = 0;
776776
self.bars.read(bar_idx, 0, value.as_mut_bytes());
777777
value

src/vmm/src/pci/configuration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const FIRST_CAPABILITY_OFFSET: u8 = 0x40;
3939
const CAPABILITY_MAX_OFFSET: u16 = 192;
4040

4141
/// First register in the BARs region
42-
pub const BAR0_REG: u8 = 4;
42+
pub const BAR0_REG_IDX: u16 = 4;
4343
/// Number of BAR registers
4444
pub const NUM_BAR_REGS: u8 = 6;
4545

0 commit comments

Comments
 (0)