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_malloc.h>
10 #include "cpt_pmd_logs.h"
11 #include "cpt_pmd_ops_helper.h"
13 #include "otx_cryptodev.h"
14 #include "otx_cryptodev_hw_access.h"
15 #include "otx_cryptodev_ops.h"
17 static int otx_cryptodev_probe_count;
18 static rte_spinlock_t otx_probe_count_lock = RTE_SPINLOCK_INITIALIZER;
20 static struct rte_mempool *otx_cpt_meta_pool;
21 static int otx_cpt_op_mlen;
22 static int otx_cpt_op_sb_mlen;
25 * Initializes global variables used by fast-path code
28 * - 0 on success, errcode on error
31 init_global_resources(void)
33 /* Get meta len for scatter gather mode */
34 otx_cpt_op_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
36 /* Extra 4B saved for future considerations */
37 otx_cpt_op_mlen += 4 * sizeof(uint64_t);
39 otx_cpt_meta_pool = rte_mempool_create("cpt_metabuf-pool", 4096 * 16,
40 otx_cpt_op_mlen, 512, 0,
41 NULL, NULL, NULL, NULL,
43 if (!otx_cpt_meta_pool) {
44 CPT_LOG_ERR("cpt metabuf pool not created");
48 /* Get meta len for direct mode */
49 otx_cpt_op_sb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
51 /* Extra 4B saved for future considerations */
52 otx_cpt_op_sb_mlen += 4 * sizeof(uint64_t);
58 cleanup_global_resources(void)
61 rte_spinlock_lock(&otx_probe_count_lock);
63 /* Decrement the cryptodev count */
64 otx_cryptodev_probe_count--;
67 if (otx_cpt_meta_pool && otx_cryptodev_probe_count == 0)
68 rte_mempool_free(otx_cpt_meta_pool);
71 rte_spinlock_unlock(&otx_probe_count_lock);
77 otx_cpt_alarm_cb(void *arg)
79 struct cpt_vf *cptvf = arg;
80 otx_cpt_poll_misc(cptvf);
81 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
82 otx_cpt_alarm_cb, cptvf);
86 otx_cpt_periodic_alarm_start(void *arg)
88 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
89 otx_cpt_alarm_cb, arg);
93 otx_cpt_periodic_alarm_stop(void *arg)
95 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
99 otx_cpt_common_vars_init(struct cpt_vf *cptvf)
101 cptvf->meta_info.cptvf_meta_pool = otx_cpt_meta_pool;
102 cptvf->meta_info.cptvf_op_mlen = otx_cpt_op_mlen;
103 cptvf->meta_info.cptvf_op_sb_mlen = otx_cpt_op_sb_mlen;
107 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
109 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
110 struct cpt_vf *cptvf = NULL;
115 if (pdev->mem_resource[0].phys_addr == 0ULL)
118 /* for secondary processes, we don't initialise any further as primary
119 * has already done this work.
121 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
124 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
125 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
129 CPT_LOG_ERR("Cannot allocate memory for device private data");
133 snprintf(dev_name, 32, "%02x:%02x.%x",
134 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
136 reg_base = pdev->mem_resource[0].addr;
138 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
143 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
145 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
150 /* Start off timer for mailbox interrupts */
151 otx_cpt_periodic_alarm_start(cptvf);
153 rte_spinlock_lock(&otx_probe_count_lock);
154 if (!otx_cryptodev_probe_count) {
155 ret = init_global_resources();
157 rte_spinlock_unlock(&otx_probe_count_lock);
161 otx_cryptodev_probe_count++;
162 rte_spinlock_unlock(&otx_probe_count_lock);
164 /* Initialize data path variables used by common code */
165 otx_cpt_common_vars_init(cptvf);
167 c_dev->dev_ops = NULL;
169 c_dev->enqueue_burst = NULL;
170 c_dev->dequeue_burst = NULL;
172 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
173 RTE_CRYPTODEV_FF_HW_ACCELERATED |
174 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
175 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
176 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
177 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
179 /* Save dev private data */
180 c_dev->data->dev_private = cptvf;
185 otx_cpt_periodic_alarm_stop(cptvf);
186 otx_cpt_deinit_device(cptvf);
190 /* Free private data allocated */