crypto/octeontx: add basic device operations
[dpdk.git] / drivers / crypto / octeontx / otx_cryptodev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4
5 #include <rte_alarm.h>
6 #include <rte_bus_pci.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_malloc.h>
10
11 #include "cpt_pmd_logs.h"
12 #include "cpt_pmd_ops_helper.h"
13
14 #include "otx_cryptodev.h"
15 #include "otx_cryptodev_capabilities.h"
16 #include "otx_cryptodev_hw_access.h"
17 #include "otx_cryptodev_ops.h"
18
19 static int otx_cryptodev_probe_count;
20 static rte_spinlock_t otx_probe_count_lock = RTE_SPINLOCK_INITIALIZER;
21
22 static struct rte_mempool *otx_cpt_meta_pool;
23 static int otx_cpt_op_mlen;
24 static int otx_cpt_op_sb_mlen;
25
26 /*
27  * Initializes global variables used by fast-path code
28  *
29  * @return
30  *   - 0 on success, errcode on error
31  */
32 static int
33 init_global_resources(void)
34 {
35         /* Get meta len for scatter gather mode */
36         otx_cpt_op_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
37
38         /* Extra 4B saved for future considerations */
39         otx_cpt_op_mlen += 4 * sizeof(uint64_t);
40
41         otx_cpt_meta_pool = rte_mempool_create("cpt_metabuf-pool", 4096 * 16,
42                                                otx_cpt_op_mlen, 512, 0,
43                                                NULL, NULL, NULL, NULL,
44                                                SOCKET_ID_ANY, 0);
45         if (!otx_cpt_meta_pool) {
46                 CPT_LOG_ERR("cpt metabuf pool not created");
47                 return -ENOMEM;
48         }
49
50         /* Get meta len for direct mode */
51         otx_cpt_op_sb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
52
53         /* Extra 4B saved for future considerations */
54         otx_cpt_op_sb_mlen += 4 * sizeof(uint64_t);
55
56         return 0;
57 }
58
59 void
60 cleanup_global_resources(void)
61 {
62         /* Take lock */
63         rte_spinlock_lock(&otx_probe_count_lock);
64
65         /* Decrement the cryptodev count */
66         otx_cryptodev_probe_count--;
67
68         /* Free buffers */
69         if (otx_cpt_meta_pool && otx_cryptodev_probe_count == 0)
70                 rte_mempool_free(otx_cpt_meta_pool);
71
72         /* Free lock */
73         rte_spinlock_unlock(&otx_probe_count_lock);
74 }
75
76 /* Alarm routines */
77
78 static void
79 otx_cpt_alarm_cb(void *arg)
80 {
81         struct cpt_vf *cptvf = arg;
82         otx_cpt_poll_misc(cptvf);
83         rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
84                           otx_cpt_alarm_cb, cptvf);
85 }
86
87 static int
88 otx_cpt_periodic_alarm_start(void *arg)
89 {
90         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
91                                  otx_cpt_alarm_cb, arg);
92 }
93
94 static int
95 otx_cpt_periodic_alarm_stop(void *arg)
96 {
97         return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
98 }
99
100 /* PMD ops */
101
102 static int
103 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
104                    struct rte_cryptodev_config *config __rte_unused)
105 {
106         CPT_PMD_INIT_FUNC_TRACE();
107         return 0;
108 }
109
110 static int
111 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
112 {
113         void *cptvf = c_dev->data->dev_private;
114
115         CPT_PMD_INIT_FUNC_TRACE();
116
117         return otx_cpt_start_device(cptvf);
118 }
119
120 static void
121 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
122 {
123         void *cptvf = c_dev->data->dev_private;
124
125         CPT_PMD_INIT_FUNC_TRACE();
126
127         otx_cpt_stop_device(cptvf);
128 }
129
130 static int
131 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
132 {
133         void *cptvf = c_dev->data->dev_private;
134
135         CPT_PMD_INIT_FUNC_TRACE();
136
137         otx_cpt_periodic_alarm_stop(cptvf);
138         otx_cpt_deinit_device(cptvf);
139
140         return 0;
141 }
142
143 static void
144 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
145 {
146         CPT_PMD_INIT_FUNC_TRACE();
147         if (info != NULL) {
148                 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
149                 info->feature_flags = dev->feature_flags;
150                 info->capabilities = otx_get_capabilities();
151                 info->sym.max_nb_sessions = 0;
152                 info->driver_id = otx_cryptodev_driver_id;
153                 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
154                 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
155         }
156 }
157
158 static void
159 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
160                   struct rte_cryptodev_stats *stats __rte_unused)
161 {
162         CPT_PMD_INIT_FUNC_TRACE();
163 }
164
165 static void
166 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
167 {
168         CPT_PMD_INIT_FUNC_TRACE();
169 }
170
171 static struct rte_cryptodev_ops cptvf_ops = {
172         /* Device related operations */
173         .dev_configure = otx_cpt_dev_config,
174         .dev_start = otx_cpt_dev_start,
175         .dev_stop = otx_cpt_dev_stop,
176         .dev_close = otx_cpt_dev_close,
177         .dev_infos_get = otx_cpt_dev_info_get,
178
179         .stats_get = otx_cpt_stats_get,
180         .stats_reset = otx_cpt_stats_reset,
181         .queue_pair_setup = NULL,
182         .queue_pair_release = NULL,
183         .queue_pair_count = NULL,
184
185         /* Crypto related operations */
186         .sym_session_get_size = NULL,
187         .sym_session_configure = NULL,
188         .sym_session_clear = NULL
189 };
190
191 static void
192 otx_cpt_common_vars_init(struct cpt_vf *cptvf)
193 {
194         cptvf->meta_info.cptvf_meta_pool = otx_cpt_meta_pool;
195         cptvf->meta_info.cptvf_op_mlen = otx_cpt_op_mlen;
196         cptvf->meta_info.cptvf_op_sb_mlen = otx_cpt_op_sb_mlen;
197 }
198
199 int
200 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
201 {
202         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
203         struct cpt_vf *cptvf = NULL;
204         void *reg_base;
205         char dev_name[32];
206         int ret;
207
208         if (pdev->mem_resource[0].phys_addr == 0ULL)
209                 return -EIO;
210
211         /* for secondary processes, we don't initialise any further as primary
212          * has already done this work.
213          */
214         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
215                 return 0;
216
217         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
218                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
219                         rte_socket_id());
220
221         if (cptvf == NULL) {
222                 CPT_LOG_ERR("Cannot allocate memory for device private data");
223                 return -ENOMEM;
224         }
225
226         snprintf(dev_name, 32, "%02x:%02x.%x",
227                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
228
229         reg_base = pdev->mem_resource[0].addr;
230         if (!reg_base) {
231                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
232                 ret = -ENODEV;
233                 goto fail;
234         }
235
236         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
237         if (ret) {
238                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
239                 ret = -EIO;
240                 goto fail;
241         }
242
243         /* Start off timer for mailbox interrupts */
244         otx_cpt_periodic_alarm_start(cptvf);
245
246         rte_spinlock_lock(&otx_probe_count_lock);
247         if (!otx_cryptodev_probe_count) {
248                 ret = init_global_resources();
249                 if (ret) {
250                         rte_spinlock_unlock(&otx_probe_count_lock);
251                         goto init_fail;
252                 }
253         }
254         otx_cryptodev_probe_count++;
255         rte_spinlock_unlock(&otx_probe_count_lock);
256
257         /* Initialize data path variables used by common code */
258         otx_cpt_common_vars_init(cptvf);
259
260         c_dev->dev_ops = &cptvf_ops;
261
262         c_dev->enqueue_burst = NULL;
263         c_dev->dequeue_burst = NULL;
264
265         c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
266                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
267                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
268                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
269                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
270                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
271
272         /* Save dev private data */
273         c_dev->data->dev_private = cptvf;
274
275         return 0;
276
277 init_fail:
278         otx_cpt_periodic_alarm_stop(cptvf);
279         otx_cpt_deinit_device(cptvf);
280
281 fail:
282         if (cptvf) {
283                 /* Free private data allocated */
284                 rte_free(cptvf);
285         }
286
287         return ret;
288 }