Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2023 NVIDIA Corporation & Affiliates
3 : : */
4 : :
5 : : #include <rte_malloc.h>
6 : : #include <rte_mempool.h>
7 : : #include <rte_eal_paging.h>
8 : : #include <rte_errno.h>
9 : : #include <rte_log.h>
10 : : #include <bus_pci_driver.h>
11 : : #include <rte_memory.h>
12 : : #include <rte_io.h>
13 : :
14 : : #include <mlx5_glue.h>
15 : : #include <mlx5_common.h>
16 : : #include <mlx5_devx_cmds.h>
17 : : #include <mlx5_common_os.h>
18 : :
19 : : #include "mlx5_crypto_utils.h"
20 : : #include "mlx5_crypto.h"
21 : :
22 : : /*
23 : : * AES-GCM uses indirect KLM mode. The UMR WQE comprises of WQE control +
24 : : * UMR control + mkey context + indirect KLM. The WQE size is aligned to
25 : : * be 3 WQEBBS.
26 : : */
27 : : #define MLX5_UMR_GCM_WQE_SIZE \
28 : : (RTE_ALIGN(sizeof(struct mlx5_umr_wqe) + sizeof(struct mlx5_wqe_dseg), \
29 : : MLX5_SEND_WQE_BB))
30 : :
31 : : #define MLX5_UMR_GCM_WQE_SET_SIZE \
32 : : (MLX5_UMR_GCM_WQE_SIZE + \
33 : : RTE_ALIGN(sizeof(struct mlx5_wqe_send_en_wqe), \
34 : : MLX5_SEND_WQE_BB))
35 : :
36 : : #define MLX5_UMR_GCM_WQE_STRIDE \
37 : : (MLX5_UMR_GCM_WQE_SIZE / MLX5_SEND_WQE_BB)
38 : :
39 : : #define MLX5_MMO_CRYPTO_OPC (MLX5_OPCODE_MMO | \
40 : : (MLX5_OPC_MOD_MMO_CRYPTO << WQE_CSEG_OPC_MOD_OFFSET))
41 : :
42 : : /*
43 : : * The status default value is RTE_CRYPTO_OP_STATUS_SUCCESS.
44 : : * Copy tag should fill different value to status.
45 : : */
46 : : #define MLX5_CRYPTO_OP_STATUS_GCM_TAG_COPY (RTE_CRYPTO_OP_STATUS_SUCCESS + 1)
47 : :
48 : : struct mlx5_crypto_gcm_op_info {
49 : : bool need_umr;
50 : : bool is_oop;
51 : : bool is_enc;
52 : : void *digest;
53 : : void *src_addr;
54 : : };
55 : :
56 : : struct mlx5_crypto_gcm_data {
57 : : void *src_addr;
58 : : uint32_t src_bytes;
59 : : void *dst_addr;
60 : : uint32_t dst_bytes;
61 : : uint32_t src_mkey;
62 : : uint32_t dst_mkey;
63 : : };
64 : :
65 : : struct mlx5_crypto_gcm_tag_cpy_info {
66 : : void *digest;
67 : : uint8_t tag_len;
68 : : } __rte_packed;
69 : :
70 : : static struct rte_cryptodev_capabilities mlx5_crypto_gcm_caps[] = {
71 : : {
72 : : .op = RTE_CRYPTO_OP_TYPE_UNDEFINED,
73 : : },
74 : : {
75 : : .op = RTE_CRYPTO_OP_TYPE_UNDEFINED,
76 : : }
77 : : };
78 : :
79 : : int
80 : 0 : mlx5_crypto_dek_fill_gcm_attr(struct mlx5_crypto_dek *dek,
81 : : struct mlx5_devx_dek_attr *dek_attr,
82 : : void *cb_ctx)
83 : : {
84 : : uint32_t offset = 0;
85 : : struct mlx5_crypto_dek_ctx *ctx = cb_ctx;
86 : 0 : struct rte_crypto_aead_xform *aead_ctx = &ctx->xform->aead;
87 : :
88 [ # # ]: 0 : if (aead_ctx->algo != RTE_CRYPTO_AEAD_AES_GCM) {
89 : 0 : DRV_LOG(ERR, "Only AES-GCM algo supported.");
90 : 0 : return -EINVAL;
91 : : }
92 : 0 : dek_attr->key_purpose = MLX5_CRYPTO_KEY_PURPOSE_GCM;
93 [ # # # ]: 0 : switch (aead_ctx->key.length) {
94 : 0 : case 16:
95 : : offset = 16;
96 : 0 : dek->size = 16;
97 : 0 : dek_attr->key_size = MLX5_CRYPTO_KEY_SIZE_128b;
98 : 0 : break;
99 : 0 : case 32:
100 : 0 : dek->size = 32;
101 : 0 : dek_attr->key_size = MLX5_CRYPTO_KEY_SIZE_256b;
102 : 0 : break;
103 : 0 : default:
104 : 0 : DRV_LOG(ERR, "Wrapped key size not supported.");
105 : 0 : return -EINVAL;
106 : : }
107 : 0 : memcpy(&dek_attr->key[offset], aead_ctx->key.data, aead_ctx->key.length);
108 : 0 : memcpy(&dek->data, aead_ctx->key.data, aead_ctx->key.length);
109 : 0 : return 0;
110 : : }
111 : :
112 : : static int
113 : 0 : mlx5_crypto_generate_gcm_cap(struct mlx5_hca_crypto_mmo_attr *mmo_attr,
114 : : struct rte_cryptodev_capabilities *cap)
115 : : {
116 : : /* Init key size. */
117 : 0 : if (mmo_attr->gcm_128_encrypt && mmo_attr->gcm_128_decrypt &&
118 [ # # ]: 0 : mmo_attr->gcm_256_encrypt && mmo_attr->gcm_256_decrypt) {
119 : 0 : cap->sym.aead.key_size.min = 16;
120 : 0 : cap->sym.aead.key_size.max = 32;
121 : 0 : cap->sym.aead.key_size.increment = 16;
122 [ # # ]: 0 : } else if (mmo_attr->gcm_256_encrypt && mmo_attr->gcm_256_decrypt) {
123 : 0 : cap->sym.aead.key_size.min = 32;
124 : 0 : cap->sym.aead.key_size.max = 32;
125 : 0 : cap->sym.aead.key_size.increment = 0;
126 [ # # ]: 0 : } else if (mmo_attr->gcm_128_encrypt && mmo_attr->gcm_128_decrypt) {
127 : 0 : cap->sym.aead.key_size.min = 16;
128 : 0 : cap->sym.aead.key_size.max = 16;
129 : 0 : cap->sym.aead.key_size.increment = 0;
130 : : } else {
131 : 0 : DRV_LOG(ERR, "No available AES-GCM encryption/decryption supported.");
132 : 0 : return -1;
133 : : }
134 : : /* Init tag size. */
135 [ # # ]: 0 : if (mmo_attr->gcm_auth_tag_128 && mmo_attr->gcm_auth_tag_96) {
136 : 0 : cap->sym.aead.digest_size.min = 12;
137 : 0 : cap->sym.aead.digest_size.max = 16;
138 : 0 : cap->sym.aead.digest_size.increment = 4;
139 [ # # ]: 0 : } else if (mmo_attr->gcm_auth_tag_96) {
140 : 0 : cap->sym.aead.digest_size.min = 12;
141 : 0 : cap->sym.aead.digest_size.max = 12;
142 : 0 : cap->sym.aead.digest_size.increment = 0;
143 [ # # ]: 0 : } else if (mmo_attr->gcm_auth_tag_128) {
144 : 0 : cap->sym.aead.digest_size.min = 16;
145 : 0 : cap->sym.aead.digest_size.max = 16;
146 : 0 : cap->sym.aead.digest_size.increment = 0;
147 : : } else {
148 : 0 : DRV_LOG(ERR, "No available AES-GCM tag size supported.");
149 : 0 : return -1;
150 : : }
151 : : /* Init AAD size. */
152 : 0 : cap->sym.aead.aad_size.min = 0;
153 : 0 : cap->sym.aead.aad_size.max = UINT16_MAX;
154 : 0 : cap->sym.aead.aad_size.increment = 1;
155 : : /* Init IV size. */
156 : 0 : cap->sym.aead.iv_size.min = 12;
157 : 0 : cap->sym.aead.iv_size.max = 12;
158 : 0 : cap->sym.aead.iv_size.increment = 0;
159 : : /* Init left items. */
160 : 0 : cap->op = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
161 : 0 : cap->sym.xform_type = RTE_CRYPTO_SYM_XFORM_AEAD;
162 : 0 : cap->sym.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
163 : 0 : return 0;
164 : : }
165 : :
166 : : static int
167 : 0 : mlx5_crypto_sym_gcm_session_configure(struct rte_cryptodev *dev,
168 : : struct rte_crypto_sym_xform *xform,
169 : : struct rte_cryptodev_sym_session *session)
170 : : {
171 : 0 : struct mlx5_crypto_priv *priv = dev->data->dev_private;
172 : 0 : struct mlx5_crypto_session *sess_private_data = CRYPTODEV_GET_SYM_SESS_PRIV(session);
173 : : struct rte_crypto_aead_xform *aead = &xform->aead;
174 : : uint32_t op_type;
175 : :
176 [ # # ]: 0 : if (unlikely(xform->next != NULL)) {
177 : 0 : DRV_LOG(ERR, "Xform next is not supported.");
178 : 0 : return -ENOTSUP;
179 : : }
180 [ # # ]: 0 : if (aead->algo != RTE_CRYPTO_AEAD_AES_GCM) {
181 : 0 : DRV_LOG(ERR, "Only AES-GCM algorithm is supported.");
182 : 0 : return -ENOTSUP;
183 : : }
184 [ # # ]: 0 : if (aead->op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
185 : : op_type = MLX5_CRYPTO_OP_TYPE_ENCRYPTION;
186 : : else
187 : : op_type = MLX5_CRYPTO_OP_TYPE_DECRYPTION;
188 : 0 : sess_private_data->op_type = op_type;
189 [ # # ]: 0 : sess_private_data->mmo_ctrl = rte_cpu_to_be_32
190 : : (op_type << MLX5_CRYPTO_MMO_OP_OFFSET |
191 : : MLX5_ENCRYPTION_TYPE_AES_GCM << MLX5_CRYPTO_MMO_TYPE_OFFSET);
192 [ # # ]: 0 : sess_private_data->wqe_aad_len = rte_cpu_to_be_32((uint32_t)aead->aad_length);
193 [ # # ]: 0 : sess_private_data->wqe_tag_len = rte_cpu_to_be_32((uint32_t)aead->digest_length);
194 : 0 : sess_private_data->aad_len = aead->aad_length;
195 : 0 : sess_private_data->tag_len = aead->digest_length;
196 : 0 : sess_private_data->iv_offset = aead->iv.offset;
197 : 0 : sess_private_data->iv_len = aead->iv.length;
198 : 0 : sess_private_data->dek = mlx5_crypto_dek_prepare(priv, xform);
199 [ # # ]: 0 : if (sess_private_data->dek == NULL) {
200 : 0 : DRV_LOG(ERR, "Failed to prepare dek.");
201 : 0 : return -ENOMEM;
202 : : }
203 : 0 : sess_private_data->dek_id =
204 [ # # ]: 0 : rte_cpu_to_be_32(sess_private_data->dek->obj->id &
205 : : 0xffffff);
206 : 0 : DRV_LOG(DEBUG, "Session %p was configured.", sess_private_data);
207 : 0 : return 0;
208 : : }
209 : :
210 : : static void *
211 : 0 : mlx5_crypto_gcm_mkey_klm_update(struct mlx5_crypto_priv *priv,
212 : : struct mlx5_crypto_qp *qp __rte_unused,
213 : : uint32_t idx)
214 : : {
215 : 0 : return &qp->klm_array[idx * priv->max_klm_num];
216 : : }
217 : :
218 : : static int
219 : 0 : mlx5_crypto_gcm_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
220 : : {
221 : 0 : struct mlx5_crypto_priv *priv = dev->data->dev_private;
222 : 0 : struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
223 : :
224 [ # # ]: 0 : if (qp->umr_qp_obj.qp != NULL)
225 : 0 : mlx5_devx_qp_destroy(&qp->umr_qp_obj);
226 [ # # ]: 0 : if (qp->qp_obj.qp != NULL)
227 : 0 : mlx5_devx_qp_destroy(&qp->qp_obj);
228 [ # # ]: 0 : if (qp->cq_obj.cq != NULL)
229 : 0 : mlx5_devx_cq_destroy(&qp->cq_obj);
230 [ # # ]: 0 : if (qp->mr.obj != NULL) {
231 : 0 : void *opaq = qp->mr.addr;
232 : :
233 : 0 : priv->dereg_mr_cb(&qp->mr);
234 : 0 : rte_free(opaq);
235 : : }
236 : 0 : mlx5_crypto_indirect_mkeys_release(qp, qp->entries_n);
237 : 0 : mlx5_mr_btree_free(&qp->mr_ctrl.cache_bh);
238 : 0 : rte_free(qp);
239 : 0 : dev->data->queue_pairs[qp_id] = NULL;
240 : 0 : return 0;
241 : : }
242 : :
243 : : static void
244 : 0 : mlx5_crypto_gcm_init_qp(struct mlx5_crypto_qp *qp)
245 : : {
246 : 0 : volatile struct mlx5_gga_wqe *restrict wqe =
247 : : (volatile struct mlx5_gga_wqe *)qp->qp_obj.wqes;
248 : 0 : volatile union mlx5_gga_crypto_opaque *opaq = qp->opaque_addr;
249 [ # # ]: 0 : const uint32_t sq_ds = rte_cpu_to_be_32((qp->qp_obj.qp->id << 8) | 4u);
250 : : const uint32_t flags = RTE_BE32(MLX5_COMP_ALWAYS <<
251 : : MLX5_COMP_MODE_OFFSET);
252 [ # # ]: 0 : const uint32_t opaq_lkey = rte_cpu_to_be_32(qp->mr.lkey);
253 : : int i;
254 : :
255 : : /* All the next fields state should stay constant. */
256 [ # # ]: 0 : for (i = 0; i < qp->entries_n; ++i, ++wqe) {
257 : 0 : wqe->sq_ds = sq_ds;
258 : 0 : wqe->flags = flags;
259 : 0 : wqe->opaque_lkey = opaq_lkey;
260 : 0 : wqe->opaque_vaddr = rte_cpu_to_be_64((uint64_t)(uintptr_t)&opaq[i]);
261 : : }
262 : 0 : }
263 : :
264 : : static inline int
265 : 0 : mlx5_crypto_gcm_umr_qp_setup(struct rte_cryptodev *dev, struct mlx5_crypto_qp *qp,
266 : : int socket_id)
267 : : {
268 : 0 : struct mlx5_crypto_priv *priv = dev->data->dev_private;
269 : 0 : struct mlx5_devx_qp_attr attr = {0};
270 : : uint32_t ret;
271 : : uint32_t log_wqbb_n;
272 : :
273 : : /* Set UMR + SEND_EN WQE as maximum same with crypto. */
274 [ # # ]: 0 : log_wqbb_n = rte_log2_u32(qp->entries_n *
275 : : (MLX5_UMR_GCM_WQE_SET_SIZE / MLX5_SEND_WQE_BB));
276 : 0 : attr.pd = priv->cdev->pdn;
277 [ # # ]: 0 : attr.uar_index = mlx5_os_get_devx_uar_page_id(priv->uar.obj);
278 : 0 : attr.cqn = qp->cq_obj.cq->id;
279 : : attr.num_of_receive_wqes = 0;
280 : 0 : attr.num_of_send_wqbbs = RTE_BIT32(log_wqbb_n);
281 : 0 : attr.ts_format =
282 : 0 : mlx5_ts_format_conv(priv->cdev->config.hca_attr.qp_ts_format);
283 : 0 : attr.cd_master = 1;
284 : 0 : ret = mlx5_devx_qp_create(priv->cdev->ctx, &qp->umr_qp_obj,
285 : 0 : attr.num_of_send_wqbbs * MLX5_SEND_WQE_BB,
286 : : &attr, socket_id);
287 [ # # ]: 0 : if (ret) {
288 : 0 : DRV_LOG(ERR, "Failed to create UMR QP.");
289 : 0 : return -1;
290 : : }
291 [ # # ]: 0 : if (mlx5_devx_qp2rts(&qp->umr_qp_obj, qp->umr_qp_obj.qp->id)) {
292 : 0 : DRV_LOG(ERR, "Failed to change UMR QP state to RTS.");
293 : 0 : return -1;
294 : : }
295 : : /* Save the UMR WQEBBS for checking the WQE boundary. */
296 : 0 : qp->umr_wqbbs = attr.num_of_send_wqbbs;
297 : 0 : return 0;
298 : : }
299 : :
300 : : static int
301 : 0 : mlx5_crypto_gcm_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
302 : : const struct rte_cryptodev_qp_conf *qp_conf,
303 : : int socket_id)
304 : : {
305 : 0 : struct mlx5_crypto_priv *priv = dev->data->dev_private;
306 : 0 : struct mlx5_hca_attr *attr = &priv->cdev->config.hca_attr;
307 : : struct mlx5_crypto_qp *qp;
308 : 0 : struct mlx5_devx_cq_attr cq_attr = {
309 [ # # ]: 0 : .uar_page_id = mlx5_os_get_devx_uar_page_id(priv->uar.obj),
310 : : };
311 : 0 : struct mlx5_devx_qp_attr qp_attr = {
312 : 0 : .pd = priv->cdev->pdn,
313 : 0 : .uar_index = mlx5_os_get_devx_uar_page_id(priv->uar.obj),
314 : : .user_index = qp_id,
315 : : };
316 : 0 : struct mlx5_devx_mkey_attr mkey_attr = {
317 : : .pd = priv->cdev->pdn,
318 : : .umr_en = 1,
319 : 0 : .klm_num = priv->max_klm_num,
320 : : };
321 [ # # ]: 0 : uint32_t log_ops_n = rte_log2_u32(qp_conf->nb_descriptors);
322 : 0 : uint32_t entries = RTE_BIT32(log_ops_n);
323 : : uint32_t alloc_size = sizeof(*qp);
324 : : size_t mr_size, opaq_size;
325 : : void *mr_buf;
326 : : int ret;
327 : :
328 : : alloc_size = RTE_ALIGN(alloc_size, RTE_CACHE_LINE_SIZE);
329 : 0 : alloc_size += (sizeof(struct rte_crypto_op *) +
330 : 0 : sizeof(struct mlx5_devx_obj *)) * entries;
331 : 0 : qp = rte_zmalloc_socket(__func__, alloc_size, RTE_CACHE_LINE_SIZE,
332 : : socket_id);
333 [ # # ]: 0 : if (qp == NULL) {
334 : 0 : DRV_LOG(ERR, "Failed to allocate qp memory.");
335 : 0 : rte_errno = ENOMEM;
336 : 0 : return -rte_errno;
337 : : }
338 : 0 : qp->priv = priv;
339 : 0 : qp->entries_n = entries;
340 [ # # ]: 0 : if (mlx5_mr_ctrl_init(&qp->mr_ctrl, &priv->cdev->mr_scache.dev_gen,
341 : : priv->dev_config.socket_id)) {
342 : 0 : DRV_LOG(ERR, "Cannot allocate MR Btree for qp %u.",
343 : : (uint32_t)qp_id);
344 : 0 : rte_errno = ENOMEM;
345 : 0 : goto err;
346 : : }
347 : : /*
348 : : * The following KLM pointer must be aligned with
349 : : * MLX5_UMR_KLM_PTR_ALIGN. Aligned opaq_size here
350 : : * to make the KLM pointer with offset be aligned.
351 : : */
352 : 0 : opaq_size = RTE_ALIGN(sizeof(union mlx5_gga_crypto_opaque) * entries,
353 : : MLX5_UMR_KLM_PTR_ALIGN);
354 : 0 : mr_size = (priv->max_klm_num * sizeof(struct mlx5_klm) * entries) + opaq_size;
355 : 0 : mr_buf = rte_calloc(__func__, (size_t)1, mr_size, MLX5_UMR_KLM_PTR_ALIGN);
356 [ # # ]: 0 : if (mr_buf == NULL) {
357 : 0 : DRV_LOG(ERR, "Failed to allocate mr memory.");
358 : 0 : rte_errno = ENOMEM;
359 : 0 : goto err;
360 : : }
361 [ # # ]: 0 : if (priv->reg_mr_cb(priv->cdev->pd, mr_buf, mr_size, &qp->mr) != 0) {
362 : 0 : rte_free(mr_buf);
363 : 0 : DRV_LOG(ERR, "Failed to register opaque MR.");
364 : 0 : rte_errno = ENOMEM;
365 : 0 : goto err;
366 : : }
367 : 0 : qp->opaque_addr = qp->mr.addr;
368 : 0 : qp->klm_array = RTE_PTR_ADD(qp->opaque_addr, opaq_size);
369 : : /*
370 : : * Triple the CQ size as UMR QP which contains UMR and SEND_EN WQE
371 : : * will share this CQ .
372 : : */
373 [ # # ]: 0 : qp->cq_entries_n = rte_align32pow2(entries * 3);
374 : 0 : ret = mlx5_devx_cq_create(priv->cdev->ctx, &qp->cq_obj,
375 [ # # ]: 0 : rte_log2_u32(qp->cq_entries_n),
376 : : &cq_attr, socket_id);
377 [ # # ]: 0 : if (ret != 0) {
378 : 0 : DRV_LOG(ERR, "Failed to create CQ.");
379 : 0 : goto err;
380 : : }
381 : 0 : qp_attr.cqn = qp->cq_obj.cq->id;
382 : 0 : qp_attr.ts_format = mlx5_ts_format_conv(attr->qp_ts_format);
383 : 0 : qp_attr.num_of_receive_wqes = 0;
384 : 0 : qp_attr.num_of_send_wqbbs = entries;
385 : 0 : qp_attr.mmo = attr->crypto_mmo.crypto_mmo_qp;
386 : : /* Set MMO QP as follower as the input data may depend on UMR. */
387 : 0 : qp_attr.cd_slave_send = 1;
388 : 0 : ret = mlx5_devx_qp_create(priv->cdev->ctx, &qp->qp_obj,
389 : 0 : qp_attr.num_of_send_wqbbs * MLX5_WQE_SIZE,
390 : : &qp_attr, socket_id);
391 [ # # ]: 0 : if (ret != 0) {
392 : 0 : DRV_LOG(ERR, "Failed to create QP.");
393 : 0 : goto err;
394 : : }
395 : 0 : mlx5_crypto_gcm_init_qp(qp);
396 : 0 : ret = mlx5_devx_qp2rts(&qp->qp_obj, 0);
397 [ # # ]: 0 : if (ret)
398 : 0 : goto err;
399 : 0 : qp->ops = (struct rte_crypto_op **)(qp + 1);
400 : 0 : qp->mkey = (struct mlx5_devx_obj **)(qp->ops + entries);
401 [ # # ]: 0 : if (mlx5_crypto_gcm_umr_qp_setup(dev, qp, socket_id)) {
402 : 0 : DRV_LOG(ERR, "Failed to setup UMR QP.");
403 : 0 : goto err;
404 : : }
405 : 0 : DRV_LOG(INFO, "QP %u: SQN=0x%X CQN=0x%X entries num = %u",
406 : : (uint32_t)qp_id, qp->qp_obj.qp->id, qp->cq_obj.cq->id, entries);
407 [ # # ]: 0 : if (mlx5_crypto_indirect_mkeys_prepare(priv, qp, &mkey_attr,
408 : : mlx5_crypto_gcm_mkey_klm_update)) {
409 : 0 : DRV_LOG(ERR, "Cannot allocate indirect memory regions.");
410 : 0 : rte_errno = ENOMEM;
411 : 0 : goto err;
412 : : }
413 : 0 : dev->data->queue_pairs[qp_id] = qp;
414 : 0 : return 0;
415 : 0 : err:
416 : 0 : mlx5_crypto_gcm_qp_release(dev, qp_id);
417 : 0 : return -1;
418 : : }
419 : :
420 : : static __rte_always_inline void
421 : : mlx5_crypto_gcm_get_op_info(struct mlx5_crypto_qp *qp,
422 : : struct rte_crypto_op *op,
423 : : struct mlx5_crypto_gcm_op_info *op_info)
424 : : {
425 : : struct mlx5_crypto_session *sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
426 : 0 : struct rte_mbuf *m_src = op->sym->m_src;
427 : 0 : void *aad_addr = op->sym->aead.aad.data;
428 : 0 : void *tag_addr = op->sym->aead.digest.data;
429 : 0 : void *src_addr = rte_pktmbuf_mtod_offset(m_src, void *, op->sym->aead.data.offset);
430 : : struct rte_mbuf *m_dst = m_src;
431 : : void *dst_addr = src_addr;
432 : : void *expected_aad = NULL;
433 : : void *expected_tag = NULL;
434 : 0 : bool is_enc = sess->op_type == MLX5_CRYPTO_OP_TYPE_ENCRYPTION;
435 : : bool cp_aad = false;
436 : : bool cp_tag = false;
437 : :
438 : : op_info->is_oop = false;
439 : : op_info->need_umr = false;
440 : : op_info->is_enc = is_enc;
441 : : op_info->digest = NULL;
442 : : op_info->src_addr = aad_addr;
443 [ # # ]: 0 : if (op->sym->m_dst && op->sym->m_dst != m_src) {
444 : : op_info->is_oop = true;
445 : : m_dst = op->sym->m_dst;
446 : 0 : dst_addr = rte_pktmbuf_mtod_offset(m_dst, void *, op->sym->aead.data.offset);
447 [ # # ]: 0 : if (m_dst->nb_segs > 1) {
448 : : op_info->need_umr = true;
449 : : return;
450 : : }
451 : : /*
452 : : * If the op's mbuf has extra data offset, don't copy AAD to
453 : : * this area.
454 : : */
455 [ # # # # ]: 0 : if (rte_pktmbuf_headroom(m_dst) < sess->aad_len ||
456 : : op->sym->aead.data.offset) {
457 : : op_info->need_umr = true;
458 : : return;
459 : : }
460 : : }
461 [ # # ]: 0 : if (m_src->nb_segs > 1) {
462 : : op_info->need_umr = true;
463 : : return;
464 : : }
465 : 0 : expected_aad = RTE_PTR_SUB(src_addr, sess->aad_len);
466 [ # # ]: 0 : if (expected_aad != aad_addr) {
467 : : /*
468 : : * If the op's mbuf has extra data offset, don't copy AAD to
469 : : * this area.
470 : : */
471 [ # # # # ]: 0 : if (sess->aad_len > MLX5_CRYPTO_GCM_MAX_AAD ||
472 [ # # ]: 0 : sess->aad_len > rte_pktmbuf_headroom(m_src) ||
473 : : op->sym->aead.data.offset) {
474 : : op_info->need_umr = true;
475 : : return;
476 : : }
477 : : cp_aad = true;
478 : : op_info->src_addr = expected_aad;
479 : : }
480 [ # # ]: 0 : expected_tag = RTE_PTR_ADD(is_enc ? dst_addr : src_addr, op->sym->aead.data.length);
481 [ # # ]: 0 : if (expected_tag != tag_addr) {
482 [ # # ]: 0 : struct rte_mbuf *mbuf = is_enc ? m_dst : m_src;
483 : :
484 : : /*
485 : : * If op's mbuf is not fully set as payload, don't copy digest to
486 : : * the left area.
487 : : */
488 [ # # ]: 0 : if (rte_pktmbuf_tailroom(mbuf) < sess->tag_len ||
489 [ # # ]: 0 : rte_pktmbuf_data_len(mbuf) != op->sym->aead.data.length) {
490 : : op_info->need_umr = true;
491 : : return;
492 : : }
493 [ # # ]: 0 : if (is_enc) {
494 : : op_info->digest = expected_tag;
495 : 0 : qp->cpy_tag_op++;
496 : : } else {
497 : : cp_tag = true;
498 : : }
499 : : }
500 [ # # ]: 0 : if (cp_aad)
501 : : memcpy(expected_aad, aad_addr, sess->aad_len);
502 [ # # ]: 0 : if (cp_tag)
503 : 0 : memcpy(expected_tag, tag_addr, sess->tag_len);
504 : : }
505 : :
506 : : static __rte_always_inline uint32_t
507 : : _mlx5_crypto_gcm_umr_build_mbuf_klm(struct mlx5_crypto_qp *qp,
508 : : struct rte_mbuf *mbuf,
509 : : struct mlx5_klm *klm,
510 : : uint32_t offset,
511 : : uint32_t *remain)
512 : : {
513 : 0 : uint32_t data_len = (rte_pktmbuf_data_len(mbuf) - offset);
514 : 0 : uintptr_t addr = rte_pktmbuf_mtod_offset(mbuf, uintptr_t, offset);
515 : :
516 : : if (data_len > *remain)
517 : : data_len = *remain;
518 : 0 : *remain -= data_len;
519 : 0 : klm->byte_count = rte_cpu_to_be_32(data_len);
520 [ # # # # : 0 : klm->address = rte_cpu_to_be_64(addr);
# # # # #
# # # # #
# # ]
521 : 0 : klm->mkey = mlx5_mr_mb2mr(&qp->mr_ctrl, mbuf);
522 : : return klm->mkey;
523 : : }
524 : :
525 : : static __rte_always_inline int
526 : : mlx5_crypto_gcm_build_mbuf_chain_klms(struct mlx5_crypto_qp *qp,
527 : : struct rte_crypto_op *op,
528 : : struct rte_mbuf *mbuf,
529 : : struct mlx5_klm *klm)
530 : : {
531 : 0 : uint32_t remain_len = op->sym->aead.data.length;
532 : : __rte_unused uint32_t nb_segs = mbuf->nb_segs;
533 : : uint32_t klm_n = 0;
534 : :
535 : : /* mbuf seg num should be less than max_segs_num. */
536 : : MLX5_ASSERT(nb_segs <= qp->priv->max_segs_num);
537 : : /* First mbuf needs to take the data offset. */
538 [ # # # # ]: 0 : if (unlikely(_mlx5_crypto_gcm_umr_build_mbuf_klm(qp, mbuf, klm,
539 : : op->sym->aead.data.offset, &remain_len) == UINT32_MAX)) {
540 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
541 : 0 : return 0;
542 : : }
543 : 0 : klm++;
544 : : klm_n++;
545 [ # # # # ]: 0 : while (remain_len) {
546 : : nb_segs--;
547 [ # # # # ]: 0 : mbuf = mbuf->next;
548 : : MLX5_ASSERT(mbuf && nb_segs);
549 [ # # # # ]: 0 : if (unlikely(_mlx5_crypto_gcm_umr_build_mbuf_klm(qp, mbuf, klm,
550 : : 0, &remain_len) == UINT32_MAX)) {
551 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
552 : 0 : return 0;
553 : : }
554 : 0 : klm++;
555 : 0 : klm_n++;
556 : : }
557 : 0 : return klm_n;
558 : : }
559 : :
560 : : static __rte_always_inline int
561 : : mlx5_crypto_gcm_build_klm_by_addr(struct mlx5_crypto_qp *qp,
562 : : struct mlx5_klm *klm,
563 : : void *addr,
564 : : uint32_t len)
565 : : {
566 : 0 : klm->byte_count = rte_cpu_to_be_32(len);
567 : 0 : klm->address = rte_cpu_to_be_64((uintptr_t)addr);
568 : 0 : klm->mkey = mlx5_mr_addr2mr_bh(&qp->mr_ctrl, (uintptr_t)addr);
569 [ # # # # ]: 0 : if (klm->mkey == UINT32_MAX)
570 : : return 0;
571 : : return 1;
572 : : }
573 : :
574 : : static __rte_always_inline int
575 : : mlx5_crypto_gcm_build_op_klm(struct mlx5_crypto_qp *qp,
576 : : struct rte_crypto_op *op,
577 : : struct mlx5_crypto_gcm_op_info *op_info,
578 : : struct mlx5_klm *klm,
579 : : uint32_t *len)
580 : : {
581 : : struct mlx5_crypto_session *sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
582 : : struct mlx5_klm *digest = NULL, *aad = NULL;
583 : 0 : uint32_t total_len = op->sym->aead.data.length + sess->aad_len + sess->tag_len;
584 : : uint32_t klm_n = 0, klm_src = 0, klm_dst = 0;
585 : :
586 : : /* Build AAD KLM. */
587 : : aad = klm;
588 : 0 : if (!mlx5_crypto_gcm_build_klm_by_addr(qp, aad, op->sym->aead.aad.data, sess->aad_len))
589 : : return 0;
590 : : klm_n++;
591 : : /* Build src mubf KLM. */
592 [ # # ]: 0 : klm_src = mlx5_crypto_gcm_build_mbuf_chain_klms(qp, op, op->sym->m_src, &klm[klm_n]);
593 [ # # ]: 0 : if (!klm_src)
594 : : return 0;
595 : 0 : klm_n += klm_src;
596 : : /* Reserve digest KLM if needed. */
597 [ # # ]: 0 : if (!op_info->is_oop ||
598 [ # # ]: 0 : sess->op_type == MLX5_CRYPTO_OP_TYPE_DECRYPTION) {
599 : 0 : digest = &klm[klm_n];
600 : 0 : klm_n++;
601 : : }
602 : : /* Build dst mbuf KLM. */
603 [ # # ]: 0 : if (op_info->is_oop) {
604 : 0 : klm[klm_n] = *aad;
605 : 0 : klm_n++;
606 : 0 : klm_dst = mlx5_crypto_gcm_build_mbuf_chain_klms(qp, op, op->sym->m_dst,
607 [ # # ]: 0 : &klm[klm_n]);
608 [ # # ]: 0 : if (!klm_dst)
609 : : return 0;
610 : 0 : klm_n += klm_dst;
611 : 0 : total_len += (op->sym->aead.data.length + sess->aad_len);
612 : : }
613 : : /* Update digest at the end if it is not set. */
614 [ # # ]: 0 : if (!digest) {
615 : 0 : digest = &klm[klm_n];
616 : 0 : klm_n++;
617 : : }
618 : : /* Build digest KLM. */
619 : 0 : if (!mlx5_crypto_gcm_build_klm_by_addr(qp, digest, op->sym->aead.digest.data,
620 [ # # ]: 0 : sess->tag_len))
621 : : return 0;
622 : : *len = total_len;
623 : 0 : return klm_n;
624 : : }
625 : :
626 : : static __rte_always_inline struct mlx5_wqe_cseg *
627 : : mlx5_crypto_gcm_get_umr_wqe(struct mlx5_crypto_qp *qp)
628 : : {
629 : 0 : uint32_t wqe_offset = qp->umr_pi & (qp->umr_wqbbs - 1);
630 : 0 : uint32_t left_wqbbs = qp->umr_wqbbs - wqe_offset;
631 : : struct mlx5_wqe_cseg *wqe;
632 : :
633 : : /* If UMR WQE is near the boundary. */
634 : 0 : if (left_wqbbs < MLX5_UMR_GCM_WQE_STRIDE) {
635 : : /* Append NOP WQE as the left WQEBBS is not enough for UMR. */
636 : 0 : wqe = RTE_PTR_ADD(qp->umr_qp_obj.umem_buf, wqe_offset * MLX5_SEND_WQE_BB);
637 [ # # ]: 0 : wqe->opcode = rte_cpu_to_be_32(MLX5_OPCODE_NOP | ((uint32_t)qp->umr_pi << 8));
638 [ # # ]: 0 : wqe->sq_ds = rte_cpu_to_be_32((qp->umr_qp_obj.qp->id << 8) | (left_wqbbs << 2));
639 : 0 : wqe->flags = RTE_BE32(0);
640 : 0 : wqe->misc = RTE_BE32(0);
641 : 0 : qp->umr_pi += left_wqbbs;
642 : 0 : wqe_offset = qp->umr_pi & (qp->umr_wqbbs - 1);
643 : : }
644 : 0 : wqe_offset *= MLX5_SEND_WQE_BB;
645 [ # # ]: 0 : return RTE_PTR_ADD(qp->umr_qp_obj.umem_buf, wqe_offset);
646 : : }
647 : :
648 : : static __rte_always_inline int
649 : : mlx5_crypto_gcm_build_umr(struct mlx5_crypto_qp *qp,
650 : : struct rte_crypto_op *op,
651 : : uint32_t idx,
652 : : struct mlx5_crypto_gcm_op_info *op_info,
653 : : struct mlx5_crypto_gcm_data *data)
654 : : {
655 : 0 : struct mlx5_crypto_priv *priv = qp->priv;
656 : 0 : struct mlx5_crypto_session *sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
657 : : struct mlx5_wqe_cseg *wqe;
658 : : struct mlx5_wqe_umr_cseg *ucseg;
659 : : struct mlx5_wqe_mkey_cseg *mkc;
660 : : struct mlx5_klm *iklm;
661 [ # # ]: 0 : struct mlx5_klm *klm = &qp->klm_array[idx * priv->max_klm_num];
662 : : uint16_t klm_size, klm_align;
663 : : uint32_t total_len;
664 : :
665 : : /* Build KLM base on the op. */
666 : 0 : klm_size = mlx5_crypto_gcm_build_op_klm(qp, op, op_info, klm, &total_len);
667 [ # # ]: 0 : if (!klm_size)
668 : : return -EINVAL;
669 [ # # ]: 0 : klm_align = RTE_ALIGN(klm_size, 4);
670 : : /* Get UMR WQE memory. */
671 : : wqe = mlx5_crypto_gcm_get_umr_wqe(qp);
672 : : memset(wqe, 0, MLX5_UMR_GCM_WQE_SIZE);
673 : : /* Set WQE control seg. Non-inline KLM UMR WQE size must be 9 WQE_DS. */
674 [ # # ]: 0 : wqe->opcode = rte_cpu_to_be_32(MLX5_OPCODE_UMR | ((uint32_t)qp->umr_pi << 8));
675 [ # # ]: 0 : wqe->sq_ds = rte_cpu_to_be_32((qp->umr_qp_obj.qp->id << 8) | 9);
676 : 0 : wqe->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR << MLX5_COMP_MODE_OFFSET);
677 [ # # ]: 0 : wqe->misc = rte_cpu_to_be_32(qp->mkey[idx]->id);
678 : : /* Set UMR WQE control seg. */
679 : : ucseg = (struct mlx5_wqe_umr_cseg *)(wqe + 1);
680 : 0 : ucseg->mkey_mask |= RTE_BE64(1u << 0);
681 [ # # ]: 0 : ucseg->ko_to_bs = rte_cpu_to_be_32(klm_align << MLX5_UMRC_KO_OFFSET);
682 : : /* Set mkey context seg. */
683 : : mkc = (struct mlx5_wqe_mkey_cseg *)(ucseg + 1);
684 [ # # ]: 0 : mkc->len = rte_cpu_to_be_64(total_len);
685 [ # # ]: 0 : mkc->qpn_mkey = rte_cpu_to_be_32(0xffffff00 | (qp->mkey[idx]->id & 0xff));
686 : : /* Set UMR pointer to data seg. */
687 : : iklm = (struct mlx5_klm *)(mkc + 1);
688 : 0 : iklm->address = rte_cpu_to_be_64((uintptr_t)((char *)klm));
689 [ # # ]: 0 : iklm->mkey = rte_cpu_to_be_32(qp->mr.lkey);
690 [ # # ]: 0 : data->src_mkey = rte_cpu_to_be_32(qp->mkey[idx]->id);
691 : : data->dst_mkey = data->src_mkey;
692 : : data->src_addr = 0;
693 : 0 : data->src_bytes = sess->aad_len + op->sym->aead.data.length;
694 : : data->dst_bytes = data->src_bytes;
695 [ # # ]: 0 : if (op_info->is_enc)
696 : 0 : data->dst_bytes += sess->tag_len;
697 : : else
698 : 0 : data->src_bytes += sess->tag_len;
699 [ # # ]: 0 : if (op_info->is_oop)
700 : 0 : data->dst_addr = (void *)(uintptr_t)(data->src_bytes);
701 : : else
702 : : data->dst_addr = 0;
703 : : /* Clear the padding memory. */
704 : 0 : memset(&klm[klm_size], 0, sizeof(struct mlx5_klm) * (klm_align - klm_size));
705 : : /* Update PI and WQE */
706 : 0 : qp->umr_pi += MLX5_UMR_GCM_WQE_STRIDE;
707 : 0 : qp->umr_wqe = (uint8_t *)wqe;
708 : 0 : return 0;
709 : : }
710 : :
711 : : static __rte_always_inline void
712 : : mlx5_crypto_gcm_build_send_en(struct mlx5_crypto_qp *qp)
713 : : {
714 : 0 : uint32_t wqe_offset = (qp->umr_pi & (qp->umr_wqbbs - 1)) * MLX5_SEND_WQE_BB;
715 : 0 : struct mlx5_wqe_cseg *cs = RTE_PTR_ADD(qp->umr_qp_obj.wqes, wqe_offset);
716 : 0 : struct mlx5_wqe_qseg *qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
717 : :
718 [ # # ]: 0 : cs->opcode = rte_cpu_to_be_32(MLX5_OPCODE_SEND_EN | ((uint32_t)qp->umr_pi << 8));
719 [ # # ]: 0 : cs->sq_ds = rte_cpu_to_be_32((qp->umr_qp_obj.qp->id << 8) | 2);
720 : : /*
721 : : * No need to generate the SEND_EN CQE as we want only GGA CQE
722 : : * in the CQ normally. We can compare qp->last_send_gga_pi with
723 : : * qp->pi to know if all SEND_EN be consumed.
724 : : */
725 : 0 : cs->flags = RTE_BE32((MLX5_COMP_ONLY_FIRST_ERR << MLX5_COMP_MODE_OFFSET) |
726 : : MLX5_WQE_CTRL_INITIATOR_SMALL_FENCE);
727 : 0 : cs->misc = RTE_BE32(0);
728 [ # # ]: 0 : qs->max_index = rte_cpu_to_be_32(qp->pi);
729 [ # # ]: 0 : qs->qpn_cqn = rte_cpu_to_be_32(qp->qp_obj.qp->id);
730 : 0 : qp->umr_wqe = (uint8_t *)cs;
731 : 0 : qp->umr_pi += 1;
732 : : }
733 : :
734 : : static __rte_always_inline void
735 : : mlx5_crypto_gcm_wqe_set(struct mlx5_crypto_qp *qp,
736 : : struct rte_crypto_op *op,
737 : : uint32_t idx,
738 : : struct mlx5_crypto_gcm_data *data)
739 : : {
740 : 0 : struct mlx5_crypto_session *sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
741 : 0 : struct mlx5_gga_wqe *wqe = &((struct mlx5_gga_wqe *)qp->qp_obj.wqes)[idx];
742 : 0 : union mlx5_gga_crypto_opaque *opaq = qp->opaque_addr;
743 : :
744 : 0 : memcpy(opaq[idx].cp.iv,
745 [ # # ]: 0 : rte_crypto_op_ctod_offset(op, uint8_t *, sess->iv_offset), sess->iv_len);
746 : 0 : opaq[idx].cp.tag_size = sess->wqe_tag_len;
747 : 0 : opaq[idx].cp.aad_size = sess->wqe_aad_len;
748 : : /* Update control seg. */
749 [ # # ]: 0 : wqe->opcode = rte_cpu_to_be_32(MLX5_MMO_CRYPTO_OPC + (qp->pi << 8));
750 : 0 : wqe->gga_ctrl1 = sess->mmo_ctrl;
751 : 0 : wqe->gga_ctrl2 = sess->dek_id;
752 : 0 : wqe->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR << MLX5_COMP_MODE_OFFSET);
753 : : /* Update op_info seg. */
754 [ # # ]: 0 : wqe->gather.bcount = rte_cpu_to_be_32(data->src_bytes);
755 : 0 : wqe->gather.lkey = data->src_mkey;
756 : 0 : wqe->gather.pbuf = rte_cpu_to_be_64((uintptr_t)data->src_addr);
757 : : /* Update output seg. */
758 [ # # ]: 0 : wqe->scatter.bcount = rte_cpu_to_be_32(data->dst_bytes);
759 : 0 : wqe->scatter.lkey = data->dst_mkey;
760 : 0 : wqe->scatter.pbuf = rte_cpu_to_be_64((uintptr_t)data->dst_addr);
761 : 0 : qp->wqe = (uint8_t *)wqe;
762 : : }
763 : :
764 : : static uint16_t
765 : 0 : mlx5_crypto_gcm_enqueue_burst(void *queue_pair,
766 : : struct rte_crypto_op **ops,
767 : : uint16_t nb_ops)
768 : : {
769 : : struct mlx5_crypto_qp *qp = queue_pair;
770 : : struct mlx5_crypto_session *sess;
771 : 0 : struct mlx5_crypto_priv *priv = qp->priv;
772 : : struct mlx5_crypto_gcm_tag_cpy_info *tag;
773 : : struct mlx5_crypto_gcm_data gcm_data;
774 : : struct rte_crypto_op *op;
775 : : struct mlx5_crypto_gcm_op_info op_info;
776 : 0 : uint16_t mask = qp->entries_n - 1;
777 : 0 : uint16_t remain = qp->entries_n - (qp->pi - qp->qp_ci);
778 : : uint32_t idx;
779 : : uint16_t umr_cnt = 0;
780 : :
781 : : if (remain < nb_ops)
782 : : nb_ops = remain;
783 : : else
784 : : remain = nb_ops;
785 [ # # ]: 0 : if (unlikely(remain == 0))
786 : : return 0;
787 : : do {
788 : 0 : op = *ops++;
789 : 0 : sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
790 [ # # ]: 0 : idx = qp->pi & mask;
791 : : mlx5_crypto_gcm_get_op_info(qp, op, &op_info);
792 [ # # ]: 0 : if (!op_info.need_umr) {
793 : : gcm_data.src_addr = op_info.src_addr;
794 : 0 : gcm_data.src_bytes = op->sym->aead.data.length + sess->aad_len;
795 [ # # ]: 0 : gcm_data.src_mkey = mlx5_mr_mb2mr(&qp->mr_ctrl, op->sym->m_src);
796 [ # # ]: 0 : if (op_info.is_oop) {
797 [ # # ]: 0 : gcm_data.dst_addr = RTE_PTR_SUB
798 : : (rte_pktmbuf_mtod_offset(op->sym->m_dst,
799 : : void *, op->sym->aead.data.offset), sess->aad_len);
800 : : gcm_data.dst_mkey = mlx5_mr_mb2mr(&qp->mr_ctrl, op->sym->m_dst);
801 : : } else {
802 : : gcm_data.dst_addr = gcm_data.src_addr;
803 : : gcm_data.dst_mkey = gcm_data.src_mkey;
804 : : }
805 : : gcm_data.dst_bytes = gcm_data.src_bytes;
806 [ # # ]: 0 : if (op_info.is_enc)
807 : 0 : gcm_data.dst_bytes += sess->tag_len;
808 : : else
809 : 0 : gcm_data.src_bytes += sess->tag_len;
810 : : } else {
811 [ # # ]: 0 : if (unlikely(mlx5_crypto_gcm_build_umr(qp, op, idx,
812 : : &op_info, &gcm_data))) {
813 : 0 : qp->stats.enqueue_err_count++;
814 [ # # ]: 0 : if (remain != nb_ops) {
815 : 0 : qp->stats.enqueued_count -= remain;
816 : 0 : break;
817 : : }
818 : : return 0;
819 : : }
820 : 0 : umr_cnt++;
821 : : }
822 : : mlx5_crypto_gcm_wqe_set(qp, op, idx, &gcm_data);
823 [ # # ]: 0 : if (op_info.digest) {
824 : 0 : tag = (struct mlx5_crypto_gcm_tag_cpy_info *)op->sym->aead.digest.data;
825 : 0 : tag->digest = op_info.digest;
826 : 0 : tag->tag_len = sess->tag_len;
827 : 0 : op->status = MLX5_CRYPTO_OP_STATUS_GCM_TAG_COPY;
828 : : } else {
829 : 0 : op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
830 : : }
831 : 0 : qp->ops[idx] = op;
832 : 0 : qp->pi++;
833 [ # # ]: 0 : } while (--remain);
834 : 0 : qp->stats.enqueued_count += nb_ops;
835 : : /* Update the last GGA cseg with COMP. */
836 : 0 : ((struct mlx5_wqe_cseg *)qp->wqe)->flags =
837 : : RTE_BE32(MLX5_COMP_ALWAYS << MLX5_COMP_MODE_OFFSET);
838 : : /* Only when there are no pending SEND_EN WQEs in background. */
839 [ # # # # ]: 0 : if (!umr_cnt && !qp->has_umr) {
840 : 0 : mlx5_doorbell_ring(&priv->uar.bf_db, *(volatile uint64_t *)qp->wqe,
841 : 0 : qp->pi, &qp->qp_obj.db_rec[MLX5_SND_DBR],
842 : 0 : !priv->uar.dbnc);
843 : : } else {
844 : : mlx5_crypto_gcm_build_send_en(qp);
845 : 0 : mlx5_doorbell_ring(&priv->uar.bf_db, *(volatile uint64_t *)qp->umr_wqe,
846 : 0 : qp->umr_pi, &qp->umr_qp_obj.db_rec[MLX5_SND_DBR],
847 : 0 : !priv->uar.dbnc);
848 : 0 : qp->last_gga_pi = qp->pi;
849 : 0 : qp->has_umr = true;
850 : : }
851 : : return nb_ops;
852 : : }
853 : :
854 : : static __rte_noinline void
855 : 0 : mlx5_crypto_gcm_cqe_err_handle(struct mlx5_crypto_qp *qp, struct rte_crypto_op *op)
856 : : {
857 : : uint8_t op_code;
858 : 0 : const uint32_t idx = qp->cq_ci & (qp->entries_n - 1);
859 : 0 : volatile struct mlx5_err_cqe *cqe = (volatile struct mlx5_err_cqe *)
860 : 0 : &qp->cq_obj.cqes[idx];
861 : :
862 : 0 : op_code = rte_be_to_cpu_32(cqe->s_wqe_opcode_qpn) >> MLX5_CQ_INDEX_WIDTH;
863 : 0 : DRV_LOG(ERR, "CQE ERR:0x%x, Vendor_ERR:0x%x, OP:0x%x, QPN:0x%x, WQE_CNT:0x%x",
864 : : cqe->syndrome, cqe->vendor_err_synd, op_code,
865 : : (rte_be_to_cpu_32(cqe->s_wqe_opcode_qpn) & 0xffffff),
866 : : rte_be_to_cpu_16(cqe->wqe_counter));
867 [ # # ]: 0 : if (op && op_code == MLX5_OPCODE_MMO) {
868 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
869 : 0 : qp->stats.dequeue_err_count++;
870 : : }
871 : 0 : }
872 : :
873 : : static __rte_always_inline void
874 : : mlx5_crypto_gcm_fill_op(struct mlx5_crypto_qp *qp,
875 : : struct rte_crypto_op **ops,
876 : : uint16_t orci,
877 : : uint16_t rci,
878 : : uint16_t op_mask)
879 : : {
880 : : uint16_t n;
881 : :
882 : 0 : orci &= op_mask;
883 : 0 : rci &= op_mask;
884 : 0 : if (unlikely(orci > rci)) {
885 : 0 : n = op_mask - orci + 1;
886 : 0 : memcpy(ops, &qp->ops[orci], n * sizeof(*ops));
887 : : orci = 0;
888 : : } else {
889 : : n = 0;
890 : : }
891 : : /* rci can be 0 here, memcpy will skip that. */
892 : 0 : memcpy(&ops[n], &qp->ops[orci], (rci - orci) * sizeof(*ops));
893 : : }
894 : :
895 : : static __rte_always_inline void
896 : : mlx5_crypto_gcm_cpy_tag(struct mlx5_crypto_qp *qp,
897 : : uint16_t orci,
898 : : uint16_t rci,
899 : : uint16_t op_mask)
900 : : {
901 : : struct rte_crypto_op *op;
902 : : struct mlx5_crypto_gcm_tag_cpy_info *tag;
903 : :
904 [ # # # # ]: 0 : while (qp->cpy_tag_op && orci != rci) {
905 : 0 : op = qp->ops[orci & op_mask];
906 [ # # ]: 0 : if (op->status == MLX5_CRYPTO_OP_STATUS_GCM_TAG_COPY) {
907 : 0 : tag = (struct mlx5_crypto_gcm_tag_cpy_info *)op->sym->aead.digest.data;
908 : 0 : memcpy(op->sym->aead.digest.data, tag->digest, tag->tag_len);
909 : 0 : op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
910 : 0 : qp->cpy_tag_op--;
911 : : }
912 : 0 : orci++;
913 : : }
914 : : }
915 : :
916 : : static uint16_t
917 : 0 : mlx5_crypto_gcm_dequeue_burst(void *queue_pair,
918 : : struct rte_crypto_op **ops,
919 : : uint16_t nb_ops)
920 : : {
921 : : struct mlx5_crypto_qp *qp = queue_pair;
922 : : volatile struct mlx5_cqe *restrict cqe;
923 : 0 : const unsigned int cq_size = qp->cq_entries_n;
924 : 0 : const unsigned int mask = cq_size - 1;
925 : 0 : const unsigned int op_mask = qp->entries_n - 1;
926 : : uint32_t idx;
927 : 0 : uint32_t next_idx = qp->cq_ci & mask;
928 : 0 : uint16_t reported_ci = qp->reported_ci;
929 : 0 : uint16_t qp_ci = qp->qp_ci;
930 : 0 : const uint16_t max = RTE_MIN((uint16_t)(qp->pi - reported_ci), nb_ops);
931 : : uint16_t op_num = 0;
932 : : int ret;
933 : :
934 [ # # ]: 0 : if (unlikely(max == 0))
935 : : return 0;
936 [ # # ]: 0 : while (qp_ci - reported_ci < max) {
937 : : idx = next_idx;
938 : 0 : next_idx = (qp->cq_ci + 1) & mask;
939 [ # # ]: 0 : cqe = &qp->cq_obj.cqes[idx];
940 : : ret = check_cqe(cqe, cq_size, qp->cq_ci);
941 [ # # ]: 0 : if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
942 [ # # ]: 0 : if (unlikely(ret != MLX5_CQE_STATUS_HW_OWN))
943 : 0 : mlx5_crypto_gcm_cqe_err_handle(qp,
944 : 0 : qp->ops[reported_ci & op_mask]);
945 : : break;
946 : : }
947 : 0 : qp_ci = rte_be_to_cpu_16(cqe->wqe_counter) + 1;
948 [ # # ]: 0 : if (qp->has_umr &&
949 [ # # ]: 0 : (qp->last_gga_pi + 1) == qp_ci)
950 : 0 : qp->has_umr = false;
951 : 0 : qp->cq_ci++;
952 : : }
953 : : /* If wqe_counter changed, means CQE handled. */
954 [ # # ]: 0 : if (likely(qp->qp_ci != qp_ci)) {
955 : 0 : qp->qp_ci = qp_ci;
956 : 0 : rte_io_wmb();
957 [ # # ]: 0 : qp->cq_obj.db_rec[0] = rte_cpu_to_be_32(qp->cq_ci);
958 : : }
959 : : /* If reported_ci is not same with qp_ci, means op retrieved. */
960 [ # # ]: 0 : if (qp_ci != reported_ci) {
961 : 0 : op_num = RTE_MIN((uint16_t)(qp_ci - reported_ci), max);
962 : 0 : reported_ci += op_num;
963 : 0 : mlx5_crypto_gcm_cpy_tag(qp, qp->reported_ci, reported_ci, op_mask);
964 [ # # ]: 0 : mlx5_crypto_gcm_fill_op(qp, ops, qp->reported_ci, reported_ci, op_mask);
965 : 0 : qp->stats.dequeued_count += op_num;
966 : 0 : qp->reported_ci = reported_ci;
967 : : }
968 : : return op_num;
969 : : }
970 : :
971 : : int
972 : 0 : mlx5_crypto_gcm_init(struct mlx5_crypto_priv *priv)
973 : : {
974 : 0 : struct mlx5_common_device *cdev = priv->cdev;
975 : 0 : struct rte_cryptodev *crypto_dev = priv->crypto_dev;
976 : 0 : struct rte_cryptodev_ops *dev_ops = crypto_dev->dev_ops;
977 : : int ret;
978 : :
979 : : /* Override AES-GCM specified ops. */
980 : 0 : dev_ops->sym_session_configure = mlx5_crypto_sym_gcm_session_configure;
981 : 0 : mlx5_os_set_reg_mr_cb(&priv->reg_mr_cb, &priv->dereg_mr_cb);
982 : 0 : dev_ops->queue_pair_setup = mlx5_crypto_gcm_qp_setup;
983 : 0 : dev_ops->queue_pair_release = mlx5_crypto_gcm_qp_release;
984 : 0 : crypto_dev->dequeue_burst = mlx5_crypto_gcm_dequeue_burst;
985 : 0 : crypto_dev->enqueue_burst = mlx5_crypto_gcm_enqueue_burst;
986 : 0 : priv->max_klm_num = RTE_ALIGN((priv->max_segs_num + 1) * 2 + 1, MLX5_UMR_KLM_NUM_ALIGN);
987 : : /* Generate GCM capability. */
988 : 0 : ret = mlx5_crypto_generate_gcm_cap(&cdev->config.hca_attr.crypto_mmo,
989 : : mlx5_crypto_gcm_caps);
990 [ # # ]: 0 : if (ret) {
991 : 0 : DRV_LOG(ERR, "No enough AES-GCM cap.");
992 : 0 : return -1;
993 : : }
994 : 0 : priv->caps = mlx5_crypto_gcm_caps;
995 : 0 : return 0;
996 : : }
|