]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
IB/core: Add helper to convert port attributes to data rate
authorOr Har-Toov <ohartoov@nvidia.com>
Thu, 18 Dec 2025 15:58:32 +0000 (17:58 +0200)
committerLeon Romanovsky <leon@kernel.org>
Mon, 5 Jan 2026 07:42:53 +0000 (02:42 -0500)
Introduce ib_port_attr_to_rate() to compute the data rate in 100 Mbps
units (deci-Gb/sec) from a port's active_speed and active_width
attributes. This generic helper removes duplicated speed-to-rate
calculations, which are used by sysfs and the upcoming new verb.

Signed-off-by: Or Har-Toov <ohartoov@nvidia.com>
Reviewed-by: Mark Bloch <mbloch@nvidia.com>
Signed-off-by: Edward Srouji <edwards@nvidia.com>
Signed-off-by: Leon Romanovsky <leon@kernel.org>
drivers/infiniband/core/verbs.c
include/rdma/ib_verbs.h

index f495a2182c84553e938245d309e9ddb691e9b98a..8b56b6b62352397b067bb79870a5576c3f0dab93 100644 (file)
@@ -217,6 +217,57 @@ __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
 }
 EXPORT_SYMBOL(ib_rate_to_mbps);
 
+struct ib_speed_attr {
+       const char *str;
+       int speed;
+};
+
+#define IB_SPEED_ATTR(speed_type, _str, _speed) \
+       [speed_type] = {.str = _str, .speed = _speed}
+
+static const struct ib_speed_attr ib_speed_attrs[] = {
+       IB_SPEED_ATTR(IB_SPEED_SDR, " SDR", 25),
+       IB_SPEED_ATTR(IB_SPEED_DDR, " DDR", 50),
+       IB_SPEED_ATTR(IB_SPEED_QDR, " QDR", 100),
+       IB_SPEED_ATTR(IB_SPEED_FDR10, " FDR10", 100),
+       IB_SPEED_ATTR(IB_SPEED_FDR, " FDR", 140),
+       IB_SPEED_ATTR(IB_SPEED_EDR, " EDR", 250),
+       IB_SPEED_ATTR(IB_SPEED_HDR, " HDR", 500),
+       IB_SPEED_ATTR(IB_SPEED_NDR, " NDR", 1000),
+       IB_SPEED_ATTR(IB_SPEED_XDR, " XDR", 2000),
+};
+
+int ib_port_attr_to_speed_info(struct ib_port_attr *attr,
+                              struct ib_port_speed_info *speed_info)
+{
+       int speed_idx = attr->active_speed;
+
+       switch (attr->active_speed) {
+       case IB_SPEED_DDR:
+       case IB_SPEED_QDR:
+       case IB_SPEED_FDR10:
+       case IB_SPEED_FDR:
+       case IB_SPEED_EDR:
+       case IB_SPEED_HDR:
+       case IB_SPEED_NDR:
+       case IB_SPEED_XDR:
+       case IB_SPEED_SDR:
+               break;
+       default:
+               speed_idx = IB_SPEED_SDR; /* Default to SDR for invalid rates */
+               break;
+       }
+
+       speed_info->str = ib_speed_attrs[speed_idx].str;
+       speed_info->rate = ib_speed_attrs[speed_idx].speed;
+       speed_info->rate *= ib_width_enum_to_int(attr->active_width);
+       if (speed_info->rate < 0)
+               return -EINVAL;
+
+       return 0;
+}
+EXPORT_SYMBOL(ib_port_attr_to_speed_info);
+
 __attribute_const__ enum rdma_transport_type
 rdma_node_get_transport(unsigned int node_type)
 {
index 95f1e557cbb8091a4c894622ad45f139af17215f..b984f9581a731347692166820808a3317167da5f 100644 (file)
@@ -878,6 +878,20 @@ __attribute_const__ int ib_rate_to_mult(enum ib_rate rate);
  */
 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate);
 
+struct ib_port_speed_info {
+       const char *str;
+       int rate;       /* in deci-Gb/sec (100 MBps units) */
+};
+
+/**
+ * ib_port_attr_to_speed_info - Convert port attributes to speed information
+ * @attr: Port attributes containing active_speed and active_width
+ * @speed_info: Speed information to return
+ *
+ * Returns 0 on success, -EINVAL on error.
+ */
+int ib_port_attr_to_speed_info(struct ib_port_attr *attr,
+                              struct ib_port_speed_info *speed_info);
 
 /**
  * enum ib_mr_type - memory region type