New upstream version 17.11.3
[deb_dpdk.git] / drivers / crypto / zuc / rte_zuc_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 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_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_bus_vdev.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
40
41 #include "rte_zuc_pmd_private.h"
42
43 #define ZUC_MAX_BURST 4
44 #define BYTE_LEN 8
45
46 static uint8_t cryptodev_driver_id;
47
48 /** Get xform chain order. */
49 static enum zuc_operation
50 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
51 {
52         if (xform == NULL)
53                 return ZUC_OP_NOT_SUPPORTED;
54
55         if (xform->next)
56                 if (xform->next->next != NULL)
57                         return ZUC_OP_NOT_SUPPORTED;
58
59         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
60                 if (xform->next == NULL)
61                         return ZUC_OP_ONLY_AUTH;
62                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
63                         return ZUC_OP_AUTH_CIPHER;
64                 else
65                         return ZUC_OP_NOT_SUPPORTED;
66         }
67
68         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
69                 if (xform->next == NULL)
70                         return ZUC_OP_ONLY_CIPHER;
71                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
72                         return ZUC_OP_CIPHER_AUTH;
73                 else
74                         return ZUC_OP_NOT_SUPPORTED;
75         }
76
77         return ZUC_OP_NOT_SUPPORTED;
78 }
79
80
81 /** Parse crypto xform chain and set private session parameters. */
82 int
83 zuc_set_session_parameters(struct zuc_session *sess,
84                 const struct rte_crypto_sym_xform *xform)
85 {
86         const struct rte_crypto_sym_xform *auth_xform = NULL;
87         const struct rte_crypto_sym_xform *cipher_xform = NULL;
88         enum zuc_operation mode;
89
90         /* Select Crypto operation - hash then cipher / cipher then hash */
91         mode = zuc_get_mode(xform);
92
93         switch (mode) {
94         case ZUC_OP_CIPHER_AUTH:
95                 auth_xform = xform->next;
96
97                 /* Fall-through */
98         case ZUC_OP_ONLY_CIPHER:
99                 cipher_xform = xform;
100                 break;
101         case ZUC_OP_AUTH_CIPHER:
102                 cipher_xform = xform->next;
103                 /* Fall-through */
104         case ZUC_OP_ONLY_AUTH:
105                 auth_xform = xform;
106                 break;
107         case ZUC_OP_NOT_SUPPORTED:
108         default:
109                 ZUC_LOG_ERR("Unsupported operation chain order parameter");
110                 return -ENOTSUP;
111         }
112
113         if (cipher_xform) {
114                 /* Only ZUC EEA3 supported */
115                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
116                         return -ENOTSUP;
117
118                 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
119                         ZUC_LOG_ERR("Wrong IV length");
120                         return -EINVAL;
121                 }
122                 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
123
124                 /* Copy the key */
125                 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
126                                 ZUC_IV_KEY_LENGTH);
127         }
128
129         if (auth_xform) {
130                 /* Only ZUC EIA3 supported */
131                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
132                         return -ENOTSUP;
133
134                 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
135                         ZUC_LOG_ERR("Wrong digest length");
136                         return -EINVAL;
137                 }
138
139                 sess->auth_op = auth_xform->auth.op;
140
141                 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
142                         ZUC_LOG_ERR("Wrong IV length");
143                         return -EINVAL;
144                 }
145                 sess->auth_iv_offset = auth_xform->auth.iv.offset;
146
147                 /* Copy the key */
148                 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
149                                 ZUC_IV_KEY_LENGTH);
150         }
151
152
153         sess->op = mode;
154
155         return 0;
156 }
157
158 /** Get ZUC session. */
159 static struct zuc_session *
160 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
161 {
162         struct zuc_session *sess = NULL;
163
164         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
165                 if (likely(op->sym->session != NULL))
166                         sess = (struct zuc_session *)get_session_private_data(
167                                         op->sym->session,
168                                         cryptodev_driver_id);
169         } else {
170                 void *_sess = NULL;
171                 void *_sess_private_data = NULL;
172
173                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
174                         return NULL;
175
176                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
177                         return NULL;
178
179                 sess = (struct zuc_session *)_sess_private_data;
180
181                 if (unlikely(zuc_set_session_parameters(sess,
182                                 op->sym->xform) != 0)) {
183                         rte_mempool_put(qp->sess_mp, _sess);
184                         rte_mempool_put(qp->sess_mp, _sess_private_data);
185                         sess = NULL;
186                 }
187                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
188                 set_session_private_data(op->sym->session, cryptodev_driver_id,
189                         _sess_private_data);
190         }
191
192         if (unlikely(sess == NULL))
193                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
194
195
196         return sess;
197 }
198
199 /** Encrypt/decrypt mbufs. */
200 static uint8_t
201 process_zuc_cipher_op(struct rte_crypto_op **ops,
202                 struct zuc_session **sessions,
203                 uint8_t num_ops)
204 {
205         unsigned i;
206         uint8_t processed_ops = 0;
207         uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
208         uint8_t *iv[ZUC_MAX_BURST];
209         uint32_t num_bytes[ZUC_MAX_BURST];
210         uint8_t *cipher_keys[ZUC_MAX_BURST];
211         struct zuc_session *sess;
212
213         for (i = 0; i < num_ops; i++) {
214                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
215                                 || ((ops[i]->sym->cipher.data.offset
216                                         % BYTE_LEN) != 0)) {
217                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
218                         ZUC_LOG_ERR("Data Length or offset");
219                         break;
220                 }
221
222                 sess = sessions[i];
223
224 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
225                 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
226                                 (ops[i]->sym->m_dst != NULL &&
227                                 !rte_pktmbuf_is_contiguous(
228                                                 ops[i]->sym->m_dst))) {
229                         ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
230                                 "op (%p) provides noncontiguous mbuf as "
231                                 "source/destination buffer.\n", ops[i]);
232                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
233                         break;
234                 }
235 #endif
236
237                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
238                                 (ops[i]->sym->cipher.data.offset >> 3);
239                 dst[i] = ops[i]->sym->m_dst ?
240                         rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
241                                 (ops[i]->sym->cipher.data.offset >> 3) :
242                         rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
243                                 (ops[i]->sym->cipher.data.offset >> 3);
244                 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
245                                 sess->cipher_iv_offset);
246                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
247
248                 cipher_keys[i] = sess->pKey_cipher;
249
250                 processed_ops++;
251         }
252
253         sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
254                         num_bytes, processed_ops);
255
256         return processed_ops;
257 }
258
259 /** Generate/verify hash from mbufs. */
260 static int
261 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
262                 struct zuc_session **sessions,
263                 uint8_t num_ops)
264 {
265         unsigned i;
266         uint8_t processed_ops = 0;
267         uint8_t *src;
268         uint32_t *dst;
269         uint32_t length_in_bits;
270         uint8_t *iv;
271         struct zuc_session *sess;
272
273         for (i = 0; i < num_ops; i++) {
274                 /* Data must be byte aligned */
275                 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
276                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
277                         ZUC_LOG_ERR("Offset");
278                         break;
279                 }
280
281                 sess = sessions[i];
282
283                 length_in_bits = ops[i]->sym->auth.data.length;
284
285                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
286                                 (ops[i]->sym->auth.data.offset >> 3);
287                 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
288                                 sess->auth_iv_offset);
289
290                 if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
291                         dst = (uint32_t *)qp->temp_digest;
292
293                         sso_zuc_eia3_1_buffer(sess->pKey_hash,
294                                         iv, src,
295                                         length_in_bits, dst);
296                         /* Verify digest. */
297                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
298                                         ZUC_DIGEST_LENGTH) != 0)
299                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
300                 } else  {
301                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
302
303                         sso_zuc_eia3_1_buffer(sess->pKey_hash,
304                                         iv, src,
305                                         length_in_bits, dst);
306                 }
307                 processed_ops++;
308         }
309
310         return processed_ops;
311 }
312
313 /** Process a batch of crypto ops which shares the same operation type. */
314 static int
315 process_ops(struct rte_crypto_op **ops, enum zuc_operation op_type,
316                 struct zuc_session **sessions,
317                 struct zuc_qp *qp, uint8_t num_ops,
318                 uint16_t *accumulated_enqueued_ops)
319 {
320         unsigned i;
321         unsigned enqueued_ops, processed_ops;
322
323         switch (op_type) {
324         case ZUC_OP_ONLY_CIPHER:
325                 processed_ops = process_zuc_cipher_op(ops,
326                                 sessions, num_ops);
327                 break;
328         case ZUC_OP_ONLY_AUTH:
329                 processed_ops = process_zuc_hash_op(qp, ops, sessions,
330                                 num_ops);
331                 break;
332         case ZUC_OP_CIPHER_AUTH:
333                 processed_ops = process_zuc_cipher_op(ops, sessions,
334                                 num_ops);
335                 process_zuc_hash_op(qp, ops, sessions, processed_ops);
336                 break;
337         case ZUC_OP_AUTH_CIPHER:
338                 processed_ops = process_zuc_hash_op(qp, ops, sessions,
339                                 num_ops);
340                 process_zuc_cipher_op(ops, sessions, processed_ops);
341                 break;
342         default:
343                 /* Operation not supported. */
344                 processed_ops = 0;
345         }
346
347         for (i = 0; i < num_ops; i++) {
348                 /*
349                  * If there was no error/authentication failure,
350                  * change status to successful.
351                  */
352                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
353                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
354                 /* Free session if a session-less crypto op. */
355                 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
356                         memset(sessions[i], 0, sizeof(struct zuc_session));
357                         memset(ops[i]->sym->session, 0,
358                                         rte_cryptodev_get_header_session_size());
359                         rte_mempool_put(qp->sess_mp, sessions[i]);
360                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
361                         ops[i]->sym->session = NULL;
362                 }
363         }
364
365         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
366                         (void **)ops, processed_ops, NULL);
367         qp->qp_stats.enqueued_count += enqueued_ops;
368         *accumulated_enqueued_ops += enqueued_ops;
369
370         return enqueued_ops;
371 }
372
373 static uint16_t
374 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
375                 uint16_t nb_ops)
376 {
377         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
378         struct rte_crypto_op *curr_c_op;
379
380         struct zuc_session *curr_sess;
381         struct zuc_session *sessions[ZUC_MAX_BURST];
382         enum zuc_operation prev_zuc_op = ZUC_OP_NOT_SUPPORTED;
383         enum zuc_operation curr_zuc_op;
384         struct zuc_qp *qp = queue_pair;
385         unsigned i;
386         uint8_t burst_size = 0;
387         uint16_t enqueued_ops = 0;
388         uint8_t processed_ops;
389
390         for (i = 0; i < nb_ops; i++) {
391                 curr_c_op = ops[i];
392
393                 curr_sess = zuc_get_session(qp, curr_c_op);
394                 if (unlikely(curr_sess == NULL ||
395                                 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
396                         curr_c_op->status =
397                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
398                         break;
399                 }
400
401                 curr_zuc_op = curr_sess->op;
402
403                 /*
404                  * Batch ops that share the same operation type
405                  * (cipher only, auth only...).
406                  */
407                 if (burst_size == 0) {
408                         prev_zuc_op = curr_zuc_op;
409                         c_ops[0] = curr_c_op;
410                         sessions[0] = curr_sess;
411                         burst_size++;
412                 } else if (curr_zuc_op == prev_zuc_op) {
413                         c_ops[burst_size] = curr_c_op;
414                         sessions[burst_size] = curr_sess;
415                         burst_size++;
416                         /*
417                          * When there are enough ops to process in a batch,
418                          * process them, and start a new batch.
419                          */
420                         if (burst_size == ZUC_MAX_BURST) {
421                                 processed_ops = process_ops(c_ops, curr_zuc_op,
422                                                 sessions, qp, burst_size,
423                                                 &enqueued_ops);
424                                 if (processed_ops < burst_size) {
425                                         burst_size = 0;
426                                         break;
427                                 }
428
429                                 burst_size = 0;
430                         }
431                 } else {
432                         /*
433                          * Different operation type, process the ops
434                          * of the previous type.
435                          */
436                         processed_ops = process_ops(c_ops, prev_zuc_op,
437                                         sessions, qp, burst_size,
438                                         &enqueued_ops);
439                         if (processed_ops < burst_size) {
440                                 burst_size = 0;
441                                 break;
442                         }
443
444                         burst_size = 0;
445                         prev_zuc_op = curr_zuc_op;
446
447                         c_ops[0] = curr_c_op;
448                         sessions[0] = curr_sess;
449                         burst_size++;
450                 }
451         }
452
453         if (burst_size != 0) {
454                 /* Process the crypto ops of the last operation type. */
455                 processed_ops = process_ops(c_ops, prev_zuc_op,
456                                 sessions, qp, burst_size,
457                                 &enqueued_ops);
458         }
459
460         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
461         return enqueued_ops;
462 }
463
464 static uint16_t
465 zuc_pmd_dequeue_burst(void *queue_pair,
466                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
467 {
468         struct zuc_qp *qp = queue_pair;
469
470         unsigned nb_dequeued;
471
472         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
473                         (void **)c_ops, nb_ops, NULL);
474         qp->qp_stats.dequeued_count += nb_dequeued;
475
476         return nb_dequeued;
477 }
478
479 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
480
481 static int
482 cryptodev_zuc_create(const char *name,
483                 struct rte_vdev_device *vdev,
484                 struct rte_cryptodev_pmd_init_params *init_params)
485 {
486         struct rte_cryptodev *dev;
487         struct zuc_private *internals;
488         uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
489
490
491         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
492         if (dev == NULL) {
493                 ZUC_LOG_ERR("failed to create cryptodev vdev");
494                 goto init_error;
495         }
496
497         dev->driver_id = cryptodev_driver_id;
498         dev->dev_ops = rte_zuc_pmd_ops;
499
500         /* Register RX/TX burst functions for data path. */
501         dev->dequeue_burst = zuc_pmd_dequeue_burst;
502         dev->enqueue_burst = zuc_pmd_enqueue_burst;
503
504         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
505                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
506                         cpu_flags;
507
508         internals = dev->data->dev_private;
509
510         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
511         internals->max_nb_sessions = init_params->max_nb_sessions;
512
513         return 0;
514 init_error:
515         ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
516                         init_params->name);
517
518         cryptodev_zuc_remove(vdev);
519         return -EFAULT;
520 }
521
522 static int
523 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
524 {
525         struct rte_cryptodev_pmd_init_params init_params = {
526                 "",
527                 sizeof(struct zuc_private),
528                 rte_socket_id(),
529                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
530                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
531         };
532         const char *name;
533         const char *input_args;
534
535         name = rte_vdev_device_name(vdev);
536         if (name == NULL)
537                 return -EINVAL;
538         input_args = rte_vdev_device_args(vdev);
539
540         rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
541
542         return cryptodev_zuc_create(name, vdev, &init_params);
543 }
544
545 static int
546 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
547 {
548
549         struct rte_cryptodev *cryptodev;
550         const char *name;
551
552         name = rte_vdev_device_name(vdev);
553         if (name == NULL)
554                 return -EINVAL;
555
556         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
557         if (cryptodev == NULL)
558                 return -ENODEV;
559
560         return rte_cryptodev_pmd_destroy(cryptodev);
561 }
562
563 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
564         .probe = cryptodev_zuc_probe,
565         .remove = cryptodev_zuc_remove
566 };
567
568 static struct cryptodev_driver zuc_crypto_drv;
569
570 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
571 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
572         "max_nb_queue_pairs=<int> "
573         "max_nb_sessions=<int> "
574         "socket_id=<int>");
575 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
576                 cryptodev_driver_id);