From: Dragos Tatulea Add macros that can be used to exponentiate log based capabilities. When a capability can represent more than the used version (u32 , u64), value will be clamped to the largest representable power of two. Reviewed-by: Tariq Toukan Reviewed-by: Yishai Hadas Signed-off-by: Dragos Tatulea --- include/linux/mlx5/device.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/mlx5/device.h b/include/linux/mlx5/device.h index 37727b5ab78f..f45454f65d5c 100644 --- a/include/linux/mlx5/device.h +++ b/include/linux/mlx5/device.h @@ -38,6 +38,7 @@ #include #include #include +#include #if defined(__LITTLE_ENDIAN) #define MLX5_SET_HOST_ENDIANNESS 0 @@ -47,6 +48,16 @@ #error Host endianness not defined #endif +/* Largest shift count that is defined behaviour for a value of type @T. */ +#define __mlx5_max_shift(T) (BITS_PER_TYPE(T) - 1 - is_signed_type(T)) + +/* Calculate 2^@fld from a log2 cap field. Exponents that don't fit in @T + * saturate to the largest representable power of two (not to the type + * maximum), avoiding undefined shifts. + */ +#define MLX5_GET_LOGSZ_CLAMP(typ, p, fld, T) \ + ((T)((T)1 << min_t(u32, MLX5_GET(typ, p, fld), __mlx5_max_shift(T)))) + enum mlx5_inline_modes { MLX5_INLINE_MODE_NONE, MLX5_INLINE_MODE_L2, @@ -1096,6 +1107,15 @@ enum mlx5_qcam_feature_groups { #define MLX5_CAP_GEN_MAX(mdev, cap) \ MLX5_GET(cmd_hca_cap, mdev->caps.hca[MLX5_CAP_GENERAL]->max, cap) +/* 2^cap for log2-encoded general caps */ +#define MLX5_CAP_GEN_LOGSZ_U32(mdev, cap) \ + MLX5_GET_LOGSZ_CLAMP(cmd_hca_cap, \ + mdev->caps.hca[MLX5_CAP_GENERAL]->cur, cap, u32) + +#define MLX5_CAP_GEN_LOGSZ_U64(mdev, cap) \ + MLX5_GET_LOGSZ_CLAMP(cmd_hca_cap, \ + mdev->caps.hca[MLX5_CAP_GENERAL]->cur, cap, u64) + #define MLX5_CAP_GEN_2(mdev, cap) \ MLX5_GET(cmd_hca_cap_2, mdev->caps.hca[MLX5_CAP_GENERAL_2]->cur, cap) -- 2.18.1