From ab4bffa1dc775e8e63b19e7b900d40995999a849 Mon Sep 17 00:00:00 2001 From: Luke Yue Date: Sun, 19 Jan 2025 14:41:11 +0800 Subject: [PATCH] feat: add more APIs for reading device attribute Signed-off-by: Luke Yue --- src/ibverbs/device_context.rs | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/src/ibverbs/device_context.rs b/src/ibverbs/device_context.rs index 18c973c..750ab6c 100644 --- a/src/ibverbs/device_context.rs +++ b/src/ibverbs/device_context.rs @@ -45,6 +45,72 @@ impl From for Mtu { } } +#[repr(u8)] +#[derive(Debug, Clone, Copy)] +pub enum PortWidth { + Width1X = 1, + Width2X = 16, + Width4X = 2, + Width8X = 4, + Width12X = 8, +} + +impl From 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 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, } @@ -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 { @@ -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 {