crypto/ccp: support AES-GCM
[dpdk.git] / drivers / crypto / ccp / ccp_pmd_ops.c
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  */
4
5 #include <string.h>
6
7 #include <rte_common.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_malloc.h>
10
11 #include "ccp_pmd_private.h"
12 #include "ccp_dev.h"
13 #include "ccp_crypto.h"
14
15 static const struct rte_cryptodev_capabilities ccp_pmd_capabilities[] = {
16         {       /*AES-CMAC */
17                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
18                 {.sym = {
19                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
20                         {.auth = {
21                                  .algo = RTE_CRYPTO_AUTH_AES_CMAC,
22                                  .block_size = 16,
23                                  .key_size = {
24                                          .min = 16,
25                                          .max = 32,
26                                          .increment = 8
27                                  },
28                                  .digest_size = {
29                                          .min = 16,
30                                          .max = 16,
31                                          .increment = 0
32                                  },
33                         }, }
34                 }, }
35         },
36         {       /* AES ECB */
37                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
38                 {.sym = {
39                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
40                         {.cipher = {
41                                 .algo = RTE_CRYPTO_CIPHER_AES_ECB,
42                                 .block_size = 16,
43                                 .key_size = {
44                                    .min = 16,
45                                    .max = 32,
46                                    .increment = 8
47                                 },
48                                 .iv_size = {
49                                    .min = 0,
50                                    .max = 0,
51                                    .increment = 0
52                                 }
53                         }, }
54                 }, }
55         },
56         {       /* AES CBC */
57                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
58                 {.sym = {
59                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
60                         {.cipher = {
61                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
62                                 .block_size = 16,
63                                 .key_size = {
64                                         .min = 16,
65                                         .max = 32,
66                                         .increment = 8
67                                 },
68                                 .iv_size = {
69                                         .min = 16,
70                                         .max = 16,
71                                         .increment = 0
72                                 }
73                         }, }
74                 }, }
75         },
76         {       /* AES CTR */
77                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
78                 {.sym = {
79                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
80                         {.cipher = {
81                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
82                                 .block_size = 16,
83                                 .key_size = {
84                                         .min = 16,
85                                         .max = 32,
86                                         .increment = 8
87                                 },
88                                 .iv_size = {
89                                         .min = 16,
90                                         .max = 16,
91                                         .increment = 0
92                                 }
93                         }, }
94                 }, }
95         },
96         {       /* 3DES CBC */
97                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
98                 {.sym = {
99                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
100                         {.cipher = {
101                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
102                                 .block_size = 8,
103                                 .key_size = {
104                                         .min = 16,
105                                         .max = 24,
106                                         .increment = 8
107                                 },
108                                 .iv_size = {
109                                         .min = 8,
110                                         .max = 8,
111                                         .increment = 0
112                                 }
113                         }, }
114                 }, }
115         },
116         {       /* AES GCM */
117                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
118                 {.sym = {
119                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
120                         {.aead = {
121                                  .algo = RTE_CRYPTO_AEAD_AES_GCM,
122                                  .block_size = 16,
123                                  .key_size = {
124                                          .min = 16,
125                                          .max = 32,
126                                          .increment = 8
127                                  },
128                                  .digest_size = {
129                                          .min = 16,
130                                          .max = 16,
131                                          .increment = 0
132                                  },
133                                  .aad_size = {
134                                          .min = 0,
135                                          .max = 65535,
136                                          .increment = 1
137                                  },
138                                  .iv_size = {
139                                          .min = 12,
140                                          .max = 16,
141                                          .increment = 4
142                                  },
143                         }, }
144                 }, }
145         },
146         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
147 };
148
149 static int
150 ccp_pmd_config(struct rte_cryptodev *dev __rte_unused,
151                struct rte_cryptodev_config *config __rte_unused)
152 {
153         return 0;
154 }
155
156 static int
157 ccp_pmd_start(struct rte_cryptodev *dev)
158 {
159         return ccp_dev_start(dev);
160 }
161
162 static void
163 ccp_pmd_stop(struct rte_cryptodev *dev __rte_unused)
164 {
165
166 }
167
168 static int
169 ccp_pmd_close(struct rte_cryptodev *dev __rte_unused)
170 {
171         return 0;
172 }
173
174 static void
175 ccp_pmd_stats_get(struct rte_cryptodev *dev,
176                   struct rte_cryptodev_stats *stats)
177 {
178         int qp_id;
179
180         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
181                 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
182
183                 stats->enqueued_count += qp->qp_stats.enqueued_count;
184                 stats->dequeued_count += qp->qp_stats.dequeued_count;
185
186                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
187                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
188         }
189
190 }
191
192 static void
193 ccp_pmd_stats_reset(struct rte_cryptodev *dev)
194 {
195         int qp_id;
196
197         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
198                 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
199
200                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
201         }
202 }
203
204 static void
205 ccp_pmd_info_get(struct rte_cryptodev *dev,
206                  struct rte_cryptodev_info *dev_info)
207 {
208         struct ccp_private *internals = dev->data->dev_private;
209
210         if (dev_info != NULL) {
211                 dev_info->driver_id = dev->driver_id;
212                 dev_info->feature_flags = dev->feature_flags;
213                 dev_info->capabilities = ccp_pmd_capabilities;
214                 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
215                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
216         }
217 }
218
219 static int
220 ccp_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
221 {
222         struct ccp_qp *qp;
223
224         if (dev->data->queue_pairs[qp_id] != NULL) {
225                 qp = (struct ccp_qp *)dev->data->queue_pairs[qp_id];
226                 rte_ring_free(qp->processed_pkts);
227                 rte_mempool_free(qp->batch_mp);
228                 rte_free(qp);
229                 dev->data->queue_pairs[qp_id] = NULL;
230         }
231         return 0;
232 }
233
234 static int
235 ccp_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
236                 struct ccp_qp *qp)
237 {
238         unsigned int n = snprintf(qp->name, sizeof(qp->name),
239                         "ccp_pmd_%u_qp_%u",
240                         dev->data->dev_id, qp->id);
241
242         if (n > sizeof(qp->name))
243                 return -1;
244
245         return 0;
246 }
247
248 static struct rte_ring *
249 ccp_pmd_qp_create_batch_info_ring(struct ccp_qp *qp,
250                                   unsigned int ring_size, int socket_id)
251 {
252         struct rte_ring *r;
253
254         r = rte_ring_lookup(qp->name);
255         if (r) {
256                 if (r->size >= ring_size) {
257                         CCP_LOG_INFO(
258                                 "Reusing ring %s for processed packets",
259                                 qp->name);
260                         return r;
261                 }
262                 CCP_LOG_INFO(
263                         "Unable to reuse ring %s for processed packets",
264                          qp->name);
265                 return NULL;
266         }
267
268         return rte_ring_create(qp->name, ring_size, socket_id,
269                         RING_F_SP_ENQ | RING_F_SC_DEQ);
270 }
271
272 static int
273 ccp_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
274                  const struct rte_cryptodev_qp_conf *qp_conf,
275                  int socket_id, struct rte_mempool *session_pool)
276 {
277         struct ccp_private *internals = dev->data->dev_private;
278         struct ccp_qp *qp;
279         int retval = 0;
280
281         if (qp_id >= internals->max_nb_qpairs) {
282                 CCP_LOG_ERR("Invalid qp_id %u, should be less than %u",
283                             qp_id, internals->max_nb_qpairs);
284                 return (-EINVAL);
285         }
286
287         /* Free memory prior to re-allocation if needed. */
288         if (dev->data->queue_pairs[qp_id] != NULL)
289                 ccp_pmd_qp_release(dev, qp_id);
290
291         /* Allocate the queue pair data structure. */
292         qp = rte_zmalloc_socket("CCP Crypto PMD Queue Pair", sizeof(*qp),
293                                         RTE_CACHE_LINE_SIZE, socket_id);
294         if (qp == NULL) {
295                 CCP_LOG_ERR("Failed to allocate queue pair memory");
296                 return (-ENOMEM);
297         }
298
299         qp->dev = dev;
300         qp->id = qp_id;
301         dev->data->queue_pairs[qp_id] = qp;
302
303         retval = ccp_pmd_qp_set_unique_name(dev, qp);
304         if (retval) {
305                 CCP_LOG_ERR("Failed to create unique name for ccp qp");
306                 goto qp_setup_cleanup;
307         }
308
309         qp->processed_pkts = ccp_pmd_qp_create_batch_info_ring(qp,
310                         qp_conf->nb_descriptors, socket_id);
311         if (qp->processed_pkts == NULL) {
312                 CCP_LOG_ERR("Failed to create batch info ring");
313                 goto qp_setup_cleanup;
314         }
315
316         qp->sess_mp = session_pool;
317
318         /* mempool for batch info */
319         qp->batch_mp = rte_mempool_create(
320                                 qp->name,
321                                 qp_conf->nb_descriptors,
322                                 sizeof(struct ccp_batch_info),
323                                 RTE_CACHE_LINE_SIZE,
324                                 0, NULL, NULL, NULL, NULL,
325                                 SOCKET_ID_ANY, 0);
326         if (qp->batch_mp == NULL)
327                 goto qp_setup_cleanup;
328         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
329         return 0;
330
331 qp_setup_cleanup:
332         dev->data->queue_pairs[qp_id] = NULL;
333         if (qp)
334                 rte_free(qp);
335         return -1;
336 }
337
338 static int
339 ccp_pmd_qp_start(struct rte_cryptodev *dev __rte_unused,
340                  uint16_t queue_pair_id __rte_unused)
341 {
342         return -ENOTSUP;
343 }
344
345 static int
346 ccp_pmd_qp_stop(struct rte_cryptodev *dev __rte_unused,
347                 uint16_t queue_pair_id __rte_unused)
348 {
349         return -ENOTSUP;
350 }
351
352 static uint32_t
353 ccp_pmd_qp_count(struct rte_cryptodev *dev)
354 {
355         return dev->data->nb_queue_pairs;
356 }
357
358 static unsigned
359 ccp_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
360 {
361         return sizeof(struct ccp_session);
362 }
363
364 static int
365 ccp_pmd_session_configure(struct rte_cryptodev *dev,
366                           struct rte_crypto_sym_xform *xform,
367                           struct rte_cryptodev_sym_session *sess,
368                           struct rte_mempool *mempool)
369 {
370         int ret;
371         void *sess_private_data;
372
373         if (unlikely(sess == NULL || xform == NULL)) {
374                 CCP_LOG_ERR("Invalid session struct or xform");
375                 return -ENOMEM;
376         }
377
378         if (rte_mempool_get(mempool, &sess_private_data)) {
379                 CCP_LOG_ERR("Couldn't get object from session mempool");
380                 return -ENOMEM;
381         }
382         ret = ccp_set_session_parameters(sess_private_data, xform);
383         if (ret != 0) {
384                 CCP_LOG_ERR("failed configure session parameters");
385
386                 /* Return session to mempool */
387                 rte_mempool_put(mempool, sess_private_data);
388                 return ret;
389         }
390         set_session_private_data(sess, dev->driver_id,
391                                  sess_private_data);
392
393         return 0;
394 }
395
396 static void
397 ccp_pmd_session_clear(struct rte_cryptodev *dev,
398                       struct rte_cryptodev_sym_session *sess)
399 {
400         uint8_t index = dev->driver_id;
401         void *sess_priv = get_session_private_data(sess, index);
402
403         if (sess_priv) {
404                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
405
406                 rte_mempool_put(sess_mp, sess_priv);
407                 memset(sess_priv, 0, sizeof(struct ccp_session));
408                 set_session_private_data(sess, index, NULL);
409         }
410 }
411
412 struct rte_cryptodev_ops ccp_ops = {
413                 .dev_configure          = ccp_pmd_config,
414                 .dev_start              = ccp_pmd_start,
415                 .dev_stop               = ccp_pmd_stop,
416                 .dev_close              = ccp_pmd_close,
417
418                 .stats_get              = ccp_pmd_stats_get,
419                 .stats_reset            = ccp_pmd_stats_reset,
420
421                 .dev_infos_get          = ccp_pmd_info_get,
422
423                 .queue_pair_setup       = ccp_pmd_qp_setup,
424                 .queue_pair_release     = ccp_pmd_qp_release,
425                 .queue_pair_start       = ccp_pmd_qp_start,
426                 .queue_pair_stop        = ccp_pmd_qp_stop,
427                 .queue_pair_count       = ccp_pmd_qp_count,
428
429                 .session_get_size       = ccp_pmd_session_get_size,
430                 .session_configure      = ccp_pmd_session_configure,
431                 .session_clear          = ccp_pmd_session_clear,
432 };
433
434 struct rte_cryptodev_ops *ccp_pmd_ops = &ccp_ops;