1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium, Inc
6 #include <rte_bus_pci.h>
7 #include <rte_cryptodev.h>
8 #include <cryptodev_pmd.h>
9 #include <rte_eventdev.h>
10 #include <rte_event_crypto_adapter.h>
11 #include <rte_errno.h>
12 #include <rte_malloc.h>
13 #include <rte_mempool.h>
15 #include "otx_cryptodev.h"
16 #include "otx_cryptodev_capabilities.h"
17 #include "otx_cryptodev_hw_access.h"
18 #include "otx_cryptodev_mbox.h"
19 #include "otx_cryptodev_ops.h"
21 #include "cpt_pmd_logs.h"
22 #include "cpt_pmd_ops_helper.h"
23 #include "cpt_ucode.h"
24 #include "cpt_ucode_asym.h"
26 #include "ssovf_worker.h"
28 static uint64_t otx_fpm_iova[CPT_EC_ID_PMAX];
30 /* Forward declarations */
33 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
38 otx_cpt_alarm_cb(void *arg)
40 struct cpt_vf *cptvf = arg;
41 otx_cpt_poll_misc(cptvf);
42 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
43 otx_cpt_alarm_cb, cptvf);
47 otx_cpt_periodic_alarm_start(void *arg)
49 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
50 otx_cpt_alarm_cb, arg);
54 otx_cpt_periodic_alarm_stop(void *arg)
56 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
62 otx_cpt_dev_config(struct rte_cryptodev *dev,
63 struct rte_cryptodev_config *config __rte_unused)
67 CPT_PMD_INIT_FUNC_TRACE();
69 if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
70 /* Initialize shared FPM table */
71 ret = cpt_fpm_init(otx_fpm_iova);
77 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
79 void *cptvf = c_dev->data->dev_private;
81 CPT_PMD_INIT_FUNC_TRACE();
83 return otx_cpt_start_device(cptvf);
87 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
89 void *cptvf = c_dev->data->dev_private;
91 CPT_PMD_INIT_FUNC_TRACE();
93 if (c_dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
96 otx_cpt_stop_device(cptvf);
100 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
102 void *cptvf = c_dev->data->dev_private;
105 CPT_PMD_INIT_FUNC_TRACE();
107 for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
108 ret = otx_cpt_que_pair_release(c_dev, i);
113 otx_cpt_periodic_alarm_stop(cptvf);
114 otx_cpt_deinit_device(cptvf);
120 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
122 CPT_PMD_INIT_FUNC_TRACE();
124 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
125 info->feature_flags = dev->feature_flags;
126 info->capabilities = otx_get_capabilities(info->feature_flags);
127 info->sym.max_nb_sessions = 0;
128 info->driver_id = otx_cryptodev_driver_id;
129 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
130 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
135 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
136 uint16_t que_pair_id,
137 const struct rte_cryptodev_qp_conf *qp_conf,
138 int socket_id __rte_unused)
140 struct cpt_instance *instance = NULL;
141 struct rte_pci_device *pci_dev;
144 CPT_PMD_INIT_FUNC_TRACE();
146 if (dev->data->queue_pairs[que_pair_id] != NULL) {
147 ret = otx_cpt_que_pair_release(dev, que_pair_id);
152 if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
153 CPT_LOG_INFO("Number of descriptors too big %d, using default "
154 "queue length of %d", qp_conf->nb_descriptors,
158 pci_dev = RTE_DEV_TO_PCI(dev->device);
160 if (pci_dev->mem_resource[0].addr == NULL) {
161 CPT_LOG_ERR("PCI mem address null");
165 ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
166 if (ret != 0 || instance == NULL) {
167 CPT_LOG_ERR("Error getting instance handle from device %s : "
168 "ret = %d", dev->data->name, ret);
172 instance->queue_id = que_pair_id;
173 instance->sess_mp = qp_conf->mp_session;
174 instance->sess_mp_priv = qp_conf->mp_session_private;
175 dev->data->queue_pairs[que_pair_id] = instance;
181 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
183 struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
186 CPT_PMD_INIT_FUNC_TRACE();
188 ret = otx_cpt_put_resource(instance);
190 CPT_LOG_ERR("Error putting instance handle of device %s : "
191 "ret = %d", dev->data->name, ret);
195 dev->data->queue_pairs[que_pair_id] = NULL;
201 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
203 return cpt_get_session_size();
207 sym_xform_verify(struct rte_crypto_sym_xform *xform)
210 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
211 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
212 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
213 (xform->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC ||
214 xform->next->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC))
217 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
218 xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
219 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
220 (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC ||
221 xform->next->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC))
224 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
225 xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC &&
226 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
227 xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1)
230 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
231 xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 &&
232 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
233 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC)
237 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
238 xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
239 xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
246 sym_session_configure(int driver_id, struct rte_crypto_sym_xform *xform,
247 struct rte_cryptodev_sym_session *sess,
248 struct rte_mempool *pool)
250 struct rte_crypto_sym_xform *temp_xform = xform;
251 struct cpt_sess_misc *misc;
252 vq_cmd_word3_t vq_cmd_w3;
256 ret = sym_xform_verify(xform);
260 if (unlikely(rte_mempool_get(pool, &priv))) {
261 CPT_LOG_ERR("Could not allocate session private data");
265 memset(priv, 0, sizeof(struct cpt_sess_misc) +
266 offsetof(struct cpt_ctx, mc_ctx));
270 for ( ; xform != NULL; xform = xform->next) {
271 switch (xform->type) {
272 case RTE_CRYPTO_SYM_XFORM_AEAD:
273 ret = fill_sess_aead(xform, misc);
275 case RTE_CRYPTO_SYM_XFORM_CIPHER:
276 ret = fill_sess_cipher(xform, misc);
278 case RTE_CRYPTO_SYM_XFORM_AUTH:
279 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
280 ret = fill_sess_gmac(xform, misc);
282 ret = fill_sess_auth(xform, misc);
292 if ((GET_SESS_FC_TYPE(misc) == HASH_HMAC) &&
293 cpt_mac_len_verify(&temp_xform->auth)) {
294 CPT_LOG_ERR("MAC length is not supported");
295 struct cpt_ctx *ctx = SESS_PRIV(misc);
296 if (ctx->auth_key != NULL) {
297 rte_free(ctx->auth_key);
298 ctx->auth_key = NULL;
304 set_sym_session_private_data(sess, driver_id, priv);
306 misc->ctx_dma_addr = rte_mempool_virt2iova(misc) +
307 sizeof(struct cpt_sess_misc);
311 vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx,
314 misc->cpt_inst_w7 = vq_cmd_w3.u64;
320 rte_mempool_put(pool, priv);
325 sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
327 void *priv = get_sym_session_private_data(sess, driver_id);
328 struct cpt_sess_misc *misc;
329 struct rte_mempool *pool;
336 ctx = SESS_PRIV(misc);
338 if (ctx->auth_key != NULL)
339 rte_free(ctx->auth_key);
341 memset(priv, 0, cpt_get_session_size());
343 pool = rte_mempool_from_obj(priv);
345 set_sym_session_private_data(sess, driver_id, NULL);
347 rte_mempool_put(pool, priv);
351 otx_cpt_session_cfg(struct rte_cryptodev *dev,
352 struct rte_crypto_sym_xform *xform,
353 struct rte_cryptodev_sym_session *sess,
354 struct rte_mempool *pool)
356 CPT_PMD_INIT_FUNC_TRACE();
358 return sym_session_configure(dev->driver_id, xform, sess, pool);
363 otx_cpt_session_clear(struct rte_cryptodev *dev,
364 struct rte_cryptodev_sym_session *sess)
366 CPT_PMD_INIT_FUNC_TRACE();
368 return sym_session_clear(dev->driver_id, sess);
372 otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
374 return sizeof(struct cpt_asym_sess_misc);
378 otx_cpt_asym_session_cfg(struct rte_cryptodev *dev,
379 struct rte_crypto_asym_xform *xform __rte_unused,
380 struct rte_cryptodev_asym_session *sess,
381 struct rte_mempool *pool)
383 struct cpt_asym_sess_misc *priv;
386 CPT_PMD_INIT_FUNC_TRACE();
388 if (rte_mempool_get(pool, (void **)&priv)) {
389 CPT_LOG_ERR("Could not allocate session private data");
393 memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
395 ret = cpt_fill_asym_session_parameters(priv, xform);
397 CPT_LOG_ERR("Could not configure session parameters");
399 /* Return session to mempool */
400 rte_mempool_put(pool, priv);
404 priv->cpt_inst_w7 = 0;
406 set_asym_session_private_data(sess, dev->driver_id, priv);
411 otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
412 struct rte_cryptodev_asym_session *sess)
414 struct cpt_asym_sess_misc *priv;
415 struct rte_mempool *sess_mp;
417 CPT_PMD_INIT_FUNC_TRACE();
419 priv = get_asym_session_private_data(sess, dev->driver_id);
424 /* Free resources allocated during session configure */
425 cpt_free_asym_session_parameters(priv);
426 memset(priv, 0, otx_cpt_asym_session_size_get(dev));
427 sess_mp = rte_mempool_from_obj(priv);
428 set_asym_session_private_data(sess, dev->driver_id, NULL);
429 rte_mempool_put(sess_mp, priv);
432 static __rte_always_inline void * __rte_hot
433 otx_cpt_request_enqueue(struct cpt_instance *instance,
434 struct pending_queue *pqueue,
435 void *req, uint64_t cpt_inst_w7)
437 struct cpt_request_info *user_req = (struct cpt_request_info *)req;
439 if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN)) {
444 fill_cpt_inst(instance, req, cpt_inst_w7);
446 CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
448 /* Fill time_out cycles */
449 user_req->time_out = rte_get_timer_cycles() +
450 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
451 user_req->extra_time = 0;
453 /* Default mode of software queue */
454 mark_cpt_inst(instance);
456 CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
457 "op: %p", user_req, user_req->op);
461 static __rte_always_inline void * __rte_hot
462 otx_cpt_enq_single_asym(struct cpt_instance *instance,
463 struct rte_crypto_op *op,
464 struct pending_queue *pqueue)
466 struct cpt_qp_meta_info *minfo = &instance->meta_info;
467 struct rte_crypto_asym_op *asym_op = op->asym;
468 struct asym_op_params params = {0};
469 struct cpt_asym_sess_misc *sess;
475 if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
476 CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
481 sess = get_asym_session_private_data(asym_op->session,
482 otx_cryptodev_driver_id);
484 /* Store phys_addr of the mdata to meta_buf */
485 params.meta_buf = rte_mempool_virt2iova(mdata);
488 cop[0] = (uintptr_t)mdata;
489 cop[1] = (uintptr_t)op;
490 cop[2] = cop[3] = 0ULL;
492 params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
493 params.req->op = cop;
495 /* Adjust meta_buf by crypto_op data and request_info struct */
496 params.meta_buf += (4 * sizeof(uintptr_t)) +
497 sizeof(struct cpt_request_info);
499 switch (sess->xfrm_type) {
500 case RTE_CRYPTO_ASYM_XFORM_MODEX:
501 ret = cpt_modex_prep(¶ms, &sess->mod_ctx);
505 case RTE_CRYPTO_ASYM_XFORM_RSA:
506 ret = cpt_enqueue_rsa_op(op, ¶ms, sess);
510 case RTE_CRYPTO_ASYM_XFORM_ECDSA:
511 ret = cpt_enqueue_ecdsa_op(op, ¶ms, sess, otx_fpm_iova);
515 case RTE_CRYPTO_ASYM_XFORM_ECPM:
516 ret = cpt_ecpm_prep(&asym_op->ecpm, ¶ms,
517 sess->ec_ctx.curveid);
523 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
528 req = otx_cpt_request_enqueue(instance, pqueue, params.req,
530 if (unlikely(req == NULL)) {
531 CPT_LOG_DP_ERR("Could not enqueue crypto req");
538 free_op_meta(mdata, minfo->pool);
543 static __rte_always_inline void * __rte_hot
544 otx_cpt_enq_single_sym(struct cpt_instance *instance,
545 struct rte_crypto_op *op,
546 struct pending_queue *pqueue)
548 struct cpt_sess_misc *sess;
549 struct rte_crypto_sym_op *sym_op = op->sym;
550 struct cpt_request_info *prep_req;
556 sess = (struct cpt_sess_misc *)
557 get_sym_session_private_data(sym_op->session,
558 otx_cryptodev_driver_id);
560 cpt_op = sess->cpt_op;
562 if (likely(cpt_op & CPT_OP_CIPHER_MASK))
563 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
566 ret = fill_digest_params(op, sess, &instance->meta_info,
567 &mdata, (void **)&prep_req);
570 CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
571 "ret 0x%x", op, (unsigned int)cpt_op, ret);
575 /* Enqueue prepared instruction to h/w */
576 req = otx_cpt_request_enqueue(instance, pqueue, prep_req,
578 if (unlikely(req == NULL))
579 /* Buffer allocated for request preparation need to be freed */
580 free_op_meta(mdata, instance->meta_info.pool);
585 static __rte_always_inline void * __rte_hot
586 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
587 struct rte_crypto_op *op,
588 struct pending_queue *pend_q)
590 const int driver_id = otx_cryptodev_driver_id;
591 struct rte_crypto_sym_op *sym_op = op->sym;
592 struct rte_cryptodev_sym_session *sess;
596 /* Create temporary session */
597 sess = rte_cryptodev_sym_session_create(instance->sess_mp);
603 ret = sym_session_configure(driver_id, sym_op->xform, sess,
604 instance->sess_mp_priv);
608 sym_op->session = sess;
610 req = otx_cpt_enq_single_sym(instance, op, pend_q);
612 if (unlikely(req == NULL))
618 sym_session_clear(driver_id, sess);
620 rte_mempool_put(instance->sess_mp, sess);
624 #define OP_TYPE_SYM 0
625 #define OP_TYPE_ASYM 1
627 static __rte_always_inline void *__rte_hot
628 otx_cpt_enq_single(struct cpt_instance *inst,
629 struct rte_crypto_op *op,
630 struct pending_queue *pqueue,
631 const uint8_t op_type)
633 /* Check for the type */
635 if (op_type == OP_TYPE_SYM) {
636 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
637 return otx_cpt_enq_single_sym(inst, op, pqueue);
639 return otx_cpt_enq_single_sym_sessless(inst, op,
643 if (op_type == OP_TYPE_ASYM) {
644 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
645 return otx_cpt_enq_single_asym(inst, op, pqueue);
648 /* Should not reach here */
653 static __rte_always_inline uint16_t __rte_hot
654 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
655 const uint8_t op_type)
657 struct cpt_instance *instance = (struct cpt_instance *)qptr;
660 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
661 struct pending_queue *pqueue = &cptvf->pqueue;
663 count = DEFAULT_CMD_QLEN - pqueue->pending_count;
668 while (likely(count < nb_ops)) {
670 /* Enqueue single op */
671 req = otx_cpt_enq_single(instance, ops[count], pqueue, op_type);
673 if (unlikely(req == NULL))
676 pqueue->req_queue[pqueue->enq_tail] = (uintptr_t)req;
677 MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
678 pqueue->pending_count += 1;
681 otx_cpt_ring_dbell(instance, count);
686 otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
688 return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
692 otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
694 return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
697 static __rte_always_inline void
698 submit_request_to_sso(struct ssows *ws, uintptr_t req,
699 struct rte_event *rsp_info)
703 add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) |
704 ((uint64_t)(rsp_info->sched_type) << 32);
706 if (!rsp_info->sched_type)
709 rte_atomic_thread_fence(__ATOMIC_RELEASE);
710 ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]);
713 static inline union rte_event_crypto_metadata *
714 get_event_crypto_mdata(struct rte_crypto_op *op)
716 union rte_event_crypto_metadata *ec_mdata;
718 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
719 ec_mdata = rte_cryptodev_sym_session_get_user_data(
721 else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
722 op->private_data_offset)
723 ec_mdata = (union rte_event_crypto_metadata *)
724 ((uint8_t *)op + op->private_data_offset);
732 otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op)
734 union rte_event_crypto_metadata *ec_mdata;
735 struct cpt_instance *instance;
736 struct cpt_request_info *req;
737 struct rte_event *rsp_info;
738 uint8_t op_type, cdev_id;
741 ec_mdata = get_event_crypto_mdata(op);
742 if (unlikely(ec_mdata == NULL)) {
747 cdev_id = ec_mdata->request_info.cdev_id;
748 qp_id = ec_mdata->request_info.queue_pair_id;
749 rsp_info = &ec_mdata->response_info;
750 instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
752 if (unlikely(!instance->ca_enabled)) {
757 op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
759 req = otx_cpt_enq_single(instance, op,
760 &((struct cpt_vf *)instance)->pqueue, op_type);
761 if (unlikely(req == NULL))
764 otx_cpt_ring_dbell(instance, 1);
766 submit_request_to_sso(port, (uintptr_t)req, rsp_info);
772 otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
773 struct rte_crypto_rsa_xform *rsa_ctx)
776 struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
778 switch (rsa->op_type) {
779 case RTE_CRYPTO_ASYM_OP_ENCRYPT:
780 rsa->cipher.length = rsa_ctx->n.length;
781 memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
783 case RTE_CRYPTO_ASYM_OP_DECRYPT:
784 if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
785 rsa->message.length = rsa_ctx->n.length;
787 /* Get length of decrypted output */
788 rsa->message.length = rte_cpu_to_be_16
789 (*((uint16_t *)req->rptr));
791 /* Offset data pointer by length fields */
794 memcpy(rsa->message.data, req->rptr, rsa->message.length);
796 case RTE_CRYPTO_ASYM_OP_SIGN:
797 rsa->sign.length = rsa_ctx->n.length;
798 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
800 case RTE_CRYPTO_ASYM_OP_VERIFY:
801 if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
802 rsa->sign.length = rsa_ctx->n.length;
804 /* Get length of decrypted output */
805 rsa->sign.length = rte_cpu_to_be_16
806 (*((uint16_t *)req->rptr));
808 /* Offset data pointer by length fields */
811 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
813 if (memcmp(rsa->sign.data, rsa->message.data,
814 rsa->message.length)) {
815 CPT_LOG_DP_ERR("RSA verification failed");
816 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
820 CPT_LOG_DP_DEBUG("Invalid RSA operation type");
821 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
826 static __rte_always_inline void
827 otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa,
828 struct cpt_request_info *req,
829 struct cpt_asym_ec_ctx *ec)
832 int prime_len = ec_grp[ec->curveid].prime.length;
834 if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
837 /* Separate out sign r and s components */
838 memcpy(ecdsa->r.data, req->rptr, prime_len);
839 memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
841 ecdsa->r.length = prime_len;
842 ecdsa->s.length = prime_len;
845 static __rte_always_inline void
846 otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm,
847 struct cpt_request_info *req,
848 struct cpt_asym_ec_ctx *ec)
850 int prime_len = ec_grp[ec->curveid].prime.length;
852 memcpy(ecpm->r.x.data, req->rptr, prime_len);
853 memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
855 ecpm->r.x.length = prime_len;
856 ecpm->r.y.length = prime_len;
859 static __rte_always_inline void __rte_hot
860 otx_cpt_asym_post_process(struct rte_crypto_op *cop,
861 struct cpt_request_info *req)
863 struct rte_crypto_asym_op *op = cop->asym;
864 struct cpt_asym_sess_misc *sess;
866 sess = get_asym_session_private_data(op->session,
867 otx_cryptodev_driver_id);
869 switch (sess->xfrm_type) {
870 case RTE_CRYPTO_ASYM_XFORM_RSA:
871 otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
873 case RTE_CRYPTO_ASYM_XFORM_MODEX:
874 op->modex.result.length = sess->mod_ctx.modulus.length;
875 memcpy(op->modex.result.data, req->rptr,
876 op->modex.result.length);
878 case RTE_CRYPTO_ASYM_XFORM_ECDSA:
879 otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx);
881 case RTE_CRYPTO_ASYM_XFORM_ECPM:
882 otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx);
885 CPT_LOG_DP_DEBUG("Invalid crypto xform type");
886 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
891 static __rte_always_inline void __rte_hot
892 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
893 const uint8_t op_type)
895 /* H/w has returned success */
896 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
898 /* Perform further post processing */
900 if ((op_type == OP_TYPE_SYM) &&
901 (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
902 /* Check if auth verify need to be completed */
903 if (unlikely(rsp[2]))
904 compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
908 if ((op_type == OP_TYPE_ASYM) &&
909 (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
910 rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
911 otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
918 free_sym_session_data(const struct cpt_instance *instance,
919 struct rte_crypto_op *cop)
921 void *sess_private_data_t = get_sym_session_private_data(
922 cop->sym->session, otx_cryptodev_driver_id);
923 memset(sess_private_data_t, 0, cpt_get_session_size());
924 memset(cop->sym->session, 0,
925 rte_cryptodev_sym_get_existing_header_session_size(
927 rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
928 rte_mempool_put(instance->sess_mp, cop->sym->session);
929 cop->sym->session = NULL;
932 static __rte_always_inline struct rte_crypto_op *
933 otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp,
934 uint8_t cc, const uint8_t op_type)
936 struct rte_crypto_op *cop;
939 metabuf = (void *)rsp[0];
940 cop = (void *)rsp[1];
942 /* Check completion code */
943 if (likely(cc == 0)) {
944 /* H/w success pkt. Post process */
945 otx_cpt_dequeue_post_process(cop, rsp, op_type);
946 } else if (cc == ERR_GC_ICV_MISCOMPARE) {
947 /* auth data mismatch */
948 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
951 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
954 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
955 free_sym_session_data(instance, cop);
956 free_op_meta(metabuf, instance->meta_info.pool);
961 static __rte_always_inline uint16_t __rte_hot
962 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
963 const uint8_t op_type)
965 struct cpt_instance *instance = (struct cpt_instance *)qptr;
966 struct cpt_request_info *user_req;
967 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
969 int i, count, pcount;
972 struct pending_queue *pqueue = &cptvf->pqueue;
974 pcount = pqueue->pending_count;
975 count = (nb_ops > pcount) ? pcount : nb_ops;
977 for (i = 0; i < count; i++) {
978 user_req = (struct cpt_request_info *)
979 pqueue->req_queue[pqueue->deq_head];
981 if (likely((i+1) < count)) {
982 rte_prefetch_non_temporal(
983 (void *)pqueue->req_queue[i+1]);
986 ret = check_nb_command_id(user_req, instance);
988 if (unlikely(ret == ERR_REQ_PENDING)) {
989 /* Stop checking for completions */
993 /* Return completion code and op handle */
995 ops[i] = user_req->op;
997 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
998 user_req, user_req->op, ret);
1000 MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
1001 pqueue->pending_count -= 1;
1006 for (i = 0; i < nb_completed; i++) {
1007 if (likely((i + 1) < nb_completed))
1008 rte_prefetch0(ops[i+1]);
1010 ops[i] = otx_cpt_process_response(instance, (void *)ops[i],
1014 return nb_completed;
1018 otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1020 return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
1024 otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1026 return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
1030 otx_crypto_adapter_dequeue(uintptr_t get_work1)
1032 const struct cpt_instance *instance;
1033 struct cpt_request_info *req;
1034 struct rte_crypto_op *cop;
1035 uint8_t cc, op_type;
1038 req = (struct cpt_request_info *)get_work1;
1041 cop = (void *)rsp[1];
1042 op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
1046 cc = check_nb_command_id(
1047 req, (struct cpt_instance *)(uintptr_t)instance);
1048 } while (cc == ERR_REQ_PENDING);
1050 cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type);
1052 return (uintptr_t)(cop);
1055 static struct rte_cryptodev_ops cptvf_ops = {
1056 /* Device related operations */
1057 .dev_configure = otx_cpt_dev_config,
1058 .dev_start = otx_cpt_dev_start,
1059 .dev_stop = otx_cpt_dev_stop,
1060 .dev_close = otx_cpt_dev_close,
1061 .dev_infos_get = otx_cpt_dev_info_get,
1064 .stats_reset = NULL,
1065 .queue_pair_setup = otx_cpt_que_pair_setup,
1066 .queue_pair_release = otx_cpt_que_pair_release,
1068 /* Crypto related operations */
1069 .sym_session_get_size = otx_cpt_get_session_size,
1070 .sym_session_configure = otx_cpt_session_cfg,
1071 .sym_session_clear = otx_cpt_session_clear,
1073 .asym_session_get_size = otx_cpt_asym_session_size_get,
1074 .asym_session_configure = otx_cpt_asym_session_cfg,
1075 .asym_session_clear = otx_cpt_asym_session_clear,
1079 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
1081 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
1082 struct cpt_vf *cptvf = NULL;
1087 if (pdev->mem_resource[0].phys_addr == 0ULL)
1090 /* for secondary processes, we don't initialise any further as primary
1091 * has already done this work.
1093 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1096 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
1097 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
1100 if (cptvf == NULL) {
1101 CPT_LOG_ERR("Cannot allocate memory for device private data");
1105 snprintf(dev_name, 32, "%02x:%02x.%x",
1106 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1108 reg_base = pdev->mem_resource[0].addr;
1110 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
1115 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
1117 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
1122 switch (cptvf->vftype) {
1123 case OTX_CPT_VF_TYPE_AE:
1124 /* Set asymmetric cpt feature flags */
1125 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
1126 RTE_CRYPTODEV_FF_HW_ACCELERATED |
1127 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
1129 case OTX_CPT_VF_TYPE_SE:
1130 /* Set symmetric cpt feature flags */
1131 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1132 RTE_CRYPTODEV_FF_HW_ACCELERATED |
1133 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1134 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
1135 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1136 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1137 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
1138 RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
1139 RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
1142 /* Feature not supported. Abort */
1143 CPT_LOG_ERR("VF type not supported by %s", dev_name);
1148 /* Start off timer for mailbox interrupts */
1149 otx_cpt_periodic_alarm_start(cptvf);
1151 c_dev->dev_ops = &cptvf_ops;
1153 if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
1154 c_dev->enqueue_burst = otx_cpt_enqueue_sym;
1155 c_dev->dequeue_burst = otx_cpt_dequeue_sym;
1157 c_dev->enqueue_burst = otx_cpt_enqueue_asym;
1158 c_dev->dequeue_burst = otx_cpt_dequeue_asym;
1161 /* Save dev private data */
1162 c_dev->data->dev_private = cptvf;
1167 otx_cpt_deinit_device(cptvf);
1171 /* Free private data allocated */