Skip to content

Commit

Permalink
feat: add more APIs for reading device attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Yue <[email protected]>
  • Loading branch information
dragonJACson committed Jan 19, 2025
1 parent 0605743 commit ab4bffa
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/ibverbs/device_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,72 @@ impl From<u32> for Mtu {
}
}

#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum PortWidth {
Width1X = 1,
Width2X = 16,
Width4X = 2,
Width8X = 4,
Width12X = 8,
}

impl From<u8> for PortWidth {
fn from(width: u8) -> Self {
match width {
1 => PortWidth::Width1X,
16 => PortWidth::Width2X,
2 => PortWidth::Width4X,
4 => PortWidth::Width8X,
8 => PortWidth::Width12X,
_ => panic!("Unknown port width value: {width}"),
}
}
}

impl PortWidth {
pub fn to_multiplier(&self) -> u8 {
match self {
PortWidth::Width1X => 1,
PortWidth::Width2X => 2,
PortWidth::Width4X => 4,
PortWidth::Width8X => 8,
PortWidth::Width12X => 12,
}
}
}

#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum PortSpeed {
SingleDataRate = 1,
DoubleDataRate = (1 << 1),
QuadrupleDataRate = (1 << 2),
FourteenDataRateTen = (1 << 3),
FourteenDataRate = (1 << 4),
EnhancedDataRate = (1 << 5),
HighDataRate = (1 << 6),
NextDataRate = (1 << 7),
ExtendedDataRate = (1 << 8),
}

impl From<u32> for PortSpeed {
fn from(speed: u32) -> Self {
match speed {
1 => PortSpeed::SingleDataRate,
2 => PortSpeed::DoubleDataRate,
4 => PortSpeed::QuadrupleDataRate,
8 => PortSpeed::FourteenDataRateTen,
16 => PortSpeed::FourteenDataRate,
32 => PortSpeed::EnhancedDataRate,
64 => PortSpeed::HighDataRate,
128 => PortSpeed::NextDataRate,
256 => PortSpeed::EnhancedDataRate,
_ => panic!("Unknown port speed value: {speed}"),
}
}
}

pub struct PortAttr {
attr: ibv_port_attr,
}
Expand All @@ -65,6 +131,18 @@ impl PortAttr {
pub fn link_layer(&self) -> u8 {
self.attr.link_layer
}

pub fn active_width(&self) -> PortWidth {
self.attr.active_width.into()
}

pub fn active_speed(&self) -> PortSpeed {
(self.attr.active_speed as u32).into()
}

pub fn active_speed_ex(&self) -> PortSpeed {
self.attr.active_speed_ex.into()
}
}

pub struct DeviceAttr {
Expand All @@ -79,6 +157,22 @@ impl DeviceAttr {
pub fn completion_timestamp_mask(&self) -> u64 {
self.attr.completion_timestamp_mask
}

pub fn vendor_id(&self) -> u32 {
self.attr.orig_attr.vendor_id
}

pub fn vendor_part_id(&self) -> u32 {
self.attr.orig_attr.vendor_part_id
}

pub fn firmware_version(&self) -> [i8; 64] {
self.attr.orig_attr.fw_ver
}

pub fn hardware_version(&self) -> u32 {
self.attr.orig_attr.hw_ver
}
}

impl Drop for DeviceContext {
Expand Down

0 comments on commit ab4bffa

Please sign in to comment.