1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
5 #include <rte_cryptodev.h>
6 #include <cryptodev_pmd.h>
12 #include "cnxk_cryptodev.h"
13 #include "cnxk_cryptodev_ops.h"
14 #include "cnxk_cryptodev_capabilities.h"
17 #define CNXK_CPT_MAX_ASYM_OP_NUM_PARAMS 5
18 #define CNXK_CPT_MAX_ASYM_OP_MOD_LEN 1024
21 cnxk_cpt_get_mlen(void)
26 len = 2 * sizeof(uint64_t);
27 len += ROC_SE_MAX_MAC_LEN * sizeof(uint8_t);
29 len += ROC_SE_OFF_CTRL_LEN + ROC_CPT_AES_CBC_IV_LEN;
30 len += RTE_ALIGN_CEIL((ROC_SE_SG_LIST_HDR_SIZE +
31 (RTE_ALIGN_CEIL(ROC_SE_MAX_SG_IN_OUT_CNT, 4) >>
32 2) * ROC_SE_SG_ENTRY_SIZE),
39 cnxk_cpt_asym_get_mlen(void)
44 len = sizeof(uint64_t);
46 /* Get meta len for asymmetric operations */
47 len += CNXK_CPT_MAX_ASYM_OP_NUM_PARAMS * CNXK_CPT_MAX_ASYM_OP_MOD_LEN;
53 cnxk_cpt_dev_config(struct rte_cryptodev *dev,
54 struct rte_cryptodev_config *conf)
56 struct cnxk_cpt_vf *vf = dev->data->dev_private;
57 struct roc_cpt *roc_cpt = &vf->cpt;
58 uint16_t nb_lf_avail, nb_lf;
61 dev->feature_flags = cnxk_cpt_default_ff_get() & ~conf->ff_disable;
63 nb_lf_avail = roc_cpt->nb_lf_avail;
64 nb_lf = conf->nb_queue_pairs;
66 if (nb_lf > nb_lf_avail)
69 ret = roc_cpt_dev_configure(roc_cpt, nb_lf);
71 plt_err("Could not configure device");
75 if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) {
76 /* Initialize shared FPM table */
77 ret = roc_ae_fpm_get(vf->cnxk_fpm_iova);
79 plt_err("Could not get FPM table");
83 /* Init EC grp table */
84 ret = roc_ae_ec_grp_get(vf->ec_grp);
86 plt_err("Could not get EC grp table");
96 cnxk_cpt_dev_start(struct rte_cryptodev *dev)
98 struct cnxk_cpt_vf *vf = dev->data->dev_private;
99 struct roc_cpt *roc_cpt = &vf->cpt;
100 uint16_t nb_lf = roc_cpt->nb_lf;
103 for (qp_id = 0; qp_id < nb_lf; qp_id++)
104 roc_cpt_iq_enable(roc_cpt->lf[qp_id]);
110 cnxk_cpt_dev_stop(struct rte_cryptodev *dev)
112 struct cnxk_cpt_vf *vf = dev->data->dev_private;
113 struct roc_cpt *roc_cpt = &vf->cpt;
114 uint16_t nb_lf = roc_cpt->nb_lf;
117 for (qp_id = 0; qp_id < nb_lf; qp_id++)
118 roc_cpt_iq_disable(roc_cpt->lf[qp_id]);
122 cnxk_cpt_dev_close(struct rte_cryptodev *dev)
124 struct cnxk_cpt_vf *vf = dev->data->dev_private;
128 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
129 ret = cnxk_cpt_queue_pair_release(dev, i);
131 plt_err("Could not release queue pair %u", i);
136 if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) {
141 roc_cpt_dev_clear(&vf->cpt);
147 cnxk_cpt_dev_info_get(struct rte_cryptodev *dev,
148 struct rte_cryptodev_info *info)
150 struct cnxk_cpt_vf *vf = dev->data->dev_private;
151 struct roc_cpt *roc_cpt = &vf->cpt;
153 info->max_nb_queue_pairs = roc_cpt->nb_lf_avail;
154 info->feature_flags = cnxk_cpt_default_ff_get();
155 info->capabilities = cnxk_crypto_capabilities_get(vf);
156 info->sym.max_nb_sessions = 0;
157 info->min_mbuf_headroom_req = CNXK_CPT_MIN_HEADROOM_REQ;
158 info->min_mbuf_tailroom_req = CNXK_CPT_MIN_TAILROOM_REQ;
162 qp_memzone_name_get(char *name, int size, int dev_id, int qp_id)
164 snprintf(name, size, "cnxk_cpt_pq_mem_%u:%u", dev_id, qp_id);
168 cnxk_cpt_metabuf_mempool_create(const struct rte_cryptodev *dev,
169 struct cnxk_cpt_qp *qp, uint8_t qp_id,
170 uint32_t nb_elements)
172 char mempool_name[RTE_MEMPOOL_NAMESIZE];
173 struct cpt_qp_meta_info *meta_info;
174 struct rte_mempool *pool;
178 if (dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
180 mlen = cnxk_cpt_get_mlen();
183 if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) {
185 /* Get meta len required for asymmetric operations */
186 mlen = RTE_MAX(mlen, cnxk_cpt_asym_get_mlen());
189 cache_sz = RTE_MIN(RTE_MEMPOOL_CACHE_MAX_SIZE, nb_elements / 1.5);
191 /* Allocate mempool */
193 snprintf(mempool_name, RTE_MEMPOOL_NAMESIZE, "cnxk_cpt_mb_%u:%u",
194 dev->data->dev_id, qp_id);
196 pool = rte_mempool_create(mempool_name, nb_elements, mlen, cache_sz, 0,
197 NULL, NULL, NULL, NULL, rte_socket_id(), 0);
200 plt_err("Could not create mempool for metabuf");
204 meta_info = &qp->meta_info;
206 meta_info->pool = pool;
207 meta_info->mlen = mlen;
213 cnxk_cpt_metabuf_mempool_destroy(struct cnxk_cpt_qp *qp)
215 struct cpt_qp_meta_info *meta_info = &qp->meta_info;
217 rte_mempool_free(meta_info->pool);
219 meta_info->pool = NULL;
223 static struct cnxk_cpt_qp *
224 cnxk_cpt_qp_create(const struct rte_cryptodev *dev, uint16_t qp_id,
227 const struct rte_memzone *pq_mem;
228 char name[RTE_MEMZONE_NAMESIZE];
229 struct cnxk_cpt_qp *qp;
234 /* Allocate queue pair */
235 qp = rte_zmalloc_socket("CNXK Crypto PMD Queue Pair", sizeof(*qp),
238 plt_err("Could not allocate queue pair");
242 /* For pending queue */
243 len = iq_len * sizeof(struct cpt_inflight_req);
245 qp_memzone_name_get(name, RTE_MEMZONE_NAMESIZE, dev->data->dev_id,
248 pq_mem = rte_memzone_reserve_aligned(name, len, rte_socket_id(),
249 RTE_MEMZONE_SIZE_HINT_ONLY |
251 RTE_CACHE_LINE_SIZE);
252 if (pq_mem == NULL) {
253 plt_err("Could not allocate reserved memzone");
261 ret = cnxk_cpt_metabuf_mempool_create(dev, qp, qp_id, iq_len);
263 plt_err("Could not create mempool for metabuf");
267 /* Initialize pending queue */
268 qp->pend_q.req_queue = pq_mem->addr;
269 qp->pend_q.enq_tail = 0;
270 qp->pend_q.deq_head = 0;
271 qp->pend_q.pending_count = 0;
276 rte_memzone_free(pq_mem);
283 cnxk_cpt_qp_destroy(const struct rte_cryptodev *dev, struct cnxk_cpt_qp *qp)
285 const struct rte_memzone *pq_mem;
286 char name[RTE_MEMZONE_NAMESIZE];
289 cnxk_cpt_metabuf_mempool_destroy(qp);
291 qp_memzone_name_get(name, RTE_MEMZONE_NAMESIZE, dev->data->dev_id,
294 pq_mem = rte_memzone_lookup(name);
296 ret = rte_memzone_free(pq_mem);
306 cnxk_cpt_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id)
308 struct cnxk_cpt_qp *qp = dev->data->queue_pairs[qp_id];
309 struct cnxk_cpt_vf *vf = dev->data->dev_private;
310 struct roc_cpt *roc_cpt = &vf->cpt;
311 struct roc_cpt_lf *lf;
317 lf = roc_cpt->lf[qp_id];
323 ret = cnxk_cpt_qp_destroy(dev, qp);
325 plt_err("Could not destroy queue pair %d", qp_id);
329 roc_cpt->lf[qp_id] = NULL;
330 dev->data->queue_pairs[qp_id] = NULL;
336 cnxk_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
337 const struct rte_cryptodev_qp_conf *conf,
338 int socket_id __rte_unused)
340 struct cnxk_cpt_vf *vf = dev->data->dev_private;
341 struct roc_cpt *roc_cpt = &vf->cpt;
342 struct rte_pci_device *pci_dev;
343 struct cnxk_cpt_qp *qp;
346 if (dev->data->queue_pairs[qp_id] != NULL)
347 cnxk_cpt_queue_pair_release(dev, qp_id);
349 pci_dev = RTE_DEV_TO_PCI(dev->device);
351 if (pci_dev->mem_resource[2].addr == NULL) {
352 plt_err("Invalid PCI mem address");
356 qp = cnxk_cpt_qp_create(dev, qp_id, conf->nb_descriptors);
358 plt_err("Could not create queue pair %d", qp_id);
362 qp->lf.lf_id = qp_id;
363 qp->lf.nb_desc = conf->nb_descriptors;
365 ret = roc_cpt_lf_init(roc_cpt, &qp->lf);
367 plt_err("Could not initialize queue pair %d", qp_id);
372 roc_cpt->lf[qp_id] = &qp->lf;
374 ret = roc_cpt_lmtline_init(roc_cpt, &qp->lmtline, qp_id);
376 roc_cpt->lf[qp_id] = NULL;
377 plt_err("Could not init lmtline for queue pair %d", qp_id);
381 qp->sess_mp = conf->mp_session;
382 qp->sess_mp_priv = conf->mp_session_private;
383 dev->data->queue_pairs[qp_id] = qp;
388 cnxk_cpt_qp_destroy(dev, qp);
393 cnxk_cpt_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
395 return sizeof(struct cnxk_se_sess);
399 sym_xform_verify(struct rte_crypto_sym_xform *xform)
401 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
402 xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
403 xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
406 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL)
407 return CNXK_CPT_CIPHER;
409 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && xform->next == NULL)
410 return CNXK_CPT_AUTH;
412 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD && xform->next == NULL)
413 return CNXK_CPT_AEAD;
415 if (xform->next == NULL)
418 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
419 xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC &&
420 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
421 xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1)
424 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
425 xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 &&
426 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
427 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC)
430 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
431 xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
432 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
433 xform->next->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
434 return CNXK_CPT_CIPHER_ENC_AUTH_GEN;
436 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
437 xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY &&
438 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
439 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT)
440 return CNXK_CPT_AUTH_VRFY_CIPHER_DEC;
442 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
443 xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE &&
444 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
445 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
446 switch (xform->auth.algo) {
447 case RTE_CRYPTO_AUTH_SHA1_HMAC:
448 switch (xform->next->cipher.algo) {
449 case RTE_CRYPTO_CIPHER_AES_CBC:
450 return CNXK_CPT_AUTH_GEN_CIPHER_ENC;
459 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
460 xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
461 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
462 xform->next->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY) {
463 switch (xform->cipher.algo) {
464 case RTE_CRYPTO_CIPHER_AES_CBC:
465 switch (xform->next->auth.algo) {
466 case RTE_CRYPTO_AUTH_SHA1_HMAC:
467 return CNXK_CPT_CIPHER_DEC_AUTH_VRFY;
480 cnxk_cpt_inst_w7_get(struct cnxk_se_sess *sess, struct roc_cpt *roc_cpt)
482 union cpt_inst_w7 inst_w7;
484 inst_w7.s.cptr = (uint64_t)&sess->roc_se_ctx.se_ctx;
486 /* Set the engine group */
487 if (sess->zsk_flag || sess->chacha_poly)
488 inst_w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_SE];
490 inst_w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
496 sym_session_configure(struct roc_cpt *roc_cpt, int driver_id,
497 struct rte_crypto_sym_xform *xform,
498 struct rte_cryptodev_sym_session *sess,
499 struct rte_mempool *pool)
501 struct cnxk_se_sess *sess_priv;
505 ret = sym_xform_verify(xform);
506 if (unlikely(ret < 0))
509 if (unlikely(rte_mempool_get(pool, &priv))) {
510 plt_dp_err("Could not allocate session private data");
514 memset(priv, 0, sizeof(struct cnxk_se_sess));
519 case CNXK_CPT_CIPHER:
520 ret = fill_sess_cipher(xform, sess_priv);
523 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
524 ret = fill_sess_gmac(xform, sess_priv);
526 ret = fill_sess_auth(xform, sess_priv);
529 ret = fill_sess_aead(xform, sess_priv);
531 case CNXK_CPT_CIPHER_ENC_AUTH_GEN:
532 case CNXK_CPT_CIPHER_DEC_AUTH_VRFY:
533 ret = fill_sess_cipher(xform, sess_priv);
536 ret = fill_sess_auth(xform->next, sess_priv);
538 case CNXK_CPT_AUTH_VRFY_CIPHER_DEC:
539 case CNXK_CPT_AUTH_GEN_CIPHER_ENC:
540 ret = fill_sess_auth(xform, sess_priv);
543 ret = fill_sess_cipher(xform->next, sess_priv);
552 if ((sess_priv->roc_se_ctx.fc_type == ROC_SE_HASH_HMAC) &&
553 cpt_mac_len_verify(&xform->auth)) {
554 plt_dp_err("MAC length is not supported");
555 if (sess_priv->roc_se_ctx.auth_key != NULL) {
556 plt_free(sess_priv->roc_se_ctx.auth_key);
557 sess_priv->roc_se_ctx.auth_key = NULL;
564 sess_priv->cpt_inst_w7 = cnxk_cpt_inst_w7_get(sess_priv, roc_cpt);
566 set_sym_session_private_data(sess, driver_id, sess_priv);
571 rte_mempool_put(pool, priv);
577 cnxk_cpt_sym_session_configure(struct rte_cryptodev *dev,
578 struct rte_crypto_sym_xform *xform,
579 struct rte_cryptodev_sym_session *sess,
580 struct rte_mempool *pool)
582 struct cnxk_cpt_vf *vf = dev->data->dev_private;
583 struct roc_cpt *roc_cpt = &vf->cpt;
586 driver_id = dev->driver_id;
588 return sym_session_configure(roc_cpt, driver_id, xform, sess, pool);
592 sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
594 void *priv = get_sym_session_private_data(sess, driver_id);
595 struct cnxk_se_sess *sess_priv;
596 struct rte_mempool *pool;
603 if (sess_priv->roc_se_ctx.auth_key != NULL)
604 plt_free(sess_priv->roc_se_ctx.auth_key);
606 memset(priv, 0, cnxk_cpt_sym_session_get_size(NULL));
608 pool = rte_mempool_from_obj(priv);
610 set_sym_session_private_data(sess, driver_id, NULL);
612 rte_mempool_put(pool, priv);
616 cnxk_cpt_sym_session_clear(struct rte_cryptodev *dev,
617 struct rte_cryptodev_sym_session *sess)
619 return sym_session_clear(dev->driver_id, sess);
623 cnxk_ae_session_size_get(struct rte_cryptodev *dev __rte_unused)
625 return sizeof(struct cnxk_ae_sess);
629 cnxk_ae_session_clear(struct rte_cryptodev *dev,
630 struct rte_cryptodev_asym_session *sess)
632 struct rte_mempool *sess_mp;
633 struct cnxk_ae_sess *priv;
635 priv = get_asym_session_private_data(sess, dev->driver_id);
639 /* Free resources allocated in session_cfg */
640 cnxk_ae_free_session_parameters(priv);
642 /* Reset and free object back to pool */
643 memset(priv, 0, cnxk_ae_session_size_get(dev));
644 sess_mp = rte_mempool_from_obj(priv);
645 set_asym_session_private_data(sess, dev->driver_id, NULL);
646 rte_mempool_put(sess_mp, priv);
650 cnxk_ae_session_cfg(struct rte_cryptodev *dev,
651 struct rte_crypto_asym_xform *xform,
652 struct rte_cryptodev_asym_session *sess,
653 struct rte_mempool *pool)
655 struct cnxk_cpt_vf *vf = dev->data->dev_private;
656 struct roc_cpt *roc_cpt = &vf->cpt;
657 struct cnxk_ae_sess *priv;
658 union cpt_inst_w7 w7;
661 if (rte_mempool_get(pool, (void **)&priv))
664 memset(priv, 0, sizeof(struct cnxk_ae_sess));
666 ret = cnxk_ae_fill_session_parameters(priv, xform);
668 rte_mempool_put(pool, priv);
673 w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_AE];
674 priv->cpt_inst_w7 = w7.u64;
675 priv->cnxk_fpm_iova = vf->cnxk_fpm_iova;
676 priv->ec_grp = vf->ec_grp;
677 set_asym_session_private_data(sess, dev->driver_id, priv);