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>
9 #include <rte_malloc.h>
11 #include "cpt_pmd_logs.h"
12 #include "cpt_pmd_ops_helper.h"
13 #include "cpt_ucode.h"
15 #include "otx_cryptodev.h"
16 #include "otx_cryptodev_capabilities.h"
17 #include "otx_cryptodev_hw_access.h"
18 #include "otx_cryptodev_ops.h"
20 static int otx_cryptodev_probe_count;
21 static rte_spinlock_t otx_probe_count_lock = RTE_SPINLOCK_INITIALIZER;
23 static struct rte_mempool *otx_cpt_meta_pool;
24 static int otx_cpt_op_mlen;
25 static int otx_cpt_op_sb_mlen;
27 /* Forward declarations */
30 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
33 * Initializes global variables used by fast-path code
36 * - 0 on success, errcode on error
39 init_global_resources(void)
41 /* Get meta len for scatter gather mode */
42 otx_cpt_op_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
44 /* Extra 4B saved for future considerations */
45 otx_cpt_op_mlen += 4 * sizeof(uint64_t);
47 otx_cpt_meta_pool = rte_mempool_create("cpt_metabuf-pool", 4096 * 16,
48 otx_cpt_op_mlen, 512, 0,
49 NULL, NULL, NULL, NULL,
51 if (!otx_cpt_meta_pool) {
52 CPT_LOG_ERR("cpt metabuf pool not created");
56 /* Get meta len for direct mode */
57 otx_cpt_op_sb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
59 /* Extra 4B saved for future considerations */
60 otx_cpt_op_sb_mlen += 4 * sizeof(uint64_t);
66 cleanup_global_resources(void)
69 rte_spinlock_lock(&otx_probe_count_lock);
71 /* Decrement the cryptodev count */
72 otx_cryptodev_probe_count--;
75 if (otx_cpt_meta_pool && otx_cryptodev_probe_count == 0)
76 rte_mempool_free(otx_cpt_meta_pool);
79 rte_spinlock_unlock(&otx_probe_count_lock);
85 otx_cpt_alarm_cb(void *arg)
87 struct cpt_vf *cptvf = arg;
88 otx_cpt_poll_misc(cptvf);
89 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
90 otx_cpt_alarm_cb, cptvf);
94 otx_cpt_periodic_alarm_start(void *arg)
96 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
97 otx_cpt_alarm_cb, arg);
101 otx_cpt_periodic_alarm_stop(void *arg)
103 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
109 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
110 struct rte_cryptodev_config *config __rte_unused)
112 CPT_PMD_INIT_FUNC_TRACE();
117 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
119 void *cptvf = c_dev->data->dev_private;
121 CPT_PMD_INIT_FUNC_TRACE();
123 return otx_cpt_start_device(cptvf);
127 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
129 void *cptvf = c_dev->data->dev_private;
131 CPT_PMD_INIT_FUNC_TRACE();
133 otx_cpt_stop_device(cptvf);
137 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
139 void *cptvf = c_dev->data->dev_private;
142 CPT_PMD_INIT_FUNC_TRACE();
144 for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
145 ret = otx_cpt_que_pair_release(c_dev, i);
150 otx_cpt_periodic_alarm_stop(cptvf);
151 otx_cpt_deinit_device(cptvf);
157 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
159 CPT_PMD_INIT_FUNC_TRACE();
161 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
162 info->feature_flags = dev->feature_flags;
163 info->capabilities = otx_get_capabilities();
164 info->sym.max_nb_sessions = 0;
165 info->driver_id = otx_cryptodev_driver_id;
166 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
167 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
172 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
173 struct rte_cryptodev_stats *stats __rte_unused)
175 CPT_PMD_INIT_FUNC_TRACE();
179 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
181 CPT_PMD_INIT_FUNC_TRACE();
185 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
186 uint16_t que_pair_id,
187 const struct rte_cryptodev_qp_conf *qp_conf,
188 int socket_id __rte_unused)
190 void *cptvf = dev->data->dev_private;
191 struct cpt_instance *instance = NULL;
192 struct rte_pci_device *pci_dev;
195 CPT_PMD_INIT_FUNC_TRACE();
197 if (dev->data->queue_pairs[que_pair_id] != NULL) {
198 ret = otx_cpt_que_pair_release(dev, que_pair_id);
203 if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
204 CPT_LOG_INFO("Number of descriptors too big %d, using default "
205 "queue length of %d", qp_conf->nb_descriptors,
209 pci_dev = RTE_DEV_TO_PCI(dev->device);
211 if (pci_dev->mem_resource[0].addr == NULL) {
212 CPT_LOG_ERR("PCI mem address null");
216 ret = otx_cpt_get_resource(cptvf, 0, &instance);
217 if (ret != 0 || instance == NULL) {
218 CPT_LOG_ERR("Error getting instance handle from device %s : "
219 "ret = %d", dev->data->name, ret);
223 instance->queue_id = que_pair_id;
224 instance->sess_mp = qp_conf->mp_session;
225 instance->sess_mp_priv = qp_conf->mp_session_private;
226 dev->data->queue_pairs[que_pair_id] = instance;
232 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
234 struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
237 CPT_PMD_INIT_FUNC_TRACE();
239 ret = otx_cpt_put_resource(instance);
241 CPT_LOG_ERR("Error putting instance handle of device %s : "
242 "ret = %d", dev->data->name, ret);
246 dev->data->queue_pairs[que_pair_id] = NULL;
252 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
254 return cpt_get_session_size();
258 otx_cpt_session_init(void *sym_sess, uint8_t driver_id)
260 struct rte_cryptodev_sym_session *sess = sym_sess;
261 struct cpt_sess_misc *cpt_sess =
262 (struct cpt_sess_misc *) get_sym_session_private_data(sess, driver_id);
264 CPT_PMD_INIT_FUNC_TRACE();
265 cpt_sess->ctx_dma_addr = rte_mempool_virt2iova(cpt_sess) +
266 sizeof(struct cpt_sess_misc);
270 otx_cpt_session_cfg(struct rte_cryptodev *dev,
271 struct rte_crypto_sym_xform *xform,
272 struct rte_cryptodev_sym_session *sess,
273 struct rte_mempool *mempool)
275 struct rte_crypto_sym_xform *chain;
276 void *sess_private_data = NULL;
278 CPT_PMD_INIT_FUNC_TRACE();
280 if (cpt_is_algo_supported(xform))
283 if (unlikely(sess == NULL)) {
284 CPT_LOG_ERR("invalid session struct");
288 if (rte_mempool_get(mempool, &sess_private_data)) {
289 CPT_LOG_ERR("Could not allocate sess_private_data");
295 switch (chain->type) {
296 case RTE_CRYPTO_SYM_XFORM_AEAD:
297 if (fill_sess_aead(chain, sess_private_data))
300 case RTE_CRYPTO_SYM_XFORM_CIPHER:
301 if (fill_sess_cipher(chain, sess_private_data))
304 case RTE_CRYPTO_SYM_XFORM_AUTH:
305 if (chain->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
306 if (fill_sess_gmac(chain, sess_private_data))
309 if (fill_sess_auth(chain, sess_private_data))
314 CPT_LOG_ERR("Invalid crypto xform type");
319 set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
320 otx_cpt_session_init(sess, dev->driver_id);
324 if (sess_private_data)
325 rte_mempool_put(mempool, sess_private_data);
330 otx_cpt_session_clear(struct rte_cryptodev *dev,
331 struct rte_cryptodev_sym_session *sess)
333 void *sess_priv = get_sym_session_private_data(sess, dev->driver_id);
335 CPT_PMD_INIT_FUNC_TRACE();
337 memset(sess_priv, 0, otx_cpt_get_session_size(dev));
338 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
339 set_sym_session_private_data(sess, dev->driver_id, NULL);
340 rte_mempool_put(sess_mp, sess_priv);
344 static __rte_always_inline int32_t __hot
345 otx_cpt_request_enqueue(struct cpt_instance *instance,
346 struct pending_queue *pqueue,
349 struct cpt_request_info *user_req = (struct cpt_request_info *)req;
351 if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN))
354 fill_cpt_inst(instance, req);
356 CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
358 /* Fill time_out cycles */
359 user_req->time_out = rte_get_timer_cycles() +
360 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
361 user_req->extra_time = 0;
363 /* Default mode of software queue */
364 mark_cpt_inst(instance);
366 pqueue->rid_queue[pqueue->enq_tail].rid = (uintptr_t)user_req;
368 /* We will use soft queue length here to limit requests */
369 MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
370 pqueue->pending_count += 1;
372 CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
373 "op: %p", user_req, user_req->op);
377 static __rte_always_inline int __hot
378 otx_cpt_enq_single_sym(struct cpt_instance *instance,
379 struct rte_crypto_op *op,
380 struct pending_queue *pqueue)
382 struct cpt_sess_misc *sess;
383 struct rte_crypto_sym_op *sym_op = op->sym;
384 void *prep_req, *mdata = NULL;
387 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
389 sess = (struct cpt_sess_misc *)
390 get_sym_session_private_data(sym_op->session,
391 otx_cryptodev_driver_id);
393 cpt_op = sess->cpt_op;
395 if (likely(cpt_op & CPT_OP_CIPHER_MASK))
396 ret = fill_fc_params(op, sess, &cptvf->meta_info, &mdata,
399 ret = fill_digest_params(op, sess, &cptvf->meta_info,
403 CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
404 "ret 0x%x", op, (unsigned int)cpt_op, ret);
408 /* Enqueue prepared instruction to h/w */
409 ret = otx_cpt_request_enqueue(instance, pqueue, prep_req);
412 /* Buffer allocated for request preparation need to be freed */
413 free_op_meta(mdata, cptvf->meta_info.cptvf_meta_pool);
420 static __rte_always_inline int __hot
421 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
422 struct rte_crypto_op *op,
423 struct pending_queue *pqueue)
425 struct cpt_sess_misc *sess;
426 struct rte_crypto_sym_op *sym_op = op->sym;
429 void *sess_private_data_t = NULL;
431 /* Create tmp session */
433 if (rte_mempool_get(instance->sess_mp, (void **)&sess_t)) {
438 if (rte_mempool_get(instance->sess_mp_priv,
439 (void **)&sess_private_data_t)) {
444 sess = (struct cpt_sess_misc *)sess_private_data_t;
446 sess->ctx_dma_addr = rte_mempool_virt2iova(sess) +
447 sizeof(struct cpt_sess_misc);
449 ret = instance_session_cfg(sym_op->xform, (void *)sess);
455 /* Save tmp session in op */
457 sym_op->session = (struct rte_cryptodev_sym_session *)sess_t;
458 set_sym_session_private_data(sym_op->session, otx_cryptodev_driver_id,
459 sess_private_data_t);
461 /* Enqueue op with the tmp session set */
462 ret = otx_cpt_enq_single_sym(instance, op, pqueue);
470 rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
472 rte_mempool_put(instance->sess_mp, sess_t);
477 static __rte_always_inline int __hot
478 otx_cpt_enq_single(struct cpt_instance *inst,
479 struct rte_crypto_op *op,
480 struct pending_queue *pqueue)
482 /* Check for the type */
484 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
485 return otx_cpt_enq_single_sym(inst, op, pqueue);
486 else if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
487 return otx_cpt_enq_single_sym_sessless(inst, op, pqueue);
489 /* Should not reach here */
494 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
496 struct cpt_instance *instance = (struct cpt_instance *)qptr;
499 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
500 struct pending_queue *pqueue = &cptvf->pqueue;
502 count = DEFAULT_CMD_QLEN - pqueue->pending_count;
507 while (likely(count < nb_ops)) {
509 /* Enqueue single op */
510 ret = otx_cpt_enq_single(instance, ops[count], pqueue);
516 otx_cpt_ring_dbell(instance, count);
520 static __rte_always_inline void
521 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp)
523 /* H/w has returned success */
524 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
526 /* Perform further post processing */
528 if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
529 /* Check if auth verify need to be completed */
530 if (unlikely(rsp[2]))
531 compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
537 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
539 struct cpt_instance *instance = (struct cpt_instance *)qptr;
540 struct cpt_request_info *user_req;
541 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
544 int i, count, pcount;
547 struct pending_queue *pqueue = &cptvf->pqueue;
548 struct rte_crypto_op *cop;
552 pcount = pqueue->pending_count;
553 count = (nb_ops > pcount) ? pcount : nb_ops;
555 for (i = 0; i < count; i++) {
556 rid_e = &pqueue->rid_queue[pqueue->deq_head];
557 user_req = (struct cpt_request_info *)(rid_e->rid);
559 if (likely((i+1) < count))
560 rte_prefetch_non_temporal((void *)rid_e[1].rid);
562 ret = check_nb_command_id(user_req, instance);
564 if (unlikely(ret == ERR_REQ_PENDING)) {
565 /* Stop checking for completions */
569 /* Return completion code and op handle */
571 ops[i] = user_req->op;
573 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
574 user_req, user_req->op, ret);
576 MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
577 pqueue->pending_count -= 1;
582 for (i = 0; i < nb_completed; i++) {
584 rsp = (void *)ops[i];
586 if (likely((i + 1) < nb_completed))
587 rte_prefetch0(ops[i+1]);
589 metabuf = (void *)rsp[0];
590 cop = (void *)rsp[1];
594 /* Check completion code */
596 if (likely(cc[i] == 0)) {
597 /* H/w success pkt. Post process */
598 otx_cpt_dequeue_post_process(cop, rsp);
599 } else if (cc[i] == ERR_GC_ICV_MISCOMPARE) {
600 /* auth data mismatch */
601 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
604 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
607 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
608 void *sess_private_data_t =
609 get_sym_session_private_data(cop->sym->session,
610 otx_cryptodev_driver_id);
611 memset(sess_private_data_t, 0,
612 cpt_get_session_size());
613 memset(cop->sym->session, 0,
614 rte_cryptodev_sym_get_existing_header_session_size(
616 rte_mempool_put(instance->sess_mp_priv,
617 sess_private_data_t);
618 rte_mempool_put(instance->sess_mp, cop->sym->session);
619 cop->sym->session = NULL;
621 free_op_meta(metabuf, cptvf->meta_info.cptvf_meta_pool);
627 static struct rte_cryptodev_ops cptvf_ops = {
628 /* Device related operations */
629 .dev_configure = otx_cpt_dev_config,
630 .dev_start = otx_cpt_dev_start,
631 .dev_stop = otx_cpt_dev_stop,
632 .dev_close = otx_cpt_dev_close,
633 .dev_infos_get = otx_cpt_dev_info_get,
635 .stats_get = otx_cpt_stats_get,
636 .stats_reset = otx_cpt_stats_reset,
637 .queue_pair_setup = otx_cpt_que_pair_setup,
638 .queue_pair_release = otx_cpt_que_pair_release,
639 .queue_pair_count = NULL,
641 /* Crypto related operations */
642 .sym_session_get_size = otx_cpt_get_session_size,
643 .sym_session_configure = otx_cpt_session_cfg,
644 .sym_session_clear = otx_cpt_session_clear
648 otx_cpt_common_vars_init(struct cpt_vf *cptvf)
650 cptvf->meta_info.cptvf_meta_pool = otx_cpt_meta_pool;
651 cptvf->meta_info.cptvf_op_mlen = otx_cpt_op_mlen;
652 cptvf->meta_info.cptvf_op_sb_mlen = otx_cpt_op_sb_mlen;
656 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
658 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
659 struct cpt_vf *cptvf = NULL;
664 if (pdev->mem_resource[0].phys_addr == 0ULL)
667 /* for secondary processes, we don't initialise any further as primary
668 * has already done this work.
670 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
673 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
674 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
678 CPT_LOG_ERR("Cannot allocate memory for device private data");
682 snprintf(dev_name, 32, "%02x:%02x.%x",
683 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
685 reg_base = pdev->mem_resource[0].addr;
687 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
692 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
694 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
699 /* Start off timer for mailbox interrupts */
700 otx_cpt_periodic_alarm_start(cptvf);
702 rte_spinlock_lock(&otx_probe_count_lock);
703 if (!otx_cryptodev_probe_count) {
704 ret = init_global_resources();
706 rte_spinlock_unlock(&otx_probe_count_lock);
710 otx_cryptodev_probe_count++;
711 rte_spinlock_unlock(&otx_probe_count_lock);
713 /* Initialize data path variables used by common code */
714 otx_cpt_common_vars_init(cptvf);
716 c_dev->dev_ops = &cptvf_ops;
718 c_dev->enqueue_burst = otx_cpt_pkt_enqueue;
719 c_dev->dequeue_burst = otx_cpt_pkt_dequeue;
721 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
722 RTE_CRYPTODEV_FF_HW_ACCELERATED |
723 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
724 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
725 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
726 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
728 /* Save dev private data */
729 c_dev->data->dev_private = cptvf;
734 otx_cpt_periodic_alarm_stop(cptvf);
735 otx_cpt_deinit_device(cptvf);
739 /* Free private data allocated */