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 <rte_cryptodev_pmd.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
13 #include "otx_cryptodev.h"
14 #include "otx_cryptodev_capabilities.h"
15 #include "otx_cryptodev_hw_access.h"
16 #include "otx_cryptodev_mbox.h"
17 #include "otx_cryptodev_ops.h"
19 #include "cpt_pmd_logs.h"
20 #include "cpt_ucode.h"
21 #include "cpt_ucode_asym.h"
23 /* Forward declarations */
26 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
31 otx_cpt_alarm_cb(void *arg)
33 struct cpt_vf *cptvf = arg;
34 otx_cpt_poll_misc(cptvf);
35 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
36 otx_cpt_alarm_cb, cptvf);
40 otx_cpt_periodic_alarm_start(void *arg)
42 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
43 otx_cpt_alarm_cb, arg);
47 otx_cpt_periodic_alarm_stop(void *arg)
49 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
55 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
56 struct rte_cryptodev_config *config __rte_unused)
58 CPT_PMD_INIT_FUNC_TRACE();
63 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
65 void *cptvf = c_dev->data->dev_private;
67 CPT_PMD_INIT_FUNC_TRACE();
69 return otx_cpt_start_device(cptvf);
73 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
75 void *cptvf = c_dev->data->dev_private;
77 CPT_PMD_INIT_FUNC_TRACE();
79 otx_cpt_stop_device(cptvf);
83 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
85 void *cptvf = c_dev->data->dev_private;
88 CPT_PMD_INIT_FUNC_TRACE();
90 for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
91 ret = otx_cpt_que_pair_release(c_dev, i);
96 otx_cpt_periodic_alarm_stop(cptvf);
97 otx_cpt_deinit_device(cptvf);
103 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
105 CPT_PMD_INIT_FUNC_TRACE();
107 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
108 info->feature_flags = dev->feature_flags;
109 info->capabilities = otx_get_capabilities(info->feature_flags);
110 info->sym.max_nb_sessions = 0;
111 info->driver_id = otx_cryptodev_driver_id;
112 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
113 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
118 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
119 struct rte_cryptodev_stats *stats __rte_unused)
121 CPT_PMD_INIT_FUNC_TRACE();
125 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
127 CPT_PMD_INIT_FUNC_TRACE();
131 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
132 uint16_t que_pair_id,
133 const struct rte_cryptodev_qp_conf *qp_conf,
134 int socket_id __rte_unused)
136 struct cpt_instance *instance = NULL;
137 struct rte_pci_device *pci_dev;
140 CPT_PMD_INIT_FUNC_TRACE();
142 if (dev->data->queue_pairs[que_pair_id] != NULL) {
143 ret = otx_cpt_que_pair_release(dev, que_pair_id);
148 if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
149 CPT_LOG_INFO("Number of descriptors too big %d, using default "
150 "queue length of %d", qp_conf->nb_descriptors,
154 pci_dev = RTE_DEV_TO_PCI(dev->device);
156 if (pci_dev->mem_resource[0].addr == NULL) {
157 CPT_LOG_ERR("PCI mem address null");
161 ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
162 if (ret != 0 || instance == NULL) {
163 CPT_LOG_ERR("Error getting instance handle from device %s : "
164 "ret = %d", dev->data->name, ret);
168 instance->queue_id = que_pair_id;
169 instance->sess_mp = qp_conf->mp_session;
170 instance->sess_mp_priv = qp_conf->mp_session_private;
171 dev->data->queue_pairs[que_pair_id] = instance;
177 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
179 struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
182 CPT_PMD_INIT_FUNC_TRACE();
184 ret = otx_cpt_put_resource(instance);
186 CPT_LOG_ERR("Error putting instance handle of device %s : "
187 "ret = %d", dev->data->name, ret);
191 dev->data->queue_pairs[que_pair_id] = NULL;
197 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
199 return cpt_get_session_size();
203 otx_cpt_session_init(void *sym_sess, uint8_t driver_id)
205 struct rte_cryptodev_sym_session *sess = sym_sess;
206 struct cpt_sess_misc *cpt_sess =
207 (struct cpt_sess_misc *) get_sym_session_private_data(sess, driver_id);
209 CPT_PMD_INIT_FUNC_TRACE();
210 cpt_sess->ctx_dma_addr = rte_mempool_virt2iova(cpt_sess) +
211 sizeof(struct cpt_sess_misc);
215 otx_cpt_session_cfg(struct rte_cryptodev *dev,
216 struct rte_crypto_sym_xform *xform,
217 struct rte_cryptodev_sym_session *sess,
218 struct rte_mempool *mempool)
220 struct rte_crypto_sym_xform *chain;
221 void *sess_private_data = NULL;
223 CPT_PMD_INIT_FUNC_TRACE();
225 if (cpt_is_algo_supported(xform))
228 if (unlikely(sess == NULL)) {
229 CPT_LOG_ERR("invalid session struct");
233 if (rte_mempool_get(mempool, &sess_private_data)) {
234 CPT_LOG_ERR("Could not allocate sess_private_data");
240 switch (chain->type) {
241 case RTE_CRYPTO_SYM_XFORM_AEAD:
242 if (fill_sess_aead(chain, sess_private_data))
245 case RTE_CRYPTO_SYM_XFORM_CIPHER:
246 if (fill_sess_cipher(chain, sess_private_data))
249 case RTE_CRYPTO_SYM_XFORM_AUTH:
250 if (chain->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
251 if (fill_sess_gmac(chain, sess_private_data))
254 if (fill_sess_auth(chain, sess_private_data))
259 CPT_LOG_ERR("Invalid crypto xform type");
264 set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
265 otx_cpt_session_init(sess, dev->driver_id);
269 if (sess_private_data)
270 rte_mempool_put(mempool, sess_private_data);
275 otx_cpt_session_clear(struct rte_cryptodev *dev,
276 struct rte_cryptodev_sym_session *sess)
278 void *sess_priv = get_sym_session_private_data(sess, dev->driver_id);
280 CPT_PMD_INIT_FUNC_TRACE();
282 memset(sess_priv, 0, otx_cpt_get_session_size(dev));
283 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
284 set_sym_session_private_data(sess, dev->driver_id, NULL);
285 rte_mempool_put(sess_mp, sess_priv);
290 otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
292 return sizeof(struct cpt_asym_sess_misc);
296 otx_cpt_asym_session_cfg(struct rte_cryptodev *dev,
297 struct rte_crypto_asym_xform *xform __rte_unused,
298 struct rte_cryptodev_asym_session *sess,
299 struct rte_mempool *pool)
301 struct cpt_asym_sess_misc *priv;
304 CPT_PMD_INIT_FUNC_TRACE();
306 if (rte_mempool_get(pool, (void **)&priv)) {
307 CPT_LOG_ERR("Could not allocate session private data");
311 memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
313 ret = cpt_fill_asym_session_parameters(priv, xform);
315 CPT_LOG_ERR("Could not configure session parameters");
317 /* Return session to mempool */
318 rte_mempool_put(pool, priv);
322 set_asym_session_private_data(sess, dev->driver_id, priv);
327 otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
328 struct rte_cryptodev_asym_session *sess)
330 struct cpt_asym_sess_misc *priv;
331 struct rte_mempool *sess_mp;
333 CPT_PMD_INIT_FUNC_TRACE();
335 priv = get_asym_session_private_data(sess, dev->driver_id);
340 /* Free resources allocated during session configure */
341 cpt_free_asym_session_parameters(priv);
342 memset(priv, 0, otx_cpt_asym_session_size_get(dev));
343 sess_mp = rte_mempool_from_obj(priv);
344 set_asym_session_private_data(sess, dev->driver_id, NULL);
345 rte_mempool_put(sess_mp, priv);
348 static __rte_always_inline int32_t __hot
349 otx_cpt_request_enqueue(struct cpt_instance *instance,
350 struct pending_queue *pqueue,
353 struct cpt_request_info *user_req = (struct cpt_request_info *)req;
355 if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN))
358 fill_cpt_inst(instance, req);
360 CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
362 /* Fill time_out cycles */
363 user_req->time_out = rte_get_timer_cycles() +
364 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
365 user_req->extra_time = 0;
367 /* Default mode of software queue */
368 mark_cpt_inst(instance);
370 pqueue->rid_queue[pqueue->enq_tail].rid = (uintptr_t)user_req;
372 /* We will use soft queue length here to limit requests */
373 MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
374 pqueue->pending_count += 1;
376 CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
377 "op: %p", user_req, user_req->op);
381 static __rte_always_inline int __hot
382 otx_cpt_enq_single_sym(struct cpt_instance *instance,
383 struct rte_crypto_op *op,
384 struct pending_queue *pqueue)
386 struct cpt_sess_misc *sess;
387 struct rte_crypto_sym_op *sym_op = op->sym;
388 void *prep_req, *mdata = NULL;
392 sess = (struct cpt_sess_misc *)
393 get_sym_session_private_data(sym_op->session,
394 otx_cryptodev_driver_id);
396 cpt_op = sess->cpt_op;
398 if (likely(cpt_op & CPT_OP_CIPHER_MASK))
399 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
402 ret = fill_digest_params(op, sess, &instance->meta_info,
406 CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
407 "ret 0x%x", op, (unsigned int)cpt_op, ret);
411 /* Enqueue prepared instruction to h/w */
412 ret = otx_cpt_request_enqueue(instance, pqueue, prep_req);
415 /* Buffer allocated for request preparation need to be freed */
416 free_op_meta(mdata, instance->meta_info.pool);
423 static __rte_always_inline int __hot
424 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
425 struct rte_crypto_op *op,
426 struct pending_queue *pqueue)
428 struct cpt_sess_misc *sess;
429 struct rte_crypto_sym_op *sym_op = op->sym;
432 void *sess_private_data_t = NULL;
434 /* Create tmp session */
436 if (rte_mempool_get(instance->sess_mp, (void **)&sess_t)) {
441 if (rte_mempool_get(instance->sess_mp_priv,
442 (void **)&sess_private_data_t)) {
447 sess = (struct cpt_sess_misc *)sess_private_data_t;
449 sess->ctx_dma_addr = rte_mempool_virt2iova(sess) +
450 sizeof(struct cpt_sess_misc);
452 ret = instance_session_cfg(sym_op->xform, (void *)sess);
458 /* Save tmp session in op */
460 sym_op->session = (struct rte_cryptodev_sym_session *)sess_t;
461 set_sym_session_private_data(sym_op->session, otx_cryptodev_driver_id,
462 sess_private_data_t);
464 /* Enqueue op with the tmp session set */
465 ret = otx_cpt_enq_single_sym(instance, op, pqueue);
473 rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
475 rte_mempool_put(instance->sess_mp, sess_t);
480 static __rte_always_inline int __hot
481 otx_cpt_enq_single(struct cpt_instance *inst,
482 struct rte_crypto_op *op,
483 struct pending_queue *pqueue)
485 /* Check for the type */
487 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
488 return otx_cpt_enq_single_sym(inst, op, pqueue);
489 else if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
490 return otx_cpt_enq_single_sym_sessless(inst, op, pqueue);
492 /* Should not reach here */
497 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
499 struct cpt_instance *instance = (struct cpt_instance *)qptr;
502 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
503 struct pending_queue *pqueue = &cptvf->pqueue;
505 count = DEFAULT_CMD_QLEN - pqueue->pending_count;
510 while (likely(count < nb_ops)) {
512 /* Enqueue single op */
513 ret = otx_cpt_enq_single(instance, ops[count], pqueue);
519 otx_cpt_ring_dbell(instance, count);
523 static __rte_always_inline void
524 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp)
526 /* H/w has returned success */
527 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
529 /* Perform further post processing */
531 if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
532 /* Check if auth verify need to be completed */
533 if (unlikely(rsp[2]))
534 compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
540 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
542 struct cpt_instance *instance = (struct cpt_instance *)qptr;
543 struct cpt_request_info *user_req;
544 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
547 int i, count, pcount;
550 struct pending_queue *pqueue = &cptvf->pqueue;
551 struct rte_crypto_op *cop;
555 pcount = pqueue->pending_count;
556 count = (nb_ops > pcount) ? pcount : nb_ops;
558 for (i = 0; i < count; i++) {
559 rid_e = &pqueue->rid_queue[pqueue->deq_head];
560 user_req = (struct cpt_request_info *)(rid_e->rid);
562 if (likely((i+1) < count))
563 rte_prefetch_non_temporal((void *)rid_e[1].rid);
565 ret = check_nb_command_id(user_req, instance);
567 if (unlikely(ret == ERR_REQ_PENDING)) {
568 /* Stop checking for completions */
572 /* Return completion code and op handle */
574 ops[i] = user_req->op;
576 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
577 user_req, user_req->op, ret);
579 MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
580 pqueue->pending_count -= 1;
585 for (i = 0; i < nb_completed; i++) {
587 rsp = (void *)ops[i];
589 if (likely((i + 1) < nb_completed))
590 rte_prefetch0(ops[i+1]);
592 metabuf = (void *)rsp[0];
593 cop = (void *)rsp[1];
597 /* Check completion code */
599 if (likely(cc[i] == 0)) {
600 /* H/w success pkt. Post process */
601 otx_cpt_dequeue_post_process(cop, rsp);
602 } else if (cc[i] == ERR_GC_ICV_MISCOMPARE) {
603 /* auth data mismatch */
604 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
607 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
610 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
611 void *sess_private_data_t =
612 get_sym_session_private_data(cop->sym->session,
613 otx_cryptodev_driver_id);
614 memset(sess_private_data_t, 0,
615 cpt_get_session_size());
616 memset(cop->sym->session, 0,
617 rte_cryptodev_sym_get_existing_header_session_size(
619 rte_mempool_put(instance->sess_mp_priv,
620 sess_private_data_t);
621 rte_mempool_put(instance->sess_mp, cop->sym->session);
622 cop->sym->session = NULL;
624 free_op_meta(metabuf, instance->meta_info.pool);
630 static struct rte_cryptodev_ops cptvf_ops = {
631 /* Device related operations */
632 .dev_configure = otx_cpt_dev_config,
633 .dev_start = otx_cpt_dev_start,
634 .dev_stop = otx_cpt_dev_stop,
635 .dev_close = otx_cpt_dev_close,
636 .dev_infos_get = otx_cpt_dev_info_get,
638 .stats_get = otx_cpt_stats_get,
639 .stats_reset = otx_cpt_stats_reset,
640 .queue_pair_setup = otx_cpt_que_pair_setup,
641 .queue_pair_release = otx_cpt_que_pair_release,
642 .queue_pair_count = NULL,
644 /* Crypto related operations */
645 .sym_session_get_size = otx_cpt_get_session_size,
646 .sym_session_configure = otx_cpt_session_cfg,
647 .sym_session_clear = otx_cpt_session_clear,
649 .asym_session_get_size = otx_cpt_asym_session_size_get,
650 .asym_session_configure = otx_cpt_asym_session_cfg,
651 .asym_session_clear = otx_cpt_asym_session_clear,
655 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
657 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
658 struct cpt_vf *cptvf = NULL;
663 if (pdev->mem_resource[0].phys_addr == 0ULL)
666 /* for secondary processes, we don't initialise any further as primary
667 * has already done this work.
669 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
672 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
673 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
677 CPT_LOG_ERR("Cannot allocate memory for device private data");
681 snprintf(dev_name, 32, "%02x:%02x.%x",
682 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
684 reg_base = pdev->mem_resource[0].addr;
686 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
691 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
693 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
698 switch (cptvf->vftype) {
699 case OTX_CPT_VF_TYPE_AE:
700 /* Set asymmetric cpt feature flags */
701 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
702 RTE_CRYPTODEV_FF_HW_ACCELERATED |
703 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
705 case OTX_CPT_VF_TYPE_SE:
706 /* Set symmetric cpt feature flags */
707 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
708 RTE_CRYPTODEV_FF_HW_ACCELERATED |
709 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
710 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
711 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
712 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
715 /* Feature not supported. Abort */
716 CPT_LOG_ERR("VF type not supported by %s", dev_name);
721 /* Start off timer for mailbox interrupts */
722 otx_cpt_periodic_alarm_start(cptvf);
724 c_dev->dev_ops = &cptvf_ops;
726 c_dev->enqueue_burst = otx_cpt_pkt_enqueue;
727 c_dev->dequeue_burst = otx_cpt_pkt_dequeue;
729 /* Save dev private data */
730 c_dev->data->dev_private = cptvf;
735 otx_cpt_deinit_device(cptvf);
739 /* Free private data allocated */