crypto/octeontx2: move capabilities init into probe
[dpdk.git] / drivers / crypto / zuc / rte_zuc_pmd_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation
3  */
4
5 #include <string.h>
6
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
10
11 #include "zuc_pmd_private.h"
12
13 static const struct rte_cryptodev_capabilities zuc_pmd_capabilities[] = {
14         {       /* ZUC (EIA3) */
15                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
16                 {.sym = {
17                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
18                         {.auth = {
19                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
20                                 .block_size = 16,
21                                 .key_size = {
22                                         .min = 16,
23                                         .max = 16,
24                                         .increment = 0
25                                 },
26                                 .digest_size = {
27                                         .min = 4,
28                                         .max = 4,
29                                         .increment = 0
30                                 },
31                                 .iv_size = {
32                                         .min = 16,
33                                         .max = 16,
34                                         .increment = 0
35                                 }
36                         }, }
37                 }, }
38         },
39         {       /* ZUC (EEA3) */
40                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
41                 {.sym = {
42                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
43                         {.cipher = {
44                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
45                                 .block_size = 16,
46                                 .key_size = {
47                                         .min = 16,
48                                         .max = 16,
49                                         .increment = 0
50                                 },
51                                 .iv_size = {
52                                         .min = 16,
53                                         .max = 16,
54                                         .increment = 0
55                                 },
56                         }, }
57                 }, }
58         },
59         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
60 };
61
62 /** Configure device */
63 static int
64 zuc_pmd_config(__rte_unused struct rte_cryptodev *dev,
65                 __rte_unused struct rte_cryptodev_config *config)
66 {
67         return 0;
68 }
69
70 /** Start device */
71 static int
72 zuc_pmd_start(__rte_unused struct rte_cryptodev *dev)
73 {
74         return 0;
75 }
76
77 /** Stop device */
78 static void
79 zuc_pmd_stop(__rte_unused struct rte_cryptodev *dev)
80 {
81 }
82
83 /** Close device */
84 static int
85 zuc_pmd_close(__rte_unused struct rte_cryptodev *dev)
86 {
87         return 0;
88 }
89
90
91 /** Get device statistics */
92 static void
93 zuc_pmd_stats_get(struct rte_cryptodev *dev,
94                 struct rte_cryptodev_stats *stats)
95 {
96         int qp_id;
97
98         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
99                 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
100
101                 stats->enqueued_count += qp->qp_stats.enqueued_count;
102                 stats->dequeued_count += qp->qp_stats.dequeued_count;
103
104                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
105                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
106         }
107 }
108
109 /** Reset device statistics */
110 static void
111 zuc_pmd_stats_reset(struct rte_cryptodev *dev)
112 {
113         int qp_id;
114
115         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
116                 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
117
118                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
119         }
120 }
121
122
123 /** Get device info */
124 static void
125 zuc_pmd_info_get(struct rte_cryptodev *dev,
126                 struct rte_cryptodev_info *dev_info)
127 {
128         struct zuc_private *internals = dev->data->dev_private;
129
130         if (dev_info != NULL) {
131                 dev_info->driver_id = dev->driver_id;
132                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
133                 /* No limit of number of sessions */
134                 dev_info->sym.max_nb_sessions = 0;
135                 dev_info->feature_flags = dev->feature_flags;
136                 dev_info->capabilities = zuc_pmd_capabilities;
137         }
138 }
139
140 /** Release queue pair */
141 static int
142 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
143 {
144         if (dev->data->queue_pairs[qp_id] != NULL) {
145                 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
146
147                 if (qp->processed_ops)
148                         rte_ring_free(qp->processed_ops);
149
150                 rte_free(dev->data->queue_pairs[qp_id]);
151                 dev->data->queue_pairs[qp_id] = NULL;
152         }
153         return 0;
154 }
155
156 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
157 static int
158 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
159                 struct zuc_qp *qp)
160 {
161         unsigned n = snprintf(qp->name, sizeof(qp->name),
162                         "zuc_pmd_%u_qp_%u",
163                         dev->data->dev_id, qp->id);
164
165         if (n >= sizeof(qp->name))
166                 return -1;
167
168         return 0;
169 }
170
171 /** Create a ring to place processed ops on */
172 static struct rte_ring *
173 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
174                 unsigned ring_size, int socket_id)
175 {
176         struct rte_ring *r;
177
178         r = rte_ring_lookup(qp->name);
179         if (r) {
180                 if (rte_ring_get_size(r) >= ring_size) {
181                         ZUC_LOG(INFO, "Reusing existing ring %s"
182                                         " for processed packets",
183                                          qp->name);
184                         return r;
185                 }
186
187                 ZUC_LOG(ERR, "Unable to reuse existing ring %s"
188                                 " for processed packets",
189                                  qp->name);
190                 return NULL;
191         }
192
193         return rte_ring_create(qp->name, ring_size, socket_id,
194                         RING_F_SP_ENQ | RING_F_SC_DEQ);
195 }
196
197 /** Setup a queue pair */
198 static int
199 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
200                 const struct rte_cryptodev_qp_conf *qp_conf,
201                 int socket_id)
202 {
203         struct zuc_qp *qp = NULL;
204         struct zuc_private *internals = dev->data->dev_private;
205
206         /* Free memory prior to re-allocation if needed. */
207         if (dev->data->queue_pairs[qp_id] != NULL)
208                 zuc_pmd_qp_release(dev, qp_id);
209
210         /* Allocate the queue pair data structure. */
211         qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
212                                         RTE_CACHE_LINE_SIZE, socket_id);
213         if (qp == NULL)
214                 return (-ENOMEM);
215
216         qp->id = qp_id;
217         dev->data->queue_pairs[qp_id] = qp;
218
219         if (zuc_pmd_qp_set_unique_name(dev, qp))
220                 goto qp_setup_cleanup;
221
222         qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
223                         qp_conf->nb_descriptors, socket_id);
224         if (qp->processed_ops == NULL)
225                 goto qp_setup_cleanup;
226
227         qp->mb_mgr = internals->mb_mgr;
228         qp->sess_mp = qp_conf->mp_session;
229         qp->sess_mp_priv = qp_conf->mp_session_private;
230
231         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
232
233         return 0;
234
235 qp_setup_cleanup:
236         if (qp)
237                 rte_free(qp);
238
239         return -1;
240 }
241
242 /** Returns the size of the ZUC session structure */
243 static unsigned
244 zuc_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
245 {
246         return sizeof(struct zuc_session);
247 }
248
249 /** Configure a ZUC session from a crypto xform chain */
250 static int
251 zuc_pmd_sym_session_configure(struct rte_cryptodev *dev __rte_unused,
252                 struct rte_crypto_sym_xform *xform,
253                 struct rte_cryptodev_sym_session *sess,
254                 struct rte_mempool *mempool)
255 {
256         void *sess_private_data;
257         int ret;
258
259         if (unlikely(sess == NULL)) {
260                 ZUC_LOG(ERR, "invalid session struct");
261                 return -EINVAL;
262         }
263
264         if (rte_mempool_get(mempool, &sess_private_data)) {
265                 ZUC_LOG(ERR,
266                         "Couldn't get object from session mempool");
267
268                 return -ENOMEM;
269         }
270
271         ret = zuc_set_session_parameters(sess_private_data, xform);
272         if (ret != 0) {
273                 ZUC_LOG(ERR, "failed configure session parameters");
274
275                 /* Return session to mempool */
276                 rte_mempool_put(mempool, sess_private_data);
277                 return ret;
278         }
279
280         set_sym_session_private_data(sess, dev->driver_id,
281                 sess_private_data);
282
283         return 0;
284 }
285
286 /** Clear the memory of session so it doesn't leave key material behind */
287 static void
288 zuc_pmd_sym_session_clear(struct rte_cryptodev *dev,
289                 struct rte_cryptodev_sym_session *sess)
290 {
291         uint8_t index = dev->driver_id;
292         void *sess_priv = get_sym_session_private_data(sess, index);
293
294         /* Zero out the whole structure */
295         if (sess_priv) {
296                 memset(sess_priv, 0, sizeof(struct zuc_session));
297                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
298                 set_sym_session_private_data(sess, index, NULL);
299                 rte_mempool_put(sess_mp, sess_priv);
300         }
301 }
302
303 struct rte_cryptodev_ops zuc_pmd_ops = {
304                 .dev_configure      = zuc_pmd_config,
305                 .dev_start          = zuc_pmd_start,
306                 .dev_stop           = zuc_pmd_stop,
307                 .dev_close          = zuc_pmd_close,
308
309                 .stats_get          = zuc_pmd_stats_get,
310                 .stats_reset        = zuc_pmd_stats_reset,
311
312                 .dev_infos_get      = zuc_pmd_info_get,
313
314                 .queue_pair_setup   = zuc_pmd_qp_setup,
315                 .queue_pair_release = zuc_pmd_qp_release,
316
317                 .sym_session_get_size   = zuc_pmd_sym_session_get_size,
318                 .sym_session_configure  = zuc_pmd_sym_session_configure,
319                 .sym_session_clear      = zuc_pmd_sym_session_clear
320 };
321
322 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;