1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
5 #include <rte_malloc.h>
6 #include <rte_mempool.h>
9 #include <rte_bus_pci.h>
10 #include <rte_memory.h>
12 #include <mlx5_glue.h>
13 #include <mlx5_common.h>
14 #include <mlx5_devx_cmds.h>
15 #include <mlx5_common_os.h>
17 #include "mlx5_crypto_utils.h"
18 #include "mlx5_crypto.h"
20 #define MLX5_CRYPTO_DRIVER_NAME crypto_mlx5
21 #define MLX5_CRYPTO_LOG_NAME pmd.crypto.mlx5
22 #define MLX5_CRYPTO_MAX_QPS 1024
23 #define MLX5_CRYPTO_MAX_SEGS 56
25 #define MLX5_CRYPTO_FEATURE_FLAGS \
26 (RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | RTE_CRYPTODEV_FF_HW_ACCELERATED | \
27 RTE_CRYPTODEV_FF_IN_PLACE_SGL | RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT | \
28 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT | \
29 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT | \
30 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT | \
31 RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY | \
32 RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)
34 TAILQ_HEAD(mlx5_crypto_privs, mlx5_crypto_priv) mlx5_crypto_priv_list =
35 TAILQ_HEAD_INITIALIZER(mlx5_crypto_priv_list);
36 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
38 int mlx5_crypto_logtype;
40 uint8_t mlx5_crypto_driver_id;
42 const struct rte_cryptodev_capabilities mlx5_crypto_caps[] = {
44 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
46 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
48 .algo = RTE_CRYPTO_CIPHER_AES_XTS,
61 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES |
62 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES,
68 static const char mlx5_crypto_drv_name[] = RTE_STR(MLX5_CRYPTO_DRIVER_NAME);
70 static const struct rte_driver mlx5_drv = {
71 .name = mlx5_crypto_drv_name,
72 .alias = mlx5_crypto_drv_name
75 static struct cryptodev_driver mlx5_cryptodev_driver;
77 struct mlx5_crypto_session {
78 uint32_t bs_bpt_eo_es;
79 /**< bsf_size, bsf_p_type, encryption_order and encryption standard,
80 * saved in big endian format.
83 /**< crypto_block_size_pointer and reserved 24 bits saved in big
86 uint32_t iv_offset:16;
87 /**< Starting point for Initialisation Vector. */
88 struct mlx5_crypto_dek *dek; /**< Pointer to dek struct. */
89 uint32_t dek_id; /**< DEK ID */
93 mlx5_crypto_dev_infos_get(struct rte_cryptodev *dev,
94 struct rte_cryptodev_info *dev_info)
97 if (dev_info != NULL) {
98 dev_info->driver_id = mlx5_crypto_driver_id;
99 dev_info->feature_flags = MLX5_CRYPTO_FEATURE_FLAGS;
100 dev_info->capabilities = mlx5_crypto_caps;
101 dev_info->max_nb_queue_pairs = MLX5_CRYPTO_MAX_QPS;
102 dev_info->min_mbuf_headroom_req = 0;
103 dev_info->min_mbuf_tailroom_req = 0;
104 dev_info->sym.max_nb_sessions = 0;
106 * If 0, the device does not have any limitation in number of
107 * sessions that can be used.
113 mlx5_crypto_dev_configure(struct rte_cryptodev *dev,
114 struct rte_cryptodev_config *config)
116 struct mlx5_crypto_priv *priv = dev->data->dev_private;
118 if (config == NULL) {
119 DRV_LOG(ERR, "Invalid crypto dev configure parameters.");
122 if ((config->ff_disable & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) != 0) {
124 "Disabled symmetric crypto feature is not supported.");
127 if (mlx5_crypto_dek_setup(priv) != 0) {
128 DRV_LOG(ERR, "Dek hash list creation has failed.");
131 priv->dev_config = *config;
132 DRV_LOG(DEBUG, "Device %u was configured.", dev->driver_id);
137 mlx5_crypto_dev_stop(struct rte_cryptodev *dev)
143 mlx5_crypto_dev_start(struct rte_cryptodev *dev)
150 mlx5_crypto_dev_close(struct rte_cryptodev *dev)
152 struct mlx5_crypto_priv *priv = dev->data->dev_private;
154 mlx5_crypto_dek_unset(priv);
155 DRV_LOG(DEBUG, "Device %u was closed.", dev->driver_id);
160 mlx5_crypto_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
162 return sizeof(struct mlx5_crypto_session);
166 mlx5_crypto_sym_session_configure(struct rte_cryptodev *dev,
167 struct rte_crypto_sym_xform *xform,
168 struct rte_cryptodev_sym_session *session,
169 struct rte_mempool *mp)
171 struct mlx5_crypto_priv *priv = dev->data->dev_private;
172 struct mlx5_crypto_session *sess_private_data;
173 struct rte_crypto_cipher_xform *cipher;
174 uint8_t encryption_order;
177 if (unlikely(xform->next != NULL)) {
178 DRV_LOG(ERR, "Xform next is not supported.");
181 if (unlikely((xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) ||
182 (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_XTS))) {
183 DRV_LOG(ERR, "Only AES-XTS algorithm is supported.");
186 ret = rte_mempool_get(mp, (void *)&sess_private_data);
189 "Failed to get session %p private data from mempool.",
193 cipher = &xform->cipher;
194 sess_private_data->dek = mlx5_crypto_dek_prepare(priv, cipher);
195 if (sess_private_data->dek == NULL) {
196 rte_mempool_put(mp, sess_private_data);
197 DRV_LOG(ERR, "Failed to prepare dek.");
200 if (cipher->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
201 encryption_order = MLX5_ENCRYPTION_ORDER_ENCRYPTED_RAW_MEMORY;
203 encryption_order = MLX5_ENCRYPTION_ORDER_ENCRYPTED_RAW_WIRE;
204 sess_private_data->bs_bpt_eo_es = rte_cpu_to_be_32
205 (MLX5_BSF_SIZE_64B << MLX5_BSF_SIZE_OFFSET |
206 MLX5_BSF_P_TYPE_CRYPTO << MLX5_BSF_P_TYPE_OFFSET |
207 encryption_order << MLX5_ENCRYPTION_ORDER_OFFSET |
208 MLX5_ENCRYPTION_STANDARD_AES_XTS);
209 switch (xform->cipher.dataunit_len) {
211 sess_private_data->bsp_res = 0;
214 sess_private_data->bsp_res = rte_cpu_to_be_32
215 ((uint32_t)MLX5_BLOCK_SIZE_512B <<
216 MLX5_BLOCK_SIZE_OFFSET);
219 sess_private_data->bsp_res = rte_cpu_to_be_32
220 ((uint32_t)MLX5_BLOCK_SIZE_4096B <<
221 MLX5_BLOCK_SIZE_OFFSET);
224 DRV_LOG(ERR, "Cipher data unit length is not supported.");
227 sess_private_data->iv_offset = cipher->iv.offset;
228 sess_private_data->dek_id =
229 rte_cpu_to_be_32(sess_private_data->dek->obj->id &
231 set_sym_session_private_data(session, dev->driver_id,
233 DRV_LOG(DEBUG, "Session %p was configured.", sess_private_data);
238 mlx5_crypto_sym_session_clear(struct rte_cryptodev *dev,
239 struct rte_cryptodev_sym_session *sess)
241 struct mlx5_crypto_priv *priv = dev->data->dev_private;
242 struct mlx5_crypto_session *spriv = get_sym_session_private_data(sess,
245 if (unlikely(spriv == NULL)) {
246 DRV_LOG(ERR, "Failed to get session %p private data.", spriv);
249 mlx5_crypto_dek_destroy(priv, spriv->dek);
250 set_sym_session_private_data(sess, dev->driver_id, NULL);
251 rte_mempool_put(rte_mempool_from_obj(spriv), spriv);
252 DRV_LOG(DEBUG, "Session %p was cleared.", spriv);
256 mlx5_crypto_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id)
258 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
260 if (qp->qp_obj != NULL)
261 claim_zero(mlx5_devx_cmd_destroy(qp->qp_obj));
262 if (qp->umem_obj != NULL)
263 claim_zero(mlx5_glue->devx_umem_dereg(qp->umem_obj));
264 if (qp->umem_buf != NULL)
265 rte_free(qp->umem_buf);
266 mlx5_mr_btree_free(&qp->mr_ctrl.cache_bh);
267 mlx5_devx_cq_destroy(&qp->cq_obj);
269 dev->data->queue_pairs[qp_id] = NULL;
274 mlx5_crypto_qp2rts(struct mlx5_crypto_qp *qp)
277 * In Order to configure self loopback, when calling these functions the
278 * remote QP id that is used is the id of the same QP.
280 if (mlx5_devx_cmd_modify_qp_state(qp->qp_obj, MLX5_CMD_OP_RST2INIT_QP,
282 DRV_LOG(ERR, "Failed to modify QP to INIT state(%u).",
286 if (mlx5_devx_cmd_modify_qp_state(qp->qp_obj, MLX5_CMD_OP_INIT2RTR_QP,
288 DRV_LOG(ERR, "Failed to modify QP to RTR state(%u).",
292 if (mlx5_devx_cmd_modify_qp_state(qp->qp_obj, MLX5_CMD_OP_RTR2RTS_QP,
294 DRV_LOG(ERR, "Failed to modify QP to RTS state(%u).",
301 static __rte_noinline uint32_t
302 mlx5_crypto_get_block_size(struct rte_crypto_op *op)
304 uint32_t bl = op->sym->cipher.data.length;
308 return RTE_BE32(MLX5_BLOCK_SIZE_1MB << MLX5_BLOCK_SIZE_OFFSET);
310 return RTE_BE32(MLX5_BLOCK_SIZE_4096B <<
311 MLX5_BLOCK_SIZE_OFFSET);
313 return RTE_BE32(MLX5_BLOCK_SIZE_512B << MLX5_BLOCK_SIZE_OFFSET);
315 DRV_LOG(ERR, "Unknown block size: %u.", bl);
321 * Query LKey from a packet buffer for QP. If not found, add the mempool.
324 * Pointer to the priv object.
328 * Pointer to per-queue MR control structure.
330 * Mbuf offload features.
333 * Searched LKey on success, UINT32_MAX on no match.
335 static __rte_always_inline uint32_t
336 mlx5_crypto_addr2mr(struct mlx5_crypto_priv *priv, uintptr_t addr,
337 struct mlx5_mr_ctrl *mr_ctrl, uint64_t ol_flags)
341 /* Check generation bit to see if there's any change on existing MRs. */
342 if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
343 mlx5_mr_flush_local_cache(mr_ctrl);
344 /* Linear search on MR cache array. */
345 lkey = mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru,
346 MLX5_MR_CACHE_N, addr);
347 if (likely(lkey != UINT32_MAX))
349 /* Take slower bottom-half on miss. */
350 return mlx5_mr_addr2mr_bh(priv->pd, 0, &priv->mr_scache, mr_ctrl, addr,
351 !!(ol_flags & EXT_ATTACHED_MBUF));
354 static __rte_always_inline uint32_t
355 mlx5_crypto_klm_set(struct mlx5_crypto_priv *priv, struct mlx5_crypto_qp *qp,
356 struct rte_mbuf *mbuf, struct mlx5_wqe_dseg *klm,
357 uint32_t offset, uint32_t *remain)
359 uint32_t data_len = (rte_pktmbuf_data_len(mbuf) - offset);
360 uintptr_t addr = rte_pktmbuf_mtod_offset(mbuf, uintptr_t, offset);
362 if (data_len > *remain)
365 klm->bcount = rte_cpu_to_be_32(data_len);
366 klm->pbuf = rte_cpu_to_be_64(addr);
367 klm->lkey = mlx5_crypto_addr2mr(priv, addr, &qp->mr_ctrl,
373 static __rte_always_inline uint32_t
374 mlx5_crypto_klms_set(struct mlx5_crypto_priv *priv, struct mlx5_crypto_qp *qp,
375 struct rte_crypto_op *op, struct rte_mbuf *mbuf,
376 struct mlx5_wqe_dseg *klm)
378 uint32_t remain_len = op->sym->cipher.data.length;
379 uint32_t nb_segs = mbuf->nb_segs;
382 /* First mbuf needs to take the cipher offset. */
383 if (unlikely(mlx5_crypto_klm_set(priv, qp, mbuf, klm,
384 op->sym->cipher.data.offset, &remain_len) == UINT32_MAX)) {
385 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
391 if (unlikely(mbuf == NULL || nb_segs == 0)) {
392 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
395 if (unlikely(mlx5_crypto_klm_set(priv, qp, mbuf, ++klm, 0,
396 &remain_len) == UINT32_MAX)) {
397 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
405 static __rte_always_inline int
406 mlx5_crypto_wqe_set(struct mlx5_crypto_priv *priv,
407 struct mlx5_crypto_qp *qp,
408 struct rte_crypto_op *op,
409 struct mlx5_umr_wqe *umr)
411 struct mlx5_crypto_session *sess = get_sym_session_private_data
412 (op->sym->session, mlx5_crypto_driver_id);
413 struct mlx5_wqe_cseg *cseg = &umr->ctr;
414 struct mlx5_wqe_mkey_cseg *mkc = &umr->mkc;
415 struct mlx5_wqe_dseg *klms = &umr->kseg[0];
416 struct mlx5_wqe_umr_bsf_seg *bsf = ((struct mlx5_wqe_umr_bsf_seg *)
417 RTE_PTR_ADD(umr, priv->umr_wqe_size)) - 1;
419 bool ipl = op->sym->m_dst == NULL || op->sym->m_dst == op->sym->m_src;
421 uint32_t klm_n = mlx5_crypto_klms_set(priv, qp, op,
422 ipl ? op->sym->m_src : op->sym->m_dst, klms);
424 if (unlikely(klm_n == 0))
426 bsf->bs_bpt_eo_es = sess->bs_bpt_eo_es;
427 if (unlikely(!sess->bsp_res)) {
428 bsf->bsp_res = mlx5_crypto_get_block_size(op);
429 if (unlikely(bsf->bsp_res == UINT32_MAX)) {
430 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
434 bsf->bsp_res = sess->bsp_res;
436 bsf->raw_data_size = rte_cpu_to_be_32(op->sym->cipher.data.length);
437 memcpy(bsf->xts_initial_tweak,
438 rte_crypto_op_ctod_offset(op, uint8_t *, sess->iv_offset), 16);
439 bsf->res_dp = sess->dek_id;
440 mkc->len = rte_cpu_to_be_64(op->sym->cipher.data.length);
441 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) | MLX5_OPCODE_UMR);
442 qp->db_pi += priv->umr_wqe_stride;
443 /* Set RDMA_WRITE WQE. */
444 cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
445 klms = RTE_PTR_ADD(cseg, sizeof(struct mlx5_rdma_write_wqe));
447 klm_n = mlx5_crypto_klms_set(priv, qp, op, op->sym->m_src,
449 if (unlikely(klm_n == 0))
452 memcpy(klms, &umr->kseg[0], sizeof(*klms) * klm_n);
455 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj->id << 8) | ds);
456 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) |
457 MLX5_OPCODE_RDMA_WRITE);
458 ds = RTE_ALIGN(ds, 4);
459 qp->db_pi += ds >> 2;
460 /* Set NOP WQE if needed. */
461 if (priv->max_rdmar_ds > ds) {
463 ds = priv->max_rdmar_ds - ds;
464 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj->id << 8) | ds);
465 cseg->opcode = rte_cpu_to_be_32((qp->db_pi << 8) |
467 qp->db_pi += ds >> 2; /* Here, DS is 4 aligned for sure. */
469 qp->wqe = (uint8_t *)cseg;
473 static __rte_always_inline void
474 mlx5_crypto_uar_write(uint64_t val, struct mlx5_crypto_priv *priv)
477 *priv->uar_addr = val;
478 #else /* !RTE_ARCH_64 */
479 rte_spinlock_lock(&priv->uar32_sl);
480 *(volatile uint32_t *)priv->uar_addr = val;
482 *((volatile uint32_t *)priv->uar_addr + 1) = val >> 32;
483 rte_spinlock_unlock(&priv->uar32_sl);
488 mlx5_crypto_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
491 struct mlx5_crypto_qp *qp = queue_pair;
492 struct mlx5_crypto_priv *priv = qp->priv;
493 struct mlx5_umr_wqe *umr;
494 struct rte_crypto_op *op;
495 uint16_t mask = qp->entries_n - 1;
496 uint16_t remain = qp->entries_n - (qp->pi - qp->ci);
502 if (unlikely(remain == 0))
506 umr = RTE_PTR_ADD(qp->umem_buf, priv->wqe_set_size * qp->pi);
507 if (unlikely(mlx5_crypto_wqe_set(priv, qp, op, umr) == 0)) {
508 qp->stats.enqueue_err_count++;
509 if (remain != nb_ops) {
510 qp->stats.enqueued_count -= remain;
515 qp->ops[qp->pi] = op;
516 qp->pi = (qp->pi + 1) & mask;
518 qp->stats.enqueued_count += nb_ops;
520 qp->db_rec[MLX5_SND_DBR] = rte_cpu_to_be_32(qp->db_pi);
522 mlx5_crypto_uar_write(*(volatile uint64_t *)qp->wqe, qp->priv);
527 static __rte_noinline void
528 mlx5_crypto_cqe_err_handle(struct mlx5_crypto_qp *qp, struct rte_crypto_op *op)
530 const uint32_t idx = qp->ci & (qp->entries_n - 1);
531 volatile struct mlx5_err_cqe *cqe = (volatile struct mlx5_err_cqe *)
532 &qp->cq_obj.cqes[idx];
534 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
535 qp->stats.dequeue_err_count++;
536 DRV_LOG(ERR, "CQE ERR:%x.\n", rte_be_to_cpu_32(cqe->syndrome));
540 mlx5_crypto_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
543 struct mlx5_crypto_qp *qp = queue_pair;
544 volatile struct mlx5_cqe *restrict cqe;
545 struct rte_crypto_op *restrict op;
546 const unsigned int cq_size = qp->entries_n;
547 const unsigned int mask = cq_size - 1;
549 uint32_t next_idx = qp->ci & mask;
550 const uint16_t max = RTE_MIN((uint16_t)(qp->pi - qp->ci), nb_ops);
554 if (unlikely(max == 0))
558 next_idx = (qp->ci + 1) & mask;
560 cqe = &qp->cq_obj.cqes[idx];
561 ret = check_cqe(cqe, cq_size, qp->ci);
563 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
564 if (unlikely(ret != MLX5_CQE_STATUS_HW_OWN))
565 mlx5_crypto_cqe_err_handle(qp, op);
568 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
572 if (likely(i != 0)) {
574 qp->cq_obj.db_rec[0] = rte_cpu_to_be_32(qp->ci);
575 qp->stats.dequeued_count += i;
581 mlx5_crypto_qp_init(struct mlx5_crypto_priv *priv, struct mlx5_crypto_qp *qp)
585 for (i = 0 ; i < qp->entries_n; i++) {
586 struct mlx5_wqe_cseg *cseg = RTE_PTR_ADD(qp->umem_buf, i *
588 struct mlx5_wqe_umr_cseg *ucseg = (struct mlx5_wqe_umr_cseg *)
590 struct mlx5_wqe_umr_bsf_seg *bsf =
591 (struct mlx5_wqe_umr_bsf_seg *)(RTE_PTR_ADD(cseg,
592 priv->umr_wqe_size)) - 1;
593 struct mlx5_wqe_rseg *rseg;
596 cseg->sq_ds = rte_cpu_to_be_32((qp->qp_obj->id << 8) |
597 (priv->umr_wqe_size / MLX5_WSEG_SIZE));
598 cseg->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
599 MLX5_COMP_MODE_OFFSET);
600 cseg->misc = rte_cpu_to_be_32(qp->mkey[i]->id);
601 ucseg->if_cf_toe_cq_res = RTE_BE32(1u << MLX5_UMRC_IF_OFFSET);
602 ucseg->mkey_mask = RTE_BE64(1u << 0); /* Mkey length bit. */
603 ucseg->ko_to_bs = rte_cpu_to_be_32
604 ((RTE_ALIGN(priv->max_segs_num, 4u) <<
605 MLX5_UMRC_KO_OFFSET) | (4 << MLX5_UMRC_TO_BS_OFFSET));
606 bsf->keytag = priv->keytag;
607 /* Init RDMA WRITE WQE. */
608 cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
609 cseg->flags = RTE_BE32((MLX5_COMP_ALWAYS <<
610 MLX5_COMP_MODE_OFFSET) |
611 MLX5_WQE_CTRL_INITIATOR_SMALL_FENCE);
612 rseg = (struct mlx5_wqe_rseg *)(cseg + 1);
613 rseg->rkey = rte_cpu_to_be_32(qp->mkey[i]->id);
618 mlx5_crypto_indirect_mkeys_prepare(struct mlx5_crypto_priv *priv,
619 struct mlx5_crypto_qp *qp)
621 struct mlx5_umr_wqe *umr;
623 struct mlx5_devx_mkey_attr attr = {
628 .klm_num = RTE_ALIGN(priv->max_segs_num, 4),
631 for (umr = (struct mlx5_umr_wqe *)qp->umem_buf, i = 0;
632 i < qp->entries_n; i++, umr = RTE_PTR_ADD(umr, priv->wqe_set_size)) {
633 attr.klm_array = (struct mlx5_klm *)&umr->kseg[0];
634 qp->mkey[i] = mlx5_devx_cmd_mkey_create(priv->ctx, &attr);
636 DRV_LOG(ERR, "Failed to allocate indirect mkey.");
644 mlx5_crypto_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
645 const struct rte_cryptodev_qp_conf *qp_conf,
648 struct mlx5_crypto_priv *priv = dev->data->dev_private;
649 struct mlx5_devx_qp_attr attr = {0};
650 struct mlx5_crypto_qp *qp;
651 uint16_t log_nb_desc = rte_log2_u32(qp_conf->nb_descriptors);
652 uint32_t umem_size = RTE_BIT32(log_nb_desc) *
654 sizeof(*qp->db_rec) * 2;
655 uint32_t alloc_size = sizeof(*qp);
656 struct mlx5_devx_cq_attr cq_attr = {
657 .uar_page_id = mlx5_os_get_devx_uar_page_id(priv->uar),
660 if (dev->data->queue_pairs[qp_id] != NULL)
661 mlx5_crypto_queue_pair_release(dev, qp_id);
662 alloc_size = RTE_ALIGN(alloc_size, RTE_CACHE_LINE_SIZE);
663 alloc_size += (sizeof(struct rte_crypto_op *) +
664 sizeof(struct mlx5_devx_obj *)) *
665 RTE_BIT32(log_nb_desc);
666 qp = rte_zmalloc_socket(__func__, alloc_size, RTE_CACHE_LINE_SIZE,
669 DRV_LOG(ERR, "Failed to allocate QP memory.");
673 if (mlx5_devx_cq_create(priv->ctx, &qp->cq_obj, log_nb_desc,
674 &cq_attr, socket_id) != 0) {
675 DRV_LOG(ERR, "Failed to create CQ.");
678 qp->umem_buf = rte_zmalloc_socket(__func__, umem_size, 4096, socket_id);
679 if (qp->umem_buf == NULL) {
680 DRV_LOG(ERR, "Failed to allocate QP umem.");
684 qp->umem_obj = mlx5_glue->devx_umem_reg(priv->ctx,
685 (void *)(uintptr_t)qp->umem_buf,
687 IBV_ACCESS_LOCAL_WRITE);
688 if (qp->umem_obj == NULL) {
689 DRV_LOG(ERR, "Failed to register QP umem.");
692 if (mlx5_mr_btree_init(&qp->mr_ctrl.cache_bh, MLX5_MR_BTREE_CACHE_N,
693 priv->dev_config.socket_id) != 0) {
694 DRV_LOG(ERR, "Cannot allocate MR Btree for qp %u.",
699 qp->mr_ctrl.dev_gen_ptr = &priv->mr_scache.dev_gen;
701 attr.uar_index = mlx5_os_get_devx_uar_page_id(priv->uar);
702 attr.cqn = qp->cq_obj.cq->id;
703 attr.log_page_size = rte_log2_u32(sysconf(_SC_PAGESIZE));
705 attr.sq_size = RTE_BIT32(log_nb_desc);
706 attr.dbr_umem_valid = 1;
707 attr.wq_umem_id = qp->umem_obj->umem_id;
708 attr.wq_umem_offset = 0;
709 attr.dbr_umem_id = qp->umem_obj->umem_id;
710 attr.dbr_address = RTE_BIT64(log_nb_desc) * priv->wqe_set_size;
711 qp->qp_obj = mlx5_devx_cmd_create_qp(priv->ctx, &attr);
712 if (qp->qp_obj == NULL) {
713 DRV_LOG(ERR, "Failed to create QP(%u).", rte_errno);
716 qp->db_rec = RTE_PTR_ADD(qp->umem_buf, (uintptr_t)attr.dbr_address);
717 if (mlx5_crypto_qp2rts(qp))
719 qp->mkey = (struct mlx5_devx_obj **)RTE_ALIGN((uintptr_t)(qp + 1),
720 RTE_CACHE_LINE_SIZE);
721 qp->ops = (struct rte_crypto_op **)(qp->mkey + RTE_BIT32(log_nb_desc));
722 qp->entries_n = 1 << log_nb_desc;
723 if (mlx5_crypto_indirect_mkeys_prepare(priv, qp)) {
724 DRV_LOG(ERR, "Cannot allocate indirect memory regions.");
728 mlx5_crypto_qp_init(priv, qp);
730 dev->data->queue_pairs[qp_id] = qp;
733 mlx5_crypto_queue_pair_release(dev, qp_id);
738 mlx5_crypto_stats_get(struct rte_cryptodev *dev,
739 struct rte_cryptodev_stats *stats)
743 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
744 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
746 stats->enqueued_count += qp->stats.enqueued_count;
747 stats->dequeued_count += qp->stats.dequeued_count;
748 stats->enqueue_err_count += qp->stats.enqueue_err_count;
749 stats->dequeue_err_count += qp->stats.dequeue_err_count;
754 mlx5_crypto_stats_reset(struct rte_cryptodev *dev)
758 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
759 struct mlx5_crypto_qp *qp = dev->data->queue_pairs[qp_id];
761 memset(&qp->stats, 0, sizeof(qp->stats));
765 static struct rte_cryptodev_ops mlx5_crypto_ops = {
766 .dev_configure = mlx5_crypto_dev_configure,
767 .dev_start = mlx5_crypto_dev_start,
768 .dev_stop = mlx5_crypto_dev_stop,
769 .dev_close = mlx5_crypto_dev_close,
770 .dev_infos_get = mlx5_crypto_dev_infos_get,
771 .stats_get = mlx5_crypto_stats_get,
772 .stats_reset = mlx5_crypto_stats_reset,
773 .queue_pair_setup = mlx5_crypto_queue_pair_setup,
774 .queue_pair_release = mlx5_crypto_queue_pair_release,
775 .sym_session_get_size = mlx5_crypto_sym_session_get_size,
776 .sym_session_configure = mlx5_crypto_sym_session_configure,
777 .sym_session_clear = mlx5_crypto_sym_session_clear,
778 .sym_get_raw_dp_ctx_size = NULL,
779 .sym_configure_raw_dp_ctx = NULL,
783 mlx5_crypto_hw_global_release(struct mlx5_crypto_priv *priv)
785 if (priv->pd != NULL) {
786 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
789 if (priv->uar != NULL) {
790 mlx5_glue->devx_free_uar(priv->uar);
796 mlx5_crypto_pd_create(struct mlx5_crypto_priv *priv)
798 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
799 struct mlx5dv_obj obj;
800 struct mlx5dv_pd pd_info;
803 priv->pd = mlx5_glue->alloc_pd(priv->ctx);
804 if (priv->pd == NULL) {
805 DRV_LOG(ERR, "Failed to allocate PD.");
806 return errno ? -errno : -ENOMEM;
808 obj.pd.in = priv->pd;
809 obj.pd.out = &pd_info;
810 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
812 DRV_LOG(ERR, "Fail to get PD object info.");
813 mlx5_glue->dealloc_pd(priv->pd);
817 priv->pdn = pd_info.pdn;
821 DRV_LOG(ERR, "Cannot get pdn - no DV support.");
823 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
827 mlx5_crypto_hw_global_prepare(struct mlx5_crypto_priv *priv)
829 if (mlx5_crypto_pd_create(priv) != 0)
831 priv->uar = mlx5_devx_alloc_uar(priv->ctx, -1);
833 priv->uar_addr = mlx5_os_get_devx_uar_reg_addr(priv->uar);
834 if (priv->uar == NULL || priv->uar_addr == NULL) {
836 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
837 DRV_LOG(ERR, "Failed to allocate UAR.");
845 mlx5_crypto_args_check_handler(const char *key, const char *val, void *opaque)
847 struct mlx5_crypto_devarg_params *devarg_prms = opaque;
848 struct mlx5_devx_crypto_login_attr *attr = &devarg_prms->login_attr;
854 if (strcmp(key, "class") == 0)
856 if (strcmp(key, "wcs_file") == 0) {
857 file = fopen(val, "rb");
862 for (i = 0 ; i < MLX5_CRYPTO_CREDENTIAL_SIZE ; i++) {
863 ret = fscanf(file, "%02hhX", &attr->credential[i]);
867 "Failed to read credential from file.");
873 devarg_prms->login_devarg = true;
877 tmp = strtoul(val, NULL, 0);
879 DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
882 if (strcmp(key, "max_segs_num") == 0) {
883 if (!tmp || tmp > MLX5_CRYPTO_MAX_SEGS) {
884 DRV_LOG(WARNING, "Invalid max_segs_num: %d, should"
886 (uint32_t)tmp, MLX5_CRYPTO_MAX_SEGS);
890 devarg_prms->max_segs_num = (uint32_t)tmp;
891 } else if (strcmp(key, "import_kek_id") == 0) {
892 attr->session_import_kek_ptr = (uint32_t)tmp;
893 } else if (strcmp(key, "credential_id") == 0) {
894 attr->credential_pointer = (uint32_t)tmp;
895 } else if (strcmp(key, "keytag") == 0) {
896 devarg_prms->keytag = tmp;
898 DRV_LOG(WARNING, "Invalid key %s.", key);
904 mlx5_crypto_parse_devargs(struct rte_devargs *devargs,
905 struct mlx5_crypto_devarg_params *devarg_prms)
907 struct mlx5_devx_crypto_login_attr *attr = &devarg_prms->login_attr;
908 struct rte_kvargs *kvlist;
910 /* Default values. */
911 attr->credential_pointer = 0;
912 attr->session_import_kek_ptr = 0;
913 devarg_prms->keytag = 0;
914 devarg_prms->max_segs_num = 8;
915 if (devargs == NULL) {
917 "No login devargs in order to enable crypto operations in the device.");
921 kvlist = rte_kvargs_parse(devargs->args, NULL);
922 if (kvlist == NULL) {
923 DRV_LOG(ERR, "Failed to parse devargs.");
927 if (rte_kvargs_process(kvlist, NULL, mlx5_crypto_args_check_handler,
929 DRV_LOG(ERR, "Devargs handler function Failed.");
930 rte_kvargs_free(kvlist);
934 rte_kvargs_free(kvlist);
935 if (devarg_prms->login_devarg == false) {
937 "No login credential devarg in order to enable crypto operations "
946 * Callback for memory event.
956 mlx5_crypto_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
957 size_t len, void *arg __rte_unused)
959 struct mlx5_crypto_priv *priv;
961 /* Must be called from the primary process. */
962 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
963 switch (event_type) {
964 case RTE_MEM_EVENT_FREE:
965 pthread_mutex_lock(&priv_list_lock);
966 /* Iterate all the existing mlx5 devices. */
967 TAILQ_FOREACH(priv, &mlx5_crypto_priv_list, next)
968 mlx5_free_mr_by_addr(&priv->mr_scache,
969 priv->ctx->device->name,
971 pthread_mutex_unlock(&priv_list_lock);
973 case RTE_MEM_EVENT_ALLOC:
980 mlx5_crypto_dev_probe(struct rte_device *dev)
982 struct ibv_device *ibv;
983 struct rte_cryptodev *crypto_dev;
984 struct ibv_context *ctx;
985 struct mlx5_devx_obj *login;
986 struct mlx5_crypto_priv *priv;
987 struct mlx5_crypto_devarg_params devarg_prms = { 0 };
988 struct mlx5_hca_attr attr = { 0 };
989 struct rte_cryptodev_pmd_init_params init_params = {
991 .private_data_size = sizeof(struct mlx5_crypto_priv),
992 .socket_id = dev->numa_node,
993 .max_nb_queue_pairs =
994 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
996 uint16_t rdmw_wqe_size;
999 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1000 DRV_LOG(ERR, "Non-primary process type is not supported.");
1001 rte_errno = ENOTSUP;
1004 ibv = mlx5_os_get_ibv_dev(dev);
1007 ctx = mlx5_glue->dv_open_device(ibv);
1009 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
1013 if (mlx5_devx_cmd_query_hca_attr(ctx, &attr) != 0 ||
1014 attr.crypto == 0 || attr.aes_xts == 0) {
1015 DRV_LOG(ERR, "Not enough capabilities to support crypto "
1016 "operations, maybe old FW/OFED version?");
1017 claim_zero(mlx5_glue->close_device(ctx));
1018 rte_errno = ENOTSUP;
1021 ret = mlx5_crypto_parse_devargs(dev->devargs, &devarg_prms);
1023 DRV_LOG(ERR, "Failed to parse devargs.");
1024 claim_zero(mlx5_glue->close_device(ctx));
1027 login = mlx5_devx_cmd_create_crypto_login_obj(ctx,
1028 &devarg_prms.login_attr);
1029 if (login == NULL) {
1030 DRV_LOG(ERR, "Failed to configure login.");
1031 claim_zero(mlx5_glue->close_device(ctx));
1034 crypto_dev = rte_cryptodev_pmd_create(ibv->name, dev,
1036 if (crypto_dev == NULL) {
1037 DRV_LOG(ERR, "Failed to create device \"%s\".", ibv->name);
1038 claim_zero(mlx5_glue->close_device(ctx));
1042 "Crypto device %s was created successfully.", ibv->name);
1043 crypto_dev->dev_ops = &mlx5_crypto_ops;
1044 crypto_dev->dequeue_burst = mlx5_crypto_dequeue_burst;
1045 crypto_dev->enqueue_burst = mlx5_crypto_enqueue_burst;
1046 crypto_dev->feature_flags = MLX5_CRYPTO_FEATURE_FLAGS;
1047 crypto_dev->driver_id = mlx5_crypto_driver_id;
1048 priv = crypto_dev->data->dev_private;
1050 priv->login_obj = login;
1051 priv->crypto_dev = crypto_dev;
1052 if (mlx5_crypto_hw_global_prepare(priv) != 0) {
1053 rte_cryptodev_pmd_destroy(priv->crypto_dev);
1054 claim_zero(mlx5_glue->close_device(priv->ctx));
1057 if (mlx5_mr_btree_init(&priv->mr_scache.cache,
1058 MLX5_MR_BTREE_CACHE_N * 2, rte_socket_id()) != 0) {
1059 DRV_LOG(ERR, "Failed to allocate shared cache MR memory.");
1060 mlx5_crypto_hw_global_release(priv);
1061 rte_cryptodev_pmd_destroy(priv->crypto_dev);
1062 claim_zero(mlx5_glue->close_device(priv->ctx));
1066 priv->mr_scache.reg_mr_cb = mlx5_common_verbs_reg_mr;
1067 priv->mr_scache.dereg_mr_cb = mlx5_common_verbs_dereg_mr;
1068 priv->keytag = rte_cpu_to_be_64(devarg_prms.keytag);
1069 priv->max_segs_num = devarg_prms.max_segs_num;
1070 priv->umr_wqe_size = sizeof(struct mlx5_wqe_umr_bsf_seg) +
1071 sizeof(struct mlx5_umr_wqe) +
1072 RTE_ALIGN(priv->max_segs_num, 4) *
1073 sizeof(struct mlx5_wqe_dseg);
1074 rdmw_wqe_size = sizeof(struct mlx5_rdma_write_wqe) +
1075 sizeof(struct mlx5_wqe_dseg) *
1076 (priv->max_segs_num <= 2 ? 2 : 2 +
1077 RTE_ALIGN(priv->max_segs_num - 2, 4));
1078 priv->wqe_set_size = priv->umr_wqe_size + rdmw_wqe_size;
1079 priv->umr_wqe_stride = priv->umr_wqe_size / MLX5_SEND_WQE_BB;
1080 priv->max_rdmar_ds = rdmw_wqe_size / sizeof(struct mlx5_wqe_dseg);
1081 /* Register callback function for global shared MR cache management. */
1082 if (TAILQ_EMPTY(&mlx5_crypto_priv_list))
1083 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
1084 mlx5_crypto_mr_mem_event_cb,
1086 pthread_mutex_lock(&priv_list_lock);
1087 TAILQ_INSERT_TAIL(&mlx5_crypto_priv_list, priv, next);
1088 pthread_mutex_unlock(&priv_list_lock);
1093 mlx5_crypto_dev_remove(struct rte_device *dev)
1095 struct mlx5_crypto_priv *priv = NULL;
1097 pthread_mutex_lock(&priv_list_lock);
1098 TAILQ_FOREACH(priv, &mlx5_crypto_priv_list, next)
1099 if (priv->crypto_dev->device == dev)
1102 TAILQ_REMOVE(&mlx5_crypto_priv_list, priv, next);
1103 pthread_mutex_unlock(&priv_list_lock);
1105 if (TAILQ_EMPTY(&mlx5_crypto_priv_list))
1106 rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB",
1108 mlx5_mr_release_cache(&priv->mr_scache);
1109 mlx5_crypto_hw_global_release(priv);
1110 rte_cryptodev_pmd_destroy(priv->crypto_dev);
1111 claim_zero(mlx5_devx_cmd_destroy(priv->login_obj));
1112 claim_zero(mlx5_glue->close_device(priv->ctx));
1117 static const struct rte_pci_id mlx5_crypto_pci_id_map[] = {
1119 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1120 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
1127 static struct mlx5_class_driver mlx5_crypto_driver = {
1128 .drv_class = MLX5_CLASS_CRYPTO,
1129 .name = RTE_STR(MLX5_CRYPTO_DRIVER_NAME),
1130 .id_table = mlx5_crypto_pci_id_map,
1131 .probe = mlx5_crypto_dev_probe,
1132 .remove = mlx5_crypto_dev_remove,
1135 RTE_INIT(rte_mlx5_crypto_init)
1138 if (mlx5_glue != NULL)
1139 mlx5_class_driver_register(&mlx5_crypto_driver);
1142 RTE_PMD_REGISTER_CRYPTO_DRIVER(mlx5_cryptodev_driver, mlx5_drv,
1143 mlx5_crypto_driver_id);
1145 RTE_LOG_REGISTER_DEFAULT(mlx5_crypto_logtype, NOTICE)
1146 RTE_PMD_EXPORT_NAME(MLX5_CRYPTO_DRIVER_NAME, __COUNTER__);
1147 RTE_PMD_REGISTER_PCI_TABLE(MLX5_CRYPTO_DRIVER_NAME, mlx5_crypto_pci_id_map);
1148 RTE_PMD_REGISTER_KMOD_DEP(MLX5_CRYPTO_DRIVER_NAME, "* ib_uverbs & mlx5_core & mlx5_ib");