crypto/octeontx: add global resource init
[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_malloc.h>
9
10 #include "cpt_pmd_logs.h"
11 #include "cpt_pmd_ops_helper.h"
12
13 #include "otx_cryptodev.h"
14 #include "otx_cryptodev_hw_access.h"
15 #include "otx_cryptodev_ops.h"
16
17 static int otx_cryptodev_probe_count;
18 static rte_spinlock_t otx_probe_count_lock = RTE_SPINLOCK_INITIALIZER;
19
20 static struct rte_mempool *otx_cpt_meta_pool;
21 static int otx_cpt_op_mlen;
22 static int otx_cpt_op_sb_mlen;
23
24 /*
25  * Initializes global variables used by fast-path code
26  *
27  * @return
28  *   - 0 on success, errcode on error
29  */
30 static int
31 init_global_resources(void)
32 {
33         /* Get meta len for scatter gather mode */
34         otx_cpt_op_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
35
36         /* Extra 4B saved for future considerations */
37         otx_cpt_op_mlen += 4 * sizeof(uint64_t);
38
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,
42                                                SOCKET_ID_ANY, 0);
43         if (!otx_cpt_meta_pool) {
44                 CPT_LOG_ERR("cpt metabuf pool not created");
45                 return -ENOMEM;
46         }
47
48         /* Get meta len for direct mode */
49         otx_cpt_op_sb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
50
51         /* Extra 4B saved for future considerations */
52         otx_cpt_op_sb_mlen += 4 * sizeof(uint64_t);
53
54         return 0;
55 }
56
57 void
58 cleanup_global_resources(void)
59 {
60         /* Take lock */
61         rte_spinlock_lock(&otx_probe_count_lock);
62
63         /* Decrement the cryptodev count */
64         otx_cryptodev_probe_count--;
65
66         /* Free buffers */
67         if (otx_cpt_meta_pool && otx_cryptodev_probe_count == 0)
68                 rte_mempool_free(otx_cpt_meta_pool);
69
70         /* Free lock */
71         rte_spinlock_unlock(&otx_probe_count_lock);
72 }
73
74 /* Alarm routines */
75
76 static void
77 otx_cpt_alarm_cb(void *arg)
78 {
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);
83 }
84
85 static int
86 otx_cpt_periodic_alarm_start(void *arg)
87 {
88         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
89                                  otx_cpt_alarm_cb, arg);
90 }
91
92 static int
93 otx_cpt_periodic_alarm_stop(void *arg)
94 {
95         return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
96 }
97
98 static void
99 otx_cpt_common_vars_init(struct cpt_vf *cptvf)
100 {
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;
104 }
105
106 int
107 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
108 {
109         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
110         struct cpt_vf *cptvf = NULL;
111         void *reg_base;
112         char dev_name[32];
113         int ret;
114
115         if (pdev->mem_resource[0].phys_addr == 0ULL)
116                 return -EIO;
117
118         /* for secondary processes, we don't initialise any further as primary
119          * has already done this work.
120          */
121         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
122                 return 0;
123
124         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
125                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
126                         rte_socket_id());
127
128         if (cptvf == NULL) {
129                 CPT_LOG_ERR("Cannot allocate memory for device private data");
130                 return -ENOMEM;
131         }
132
133         snprintf(dev_name, 32, "%02x:%02x.%x",
134                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
135
136         reg_base = pdev->mem_resource[0].addr;
137         if (!reg_base) {
138                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
139                 ret = -ENODEV;
140                 goto fail;
141         }
142
143         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
144         if (ret) {
145                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
146                 ret = -EIO;
147                 goto fail;
148         }
149
150         /* Start off timer for mailbox interrupts */
151         otx_cpt_periodic_alarm_start(cptvf);
152
153         rte_spinlock_lock(&otx_probe_count_lock);
154         if (!otx_cryptodev_probe_count) {
155                 ret = init_global_resources();
156                 if (ret) {
157                         rte_spinlock_unlock(&otx_probe_count_lock);
158                         goto init_fail;
159                 }
160         }
161         otx_cryptodev_probe_count++;
162         rte_spinlock_unlock(&otx_probe_count_lock);
163
164         /* Initialize data path variables used by common code */
165         otx_cpt_common_vars_init(cptvf);
166
167         c_dev->dev_ops = NULL;
168
169         c_dev->enqueue_burst = NULL;
170         c_dev->dequeue_burst = NULL;
171
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;
178
179         /* Save dev private data */
180         c_dev->data->dev_private = cptvf;
181
182         return 0;
183
184 init_fail:
185         otx_cpt_periodic_alarm_stop(cptvf);
186         otx_cpt_deinit_device(cptvf);
187
188 fail:
189         if (cptvf) {
190                 /* Free private data allocated */
191                 rte_free(cptvf);
192         }
193
194         return ret;
195 }