drivers: use SPDX tag for Cavium copyright files
[dpdk.git] / drivers / crypto / armv8 / rte_armv8_pmd_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
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 "armv8_crypto_defs.h"
12
13 #include "rte_armv8_pmd_private.h"
14
15 static const struct rte_cryptodev_capabilities
16         armv8_crypto_pmd_capabilities[] = {
17         {       /* SHA1 HMAC */
18                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
19                         {.sym = {
20                                 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
21                                 {.auth = {
22                                         .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
23                                         .block_size = 64,
24                                         .key_size = {
25                                                 .min = 1,
26                                                 .max = 64,
27                                                 .increment = 1
28                                         },
29                                         .digest_size = {
30                                                 .min = 20,
31                                                 .max = 20,
32                                                 .increment = 0
33                                         },
34                                         .iv_size = { 0 }
35                                 }, }
36                         }, }
37         },
38         {       /* SHA256 HMAC */
39                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
40                         {.sym = {
41                                 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
42                                 {.auth = {
43                                         .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
44                                         .block_size = 64,
45                                         .key_size = {
46                                                 .min = 1,
47                                                 .max = 64,
48                                                 .increment = 1
49                                         },
50                                         .digest_size = {
51                                                 .min = 32,
52                                                 .max = 32,
53                                                 .increment = 0
54                                         },
55                                         .iv_size = { 0 }
56                                 }, }
57                         }, }
58         },
59         {       /* AES CBC */
60                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
61                         {.sym = {
62                                 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
63                                 {.cipher = {
64                                         .algo = RTE_CRYPTO_CIPHER_AES_CBC,
65                                         .block_size = 16,
66                                         .key_size = {
67                                                 .min = 16,
68                                                 .max = 16,
69                                                 .increment = 0
70                                         },
71                                         .iv_size = {
72                                                 .min = 16,
73                                                 .max = 16,
74                                                 .increment = 0
75                                         }
76                                 }, }
77                         }, }
78         },
79
80         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
81 };
82
83
84 /** Configure device */
85 static int
86 armv8_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
87                 __rte_unused struct rte_cryptodev_config *config)
88 {
89         return 0;
90 }
91
92 /** Start device */
93 static int
94 armv8_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
95 {
96         return 0;
97 }
98
99 /** Stop device */
100 static void
101 armv8_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
102 {
103 }
104
105 /** Close device */
106 static int
107 armv8_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
108 {
109         return 0;
110 }
111
112
113 /** Get device statistics */
114 static void
115 armv8_crypto_pmd_stats_get(struct rte_cryptodev *dev,
116                 struct rte_cryptodev_stats *stats)
117 {
118         int qp_id;
119
120         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
121                 struct armv8_crypto_qp *qp = dev->data->queue_pairs[qp_id];
122
123                 stats->enqueued_count += qp->stats.enqueued_count;
124                 stats->dequeued_count += qp->stats.dequeued_count;
125
126                 stats->enqueue_err_count += qp->stats.enqueue_err_count;
127                 stats->dequeue_err_count += qp->stats.dequeue_err_count;
128         }
129 }
130
131 /** Reset device statistics */
132 static void
133 armv8_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
134 {
135         int qp_id;
136
137         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
138                 struct armv8_crypto_qp *qp = dev->data->queue_pairs[qp_id];
139
140                 memset(&qp->stats, 0, sizeof(qp->stats));
141         }
142 }
143
144
145 /** Get device info */
146 static void
147 armv8_crypto_pmd_info_get(struct rte_cryptodev *dev,
148                 struct rte_cryptodev_info *dev_info)
149 {
150         struct armv8_crypto_private *internals = dev->data->dev_private;
151
152         if (dev_info != NULL) {
153                 dev_info->driver_id = dev->driver_id;
154                 dev_info->feature_flags = dev->feature_flags;
155                 dev_info->capabilities = armv8_crypto_pmd_capabilities;
156                 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
157                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
158         }
159 }
160
161 /** Release queue pair */
162 static int
163 armv8_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
164 {
165
166         if (dev->data->queue_pairs[qp_id] != NULL) {
167                 rte_free(dev->data->queue_pairs[qp_id]);
168                 dev->data->queue_pairs[qp_id] = NULL;
169         }
170
171         return 0;
172 }
173
174 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
175 static int
176 armv8_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
177                 struct armv8_crypto_qp *qp)
178 {
179         unsigned int n;
180
181         n = snprintf(qp->name, sizeof(qp->name), "armv8_crypto_pmd_%u_qp_%u",
182                         dev->data->dev_id, qp->id);
183
184         if (n >= sizeof(qp->name))
185                 return -1;
186
187         return 0;
188 }
189
190
191 /** Create a ring to place processed operations on */
192 static struct rte_ring *
193 armv8_crypto_pmd_qp_create_processed_ops_ring(struct armv8_crypto_qp *qp,
194                 unsigned int ring_size, int socket_id)
195 {
196         struct rte_ring *r;
197
198         r = rte_ring_lookup(qp->name);
199         if (r) {
200                 if (rte_ring_get_size(r) >= ring_size) {
201                         ARMV8_CRYPTO_LOG_INFO(
202                                 "Reusing existing ring %s for processed ops",
203                                  qp->name);
204                         return r;
205                 }
206
207                 ARMV8_CRYPTO_LOG_ERR(
208                         "Unable to reuse existing ring %s for processed ops",
209                          qp->name);
210                 return NULL;
211         }
212
213         return rte_ring_create(qp->name, ring_size, socket_id,
214                         RING_F_SP_ENQ | RING_F_SC_DEQ);
215 }
216
217
218 /** Setup a queue pair */
219 static int
220 armv8_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
221                 const struct rte_cryptodev_qp_conf *qp_conf,
222                 int socket_id, struct rte_mempool *session_pool)
223 {
224         struct armv8_crypto_qp *qp = NULL;
225
226         /* Free memory prior to re-allocation if needed. */
227         if (dev->data->queue_pairs[qp_id] != NULL)
228                 armv8_crypto_pmd_qp_release(dev, qp_id);
229
230         /* Allocate the queue pair data structure. */
231         qp = rte_zmalloc_socket("ARMv8 PMD Queue Pair", sizeof(*qp),
232                                         RTE_CACHE_LINE_SIZE, socket_id);
233         if (qp == NULL)
234                 return -ENOMEM;
235
236         qp->id = qp_id;
237         dev->data->queue_pairs[qp_id] = qp;
238
239         if (armv8_crypto_pmd_qp_set_unique_name(dev, qp) != 0)
240                 goto qp_setup_cleanup;
241
242         qp->processed_ops = armv8_crypto_pmd_qp_create_processed_ops_ring(qp,
243                         qp_conf->nb_descriptors, socket_id);
244         if (qp->processed_ops == NULL)
245                 goto qp_setup_cleanup;
246
247         qp->sess_mp = session_pool;
248
249         memset(&qp->stats, 0, sizeof(qp->stats));
250
251         return 0;
252
253 qp_setup_cleanup:
254         if (qp)
255                 rte_free(qp);
256
257         return -1;
258 }
259
260 /** Start queue pair */
261 static int
262 armv8_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
263                 __rte_unused uint16_t queue_pair_id)
264 {
265         return -ENOTSUP;
266 }
267
268 /** Stop queue pair */
269 static int
270 armv8_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
271                 __rte_unused uint16_t queue_pair_id)
272 {
273         return -ENOTSUP;
274 }
275
276 /** Return the number of allocated queue pairs */
277 static uint32_t
278 armv8_crypto_pmd_qp_count(struct rte_cryptodev *dev)
279 {
280         return dev->data->nb_queue_pairs;
281 }
282
283 /** Returns the size of the session structure */
284 static unsigned
285 armv8_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
286 {
287         return sizeof(struct armv8_crypto_session);
288 }
289
290 /** Configure the session from a crypto xform chain */
291 static int
292 armv8_crypto_pmd_session_configure(struct rte_cryptodev *dev,
293                 struct rte_crypto_sym_xform *xform,
294                 struct rte_cryptodev_sym_session *sess,
295                 struct rte_mempool *mempool)
296 {
297         void *sess_private_data;
298         int ret;
299
300         if (unlikely(sess == NULL)) {
301                 ARMV8_CRYPTO_LOG_ERR("invalid session struct");
302                 return -EINVAL;
303         }
304
305         if (rte_mempool_get(mempool, &sess_private_data)) {
306                 CDEV_LOG_ERR(
307                         "Couldn't get object from session mempool");
308                 return -ENOMEM;
309         }
310
311         ret = armv8_crypto_set_session_parameters(sess_private_data, xform);
312         if (ret != 0) {
313                 ARMV8_CRYPTO_LOG_ERR("failed configure session parameters");
314
315                 /* Return session to mempool */
316                 rte_mempool_put(mempool, sess_private_data);
317                 return ret;
318         }
319
320         set_session_private_data(sess, dev->driver_id,
321                         sess_private_data);
322
323         return 0;
324 }
325
326 /** Clear the memory of session so it doesn't leave key material behind */
327 static void
328 armv8_crypto_pmd_session_clear(struct rte_cryptodev *dev,
329                 struct rte_cryptodev_sym_session *sess)
330 {
331         uint8_t index = dev->driver_id;
332         void *sess_priv = get_session_private_data(sess, index);
333
334         /* Zero out the whole structure */
335         if (sess_priv) {
336                 memset(sess_priv, 0, sizeof(struct armv8_crypto_session));
337                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
338                 set_session_private_data(sess, index, NULL);
339                 rte_mempool_put(sess_mp, sess_priv);
340         }
341 }
342
343 struct rte_cryptodev_ops armv8_crypto_pmd_ops = {
344                 .dev_configure          = armv8_crypto_pmd_config,
345                 .dev_start              = armv8_crypto_pmd_start,
346                 .dev_stop               = armv8_crypto_pmd_stop,
347                 .dev_close              = armv8_crypto_pmd_close,
348
349                 .stats_get              = armv8_crypto_pmd_stats_get,
350                 .stats_reset            = armv8_crypto_pmd_stats_reset,
351
352                 .dev_infos_get          = armv8_crypto_pmd_info_get,
353
354                 .queue_pair_setup       = armv8_crypto_pmd_qp_setup,
355                 .queue_pair_release     = armv8_crypto_pmd_qp_release,
356                 .queue_pair_start       = armv8_crypto_pmd_qp_start,
357                 .queue_pair_stop        = armv8_crypto_pmd_qp_stop,
358                 .queue_pair_count       = armv8_crypto_pmd_qp_count,
359
360                 .session_get_size       = armv8_crypto_pmd_session_get_size,
361                 .session_configure      = armv8_crypto_pmd_session_configure,
362                 .session_clear          = armv8_crypto_pmd_session_clear
363 };
364
365 struct rte_cryptodev_ops *rte_armv8_crypto_pmd_ops = &armv8_crypto_pmd_ops;