1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
5 #include <rte_malloc.h>
6 #include <rte_mempool.h>
7 #include <rte_eal_paging.h>
10 #include <rte_bus_pci.h>
11 #include <rte_memory.h>
13 #include <mlx5_glue.h>
14 #include <mlx5_common.h>
15 #include <mlx5_devx_cmds.h>
16 #include <mlx5_common_os.h>
18 #include "mlx5_crypto_utils.h"
19 #include "mlx5_crypto.h"
21 #define MLX5_CRYPTO_DRIVER_NAME crypto_mlx5
22 #define MLX5_CRYPTO_LOG_NAME pmd.crypto.mlx5
23 #define MLX5_CRYPTO_MAX_QPS 1024
24 #define MLX5_CRYPTO_MAX_SEGS 56
26 #define MLX5_CRYPTO_FEATURE_FLAGS \
27 (RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | RTE_CRYPTODEV_FF_HW_ACCELERATED | \
28 RTE_CRYPTODEV_FF_IN_PLACE_SGL | RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT | \
29 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT | \
30 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT | \
31 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT | \
32 RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY | \
33 RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)
35 TAILQ_HEAD(mlx5_crypto_privs, mlx5_crypto_priv) mlx5_crypto_priv_list =
36 TAILQ_HEAD_INITIALIZER(mlx5_crypto_priv_list);
37 static pthread_mutex_t priv_list_lock;
39 int mlx5_crypto_logtype;
41 uint8_t mlx5_crypto_driver_id;
43 const struct rte_cryptodev_capabilities mlx5_crypto_caps[] = {
45 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
47 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
49 .algo = RTE_CRYPTO_CIPHER_AES_XTS,
62 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES |
63 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES |
64 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_1_MEGABYTES,
70 static const char mlx5_crypto_drv_name[] = RTE_STR(MLX5_CRYPTO_DRIVER_NAME);
72 static const struct rte_driver mlx5_drv = {
73 .name = mlx5_crypto_drv_name,
74 .alias = mlx5_crypto_drv_name
77 static struct cryptodev_driver mlx5_cryptodev_driver;
79 struct mlx5_crypto_session {
80 uint32_t bs_bpt_eo_es;
81 /**< bsf_size, bsf_p_type, encryption_order and encryption standard,
82 * saved in big endian format.
85 /**< crypto_block_size_pointer and reserved 24 bits saved in big
88 uint32_t iv_offset:16;
89 /**< Starting point for Initialisation Vector. */
90 struct mlx5_crypto_dek *dek; /**< Pointer to dek struct. */
91 uint32_t dek_id; /**< DEK ID */
95 mlx5_crypto_dev_infos_get(struct rte_cryptodev *dev,
96 struct rte_cryptodev_info *dev_info)
99 if (dev_info != NULL) {
100 dev_info->driver_id = mlx5_crypto_driver_id;
101 dev_info->feature_flags = MLX5_CRYPTO_FEATURE_FLAGS;
102 dev_info->capabilities = mlx5_crypto_caps;
103 dev_info->max_nb_queue_pairs = MLX5_CRYPTO_MAX_QPS;
104 dev_info->min_mbuf_headroom_req = 0;
105 dev_info->min_mbuf_tailroom_req = 0;
106 dev_info->sym.max_nb_sessions = 0;
108 * If 0, the device does not have any limitation in number of
109 * sessions that can be used.
115 mlx5_crypto_dev_configure(struct rte_cryptodev *dev,
116 struct rte_cryptodev_config *config)
118 struct mlx5_crypto_priv *priv = dev->data->dev_private;
120 if (config == NULL) {
121 DRV_LOG(ERR, "Invalid crypto dev configure parameters.");
124 if ((config->ff_disable & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) != 0) {
126 "Disabled symmetric crypto feature is not supported.");
129 if (mlx5_crypto_dek_setup(priv) != 0) {
130 DRV_LOG(ERR, "Dek hash list creation has failed.");
133 priv->dev_config = *config;
134 DRV_LOG(DEBUG, "Device %u was configured.", dev->driver_id);
139 mlx5_crypto_dev_stop(struct rte_cryptodev *dev)
145 mlx5_crypto_dev_start(struct rte_cryptodev *dev)
147 struct mlx5_crypto_priv *priv = dev->data->dev_private;
149 return mlx5_dev_mempool_subscribe(priv->cdev);
153 mlx5_crypto_dev_close(struct rte_cryptodev *dev)
155 struct mlx5_crypto_priv *priv = dev->data->dev_private;
157 mlx5_crypto_dek_unset(priv);
158 DRV_LOG(DEBUG, "Device %u was closed.", dev->driver_id);
163 mlx5_crypto_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
165 return sizeof(struct mlx5_crypto_session);
169 mlx5_crypto_sym_session_configure(struct rte_cryptodev *dev,
170 struct rte_crypto_sym_xform *xform,
171 struct rte_cryptodev_sym_session *session,
172 struct rte_mempool *mp)
174 struct mlx5_crypto_priv *priv = dev->data->dev_private;
175 struct mlx5_crypto_session *sess_private_data;
176 struct rte_crypto_cipher_xform *cipher;
177 uint8_t encryption_order;
180 if (unlikely(xform->next != NULL)) {
181 DRV_LOG(ERR, "Xform next is not supported.");
184 if (unlikely((xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) ||
185 (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_XTS))) {
186 DRV_LOG(ERR, "Only AES-XTS algorithm is supported.");
189 ret = rte_mempool_get(mp, (void *)&sess_private_data);
192 "Failed to get session %p private data from mempool.",
196 cipher = &xform->cipher;
197 sess_private_data->dek = mlx5_crypto_dek_prepare(priv, cipher);
198 if (sess_private_data->dek == NULL) {
199 rte_mempool_put(mp, sess_private_data);
200 DRV_LOG(ERR, "Failed to prepare dek.");
203 if (cipher->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
204 encryption_order = MLX5_ENCRYPTION_ORDER_ENCRYPTED_RAW_MEMORY;
206 encryption_order = MLX5_ENCRYPTION_ORDER_ENCRYPTED_RAW_WIRE;
207 sess_private_data->bs_bpt_eo_es = rte_cpu_to_be_32
208 (MLX5_BSF_SIZE_64B << MLX5_BSF_SIZE_OFFSET |
209 MLX5_BSF_P_TYPE_CRYPTO << MLX5_BSF_P_TYPE_OFFSET |
210 encryption_order << MLX5_ENCRYPTION_ORDER_OFFSET |
211 MLX5_ENCRYPTION_STANDARD_AES_XTS);
212 switch (xform->cipher.dataunit_len) {
214 sess_private_data->bsp_res = 0;
217 sess_private_data->bsp_res = rte_cpu_to_be_32
218 ((uint32_t)MLX5_BLOCK_SIZE_512B <<
219 MLX5_BLOCK_SIZE_OFFSET);
222 sess_private_data->bsp_res = rte_cpu_to_be_32
223 ((uint32_t)MLX5_BLOCK_SIZE_4096B <<
224 MLX5_BLOCK_SIZE_OFFSET);
227 sess_private_data->bsp_res = rte_cpu_to_be_32
228 ((uint32_t)MLX5_BLOCK_SIZE_1MB <<
229 MLX5_BLOCK_SIZE_OFFSET);
232 DRV_LOG(ERR, "Cipher data unit length is not supported.");
235 sess_private_data->iv_offset = cipher->iv.offset;
236 sess_private_data->dek_id =
237 rte_cpu_to_be_32(sess_private_data->dek->obj->id &
239 set_sym_session_private_data(session, dev->driver_id,
241 DRV_LOG(DEBUG, "Session %p was configured.", sess_private_data);
246 mlx5_crypto_sym_session_clear(struct rte_cryptodev *dev,
247 struct rte_cryptodev_sym_session *sess)
249 struct mlx5_crypto_priv *priv = dev->data->dev_private;
250 struct mlx5_crypto_session *spriv = get_sym_session_private_data(sess,
253 if (unlikely(spriv == NULL)) {
254 DRV_LOG(ERR, "Failed to get session %p private data.", spriv);
257 mlx5_crypto_dek_destroy(priv, spriv->dek);
258 set_sym_session_private_data(sess, dev->driver_id, NULL);
259 rte_mempool_put(rte_mempool_from_obj(spriv), spriv);
260 DRV_LOG(DEBUG, "Session %p was cleared.", spriv);
264 mlx5_crypto_indirect_mkeys_release(struct mlx5_crypto_qp *qp, uint16_t n)
268 for (i = 0; i < n; i++)
270 claim_zero(mlx5_devx_cmd_destroy(qp->mkey[i]));
274 mlx5_crypto_qp_release(struct mlx5_crypto_qp *qp)
278 mlx5_devx_qp_destroy(&qp->qp_obj);
279 mlx5_mr_btree_free(&qp->mr_ctrl.cache_bh);
280 mlx5_devx_cq_destroy(&qp->cq_obj);
285 mlx5_crypto_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id)
287 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
289 mlx5_crypto_indirect_mkeys_release(qp, qp->entries_n);
290 mlx5_crypto_qp_release(qp);
291 dev->data->queue_pairs[qp_id] = NULL;
295 static __rte_noinline uint32_t
296 mlx5_crypto_get_block_size(struct rte_crypto_op *op)
298 uint32_t bl = op->sym->cipher.data.length;
302 return RTE_BE32(MLX5_BLOCK_SIZE_1MB << MLX5_BLOCK_SIZE_OFFSET);
304 return RTE_BE32(MLX5_BLOCK_SIZE_4096B <<
305 MLX5_BLOCK_SIZE_OFFSET);
307 return RTE_BE32(MLX5_BLOCK_SIZE_512B << MLX5_BLOCK_SIZE_OFFSET);
309 DRV_LOG(ERR, "Unknown block size: %u.", bl);
314 static __rte_always_inline uint32_t
315 mlx5_crypto_klm_set(struct mlx5_crypto_qp *qp, struct rte_mbuf *mbuf,
316 struct mlx5_wqe_dseg *klm, uint32_t offset,
319 uint32_t data_len = (rte_pktmbuf_data_len(mbuf) - offset);
320 uintptr_t addr = rte_pktmbuf_mtod_offset(mbuf, uintptr_t, offset);
322 if (data_len > *remain)
325 klm->bcount = rte_cpu_to_be_32(data_len);
326 klm->pbuf = rte_cpu_to_be_64(addr);
327 klm->lkey = mlx5_mr_mb2mr(&qp->mr_ctrl, mbuf);
332 static __rte_always_inline uint32_t
333 mlx5_crypto_klms_set(struct mlx5_crypto_qp *qp, struct rte_crypto_op *op,
334 struct rte_mbuf *mbuf, struct mlx5_wqe_dseg *klm)
336 uint32_t remain_len = op->sym->cipher.data.length;
337 uint32_t nb_segs = mbuf->nb_segs;
340 /* First mbuf needs to take the cipher offset. */
341 if (unlikely(mlx5_crypto_klm_set(qp, mbuf, klm,
342 op->sym->cipher.data.offset, &remain_len) == UINT32_MAX)) {
343 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
349 if (unlikely(mbuf == NULL || nb_segs == 0)) {
350 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
353 if (unlikely(mlx5_crypto_klm_set(qp, mbuf, ++klm, 0,
354 &remain_len) == UINT32_MAX)) {
355 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
363 static __rte_always_inline int
364 mlx5_crypto_wqe_set(struct mlx5_crypto_priv *priv,
365 struct mlx5_crypto_qp *qp,
366 struct rte_crypto_op *op,
367 struct mlx5_umr_wqe *umr)
369 struct mlx5_crypto_session *sess = get_sym_session_private_data
370 (op->sym->session, mlx5_crypto_driver_id);
371 struct mlx5_wqe_cseg *cseg = &umr->ctr;
372 struct mlx5_wqe_mkey_cseg *mkc = &umr->mkc;
373 struct mlx5_wqe_dseg *klms = &umr->kseg[0];
374 struct mlx5_wqe_umr_bsf_seg *bsf = ((struct mlx5_wqe_umr_bsf_seg *)
375 RTE_PTR_ADD(umr, priv->umr_wqe_size)) - 1;
377 bool ipl = op->sym->m_dst == NULL || op->sym->m_dst == op->sym->m_src;
379 uint32_t klm_n = mlx5_crypto_klms_set(qp, op,
380 ipl ? op->sym->m_src : op->sym->m_dst, klms);
382 if (unlikely(klm_n == 0))
384 bsf->bs_bpt_eo_es = sess->bs_bpt_eo_es;
385 if (unlikely(!sess->bsp_res)) {
386 bsf->bsp_res = mlx5_crypto_get_block_size(op);
387 if (unlikely(bsf->bsp_res == UINT32_MAX)) {
388 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
392 bsf->bsp_res = sess->bsp_res;
394 bsf->raw_data_size = rte_cpu_to_be_32(op->sym->cipher.data.length);
395 memcpy(bsf->xts_initial_tweak,
396 rte_crypto_op_ctod_offset(op, uint8_t *, sess->iv_offset), 16);
397 bsf->res_dp = sess->dek_id;
398 mkc->len = rte_cpu_to_be_64(op->sym->cipher.data.length);
399 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) | MLX5_OPCODE_UMR);
400 qp->db_pi += priv->umr_wqe_stride;
401 /* Set RDMA_WRITE WQE. */
402 cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
403 klms = RTE_PTR_ADD(cseg, sizeof(struct mlx5_rdma_write_wqe));
405 klm_n = mlx5_crypto_klms_set(qp, op, op->sym->m_src, klms);
406 if (unlikely(klm_n == 0))
409 memcpy(klms, &umr->kseg[0], sizeof(*klms) * klm_n);
412 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj.qp->id << 8) | ds);
413 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) |
414 MLX5_OPCODE_RDMA_WRITE);
415 ds = RTE_ALIGN(ds, 4);
416 qp->db_pi += ds >> 2;
417 /* Set NOP WQE if needed. */
418 if (priv->max_rdmar_ds > ds) {
420 ds = priv->max_rdmar_ds - ds;
421 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj.qp->id << 8) | ds);
422 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) |
424 qp->db_pi += ds >> 2; /* Here, DS is 4 aligned for sure. */
426 qp->wqe = (uint8_t *)cseg;
430 static __rte_always_inline void
431 mlx5_crypto_uar_write(uint64_t val, struct mlx5_crypto_priv *priv)
434 *priv->uar_addr = val;
435 #else /* !RTE_ARCH_64 */
436 rte_spinlock_lock(&priv->uar32_sl);
437 *(volatile uint32_t *)priv->uar_addr = val;
439 *((volatile uint32_t *)priv->uar_addr + 1) = val >> 32;
440 rte_spinlock_unlock(&priv->uar32_sl);
445 mlx5_crypto_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
448 struct mlx5_crypto_qp *qp = queue_pair;
449 struct mlx5_crypto_priv *priv = qp->priv;
450 struct mlx5_umr_wqe *umr;
451 struct rte_crypto_op *op;
452 uint16_t mask = qp->entries_n - 1;
453 uint16_t remain = qp->entries_n - (qp->pi - qp->ci);
460 if (unlikely(remain == 0))
465 umr = RTE_PTR_ADD(qp->qp_obj.umem_buf,
466 priv->wqe_set_size * idx);
467 if (unlikely(mlx5_crypto_wqe_set(priv, qp, op, umr) == 0)) {
468 qp->stats.enqueue_err_count++;
469 if (remain != nb_ops) {
470 qp->stats.enqueued_count -= remain;
478 qp->stats.enqueued_count += nb_ops;
480 qp->qp_obj.db_rec[MLX5_SND_DBR] = rte_cpu_to_be_32(qp->db_pi);
482 mlx5_crypto_uar_write(*(volatile uint64_t *)qp->wqe, qp->priv);
487 static __rte_noinline void
488 mlx5_crypto_cqe_err_handle(struct mlx5_crypto_qp *qp, struct rte_crypto_op *op)
490 const uint32_t idx = qp->ci & (qp->entries_n - 1);
491 volatile struct mlx5_err_cqe *cqe = (volatile struct mlx5_err_cqe *)
492 &qp->cq_obj.cqes[idx];
494 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
495 qp->stats.dequeue_err_count++;
496 DRV_LOG(ERR, "CQE ERR:%x.\n", rte_be_to_cpu_32(cqe->syndrome));
500 mlx5_crypto_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
503 struct mlx5_crypto_qp *qp = queue_pair;
504 volatile struct mlx5_cqe *restrict cqe;
505 struct rte_crypto_op *restrict op;
506 const unsigned int cq_size = qp->entries_n;
507 const unsigned int mask = cq_size - 1;
509 uint32_t next_idx = qp->ci & mask;
510 const uint16_t max = RTE_MIN((uint16_t)(qp->pi - qp->ci), nb_ops);
514 if (unlikely(max == 0))
518 next_idx = (qp->ci + 1) & mask;
520 cqe = &qp->cq_obj.cqes[idx];
521 ret = check_cqe(cqe, cq_size, qp->ci);
523 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
524 if (unlikely(ret != MLX5_CQE_STATUS_HW_OWN))
525 mlx5_crypto_cqe_err_handle(qp, op);
528 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
532 if (likely(i != 0)) {
534 qp->cq_obj.db_rec[0] = rte_cpu_to_be_32(qp->ci);
535 qp->stats.dequeued_count += i;
541 mlx5_crypto_qp_init(struct mlx5_crypto_priv *priv, struct mlx5_crypto_qp *qp)
545 for (i = 0 ; i < qp->entries_n; i++) {
546 struct mlx5_wqe_cseg *cseg = RTE_PTR_ADD(qp->qp_obj.umem_buf,
547 i * priv->wqe_set_size);
548 struct mlx5_wqe_umr_cseg *ucseg = (struct mlx5_wqe_umr_cseg *)
550 struct mlx5_wqe_umr_bsf_seg *bsf =
551 (struct mlx5_wqe_umr_bsf_seg *)(RTE_PTR_ADD(cseg,
552 priv->umr_wqe_size)) - 1;
553 struct mlx5_wqe_rseg *rseg;
556 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj.qp->id << 8) |
557 (priv->umr_wqe_size / MLX5_WSEG_SIZE));
558 cseg->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
559 MLX5_COMP_MODE_OFFSET);
560 cseg->misc = rte_cpu_to_be_32(qp->mkey[i]->id);
561 ucseg->if_cf_toe_cq_res = RTE_BE32(1u << MLX5_UMRC_IF_OFFSET);
562 ucseg->mkey_mask = RTE_BE64(1u << 0); /* Mkey length bit. */
563 ucseg->ko_to_bs = rte_cpu_to_be_32
564 ((RTE_ALIGN(priv->max_segs_num, 4u) <<
565 MLX5_UMRC_KO_OFFSET) | (4 << MLX5_UMRC_TO_BS_OFFSET));
566 bsf->keytag = priv->keytag;
567 /* Init RDMA WRITE WQE. */
568 cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
569 cseg->flags = RTE_BE32((MLX5_COMP_ALWAYS <<
570 MLX5_COMP_MODE_OFFSET) |
571 MLX5_WQE_CTRL_INITIATOR_SMALL_FENCE);
572 rseg = (struct mlx5_wqe_rseg *)(cseg + 1);
573 rseg->rkey = rte_cpu_to_be_32(qp->mkey[i]->id);
578 mlx5_crypto_indirect_mkeys_prepare(struct mlx5_crypto_priv *priv,
579 struct mlx5_crypto_qp *qp)
581 struct mlx5_umr_wqe *umr;
583 struct mlx5_devx_mkey_attr attr = {
584 .pd = priv->cdev->pdn,
588 .klm_num = RTE_ALIGN(priv->max_segs_num, 4),
591 for (umr = (struct mlx5_umr_wqe *)qp->qp_obj.umem_buf, i = 0;
592 i < qp->entries_n; i++, umr = RTE_PTR_ADD(umr, priv->wqe_set_size)) {
593 attr.klm_array = (struct mlx5_klm *)&umr->kseg[0];
594 qp->mkey[i] = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &attr);
600 DRV_LOG(ERR, "Failed to allocate indirect mkey.");
601 mlx5_crypto_indirect_mkeys_release(qp, i);
606 mlx5_crypto_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
607 const struct rte_cryptodev_qp_conf *qp_conf,
610 struct mlx5_crypto_priv *priv = dev->data->dev_private;
611 struct mlx5_devx_qp_attr attr = {0};
612 struct mlx5_crypto_qp *qp;
613 uint16_t log_nb_desc = rte_log2_u32(qp_conf->nb_descriptors);
615 uint32_t alloc_size = sizeof(*qp);
616 struct mlx5_devx_cq_attr cq_attr = {
617 .uar_page_id = mlx5_os_get_devx_uar_page_id(priv->uar),
620 if (dev->data->queue_pairs[qp_id] != NULL)
621 mlx5_crypto_queue_pair_release(dev, qp_id);
622 alloc_size = RTE_ALIGN(alloc_size, RTE_CACHE_LINE_SIZE);
623 alloc_size += (sizeof(struct rte_crypto_op *) +
624 sizeof(struct mlx5_devx_obj *)) *
625 RTE_BIT32(log_nb_desc);
626 qp = rte_zmalloc_socket(__func__, alloc_size, RTE_CACHE_LINE_SIZE,
629 DRV_LOG(ERR, "Failed to allocate QP memory.");
633 if (mlx5_devx_cq_create(priv->cdev->ctx, &qp->cq_obj, log_nb_desc,
634 &cq_attr, socket_id) != 0) {
635 DRV_LOG(ERR, "Failed to create CQ.");
638 attr.pd = priv->cdev->pdn;
639 attr.uar_index = mlx5_os_get_devx_uar_page_id(priv->uar);
640 attr.cqn = qp->cq_obj.cq->id;
642 attr.sq_size = RTE_BIT32(log_nb_desc);
644 mlx5_ts_format_conv(priv->cdev->config.hca_attr.qp_ts_format);
645 ret = mlx5_devx_qp_create(priv->cdev->ctx, &qp->qp_obj, log_nb_desc,
648 DRV_LOG(ERR, "Failed to create QP.");
651 if (mlx5_mr_ctrl_init(&qp->mr_ctrl, priv->cdev,
652 priv->dev_config.socket_id) != 0) {
653 DRV_LOG(ERR, "Cannot allocate MR Btree for qp %u.",
659 * In Order to configure self loopback, when calling devx qp2rts the
660 * remote QP id that is used is the id of the same QP.
662 if (mlx5_devx_qp2rts(&qp->qp_obj, qp->qp_obj.qp->id))
664 qp->mkey = (struct mlx5_devx_obj **)RTE_ALIGN((uintptr_t)(qp + 1),
665 RTE_CACHE_LINE_SIZE);
666 qp->ops = (struct rte_crypto_op **)(qp->mkey + RTE_BIT32(log_nb_desc));
667 qp->entries_n = 1 << log_nb_desc;
668 if (mlx5_crypto_indirect_mkeys_prepare(priv, qp)) {
669 DRV_LOG(ERR, "Cannot allocate indirect memory regions.");
673 mlx5_crypto_qp_init(priv, qp);
675 dev->data->queue_pairs[qp_id] = qp;
678 mlx5_crypto_qp_release(qp);
683 mlx5_crypto_stats_get(struct rte_cryptodev *dev,
684 struct rte_cryptodev_stats *stats)
688 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
689 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
691 stats->enqueued_count += qp->stats.enqueued_count;
692 stats->dequeued_count += qp->stats.dequeued_count;
693 stats->enqueue_err_count += qp->stats.enqueue_err_count;
694 stats->dequeue_err_count += qp->stats.dequeue_err_count;
699 mlx5_crypto_stats_reset(struct rte_cryptodev *dev)
703 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
704 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
706 memset(&qp->stats, 0, sizeof(qp->stats));
710 static struct rte_cryptodev_ops mlx5_crypto_ops = {
711 .dev_configure = mlx5_crypto_dev_configure,
712 .dev_start = mlx5_crypto_dev_start,
713 .dev_stop = mlx5_crypto_dev_stop,
714 .dev_close = mlx5_crypto_dev_close,
715 .dev_infos_get = mlx5_crypto_dev_infos_get,
716 .stats_get = mlx5_crypto_stats_get,
717 .stats_reset = mlx5_crypto_stats_reset,
718 .queue_pair_setup = mlx5_crypto_queue_pair_setup,
719 .queue_pair_release = mlx5_crypto_queue_pair_release,
720 .sym_session_get_size = mlx5_crypto_sym_session_get_size,
721 .sym_session_configure = mlx5_crypto_sym_session_configure,
722 .sym_session_clear = mlx5_crypto_sym_session_clear,
723 .sym_get_raw_dp_ctx_size = NULL,
724 .sym_configure_raw_dp_ctx = NULL,
728 mlx5_crypto_uar_release(struct mlx5_crypto_priv *priv)
730 if (priv->uar != NULL) {
731 mlx5_glue->devx_free_uar(priv->uar);
737 mlx5_crypto_uar_prepare(struct mlx5_crypto_priv *priv)
739 priv->uar = mlx5_devx_alloc_uar(priv->cdev);
741 priv->uar_addr = mlx5_os_get_devx_uar_reg_addr(priv->uar);
742 if (priv->uar == NULL || priv->uar_addr == NULL) {
744 DRV_LOG(ERR, "Failed to allocate UAR.");
752 mlx5_crypto_args_check_handler(const char *key, const char *val, void *opaque)
754 struct mlx5_crypto_devarg_params *devarg_prms = opaque;
755 struct mlx5_devx_crypto_login_attr *attr = &devarg_prms->login_attr;
761 if (strcmp(key, "class") == 0)
763 if (strcmp(key, "wcs_file") == 0) {
764 file = fopen(val, "rb");
769 for (i = 0 ; i < MLX5_CRYPTO_CREDENTIAL_SIZE ; i++) {
770 ret = fscanf(file, "%02hhX", &attr->credential[i]);
774 "Failed to read credential from file.");
780 devarg_prms->login_devarg = true;
784 tmp = strtoul(val, NULL, 0);
786 DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
789 if (strcmp(key, "max_segs_num") == 0) {
790 if (!tmp || tmp > MLX5_CRYPTO_MAX_SEGS) {
791 DRV_LOG(WARNING, "Invalid max_segs_num: %d, should"
793 (uint32_t)tmp, MLX5_CRYPTO_MAX_SEGS);
797 devarg_prms->max_segs_num = (uint32_t)tmp;
798 } else if (strcmp(key, "import_kek_id") == 0) {
799 attr->session_import_kek_ptr = (uint32_t)tmp;
800 } else if (strcmp(key, "credential_id") == 0) {
801 attr->credential_pointer = (uint32_t)tmp;
802 } else if (strcmp(key, "keytag") == 0) {
803 devarg_prms->keytag = tmp;
805 DRV_LOG(WARNING, "Invalid key %s.", key);
811 mlx5_crypto_parse_devargs(struct rte_devargs *devargs,
812 struct mlx5_crypto_devarg_params *devarg_prms)
814 struct mlx5_devx_crypto_login_attr *attr = &devarg_prms->login_attr;
815 struct rte_kvargs *kvlist;
817 /* Default values. */
818 attr->credential_pointer = 0;
819 attr->session_import_kek_ptr = 0;
820 devarg_prms->keytag = 0;
821 devarg_prms->max_segs_num = 8;
822 if (devargs == NULL) {
824 "No login devargs in order to enable crypto operations in the device.");
828 kvlist = rte_kvargs_parse(devargs->args, NULL);
829 if (kvlist == NULL) {
830 DRV_LOG(ERR, "Failed to parse devargs.");
834 if (rte_kvargs_process(kvlist, NULL, mlx5_crypto_args_check_handler,
836 DRV_LOG(ERR, "Devargs handler function Failed.");
837 rte_kvargs_free(kvlist);
841 rte_kvargs_free(kvlist);
842 if (devarg_prms->login_devarg == false) {
844 "No login credential devarg in order to enable crypto operations "
853 mlx5_crypto_dev_probe(struct mlx5_common_device *cdev)
855 struct rte_cryptodev *crypto_dev;
856 struct mlx5_devx_obj *login;
857 struct mlx5_crypto_priv *priv;
858 struct mlx5_crypto_devarg_params devarg_prms = { 0 };
859 struct rte_cryptodev_pmd_init_params init_params = {
861 .private_data_size = sizeof(struct mlx5_crypto_priv),
862 .socket_id = cdev->dev->numa_node,
863 .max_nb_queue_pairs =
864 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
866 const char *ibdev_name = mlx5_os_get_ctx_device_name(cdev->ctx);
867 uint16_t rdmw_wqe_size;
870 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
871 DRV_LOG(ERR, "Non-primary process type is not supported.");
875 if (!cdev->config.hca_attr.crypto || !cdev->config.hca_attr.aes_xts) {
876 DRV_LOG(ERR, "Not enough capabilities to support crypto "
877 "operations, maybe old FW/OFED version?");
881 ret = mlx5_crypto_parse_devargs(cdev->dev->devargs, &devarg_prms);
883 DRV_LOG(ERR, "Failed to parse devargs.");
886 crypto_dev = rte_cryptodev_pmd_create(ibdev_name, cdev->dev,
888 if (crypto_dev == NULL) {
889 DRV_LOG(ERR, "Failed to create device \"%s\".", ibdev_name);
893 "Crypto device %s was created successfully.", ibdev_name);
894 crypto_dev->dev_ops = &mlx5_crypto_ops;
895 crypto_dev->dequeue_burst = mlx5_crypto_dequeue_burst;
896 crypto_dev->enqueue_burst = mlx5_crypto_enqueue_burst;
897 crypto_dev->feature_flags = MLX5_CRYPTO_FEATURE_FLAGS;
898 crypto_dev->driver_id = mlx5_crypto_driver_id;
899 priv = crypto_dev->data->dev_private;
901 priv->crypto_dev = crypto_dev;
902 if (mlx5_crypto_uar_prepare(priv) != 0) {
903 rte_cryptodev_pmd_destroy(priv->crypto_dev);
906 login = mlx5_devx_cmd_create_crypto_login_obj(cdev->ctx,
907 &devarg_prms.login_attr);
909 DRV_LOG(ERR, "Failed to configure login.");
910 mlx5_crypto_uar_release(priv);
911 rte_cryptodev_pmd_destroy(priv->crypto_dev);
914 priv->login_obj = login;
915 priv->keytag = rte_cpu_to_be_64(devarg_prms.keytag);
916 priv->max_segs_num = devarg_prms.max_segs_num;
917 priv->umr_wqe_size = sizeof(struct mlx5_wqe_umr_bsf_seg) +
918 sizeof(struct mlx5_wqe_cseg) +
919 sizeof(struct mlx5_wqe_umr_cseg) +
920 sizeof(struct mlx5_wqe_mkey_cseg) +
921 RTE_ALIGN(priv->max_segs_num, 4) *
922 sizeof(struct mlx5_wqe_dseg);
923 rdmw_wqe_size = sizeof(struct mlx5_rdma_write_wqe) +
924 sizeof(struct mlx5_wqe_dseg) *
925 (priv->max_segs_num <= 2 ? 2 : 2 +
926 RTE_ALIGN(priv->max_segs_num - 2, 4));
927 priv->wqe_set_size = priv->umr_wqe_size + rdmw_wqe_size;
928 priv->umr_wqe_stride = priv->umr_wqe_size / MLX5_SEND_WQE_BB;
929 priv->max_rdmar_ds = rdmw_wqe_size / sizeof(struct mlx5_wqe_dseg);
930 pthread_mutex_lock(&priv_list_lock);
931 TAILQ_INSERT_TAIL(&mlx5_crypto_priv_list, priv, next);
932 pthread_mutex_unlock(&priv_list_lock);
934 rte_cryptodev_pmd_probing_finish(crypto_dev);
940 mlx5_crypto_dev_remove(struct mlx5_common_device *cdev)
942 struct mlx5_crypto_priv *priv = NULL;
944 pthread_mutex_lock(&priv_list_lock);
945 TAILQ_FOREACH(priv, &mlx5_crypto_priv_list, next)
946 if (priv->crypto_dev->device == cdev->dev)
949 TAILQ_REMOVE(&mlx5_crypto_priv_list, priv, next);
950 pthread_mutex_unlock(&priv_list_lock);
952 claim_zero(mlx5_devx_cmd_destroy(priv->login_obj));
953 mlx5_crypto_uar_release(priv);
954 rte_cryptodev_pmd_destroy(priv->crypto_dev);
959 static const struct rte_pci_id mlx5_crypto_pci_id_map[] = {
961 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
962 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
969 static struct mlx5_class_driver mlx5_crypto_driver = {
970 .drv_class = MLX5_CLASS_CRYPTO,
971 .name = RTE_STR(MLX5_CRYPTO_DRIVER_NAME),
972 .id_table = mlx5_crypto_pci_id_map,
973 .probe = mlx5_crypto_dev_probe,
974 .remove = mlx5_crypto_dev_remove,
977 RTE_INIT(rte_mlx5_crypto_init)
979 pthread_mutex_init(&priv_list_lock, NULL);
981 if (mlx5_glue != NULL)
982 mlx5_class_driver_register(&mlx5_crypto_driver);
985 RTE_PMD_REGISTER_CRYPTO_DRIVER(mlx5_cryptodev_driver, mlx5_drv,
986 mlx5_crypto_driver_id);
988 RTE_LOG_REGISTER_DEFAULT(mlx5_crypto_logtype, NOTICE)
989 RTE_PMD_EXPORT_NAME(MLX5_CRYPTO_DRIVER_NAME, __COUNTER__);
990 RTE_PMD_REGISTER_PCI_TABLE(MLX5_CRYPTO_DRIVER_NAME, mlx5_crypto_pci_id_map);
991 RTE_PMD_REGISTER_KMOD_DEP(MLX5_CRYPTO_DRIVER_NAME, "* ib_uverbs & mlx5_core & mlx5_ib");