net/mlx5: Refactor the encryption key creation
authorJianbo Liu <jianbol@nvidia.com>
Thu, 28 Jul 2022 03:59:29 +0000 (03:59 +0000)
committerSaeed Mahameed <saeedm@nvidia.com>
Tue, 31 Jan 2023 03:10:05 +0000 (19:10 -0800)
Move the common code to general functions which can be used by fast
update encryption key in later patches.

Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
drivers/net/ethernet/mellanox/mlx5/core/lib/crypto.c

index bc2a72491e10149a30d32d0e75a124e448cf384f..81fe5c3763a538490663ef961f95f1ed6a6d484f 100644 (file)
@@ -9,50 +9,79 @@ struct mlx5_crypto_dek_priv {
        int log_dek_obj_range;
 };
 
+static int mlx5_crypto_dek_get_key_sz(struct mlx5_core_dev *mdev,
+                                     u32 sz_bytes, u8 *key_sz_p)
+{
+       u32 sz_bits = sz_bytes * BITS_PER_BYTE;
+
+       switch (sz_bits) {
+       case 128:
+               *key_sz_p = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_KEY_SIZE_128;
+               break;
+       case 256:
+               *key_sz_p = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_KEY_SIZE_256;
+               break;
+       default:
+               mlx5_core_err(mdev, "Crypto offload error, invalid key size (%u bits)\n",
+                             sz_bits);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int mlx5_crypto_dek_fill_key(struct mlx5_core_dev *mdev, u8 *key_obj,
+                                   const void *key, u32 sz_bytes)
+{
+       void *dst;
+       u8 key_sz;
+       int err;
+
+       err = mlx5_crypto_dek_get_key_sz(mdev, sz_bytes, &key_sz);
+       if (err)
+               return err;
+
+       MLX5_SET(encryption_key_obj, key_obj, key_size, key_sz);
+
+       if (sz_bytes == 16)
+               /* For key size of 128b the MSBs are reserved. */
+               dst = MLX5_ADDR_OF(encryption_key_obj, key_obj, key[1]);
+       else
+               dst = MLX5_ADDR_OF(encryption_key_obj, key_obj, key);
+
+       memcpy(dst, key, sz_bytes);
+
+       return 0;
+}
+
 int mlx5_create_encryption_key(struct mlx5_core_dev *mdev,
                               const void *key, u32 sz_bytes,
                               u32 key_type, u32 *p_key_id)
 {
        u32 in[MLX5_ST_SZ_DW(create_encryption_key_in)] = {};
        u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
-       u32 sz_bits = sz_bytes * BITS_PER_BYTE;
-       u8  general_obj_key_size;
        u64 general_obj_types;
-       void *obj, *key_p;
+       void *obj;
        int err;
 
-       obj = MLX5_ADDR_OF(create_encryption_key_in, in, encryption_key_object);
-       key_p = MLX5_ADDR_OF(encryption_key_obj, obj, key);
-
        general_obj_types = MLX5_CAP_GEN_64(mdev, general_obj_types);
        if (!(general_obj_types &
              MLX5_HCA_CAP_GENERAL_OBJECT_TYPES_ENCRYPTION_KEY))
                return -EINVAL;
 
-       switch (sz_bits) {
-       case 128:
-               general_obj_key_size =
-                       MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_KEY_SIZE_128;
-               key_p += sz_bytes;
-               break;
-       case 256:
-               general_obj_key_size =
-                       MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_KEY_SIZE_256;
-               break;
-       default:
-               return -EINVAL;
-       }
-
-       memcpy(key_p, key, sz_bytes);
-
-       MLX5_SET(encryption_key_obj, obj, key_size, general_obj_key_size);
-       MLX5_SET(encryption_key_obj, obj, key_purpose, key_type);
        MLX5_SET(general_obj_in_cmd_hdr, in, opcode,
                 MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
        MLX5_SET(general_obj_in_cmd_hdr, in, obj_type,
                 MLX5_GENERAL_OBJECT_TYPES_ENCRYPTION_KEY);
+
+       obj = MLX5_ADDR_OF(create_encryption_key_in, in, encryption_key_object);
+       MLX5_SET(encryption_key_obj, obj, key_purpose, key_type);
        MLX5_SET(encryption_key_obj, obj, pd, mdev->mlx5e_res.hw_objs.pdn);
 
+       err = mlx5_crypto_dek_fill_key(mdev, obj, key, sz_bytes);
+       if (err)
+               return err;
+
        err = mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
        if (!err)
                *p_key_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);