Skip to content

Commit 473e3ff

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 9096b79 commit 473e3ff

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

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::common_config::{
3434
use crate::devices::virtio::transport::{VirtioInterrupt, VirtioInterruptType};
3535
use crate::logger::{debug, error};
3636
use crate::pci::configuration::{
37-
BAR0_REG, Bars, NUM_BAR_REGS, PciCapability, PciConfiguration, PciConfigurationState,
37+
BAR0_REG_IDX, Bars, NUM_BAR_REGS, PciCapability, PciConfiguration, PciConfigurationState,
3838
};
3939
use crate::pci::msix::{MsixCap, MsixConfig, MsixConfigState};
4040
use crate::pci::{
@@ -740,10 +740,10 @@ impl PciDevice for VirtioPciDevice {
740740
offset: u8,
741741
data: &[u8],
742742
) -> Option<Arc<Barrier>> {
743-
if u16::from(BAR0_REG) <= reg_idx && reg_idx < u16::from(BAR0_REG + NUM_BAR_REGS) {
744-
// reg_idx is in [BAR0_REG, BAR0_REG+NUM_BAR_REGS), so the difference is 0..5.
743+
if BAR0_REG_IDX <= reg_idx && reg_idx < BAR0_REG_IDX + u16::from(NUM_BAR_REGS) {
744+
// reg_idx is in [BAR0_REG_IDX, BAR0_REG_IDX+NUM_BAR_REGS), so the difference is 0..5.
745745
#[allow(clippy::cast_possible_truncation)]
746-
let bar_idx = (reg_idx - u16::from(BAR0_REG)) as u8;
746+
let bar_idx = (reg_idx - BAR0_REG_IDX) as u8;
747747
self.bars.write(bar_idx, offset, data);
748748
None
749749
} else {
@@ -772,10 +772,10 @@ impl PciDevice for VirtioPciDevice {
772772
}
773773

774774
fn read_config_register(&mut self, reg_idx: u16) -> u32 {
775-
if u16::from(BAR0_REG) <= reg_idx && reg_idx < u16::from(BAR0_REG + NUM_BAR_REGS) {
776-
// reg_idx is in [BAR0_REG, BAR0_REG+NUM_BAR_REGS), so the difference is 0..5.
775+
if BAR0_REG_IDX <= reg_idx && reg_idx < BAR0_REG_IDX + u16::from(NUM_BAR_REGS) {
776+
// reg_idx is in [BAR0_REG_IDX, BAR0_REG_IDX+NUM_BAR_REGS), so the difference is 0..5.
777777
#[allow(clippy::cast_possible_truncation)]
778-
let bar_idx = (reg_idx - u16::from(BAR0_REG)) as u8;
778+
let bar_idx = (reg_idx - BAR0_REG_IDX) as u8;
779779
let mut value: u32 = 0;
780780
self.bars.read(bar_idx, 0, value.as_mut_bytes());
781781
value

src/vmm/src/pci/configuration.rs

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

2828
/// First register in the BARs region
29-
pub const BAR0_REG: u8 = 4;
29+
pub const BAR0_REG_IDX: u16 = 4;
3030
/// Number of BAR registers
3131
pub const NUM_BAR_REGS: u8 = 6;
3232

0 commit comments

Comments
 (0)