c5fd3edaf5340d11712f99b29e0bf4acfb544729
[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         {       /* SHA1 */
17                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
18                 {.sym = {
19                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
20                         {.auth = {
21                                  .algo = RTE_CRYPTO_AUTH_SHA1,
22                                  .block_size = 64,
23                                  .key_size = {
24                                          .min = 0,
25                                          .max = 0,
26                                          .increment = 0
27                                  },
28                                  .digest_size = {
29                                          .min = 20,
30                                          .max = 20,
31                                          .increment = 0
32                                  },
33                                  .aad_size = { 0 }
34                          }, }
35                 }, }
36         },
37         {       /* SHA1 HMAC */
38                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
39                 {.sym = {
40                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
41                         {.auth = {
42                                  .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
43                                  .block_size = 64,
44                                  .key_size = {
45                                          .min = 1,
46                                          .max = 64,
47                                          .increment = 1
48                                  },
49                                  .digest_size = {
50                                          .min = 20,
51                                          .max = 20,
52                                          .increment = 0
53                                  },
54                                  .aad_size = { 0 }
55                          }, }
56                 }, }
57         },
58         {       /* SHA224 */
59                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
60                 {.sym = {
61                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
62                         {.auth = {
63                                  .algo = RTE_CRYPTO_AUTH_SHA224,
64                                  .block_size = 64,
65                                  .key_size = {
66                                          .min = 0,
67                                          .max = 0,
68                                          .increment = 0
69                                  },
70                                  .digest_size = {
71                                          .min = 28,
72                                          .max = 28,
73                                          .increment = 0
74                                  },
75                                  .aad_size = { 0 }
76                          }, }
77                 }, }
78         },
79         {       /* SHA224 HMAC */
80                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
81                 {.sym = {
82                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
83                         {.auth = {
84                                  .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
85                                  .block_size = 64,
86                                  .key_size = {
87                                          .min = 1,
88                                          .max = 64,
89                                          .increment = 1
90                                  },
91                                  .digest_size = {
92                                          .min = 28,
93                                          .max = 28,
94                                          .increment = 0
95                                  },
96                                  .aad_size = { 0 }
97                          }, }
98                 }, }
99         },
100         {       /* SHA256 */
101                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
102                 {.sym = {
103                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
104                         {.auth = {
105                                  .algo = RTE_CRYPTO_AUTH_SHA256,
106                                  .block_size = 64,
107                                  .key_size = {
108                                          .min = 0,
109                                          .max = 0,
110                                          .increment = 0
111                                  },
112                                  .digest_size = {
113                                          .min = 32,
114                                          .max = 32,
115                                          .increment = 0
116                                  },
117                                  .aad_size = { 0 }
118                          }, }
119                 }, }
120         },
121         {       /* SHA256 HMAC */
122                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
123                 {.sym = {
124                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
125                         {.auth = {
126                                  .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
127                                  .block_size = 64,
128                                  .key_size = {
129                                          .min = 1,
130                                          .max = 64,
131                                          .increment = 1
132                                  },
133                                  .digest_size = {
134                                          .min = 32,
135                                          .max = 32,
136                                          .increment = 0
137                                  },
138                                  .aad_size = { 0 }
139                          }, }
140                 }, }
141         },
142         {       /* SHA384 */
143                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
144                 {.sym = {
145                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
146                         {.auth = {
147                                  .algo = RTE_CRYPTO_AUTH_SHA384,
148                                  .block_size = 128,
149                                  .key_size = {
150                                          .min = 0,
151                                          .max = 0,
152                                          .increment = 0
153                                  },
154                                  .digest_size = {
155                                          .min = 48,
156                                          .max = 48,
157                                          .increment = 0
158                                  },
159                                  .aad_size = { 0 }
160                          }, }
161                 }, }
162         },
163         {       /* SHA384 HMAC */
164                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
165                 {.sym = {
166                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
167                         {.auth = {
168                                  .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
169                                  .block_size = 128,
170                                  .key_size = {
171                                          .min = 1,
172                                          .max = 128,
173                                          .increment = 1
174                                  },
175                                  .digest_size = {
176                                          .min = 48,
177                                          .max = 48,
178                                          .increment = 0
179                                  },
180                                  .aad_size = { 0 }
181                          }, }
182                 }, }
183         },
184         {       /* SHA512  */
185                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
186                 {.sym = {
187                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
188                         {.auth = {
189                                  .algo = RTE_CRYPTO_AUTH_SHA512,
190                                  .block_size = 128,
191                                  .key_size = {
192                                          .min = 0,
193                                          .max = 0,
194                                          .increment = 0
195                                  },
196                                  .digest_size = {
197                                          .min = 64,
198                                          .max = 64,
199                                          .increment = 0
200                                  },
201                                  .aad_size = { 0 }
202                          }, }
203                 }, }
204         },
205         {       /* SHA512 HMAC */
206                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
207                 {.sym = {
208                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
209                         {.auth = {
210                                  .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
211                                  .block_size = 128,
212                                  .key_size = {
213                                          .min = 1,
214                                          .max = 128,
215                                          .increment = 1
216                                  },
217                                  .digest_size = {
218                                          .min = 64,
219                                          .max = 64,
220                                          .increment = 0
221                                  },
222                                  .aad_size = { 0 }
223                          }, }
224                 }, }
225         },
226         {       /*AES-CMAC */
227                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
228                 {.sym = {
229                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
230                         {.auth = {
231                                  .algo = RTE_CRYPTO_AUTH_AES_CMAC,
232                                  .block_size = 16,
233                                  .key_size = {
234                                          .min = 16,
235                                          .max = 32,
236                                          .increment = 8
237                                  },
238                                  .digest_size = {
239                                          .min = 16,
240                                          .max = 16,
241                                          .increment = 0
242                                  },
243                         }, }
244                 }, }
245         },
246         {       /* AES ECB */
247                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
248                 {.sym = {
249                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
250                         {.cipher = {
251                                 .algo = RTE_CRYPTO_CIPHER_AES_ECB,
252                                 .block_size = 16,
253                                 .key_size = {
254                                    .min = 16,
255                                    .max = 32,
256                                    .increment = 8
257                                 },
258                                 .iv_size = {
259                                    .min = 0,
260                                    .max = 0,
261                                    .increment = 0
262                                 }
263                         }, }
264                 }, }
265         },
266         {       /* AES CBC */
267                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
268                 {.sym = {
269                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
270                         {.cipher = {
271                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
272                                 .block_size = 16,
273                                 .key_size = {
274                                         .min = 16,
275                                         .max = 32,
276                                         .increment = 8
277                                 },
278                                 .iv_size = {
279                                         .min = 16,
280                                         .max = 16,
281                                         .increment = 0
282                                 }
283                         }, }
284                 }, }
285         },
286         {       /* AES CTR */
287                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
288                 {.sym = {
289                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
290                         {.cipher = {
291                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
292                                 .block_size = 16,
293                                 .key_size = {
294                                         .min = 16,
295                                         .max = 32,
296                                         .increment = 8
297                                 },
298                                 .iv_size = {
299                                         .min = 16,
300                                         .max = 16,
301                                         .increment = 0
302                                 }
303                         }, }
304                 }, }
305         },
306         {       /* 3DES CBC */
307                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
308                 {.sym = {
309                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
310                         {.cipher = {
311                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
312                                 .block_size = 8,
313                                 .key_size = {
314                                         .min = 16,
315                                         .max = 24,
316                                         .increment = 8
317                                 },
318                                 .iv_size = {
319                                         .min = 8,
320                                         .max = 8,
321                                         .increment = 0
322                                 }
323                         }, }
324                 }, }
325         },
326         {       /* AES GCM */
327                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
328                 {.sym = {
329                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
330                         {.aead = {
331                                  .algo = RTE_CRYPTO_AEAD_AES_GCM,
332                                  .block_size = 16,
333                                  .key_size = {
334                                          .min = 16,
335                                          .max = 32,
336                                          .increment = 8
337                                  },
338                                  .digest_size = {
339                                          .min = 16,
340                                          .max = 16,
341                                          .increment = 0
342                                  },
343                                  .aad_size = {
344                                          .min = 0,
345                                          .max = 65535,
346                                          .increment = 1
347                                  },
348                                  .iv_size = {
349                                          .min = 12,
350                                          .max = 16,
351                                          .increment = 4
352                                  },
353                         }, }
354                 }, }
355         },
356         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
357 };
358
359 static int
360 ccp_pmd_config(struct rte_cryptodev *dev __rte_unused,
361                struct rte_cryptodev_config *config __rte_unused)
362 {
363         return 0;
364 }
365
366 static int
367 ccp_pmd_start(struct rte_cryptodev *dev)
368 {
369         return ccp_dev_start(dev);
370 }
371
372 static void
373 ccp_pmd_stop(struct rte_cryptodev *dev __rte_unused)
374 {
375
376 }
377
378 static int
379 ccp_pmd_close(struct rte_cryptodev *dev __rte_unused)
380 {
381         return 0;
382 }
383
384 static void
385 ccp_pmd_stats_get(struct rte_cryptodev *dev,
386                   struct rte_cryptodev_stats *stats)
387 {
388         int qp_id;
389
390         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
391                 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
392
393                 stats->enqueued_count += qp->qp_stats.enqueued_count;
394                 stats->dequeued_count += qp->qp_stats.dequeued_count;
395
396                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
397                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
398         }
399
400 }
401
402 static void
403 ccp_pmd_stats_reset(struct rte_cryptodev *dev)
404 {
405         int qp_id;
406
407         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
408                 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
409
410                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
411         }
412 }
413
414 static void
415 ccp_pmd_info_get(struct rte_cryptodev *dev,
416                  struct rte_cryptodev_info *dev_info)
417 {
418         struct ccp_private *internals = dev->data->dev_private;
419
420         if (dev_info != NULL) {
421                 dev_info->driver_id = dev->driver_id;
422                 dev_info->feature_flags = dev->feature_flags;
423                 dev_info->capabilities = ccp_pmd_capabilities;
424                 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
425                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
426         }
427 }
428
429 static int
430 ccp_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
431 {
432         struct ccp_qp *qp;
433
434         if (dev->data->queue_pairs[qp_id] != NULL) {
435                 qp = (struct ccp_qp *)dev->data->queue_pairs[qp_id];
436                 rte_ring_free(qp->processed_pkts);
437                 rte_mempool_free(qp->batch_mp);
438                 rte_free(qp);
439                 dev->data->queue_pairs[qp_id] = NULL;
440         }
441         return 0;
442 }
443
444 static int
445 ccp_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
446                 struct ccp_qp *qp)
447 {
448         unsigned int n = snprintf(qp->name, sizeof(qp->name),
449                         "ccp_pmd_%u_qp_%u",
450                         dev->data->dev_id, qp->id);
451
452         if (n > sizeof(qp->name))
453                 return -1;
454
455         return 0;
456 }
457
458 static struct rte_ring *
459 ccp_pmd_qp_create_batch_info_ring(struct ccp_qp *qp,
460                                   unsigned int ring_size, int socket_id)
461 {
462         struct rte_ring *r;
463
464         r = rte_ring_lookup(qp->name);
465         if (r) {
466                 if (r->size >= ring_size) {
467                         CCP_LOG_INFO(
468                                 "Reusing ring %s for processed packets",
469                                 qp->name);
470                         return r;
471                 }
472                 CCP_LOG_INFO(
473                         "Unable to reuse ring %s for processed packets",
474                          qp->name);
475                 return NULL;
476         }
477
478         return rte_ring_create(qp->name, ring_size, socket_id,
479                         RING_F_SP_ENQ | RING_F_SC_DEQ);
480 }
481
482 static int
483 ccp_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
484                  const struct rte_cryptodev_qp_conf *qp_conf,
485                  int socket_id, struct rte_mempool *session_pool)
486 {
487         struct ccp_private *internals = dev->data->dev_private;
488         struct ccp_qp *qp;
489         int retval = 0;
490
491         if (qp_id >= internals->max_nb_qpairs) {
492                 CCP_LOG_ERR("Invalid qp_id %u, should be less than %u",
493                             qp_id, internals->max_nb_qpairs);
494                 return (-EINVAL);
495         }
496
497         /* Free memory prior to re-allocation if needed. */
498         if (dev->data->queue_pairs[qp_id] != NULL)
499                 ccp_pmd_qp_release(dev, qp_id);
500
501         /* Allocate the queue pair data structure. */
502         qp = rte_zmalloc_socket("CCP Crypto PMD Queue Pair", sizeof(*qp),
503                                         RTE_CACHE_LINE_SIZE, socket_id);
504         if (qp == NULL) {
505                 CCP_LOG_ERR("Failed to allocate queue pair memory");
506                 return (-ENOMEM);
507         }
508
509         qp->dev = dev;
510         qp->id = qp_id;
511         dev->data->queue_pairs[qp_id] = qp;
512
513         retval = ccp_pmd_qp_set_unique_name(dev, qp);
514         if (retval) {
515                 CCP_LOG_ERR("Failed to create unique name for ccp qp");
516                 goto qp_setup_cleanup;
517         }
518
519         qp->processed_pkts = ccp_pmd_qp_create_batch_info_ring(qp,
520                         qp_conf->nb_descriptors, socket_id);
521         if (qp->processed_pkts == NULL) {
522                 CCP_LOG_ERR("Failed to create batch info ring");
523                 goto qp_setup_cleanup;
524         }
525
526         qp->sess_mp = session_pool;
527
528         /* mempool for batch info */
529         qp->batch_mp = rte_mempool_create(
530                                 qp->name,
531                                 qp_conf->nb_descriptors,
532                                 sizeof(struct ccp_batch_info),
533                                 RTE_CACHE_LINE_SIZE,
534                                 0, NULL, NULL, NULL, NULL,
535                                 SOCKET_ID_ANY, 0);
536         if (qp->batch_mp == NULL)
537                 goto qp_setup_cleanup;
538         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
539         return 0;
540
541 qp_setup_cleanup:
542         dev->data->queue_pairs[qp_id] = NULL;
543         if (qp)
544                 rte_free(qp);
545         return -1;
546 }
547
548 static int
549 ccp_pmd_qp_start(struct rte_cryptodev *dev __rte_unused,
550                  uint16_t queue_pair_id __rte_unused)
551 {
552         return -ENOTSUP;
553 }
554
555 static int
556 ccp_pmd_qp_stop(struct rte_cryptodev *dev __rte_unused,
557                 uint16_t queue_pair_id __rte_unused)
558 {
559         return -ENOTSUP;
560 }
561
562 static uint32_t
563 ccp_pmd_qp_count(struct rte_cryptodev *dev)
564 {
565         return dev->data->nb_queue_pairs;
566 }
567
568 static unsigned
569 ccp_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
570 {
571         return sizeof(struct ccp_session);
572 }
573
574 static int
575 ccp_pmd_session_configure(struct rte_cryptodev *dev,
576                           struct rte_crypto_sym_xform *xform,
577                           struct rte_cryptodev_sym_session *sess,
578                           struct rte_mempool *mempool)
579 {
580         int ret;
581         void *sess_private_data;
582
583         if (unlikely(sess == NULL || xform == NULL)) {
584                 CCP_LOG_ERR("Invalid session struct or xform");
585                 return -ENOMEM;
586         }
587
588         if (rte_mempool_get(mempool, &sess_private_data)) {
589                 CCP_LOG_ERR("Couldn't get object from session mempool");
590                 return -ENOMEM;
591         }
592         ret = ccp_set_session_parameters(sess_private_data, xform);
593         if (ret != 0) {
594                 CCP_LOG_ERR("failed configure session parameters");
595
596                 /* Return session to mempool */
597                 rte_mempool_put(mempool, sess_private_data);
598                 return ret;
599         }
600         set_session_private_data(sess, dev->driver_id,
601                                  sess_private_data);
602
603         return 0;
604 }
605
606 static void
607 ccp_pmd_session_clear(struct rte_cryptodev *dev,
608                       struct rte_cryptodev_sym_session *sess)
609 {
610         uint8_t index = dev->driver_id;
611         void *sess_priv = get_session_private_data(sess, index);
612
613         if (sess_priv) {
614                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
615
616                 rte_mempool_put(sess_mp, sess_priv);
617                 memset(sess_priv, 0, sizeof(struct ccp_session));
618                 set_session_private_data(sess, index, NULL);
619         }
620 }
621
622 struct rte_cryptodev_ops ccp_ops = {
623                 .dev_configure          = ccp_pmd_config,
624                 .dev_start              = ccp_pmd_start,
625                 .dev_stop               = ccp_pmd_stop,
626                 .dev_close              = ccp_pmd_close,
627
628                 .stats_get              = ccp_pmd_stats_get,
629                 .stats_reset            = ccp_pmd_stats_reset,
630
631                 .dev_infos_get          = ccp_pmd_info_get,
632
633                 .queue_pair_setup       = ccp_pmd_qp_setup,
634                 .queue_pair_release     = ccp_pmd_qp_release,
635                 .queue_pair_start       = ccp_pmd_qp_start,
636                 .queue_pair_stop        = ccp_pmd_qp_stop,
637                 .queue_pair_count       = ccp_pmd_qp_count,
638
639                 .session_get_size       = ccp_pmd_session_get_size,
640                 .session_configure      = ccp_pmd_session_configure,
641                 .session_clear          = ccp_pmd_session_clear,
642 };
643
644 struct rte_cryptodev_ops *ccp_pmd_ops = &ccp_ops;