7057fcaca1469d05133c099bbf0981897afa5127
[deb_dpdk.git] / drivers / crypto / zuc / rte_zuc_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_vdev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
41
42 #include "rte_zuc_pmd_private.h"
43
44 #define ZUC_DIGEST_LENGTH 4
45 #define ZUC_MAX_BURST 8
46 #define BYTE_LEN 8
47
48 /**
49  * Global static parameter used to create a unique name for each ZUC
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", RTE_STR(CRYPTODEV_NAME_ZUC_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 zuc_operation
71 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
72 {
73         if (xform == NULL)
74                 return ZUC_OP_NOT_SUPPORTED;
75
76         if (xform->next)
77                 if (xform->next->next != NULL)
78                         return ZUC_OP_NOT_SUPPORTED;
79
80         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
81                 if (xform->next == NULL)
82                         return ZUC_OP_ONLY_AUTH;
83                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
84                         return ZUC_OP_AUTH_CIPHER;
85                 else
86                         return ZUC_OP_NOT_SUPPORTED;
87         }
88
89         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
90                 if (xform->next == NULL)
91                         return ZUC_OP_ONLY_CIPHER;
92                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
93                         return ZUC_OP_CIPHER_AUTH;
94                 else
95                         return ZUC_OP_NOT_SUPPORTED;
96         }
97
98         return ZUC_OP_NOT_SUPPORTED;
99 }
100
101
102 /** Parse crypto xform chain and set private session parameters. */
103 int
104 zuc_set_session_parameters(struct zuc_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         enum zuc_operation mode;
110
111         /* Select Crypto operation - hash then cipher / cipher then hash */
112         mode = zuc_get_mode(xform);
113
114         switch (mode) {
115         case ZUC_OP_CIPHER_AUTH:
116                 auth_xform = xform->next;
117
118                 /* Fall-through */
119         case ZUC_OP_ONLY_CIPHER:
120                 cipher_xform = xform;
121                 break;
122         case ZUC_OP_AUTH_CIPHER:
123                 cipher_xform = xform->next;
124                 /* Fall-through */
125         case ZUC_OP_ONLY_AUTH:
126                 auth_xform = xform;
127                 break;
128         case ZUC_OP_NOT_SUPPORTED:
129         default:
130                 ZUC_LOG_ERR("Unsupported operation chain order parameter");
131                 return -EINVAL;
132         }
133
134         if (cipher_xform) {
135                 /* Only ZUC EEA3 supported */
136                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
137                         return -EINVAL;
138                 /* Copy the key */
139                 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
140                                 ZUC_IV_KEY_LENGTH);
141         }
142
143         if (auth_xform) {
144                 /* Only ZUC EIA3 supported */
145                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
146                         return -EINVAL;
147                 sess->auth_op = auth_xform->auth.op;
148                 /* Copy the key */
149                 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
150                                 ZUC_IV_KEY_LENGTH);
151         }
152
153
154         sess->op = mode;
155
156         return 0;
157 }
158
159 /** Get ZUC session. */
160 static struct zuc_session *
161 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
162 {
163         struct zuc_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_ZUC_PMD))
168                         return NULL;
169
170                 sess = (struct zuc_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 zuc_session *)c_sess->_private;
178
179                 if (unlikely(zuc_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_zuc_cipher_op(struct rte_crypto_op **ops,
190                 struct zuc_session *session,
191                 uint8_t num_ops)
192 {
193         unsigned i;
194         uint8_t processed_ops = 0;
195         uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
196         uint8_t *IV[ZUC_MAX_BURST];
197         uint32_t num_bytes[ZUC_MAX_BURST];
198         uint8_t *cipher_keys[ZUC_MAX_BURST];
199
200         for (i = 0; i < num_ops; i++) {
201                 /* Sanity checks. */
202                 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
203                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
204                         ZUC_LOG_ERR("iv");
205                         break;
206                 }
207
208                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
209                                 || ((ops[i]->sym->cipher.data.offset
210                                         % BYTE_LEN) != 0)) {
211                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
212                         ZUC_LOG_ERR("Data Length or offset");
213                         break;
214                 }
215
216                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
217                                 (ops[i]->sym->cipher.data.offset >> 3);
218                 dst[i] = ops[i]->sym->m_dst ?
219                         rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
220                                 (ops[i]->sym->cipher.data.offset >> 3) :
221                         rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
222                                 (ops[i]->sym->cipher.data.offset >> 3);
223                 IV[i] = ops[i]->sym->cipher.iv.data;
224                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
225
226                 cipher_keys[i] = session->pKey_cipher;
227
228                 processed_ops++;
229         }
230
231         sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
232                         num_bytes, processed_ops);
233
234         return processed_ops;
235 }
236
237 /** Generate/verify hash from mbufs with same hash key. */
238 static int
239 process_zuc_hash_op(struct rte_crypto_op **ops,
240                 struct zuc_session *session,
241                 uint8_t num_ops)
242 {
243         unsigned i;
244         uint8_t processed_ops = 0;
245         uint8_t *src;
246         uint32_t *dst;
247         uint32_t length_in_bits;
248
249         for (i = 0; i < num_ops; i++) {
250                 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
251                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
252                         ZUC_LOG_ERR("aad");
253                         break;
254                 }
255
256                 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
257                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
258                         ZUC_LOG_ERR("digest");
259                         break;
260                 }
261
262                 /* Data must be byte aligned */
263                 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
264                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
265                         ZUC_LOG_ERR("Offset");
266                         break;
267                 }
268
269                 length_in_bits = ops[i]->sym->auth.data.length;
270
271                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
272                                 (ops[i]->sym->auth.data.offset >> 3);
273
274                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
275                         dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
276                                         ops[i]->sym->auth.digest.length);
277
278                         sso_zuc_eia3_1_buffer(session->pKey_hash,
279                                         ops[i]->sym->auth.aad.data, src,
280                                         length_in_bits, dst);
281                         /* Verify digest. */
282                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
283                                         ops[i]->sym->auth.digest.length) != 0)
284                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
285
286                         /* Trim area used for digest from mbuf. */
287                         rte_pktmbuf_trim(ops[i]->sym->m_src,
288                                         ops[i]->sym->auth.digest.length);
289                 } else  {
290                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
291
292                         sso_zuc_eia3_1_buffer(session->pKey_hash,
293                                         ops[i]->sym->auth.aad.data, src,
294                                         length_in_bits, dst);
295                 }
296                 processed_ops++;
297         }
298
299         return processed_ops;
300 }
301
302 /** Process a batch of crypto ops which shares the same session. */
303 static int
304 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
305                 struct zuc_qp *qp, uint8_t num_ops,
306                 uint16_t *accumulated_enqueued_ops)
307 {
308         unsigned i;
309         unsigned enqueued_ops, processed_ops;
310
311         switch (session->op) {
312         case ZUC_OP_ONLY_CIPHER:
313                 processed_ops = process_zuc_cipher_op(ops,
314                                 session, num_ops);
315                 break;
316         case ZUC_OP_ONLY_AUTH:
317                 processed_ops = process_zuc_hash_op(ops, session,
318                                 num_ops);
319                 break;
320         case ZUC_OP_CIPHER_AUTH:
321                 processed_ops = process_zuc_cipher_op(ops, session,
322                                 num_ops);
323                 process_zuc_hash_op(ops, session, processed_ops);
324                 break;
325         case ZUC_OP_AUTH_CIPHER:
326                 processed_ops = process_zuc_hash_op(ops, session,
327                                 num_ops);
328                 process_zuc_cipher_op(ops, session, processed_ops);
329                 break;
330         default:
331                 /* Operation not supported. */
332                 processed_ops = 0;
333         }
334
335         for (i = 0; i < num_ops; i++) {
336                 /*
337                  * If there was no error/authentication failure,
338                  * change status to successful.
339                  */
340                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
341                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
342                 /* Free session if a session-less crypto op. */
343                 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
344                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
345                         ops[i]->sym->session = NULL;
346                 }
347         }
348
349         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
350                         (void **)ops, processed_ops);
351         qp->qp_stats.enqueued_count += enqueued_ops;
352         *accumulated_enqueued_ops += enqueued_ops;
353
354         return enqueued_ops;
355 }
356
357 static uint16_t
358 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
359                 uint16_t nb_ops)
360 {
361         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
362         struct rte_crypto_op *curr_c_op;
363
364         struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
365         struct zuc_qp *qp = queue_pair;
366         unsigned i;
367         uint8_t burst_size = 0;
368         uint16_t enqueued_ops = 0;
369         uint8_t processed_ops;
370
371         for (i = 0; i < nb_ops; i++) {
372                 curr_c_op = ops[i];
373
374                 /* Set status as enqueued (not processed yet) by default. */
375                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
376
377                 curr_sess = zuc_get_session(qp, curr_c_op);
378                 if (unlikely(curr_sess == NULL ||
379                                 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
380                         curr_c_op->status =
381                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
382                         break;
383                 }
384
385                 /* Batch ops that share the same session. */
386                 if (prev_sess == NULL) {
387                         prev_sess = curr_sess;
388                         c_ops[burst_size++] = curr_c_op;
389                 } else if (curr_sess == prev_sess) {
390                         c_ops[burst_size++] = curr_c_op;
391                         /*
392                          * When there are enough ops to process in a batch,
393                          * process them, and start a new batch.
394                          */
395                         if (burst_size == ZUC_MAX_BURST) {
396                                 processed_ops = process_ops(c_ops, prev_sess,
397                                                 qp, burst_size, &enqueued_ops);
398                                 if (processed_ops < burst_size) {
399                                         burst_size = 0;
400                                         break;
401                                 }
402
403                                 burst_size = 0;
404                                 prev_sess = NULL;
405                         }
406                 } else {
407                         /*
408                          * Different session, process the ops
409                          * of the previous session.
410                          */
411                         processed_ops = process_ops(c_ops, prev_sess,
412                                         qp, burst_size, &enqueued_ops);
413                         if (processed_ops < burst_size) {
414                                 burst_size = 0;
415                                 break;
416                         }
417
418                         burst_size = 0;
419                         prev_sess = curr_sess;
420
421                         c_ops[burst_size++] = curr_c_op;
422                 }
423         }
424
425         if (burst_size != 0) {
426                 /* Process the crypto ops of the last session. */
427                 processed_ops = process_ops(c_ops, prev_sess,
428                                 qp, burst_size, &enqueued_ops);
429         }
430
431         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
432         return enqueued_ops;
433 }
434
435 static uint16_t
436 zuc_pmd_dequeue_burst(void *queue_pair,
437                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
438 {
439         struct zuc_qp *qp = queue_pair;
440
441         unsigned nb_dequeued;
442
443         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
444                         (void **)c_ops, nb_ops);
445         qp->qp_stats.dequeued_count += nb_dequeued;
446
447         return nb_dequeued;
448 }
449
450 static int cryptodev_zuc_remove(const char *name);
451
452 static int
453 cryptodev_zuc_create(const char *name,
454                 struct rte_crypto_vdev_init_params *init_params)
455 {
456         struct rte_cryptodev *dev;
457         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
458         struct zuc_private *internals;
459         uint64_t cpu_flags = 0;
460
461         /* Check CPU for supported vector instruction set */
462         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
463                 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
464         else {
465                 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
466                 return -EFAULT;
467         }
468
469
470         /* Create a unique device name. */
471         if (create_unique_device_name(crypto_dev_name,
472                         RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
473                 ZUC_LOG_ERR("failed to create unique cryptodev name");
474                 return -EINVAL;
475         }
476
477         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
478                         sizeof(struct zuc_private), init_params->socket_id);
479         if (dev == NULL) {
480                 ZUC_LOG_ERR("failed to create cryptodev vdev");
481                 goto init_error;
482         }
483
484         dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
485         dev->dev_ops = rte_zuc_pmd_ops;
486
487         /* Register RX/TX burst functions for data path. */
488         dev->dequeue_burst = zuc_pmd_dequeue_burst;
489         dev->enqueue_burst = zuc_pmd_enqueue_burst;
490
491         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
492                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
493                         cpu_flags;
494
495         internals = dev->data->dev_private;
496
497         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
498         internals->max_nb_sessions = init_params->max_nb_sessions;
499
500         return 0;
501 init_error:
502         ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed", name);
503
504         cryptodev_zuc_remove(crypto_dev_name);
505         return -EFAULT;
506 }
507
508 static int
509 cryptodev_zuc_probe(const char *name,
510                 const char *input_args)
511 {
512         struct rte_crypto_vdev_init_params init_params = {
513                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
514                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
515                 rte_socket_id()
516         };
517
518         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
519
520         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
521                         init_params.socket_id);
522         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
523                         init_params.max_nb_queue_pairs);
524         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
525                         init_params.max_nb_sessions);
526
527         return cryptodev_zuc_create(name, &init_params);
528 }
529
530 static int
531 cryptodev_zuc_remove(const char *name)
532 {
533         if (name == NULL)
534                 return -EINVAL;
535
536         RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
537                         " on numa socket %u\n",
538                         name, rte_socket_id());
539
540         return 0;
541 }
542
543 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
544         .probe = cryptodev_zuc_probe,
545         .remove = cryptodev_zuc_remove
546 };
547
548 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
549 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
550         "max_nb_queue_pairs=<int> "
551         "max_nb_sessions=<int> "
552         "socket_id=<int>");