snow3g: add driver for SNOW 3G library
[dpdk.git] / drivers / crypto / snow3g / rte_snow3g_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_hexdump.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
38 #include <rte_dev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
41 #include <rte_kvargs.h>
42
43 #include "rte_snow3g_pmd_private.h"
44
45 #define SNOW3G_MAX_BURST 8
46
47 /**
48  * Global static parameter used to create a unique name for each SNOW 3G
49  * crypto device.
50  */
51 static unsigned unique_name_id;
52
53 static inline int
54 create_unique_device_name(char *name, size_t size)
55 {
56         int ret;
57
58         if (name == NULL)
59                 return -EINVAL;
60
61         ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
62                         unique_name_id++);
63         if (ret < 0)
64                 return ret;
65         return 0;
66 }
67
68 /** Get xform chain order. */
69 static enum snow3g_operation
70 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
71 {
72         if (xform == NULL)
73                 return SNOW3G_OP_NOT_SUPPORTED;
74
75         if (xform->next)
76                 if (xform->next->next != NULL)
77                         return SNOW3G_OP_NOT_SUPPORTED;
78
79         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
80                 if (xform->next == NULL)
81                         return SNOW3G_OP_ONLY_AUTH;
82                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
83                         return SNOW3G_OP_AUTH_CIPHER;
84                 else
85                         return SNOW3G_OP_NOT_SUPPORTED;
86         }
87
88         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
89                 if (xform->next == NULL)
90                         return SNOW3G_OP_ONLY_CIPHER;
91                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
92                         return SNOW3G_OP_CIPHER_AUTH;
93                 else
94                         return SNOW3G_OP_NOT_SUPPORTED;
95         }
96
97         return SNOW3G_OP_NOT_SUPPORTED;
98 }
99
100
101 /** Parse crypto xform chain and set private session parameters. */
102 int
103 snow3g_set_session_parameters(struct snow3g_session *sess,
104                 const struct rte_crypto_sym_xform *xform)
105 {
106         const struct rte_crypto_sym_xform *auth_xform = NULL;
107         const struct rte_crypto_sym_xform *cipher_xform = NULL;
108         int mode;
109
110         /* Select Crypto operation - hash then cipher / cipher then hash */
111         mode = snow3g_get_mode(xform);
112
113         switch (mode) {
114         case SNOW3G_OP_CIPHER_AUTH:
115                 auth_xform = xform->next;
116
117                 /* Fall-through */
118         case SNOW3G_OP_ONLY_CIPHER:
119                 cipher_xform = xform;
120                 break;
121         case SNOW3G_OP_AUTH_CIPHER:
122                 cipher_xform = xform->next;
123                 /* Fall-through */
124         case SNOW3G_OP_ONLY_AUTH:
125                 auth_xform = xform;
126         }
127
128         if (mode == SNOW3G_OP_NOT_SUPPORTED) {
129                 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
130                 return -EINVAL;
131         }
132
133         if (cipher_xform) {
134                 /* Only SNOW 3G UEA2 supported */
135                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
136                         return -EINVAL;
137                 /* Initialize key */
138                 sso_snow3g_init_key_sched(xform->cipher.key.data,
139                                 &sess->pKeySched_cipher);
140         }
141
142         if (auth_xform) {
143                 /* Only SNOW 3G UIA2 supported */
144                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
145                         return -EINVAL;
146                 sess->auth_op = auth_xform->auth.op;
147                 /* Initialize key */
148                 sso_snow3g_init_key_sched(xform->auth.key.data,
149                                 &sess->pKeySched_hash);
150         }
151
152
153         sess->op = mode;
154
155         return 0;
156 }
157
158 /** Get SNOW 3G session. */
159 static struct snow3g_session *
160 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
161 {
162         struct snow3g_session *sess;
163
164         if (op->sym->type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
165                 if (unlikely(op->sym->session->type !=
166                                 RTE_CRYPTODEV_SNOW3G_PMD))
167                         return NULL;
168
169                 sess = (struct snow3g_session *)op->sym->session->_private;
170         } else  {
171                 struct rte_cryptodev_session *c_sess = NULL;
172
173                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
174                         return NULL;
175
176                 sess = (struct snow3g_session *)c_sess->_private;
177
178                 if (unlikely(snow3g_set_session_parameters(sess,
179                                 op->sym->xform) != 0))
180                         return NULL;
181         }
182
183         return sess;
184 }
185
186 /** Encrypt/decrypt mbufs with same cipher key. */
187 static uint8_t
188 process_snow3g_cipher_op(struct rte_crypto_op **ops,
189                 struct snow3g_session *session,
190                 uint8_t num_ops)
191 {
192         unsigned i;
193         uint8_t processed_ops = 0;
194         uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
195         uint8_t *IV[SNOW3G_MAX_BURST];
196         uint32_t num_bytes[SNOW3G_MAX_BURST];
197
198         for (i = 0; i < num_ops; i++) {
199                 /* Sanity checks. */
200                 if (ops[i]->sym->cipher.iv.length != 16) {
201                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
202                         SNOW3G_LOG_ERR("iv");
203                         break;
204                 }
205
206                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
207                                 ops[i]->sym->cipher.data.offset;
208                 dst[i] = ops[i]->sym->m_dst ?
209                                 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
210                                         ops[i]->sym->cipher.data.offset :
211                                 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
212                                         ops[i]->sym->cipher.data.offset;
213                 IV[i] = ops[i]->sym->cipher.iv.data;
214                 num_bytes[i] = ops[i]->sym->cipher.data.length;
215
216                 processed_ops++;
217         }
218
219         sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
220                         num_bytes, processed_ops);
221
222         return processed_ops;
223 }
224
225 /** Generate/verify hash from mbufs with same hash key. */
226 static int
227 process_snow3g_hash_op(struct rte_crypto_op **ops,
228                 struct snow3g_session *session,
229                 uint8_t num_ops)
230 {
231         unsigned i;
232         uint8_t processed_ops = 0;
233         uint8_t *src, *dst;
234         uint32_t length_in_bits;
235
236         for (i = 0; i < num_ops; i++) {
237                 if (ops[i]->sym->auth.aad.length != 16) {
238                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
239                         SNOW3G_LOG_ERR("aad");
240                         break;
241                 }
242
243                 if (ops[i]->sym->auth.digest.length != 4) {
244                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
245                         SNOW3G_LOG_ERR("digest");
246                         break;
247                 }
248
249                 length_in_bits = ops[i]->sym->auth.data.length * 8;
250
251                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
252                                 ops[i]->sym->auth.data.offset;
253
254                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
255                         dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
256                                         ops[i]->sym->auth.digest.length);
257
258                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
259                                         ops[i]->sym->auth.aad.data, src,
260                                         length_in_bits, dst);
261                         /* Verify digest. */
262                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
263                                         ops[i]->sym->auth.digest.length) != 0)
264                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
265
266                         /* Trim area used for digest from mbuf. */
267                         rte_pktmbuf_trim(ops[i]->sym->m_src,
268                                         ops[i]->sym->auth.digest.length);
269                 } else  {
270                         dst = ops[i]->sym->auth.digest.data;
271
272                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
273                                         ops[i]->sym->auth.aad.data, src,
274                                         length_in_bits, dst);
275                 }
276                 processed_ops++;
277         }
278
279         return processed_ops;
280 }
281
282 /** Process a batch of crypto ops which shares the same session. */
283 static int
284 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
285                 struct snow3g_qp *qp, uint8_t num_ops)
286 {
287         unsigned i;
288         unsigned processed_ops;
289
290         switch (session->op) {
291         case SNOW3G_OP_ONLY_CIPHER:
292                 processed_ops = process_snow3g_cipher_op(ops,
293                                 session, num_ops);
294                 break;
295         case SNOW3G_OP_ONLY_AUTH:
296                 processed_ops = process_snow3g_hash_op(ops, session,
297                                 num_ops);
298                 break;
299         case SNOW3G_OP_CIPHER_AUTH:
300                 processed_ops = process_snow3g_cipher_op(ops, session,
301                                 num_ops);
302                 process_snow3g_hash_op(ops, session, processed_ops);
303                 break;
304         case SNOW3G_OP_AUTH_CIPHER:
305                 processed_ops = process_snow3g_hash_op(ops, session,
306                                 num_ops);
307                 process_snow3g_cipher_op(ops, session, processed_ops);
308                 break;
309         default:
310                 /* Operation not supported. */
311                 processed_ops = 0;
312         }
313
314         for (i = 0; i < num_ops; i++) {
315                 /*
316                  * If there was no error/authentication failure,
317                  * change status to successful.
318                  */
319                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
320                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
321                 /* Free session if a session-less crypto op. */
322                 if (ops[i]->sym->type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
323                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
324                         ops[i]->sym->session = NULL;
325                 }
326         }
327
328         return processed_ops;
329 }
330
331 static uint16_t
332 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
333                 uint16_t nb_ops)
334 {
335         struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
336         struct rte_crypto_op *curr_c_op;
337
338         struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
339         struct snow3g_qp *qp = queue_pair;
340         unsigned i, n;
341         uint8_t burst_size = 0;
342         uint16_t enqueued_ops = 0;
343         uint8_t processed_ops;
344
345         for (i = 0; i < nb_ops; i++) {
346                 curr_c_op = ops[i];
347
348                 /* Set status as enqueued (not processed yet) by default. */
349                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
350
351                 curr_sess = snow3g_get_session(qp, curr_c_op);
352                 if (unlikely(curr_sess == NULL ||
353                                 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
354                         curr_c_op->status =
355                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
356                         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
357                         return enqueued_ops;
358                 }
359
360                 /* Batch ops that share the same session. */
361                 if (prev_sess == NULL) {
362                         prev_sess = curr_sess;
363                         c_ops[burst_size++] = curr_c_op;
364                 } else if (curr_sess == prev_sess) {
365                         c_ops[burst_size++] = curr_c_op;
366                         /*
367                          * When there are enough ops to process in a batch,
368                          * process them, and start a new batch.
369                          */
370                         if (burst_size == SNOW3G_MAX_BURST) {
371                                 processed_ops = process_ops(c_ops,
372                                                 prev_sess, qp, burst_size);
373                                 n = rte_ring_enqueue_burst(qp->processed_ops,
374                                                 (void **)c_ops,
375                                                 processed_ops);
376                                 qp->qp_stats.enqueued_count += n;
377                                 enqueued_ops += n;
378                                 if (n < burst_size) {
379                                         qp->qp_stats.enqueue_err_count +=
380                                                         nb_ops - enqueued_ops;
381                                         return enqueued_ops;
382                                 }
383                                 burst_size = 0;
384
385                                 prev_sess = NULL;
386                         }
387                 } else {
388                         /*
389                          * Different session, process the ops
390                          * of the previous session.
391                          */
392                         processed_ops = process_ops(c_ops,
393                                         prev_sess, qp, burst_size);
394                         n = rte_ring_enqueue_burst(qp->processed_ops,
395                                         (void **)c_ops,
396                                         processed_ops);
397                         qp->qp_stats.enqueued_count += n;
398                         enqueued_ops += n;
399                         if (n < burst_size) {
400                                 qp->qp_stats.enqueue_err_count +=
401                                                 nb_ops - enqueued_ops;
402                                 return enqueued_ops;
403                         }
404                         burst_size = 0;
405
406                         prev_sess = curr_sess;
407                         c_ops[burst_size++] = curr_c_op;
408                 }
409         }
410
411         if (burst_size != 0) {
412                 /* Process the crypto ops of the last session. */
413                 processed_ops = process_ops(c_ops,
414                                 prev_sess, qp, burst_size);
415                 n = rte_ring_enqueue_burst(qp->processed_ops,
416                                 (void **)c_ops,
417                                 processed_ops);
418                 qp->qp_stats.enqueued_count += n;
419                 enqueued_ops += n;
420                 if (n < burst_size) {
421                         qp->qp_stats.enqueue_err_count +=
422                                         nb_ops - enqueued_ops;
423                         return enqueued_ops;
424                 }
425         }
426
427         return enqueued_ops;
428 }
429
430 static uint16_t
431 snow3g_pmd_dequeue_burst(void *queue_pair,
432                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
433 {
434         struct snow3g_qp *qp = queue_pair;
435
436         unsigned nb_dequeued;
437
438         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
439                         (void **)c_ops, nb_ops);
440         qp->qp_stats.dequeued_count += nb_dequeued;
441
442         return nb_dequeued;
443 }
444
445 static int cryptodev_snow3g_uninit(const char *name);
446
447 static int
448 cryptodev_snow3g_create(const char *name,
449                 struct rte_crypto_vdev_init_params *init_params)
450 {
451         struct rte_cryptodev *dev;
452         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
453         struct snow3g_private *internals;
454
455         /* Create a unique device name. */
456         if (create_unique_device_name(crypto_dev_name,
457                         RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
458                 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
459                 return -EINVAL;
460         }
461
462         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
463                         sizeof(struct snow3g_private), init_params->socket_id);
464         if (dev == NULL) {
465                 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
466                 goto init_error;
467         }
468
469         dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
470         dev->dev_ops = rte_snow3g_pmd_ops;
471
472         /* Register RX/TX burst functions for data path. */
473         dev->dequeue_burst = snow3g_pmd_dequeue_burst;
474         dev->enqueue_burst = snow3g_pmd_enqueue_burst;
475
476         internals = dev->data->dev_private;
477
478         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
479         internals->max_nb_sessions = init_params->max_nb_sessions;
480
481         return 0;
482 init_error:
483         SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
484
485         cryptodev_snow3g_uninit(crypto_dev_name);
486         return -EFAULT;
487 }
488
489 static int
490 cryptodev_snow3g_init(const char *name,
491                 const char *input_args)
492 {
493         struct rte_crypto_vdev_init_params init_params = {
494                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
495                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
496                 rte_socket_id()
497         };
498
499         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
500
501         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
502                         init_params.socket_id);
503         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
504                         init_params.max_nb_queue_pairs);
505         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
506                         init_params.max_nb_sessions);
507
508         return cryptodev_snow3g_create(name, &init_params);
509 }
510
511 static int
512 cryptodev_snow3g_uninit(const char *name)
513 {
514         if (name == NULL)
515                 return -EINVAL;
516
517         RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
518                         " on numa socket %u\n",
519                         name, rte_socket_id());
520
521         return 0;
522 }
523
524 static struct rte_driver cryptodev_snow3g_pmd_drv = {
525         .name = CRYPTODEV_NAME_SNOW3G_PMD,
526         .type = PMD_VDEV,
527         .init = cryptodev_snow3g_init,
528         .uninit = cryptodev_snow3g_uninit
529 };
530
531 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);