Imported Upstream version 16.04
[deb_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 #define BYTE_LEN 8
47
48 /**
49  * Global static parameter used to create a unique name for each SNOW 3G
50  * crypto device.
51  */
52 static unsigned unique_name_id;
53
54 static inline int
55 create_unique_device_name(char *name, size_t size)
56 {
57         int ret;
58
59         if (name == NULL)
60                 return -EINVAL;
61
62         ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
63                         unique_name_id++);
64         if (ret < 0)
65                 return ret;
66         return 0;
67 }
68
69 /** Get xform chain order. */
70 static enum snow3g_operation
71 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
72 {
73         if (xform == NULL)
74                 return SNOW3G_OP_NOT_SUPPORTED;
75
76         if (xform->next)
77                 if (xform->next->next != NULL)
78                         return SNOW3G_OP_NOT_SUPPORTED;
79
80         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
81                 if (xform->next == NULL)
82                         return SNOW3G_OP_ONLY_AUTH;
83                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
84                         return SNOW3G_OP_AUTH_CIPHER;
85                 else
86                         return SNOW3G_OP_NOT_SUPPORTED;
87         }
88
89         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
90                 if (xform->next == NULL)
91                         return SNOW3G_OP_ONLY_CIPHER;
92                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
93                         return SNOW3G_OP_CIPHER_AUTH;
94                 else
95                         return SNOW3G_OP_NOT_SUPPORTED;
96         }
97
98         return SNOW3G_OP_NOT_SUPPORTED;
99 }
100
101
102 /** Parse crypto xform chain and set private session parameters. */
103 int
104 snow3g_set_session_parameters(struct snow3g_session *sess,
105                 const struct rte_crypto_sym_xform *xform)
106 {
107         const struct rte_crypto_sym_xform *auth_xform = NULL;
108         const struct rte_crypto_sym_xform *cipher_xform = NULL;
109         int mode;
110
111         /* Select Crypto operation - hash then cipher / cipher then hash */
112         mode = snow3g_get_mode(xform);
113
114         switch (mode) {
115         case SNOW3G_OP_CIPHER_AUTH:
116                 auth_xform = xform->next;
117
118                 /* Fall-through */
119         case SNOW3G_OP_ONLY_CIPHER:
120                 cipher_xform = xform;
121                 break;
122         case SNOW3G_OP_AUTH_CIPHER:
123                 cipher_xform = xform->next;
124                 /* Fall-through */
125         case SNOW3G_OP_ONLY_AUTH:
126                 auth_xform = xform;
127         }
128
129         if (mode == SNOW3G_OP_NOT_SUPPORTED) {
130                 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
131                 return -EINVAL;
132         }
133
134         if (cipher_xform) {
135                 /* Only SNOW 3G UEA2 supported */
136                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
137                         return -EINVAL;
138                 /* Initialize key */
139                 sso_snow3g_init_key_sched(xform->cipher.key.data,
140                                 &sess->pKeySched_cipher);
141         }
142
143         if (auth_xform) {
144                 /* Only SNOW 3G UIA2 supported */
145                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
146                         return -EINVAL;
147                 sess->auth_op = auth_xform->auth.op;
148                 /* Initialize key */
149                 sso_snow3g_init_key_sched(xform->auth.key.data,
150                                 &sess->pKeySched_hash);
151         }
152
153
154         sess->op = mode;
155
156         return 0;
157 }
158
159 /** Get SNOW 3G session. */
160 static struct snow3g_session *
161 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
162 {
163         struct snow3g_session *sess;
164
165         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
166                 if (unlikely(op->sym->session->dev_type !=
167                                 RTE_CRYPTODEV_SNOW3G_PMD))
168                         return NULL;
169
170                 sess = (struct snow3g_session *)op->sym->session->_private;
171         } else  {
172                 struct rte_cryptodev_session *c_sess = NULL;
173
174                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
175                         return NULL;
176
177                 sess = (struct snow3g_session *)c_sess->_private;
178
179                 if (unlikely(snow3g_set_session_parameters(sess,
180                                 op->sym->xform) != 0))
181                         return NULL;
182         }
183
184         return sess;
185 }
186
187 /** Encrypt/decrypt mbufs with same cipher key. */
188 static uint8_t
189 process_snow3g_cipher_op(struct rte_crypto_op **ops,
190                 struct snow3g_session *session,
191                 uint8_t num_ops)
192 {
193         unsigned i;
194         uint8_t processed_ops = 0;
195         uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
196         uint8_t *IV[SNOW3G_MAX_BURST];
197         uint32_t num_bytes[SNOW3G_MAX_BURST];
198
199         for (i = 0; i < num_ops; i++) {
200                 /* Sanity checks. */
201                 if (ops[i]->sym->cipher.iv.length != 16) {
202                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
203                         SNOW3G_LOG_ERR("iv");
204                         break;
205                 }
206
207                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
208                                 || ((ops[i]->sym->cipher.data.offset
209                                         % BYTE_LEN) != 0)) {
210                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
211                         SNOW3G_LOG_ERR("Data Length or offset");
212                         break;
213                 }
214
215                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
216                                 (ops[i]->sym->cipher.data.offset >> 3);
217                 dst[i] = ops[i]->sym->m_dst ?
218                         rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
219                                 (ops[i]->sym->cipher.data.offset >> 3) :
220                         rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
221                                 (ops[i]->sym->cipher.data.offset >> 3);
222                 IV[i] = ops[i]->sym->cipher.iv.data;
223                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
224
225                 processed_ops++;
226         }
227
228         sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
229                         num_bytes, processed_ops);
230
231         return processed_ops;
232 }
233
234 /** Generate/verify hash from mbufs with same hash key. */
235 static int
236 process_snow3g_hash_op(struct rte_crypto_op **ops,
237                 struct snow3g_session *session,
238                 uint8_t num_ops)
239 {
240         unsigned i;
241         uint8_t processed_ops = 0;
242         uint8_t *src, *dst;
243         uint32_t length_in_bits;
244
245         for (i = 0; i < num_ops; i++) {
246                 if (ops[i]->sym->auth.aad.length != 16) {
247                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248                         SNOW3G_LOG_ERR("aad");
249                         break;
250                 }
251
252                 if (ops[i]->sym->auth.digest.length != 4) {
253                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
254                         SNOW3G_LOG_ERR("digest");
255                         break;
256                 }
257
258                 if (((ops[i]->sym->auth.data.length % BYTE_LEN) != 0)
259                                 || ((ops[i]->sym->auth.data.offset
260                                         % BYTE_LEN) != 0)) {
261                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
262                         SNOW3G_LOG_ERR("Data Length or offset");
263                         break;
264                 }
265
266                 length_in_bits = ops[i]->sym->auth.data.length;
267
268                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
269                                 (ops[i]->sym->auth.data.offset >> 3);
270
271                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
272                         dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
273                                         ops[i]->sym->auth.digest.length);
274
275                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
276                                         ops[i]->sym->auth.aad.data, src,
277                                         length_in_bits, dst);
278                         /* Verify digest. */
279                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
280                                         ops[i]->sym->auth.digest.length) != 0)
281                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
282
283                         /* Trim area used for digest from mbuf. */
284                         rte_pktmbuf_trim(ops[i]->sym->m_src,
285                                         ops[i]->sym->auth.digest.length);
286                 } else  {
287                         dst = ops[i]->sym->auth.digest.data;
288
289                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
290                                         ops[i]->sym->auth.aad.data, src,
291                                         length_in_bits, dst);
292                 }
293                 processed_ops++;
294         }
295
296         return processed_ops;
297 }
298
299 /** Process a batch of crypto ops which shares the same session. */
300 static int
301 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
302                 struct snow3g_qp *qp, uint8_t num_ops)
303 {
304         unsigned i;
305         unsigned processed_ops;
306
307         switch (session->op) {
308         case SNOW3G_OP_ONLY_CIPHER:
309                 processed_ops = process_snow3g_cipher_op(ops,
310                                 session, num_ops);
311                 break;
312         case SNOW3G_OP_ONLY_AUTH:
313                 processed_ops = process_snow3g_hash_op(ops, session,
314                                 num_ops);
315                 break;
316         case SNOW3G_OP_CIPHER_AUTH:
317                 processed_ops = process_snow3g_cipher_op(ops, session,
318                                 num_ops);
319                 process_snow3g_hash_op(ops, session, processed_ops);
320                 break;
321         case SNOW3G_OP_AUTH_CIPHER:
322                 processed_ops = process_snow3g_hash_op(ops, session,
323                                 num_ops);
324                 process_snow3g_cipher_op(ops, session, processed_ops);
325                 break;
326         default:
327                 /* Operation not supported. */
328                 processed_ops = 0;
329         }
330
331         for (i = 0; i < num_ops; i++) {
332                 /*
333                  * If there was no error/authentication failure,
334                  * change status to successful.
335                  */
336                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
337                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
338                 /* Free session if a session-less crypto op. */
339                 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
340                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
341                         ops[i]->sym->session = NULL;
342                 }
343         }
344
345         return processed_ops;
346 }
347
348 static uint16_t
349 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
350                 uint16_t nb_ops)
351 {
352         struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
353         struct rte_crypto_op *curr_c_op;
354
355         struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
356         struct snow3g_qp *qp = queue_pair;
357         unsigned i, n;
358         uint8_t burst_size = 0;
359         uint16_t enqueued_ops = 0;
360         uint8_t processed_ops;
361
362         for (i = 0; i < nb_ops; i++) {
363                 curr_c_op = ops[i];
364
365                 /* Set status as enqueued (not processed yet) by default. */
366                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
367
368                 curr_sess = snow3g_get_session(qp, curr_c_op);
369                 if (unlikely(curr_sess == NULL ||
370                                 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
371                         curr_c_op->status =
372                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
373                         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
374                         return enqueued_ops;
375                 }
376
377                 /* Batch ops that share the same session. */
378                 if (prev_sess == NULL) {
379                         prev_sess = curr_sess;
380                         c_ops[burst_size++] = curr_c_op;
381                 } else if (curr_sess == prev_sess) {
382                         c_ops[burst_size++] = curr_c_op;
383                         /*
384                          * When there are enough ops to process in a batch,
385                          * process them, and start a new batch.
386                          */
387                         if (burst_size == SNOW3G_MAX_BURST) {
388                                 processed_ops = process_ops(c_ops,
389                                                 prev_sess, qp, burst_size);
390                                 n = rte_ring_enqueue_burst(qp->processed_ops,
391                                                 (void **)c_ops,
392                                                 processed_ops);
393                                 qp->qp_stats.enqueued_count += n;
394                                 enqueued_ops += n;
395                                 if (n < burst_size) {
396                                         qp->qp_stats.enqueue_err_count +=
397                                                         nb_ops - enqueued_ops;
398                                         return enqueued_ops;
399                                 }
400                                 burst_size = 0;
401
402                                 prev_sess = NULL;
403                         }
404                 } else {
405                         /*
406                          * Different session, process the ops
407                          * of the previous session.
408                          */
409                         processed_ops = process_ops(c_ops,
410                                         prev_sess, qp, burst_size);
411                         n = rte_ring_enqueue_burst(qp->processed_ops,
412                                         (void **)c_ops,
413                                         processed_ops);
414                         qp->qp_stats.enqueued_count += n;
415                         enqueued_ops += n;
416                         if (n < burst_size) {
417                                 qp->qp_stats.enqueue_err_count +=
418                                                 nb_ops - enqueued_ops;
419                                 return enqueued_ops;
420                         }
421                         burst_size = 0;
422
423                         prev_sess = curr_sess;
424                         c_ops[burst_size++] = curr_c_op;
425                 }
426         }
427
428         if (burst_size != 0) {
429                 /* Process the crypto ops of the last session. */
430                 processed_ops = process_ops(c_ops,
431                                 prev_sess, qp, burst_size);
432                 n = rte_ring_enqueue_burst(qp->processed_ops,
433                                 (void **)c_ops,
434                                 processed_ops);
435                 qp->qp_stats.enqueued_count += n;
436                 enqueued_ops += n;
437                 if (n < burst_size) {
438                         qp->qp_stats.enqueue_err_count +=
439                                         nb_ops - enqueued_ops;
440                         return enqueued_ops;
441                 }
442         }
443
444         return enqueued_ops;
445 }
446
447 static uint16_t
448 snow3g_pmd_dequeue_burst(void *queue_pair,
449                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
450 {
451         struct snow3g_qp *qp = queue_pair;
452
453         unsigned nb_dequeued;
454
455         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
456                         (void **)c_ops, nb_ops);
457         qp->qp_stats.dequeued_count += nb_dequeued;
458
459         return nb_dequeued;
460 }
461
462 static int cryptodev_snow3g_uninit(const char *name);
463
464 static int
465 cryptodev_snow3g_create(const char *name,
466                 struct rte_crypto_vdev_init_params *init_params)
467 {
468         struct rte_cryptodev *dev;
469         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
470         struct snow3g_private *internals;
471
472         /* Create a unique device name. */
473         if (create_unique_device_name(crypto_dev_name,
474                         RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
475                 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
476                 return -EINVAL;
477         }
478
479         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
480                         sizeof(struct snow3g_private), init_params->socket_id);
481         if (dev == NULL) {
482                 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
483                 goto init_error;
484         }
485
486         dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
487         dev->dev_ops = rte_snow3g_pmd_ops;
488
489         /* Register RX/TX burst functions for data path. */
490         dev->dequeue_burst = snow3g_pmd_dequeue_burst;
491         dev->enqueue_burst = snow3g_pmd_enqueue_burst;
492
493         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
494                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
495
496         internals = dev->data->dev_private;
497
498         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
499         internals->max_nb_sessions = init_params->max_nb_sessions;
500
501         return 0;
502 init_error:
503         SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
504
505         cryptodev_snow3g_uninit(crypto_dev_name);
506         return -EFAULT;
507 }
508
509 static int
510 cryptodev_snow3g_init(const char *name,
511                 const char *input_args)
512 {
513         struct rte_crypto_vdev_init_params init_params = {
514                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
515                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
516                 rte_socket_id()
517         };
518
519         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
520
521         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
522                         init_params.socket_id);
523         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
524                         init_params.max_nb_queue_pairs);
525         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
526                         init_params.max_nb_sessions);
527
528         return cryptodev_snow3g_create(name, &init_params);
529 }
530
531 static int
532 cryptodev_snow3g_uninit(const char *name)
533 {
534         if (name == NULL)
535                 return -EINVAL;
536
537         RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
538                         " on numa socket %u\n",
539                         name, rte_socket_id());
540
541         return 0;
542 }
543
544 static struct rte_driver cryptodev_snow3g_pmd_drv = {
545         .name = CRYPTODEV_NAME_SNOW3G_PMD,
546         .type = PMD_VDEV,
547         .init = cryptodev_snow3g_init,
548         .uninit = cryptodev_snow3g_uninit
549 };
550
551 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);