crypto/octeontx: add queue pair functions
[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 /* Forward declarations */
27
28 static int
29 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
30
31 /*
32  * Initializes global variables used by fast-path code
33  *
34  * @return
35  *   - 0 on success, errcode on error
36  */
37 static int
38 init_global_resources(void)
39 {
40         /* Get meta len for scatter gather mode */
41         otx_cpt_op_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
42
43         /* Extra 4B saved for future considerations */
44         otx_cpt_op_mlen += 4 * sizeof(uint64_t);
45
46         otx_cpt_meta_pool = rte_mempool_create("cpt_metabuf-pool", 4096 * 16,
47                                                otx_cpt_op_mlen, 512, 0,
48                                                NULL, NULL, NULL, NULL,
49                                                SOCKET_ID_ANY, 0);
50         if (!otx_cpt_meta_pool) {
51                 CPT_LOG_ERR("cpt metabuf pool not created");
52                 return -ENOMEM;
53         }
54
55         /* Get meta len for direct mode */
56         otx_cpt_op_sb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
57
58         /* Extra 4B saved for future considerations */
59         otx_cpt_op_sb_mlen += 4 * sizeof(uint64_t);
60
61         return 0;
62 }
63
64 void
65 cleanup_global_resources(void)
66 {
67         /* Take lock */
68         rte_spinlock_lock(&otx_probe_count_lock);
69
70         /* Decrement the cryptodev count */
71         otx_cryptodev_probe_count--;
72
73         /* Free buffers */
74         if (otx_cpt_meta_pool && otx_cryptodev_probe_count == 0)
75                 rte_mempool_free(otx_cpt_meta_pool);
76
77         /* Free lock */
78         rte_spinlock_unlock(&otx_probe_count_lock);
79 }
80
81 /* Alarm routines */
82
83 static void
84 otx_cpt_alarm_cb(void *arg)
85 {
86         struct cpt_vf *cptvf = arg;
87         otx_cpt_poll_misc(cptvf);
88         rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
89                           otx_cpt_alarm_cb, cptvf);
90 }
91
92 static int
93 otx_cpt_periodic_alarm_start(void *arg)
94 {
95         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
96                                  otx_cpt_alarm_cb, arg);
97 }
98
99 static int
100 otx_cpt_periodic_alarm_stop(void *arg)
101 {
102         return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
103 }
104
105 /* PMD ops */
106
107 static int
108 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
109                    struct rte_cryptodev_config *config __rte_unused)
110 {
111         CPT_PMD_INIT_FUNC_TRACE();
112         return 0;
113 }
114
115 static int
116 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
117 {
118         void *cptvf = c_dev->data->dev_private;
119
120         CPT_PMD_INIT_FUNC_TRACE();
121
122         return otx_cpt_start_device(cptvf);
123 }
124
125 static void
126 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
127 {
128         void *cptvf = c_dev->data->dev_private;
129
130         CPT_PMD_INIT_FUNC_TRACE();
131
132         otx_cpt_stop_device(cptvf);
133 }
134
135 static int
136 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
137 {
138         void *cptvf = c_dev->data->dev_private;
139         int i, ret;
140
141         CPT_PMD_INIT_FUNC_TRACE();
142
143         for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
144                 ret = otx_cpt_que_pair_release(c_dev, i);
145                 if (ret)
146                         return ret;
147         }
148
149         otx_cpt_periodic_alarm_stop(cptvf);
150         otx_cpt_deinit_device(cptvf);
151
152         return 0;
153 }
154
155 static void
156 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
157 {
158         CPT_PMD_INIT_FUNC_TRACE();
159         if (info != NULL) {
160                 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
161                 info->feature_flags = dev->feature_flags;
162                 info->capabilities = otx_get_capabilities();
163                 info->sym.max_nb_sessions = 0;
164                 info->driver_id = otx_cryptodev_driver_id;
165                 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
166                 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
167         }
168 }
169
170 static void
171 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
172                   struct rte_cryptodev_stats *stats __rte_unused)
173 {
174         CPT_PMD_INIT_FUNC_TRACE();
175 }
176
177 static void
178 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
179 {
180         CPT_PMD_INIT_FUNC_TRACE();
181 }
182
183 static int
184 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
185                        uint16_t que_pair_id,
186                        const struct rte_cryptodev_qp_conf *qp_conf,
187                        int socket_id __rte_unused,
188                        struct rte_mempool *session_pool __rte_unused)
189 {
190         void *cptvf = dev->data->dev_private;
191         struct cpt_instance *instance = NULL;
192         struct rte_pci_device *pci_dev;
193         int ret = -1;
194
195         CPT_PMD_INIT_FUNC_TRACE();
196
197         if (dev->data->queue_pairs[que_pair_id] != NULL) {
198                 ret = otx_cpt_que_pair_release(dev, que_pair_id);
199                 if (ret)
200                         return ret;
201         }
202
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,
206                              DEFAULT_CMD_QLEN);
207         }
208
209         pci_dev = RTE_DEV_TO_PCI(dev->device);
210
211         if (pci_dev->mem_resource[0].addr == NULL) {
212                 CPT_LOG_ERR("PCI mem address null");
213                 return -EIO;
214         }
215
216         ret = otx_cpt_get_resource(cptvf, 0, &instance);
217         if (ret != 0) {
218                 CPT_LOG_ERR("Error getting instance handle from device %s : "
219                             "ret = %d", dev->data->name, ret);
220                 return ret;
221         }
222
223         instance->queue_id = que_pair_id;
224         dev->data->queue_pairs[que_pair_id] = instance;
225
226         return 0;
227 }
228
229 static int
230 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
231 {
232         struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
233         int ret;
234
235         CPT_PMD_INIT_FUNC_TRACE();
236
237         ret = otx_cpt_put_resource(instance);
238         if (ret != 0) {
239                 CPT_LOG_ERR("Error putting instance handle of device %s : "
240                             "ret = %d", dev->data->name, ret);
241                 return ret;
242         }
243
244         dev->data->queue_pairs[que_pair_id] = NULL;
245
246         return 0;
247 }
248
249 static struct rte_cryptodev_ops cptvf_ops = {
250         /* Device related operations */
251         .dev_configure = otx_cpt_dev_config,
252         .dev_start = otx_cpt_dev_start,
253         .dev_stop = otx_cpt_dev_stop,
254         .dev_close = otx_cpt_dev_close,
255         .dev_infos_get = otx_cpt_dev_info_get,
256
257         .stats_get = otx_cpt_stats_get,
258         .stats_reset = otx_cpt_stats_reset,
259         .queue_pair_setup = otx_cpt_que_pair_setup,
260         .queue_pair_release = otx_cpt_que_pair_release,
261         .queue_pair_count = NULL,
262
263         /* Crypto related operations */
264         .sym_session_get_size = NULL,
265         .sym_session_configure = NULL,
266         .sym_session_clear = NULL
267 };
268
269 static void
270 otx_cpt_common_vars_init(struct cpt_vf *cptvf)
271 {
272         cptvf->meta_info.cptvf_meta_pool = otx_cpt_meta_pool;
273         cptvf->meta_info.cptvf_op_mlen = otx_cpt_op_mlen;
274         cptvf->meta_info.cptvf_op_sb_mlen = otx_cpt_op_sb_mlen;
275 }
276
277 int
278 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
279 {
280         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
281         struct cpt_vf *cptvf = NULL;
282         void *reg_base;
283         char dev_name[32];
284         int ret;
285
286         if (pdev->mem_resource[0].phys_addr == 0ULL)
287                 return -EIO;
288
289         /* for secondary processes, we don't initialise any further as primary
290          * has already done this work.
291          */
292         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
293                 return 0;
294
295         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
296                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
297                         rte_socket_id());
298
299         if (cptvf == NULL) {
300                 CPT_LOG_ERR("Cannot allocate memory for device private data");
301                 return -ENOMEM;
302         }
303
304         snprintf(dev_name, 32, "%02x:%02x.%x",
305                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
306
307         reg_base = pdev->mem_resource[0].addr;
308         if (!reg_base) {
309                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
310                 ret = -ENODEV;
311                 goto fail;
312         }
313
314         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
315         if (ret) {
316                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
317                 ret = -EIO;
318                 goto fail;
319         }
320
321         /* Start off timer for mailbox interrupts */
322         otx_cpt_periodic_alarm_start(cptvf);
323
324         rte_spinlock_lock(&otx_probe_count_lock);
325         if (!otx_cryptodev_probe_count) {
326                 ret = init_global_resources();
327                 if (ret) {
328                         rte_spinlock_unlock(&otx_probe_count_lock);
329                         goto init_fail;
330                 }
331         }
332         otx_cryptodev_probe_count++;
333         rte_spinlock_unlock(&otx_probe_count_lock);
334
335         /* Initialize data path variables used by common code */
336         otx_cpt_common_vars_init(cptvf);
337
338         c_dev->dev_ops = &cptvf_ops;
339
340         c_dev->enqueue_burst = NULL;
341         c_dev->dequeue_burst = NULL;
342
343         c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
344                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
345                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
346                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
347                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
348                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
349
350         /* Save dev private data */
351         c_dev->data->dev_private = cptvf;
352
353         return 0;
354
355 init_fail:
356         otx_cpt_periodic_alarm_stop(cptvf);
357         otx_cpt_deinit_device(cptvf);
358
359 fail:
360         if (cptvf) {
361                 /* Free private data allocated */
362                 rte_free(cptvf);
363         }
364
365         return ret;
366 }