New upstream version 17.11-rc3
[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 8
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 with same cipher key. */
200 static uint8_t
201 process_zuc_cipher_op(struct rte_crypto_op **ops,
202                 struct zuc_session *session,
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
212         for (i = 0; i < num_ops; i++) {
213                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
214                                 || ((ops[i]->sym->cipher.data.offset
215                                         % BYTE_LEN) != 0)) {
216                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
217                         ZUC_LOG_ERR("Data Length or offset");
218                         break;
219                 }
220
221 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
222                 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
223                                 (ops[i]->sym->m_dst != NULL &&
224                                 !rte_pktmbuf_is_contiguous(
225                                                 ops[i]->sym->m_dst))) {
226                         ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
227                                 "op (%p) provides noncontiguous mbuf as "
228                                 "source/destination buffer.\n", ops[i]);
229                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
230                         break;
231                 }
232 #endif
233
234                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
235                                 (ops[i]->sym->cipher.data.offset >> 3);
236                 dst[i] = ops[i]->sym->m_dst ?
237                         rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
238                                 (ops[i]->sym->cipher.data.offset >> 3) :
239                         rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
240                                 (ops[i]->sym->cipher.data.offset >> 3);
241                 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
242                                 session->cipher_iv_offset);
243                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
244
245                 cipher_keys[i] = session->pKey_cipher;
246
247                 processed_ops++;
248         }
249
250         sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
251                         num_bytes, processed_ops);
252
253         return processed_ops;
254 }
255
256 /** Generate/verify hash from mbufs with same hash key. */
257 static int
258 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
259                 struct zuc_session *session,
260                 uint8_t num_ops)
261 {
262         unsigned i;
263         uint8_t processed_ops = 0;
264         uint8_t *src;
265         uint32_t *dst;
266         uint32_t length_in_bits;
267         uint8_t *iv;
268
269         for (i = 0; i < num_ops; i++) {
270                 /* Data must be byte aligned */
271                 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
272                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
273                         ZUC_LOG_ERR("Offset");
274                         break;
275                 }
276
277                 length_in_bits = ops[i]->sym->auth.data.length;
278
279                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
280                                 (ops[i]->sym->auth.data.offset >> 3);
281                 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
282                                 session->auth_iv_offset);
283
284                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
285                         dst = (uint32_t *)qp->temp_digest;
286
287                         sso_zuc_eia3_1_buffer(session->pKey_hash,
288                                         iv, src,
289                                         length_in_bits, dst);
290                         /* Verify digest. */
291                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
292                                         ZUC_DIGEST_LENGTH) != 0)
293                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
294                 } else  {
295                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
296
297                         sso_zuc_eia3_1_buffer(session->pKey_hash,
298                                         iv, src,
299                                         length_in_bits, dst);
300                 }
301                 processed_ops++;
302         }
303
304         return processed_ops;
305 }
306
307 /** Process a batch of crypto ops which shares the same session. */
308 static int
309 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
310                 struct zuc_qp *qp, uint8_t num_ops,
311                 uint16_t *accumulated_enqueued_ops)
312 {
313         unsigned i;
314         unsigned enqueued_ops, processed_ops;
315
316         switch (session->op) {
317         case ZUC_OP_ONLY_CIPHER:
318                 processed_ops = process_zuc_cipher_op(ops,
319                                 session, num_ops);
320                 break;
321         case ZUC_OP_ONLY_AUTH:
322                 processed_ops = process_zuc_hash_op(qp, ops, session,
323                                 num_ops);
324                 break;
325         case ZUC_OP_CIPHER_AUTH:
326                 processed_ops = process_zuc_cipher_op(ops, session,
327                                 num_ops);
328                 process_zuc_hash_op(qp, ops, session, processed_ops);
329                 break;
330         case ZUC_OP_AUTH_CIPHER:
331                 processed_ops = process_zuc_hash_op(qp, ops, session,
332                                 num_ops);
333                 process_zuc_cipher_op(ops, session, processed_ops);
334                 break;
335         default:
336                 /* Operation not supported. */
337                 processed_ops = 0;
338         }
339
340         for (i = 0; i < num_ops; i++) {
341                 /*
342                  * If there was no error/authentication failure,
343                  * change status to successful.
344                  */
345                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
346                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
347                 /* Free session if a session-less crypto op. */
348                 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
349                         memset(session, 0, sizeof(struct zuc_session));
350                         memset(ops[i]->sym->session, 0,
351                                         rte_cryptodev_get_header_session_size());
352                         rte_mempool_put(qp->sess_mp, session);
353                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
354                         ops[i]->sym->session = NULL;
355                 }
356         }
357
358         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
359                         (void **)ops, processed_ops, NULL);
360         qp->qp_stats.enqueued_count += enqueued_ops;
361         *accumulated_enqueued_ops += enqueued_ops;
362
363         return enqueued_ops;
364 }
365
366 static uint16_t
367 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
368                 uint16_t nb_ops)
369 {
370         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
371         struct rte_crypto_op *curr_c_op;
372
373         struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
374         struct zuc_qp *qp = queue_pair;
375         unsigned i;
376         uint8_t burst_size = 0;
377         uint16_t enqueued_ops = 0;
378         uint8_t processed_ops;
379
380         for (i = 0; i < nb_ops; i++) {
381                 curr_c_op = ops[i];
382
383                 /* Set status as enqueued (not processed yet) by default. */
384                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
385
386                 curr_sess = zuc_get_session(qp, curr_c_op);
387                 if (unlikely(curr_sess == NULL ||
388                                 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
389                         curr_c_op->status =
390                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
391                         break;
392                 }
393
394                 /* Batch ops that share the same session. */
395                 if (prev_sess == NULL) {
396                         prev_sess = curr_sess;
397                         c_ops[burst_size++] = curr_c_op;
398                 } else if (curr_sess == prev_sess) {
399                         c_ops[burst_size++] = curr_c_op;
400                         /*
401                          * When there are enough ops to process in a batch,
402                          * process them, and start a new batch.
403                          */
404                         if (burst_size == ZUC_MAX_BURST) {
405                                 processed_ops = process_ops(c_ops, prev_sess,
406                                                 qp, burst_size, &enqueued_ops);
407                                 if (processed_ops < burst_size) {
408                                         burst_size = 0;
409                                         break;
410                                 }
411
412                                 burst_size = 0;
413                                 prev_sess = NULL;
414                         }
415                 } else {
416                         /*
417                          * Different session, process the ops
418                          * of the previous session.
419                          */
420                         processed_ops = process_ops(c_ops, prev_sess,
421                                         qp, burst_size, &enqueued_ops);
422                         if (processed_ops < burst_size) {
423                                 burst_size = 0;
424                                 break;
425                         }
426
427                         burst_size = 0;
428                         prev_sess = curr_sess;
429
430                         c_ops[burst_size++] = curr_c_op;
431                 }
432         }
433
434         if (burst_size != 0) {
435                 /* Process the crypto ops of the last session. */
436                 processed_ops = process_ops(c_ops, prev_sess,
437                                 qp, burst_size, &enqueued_ops);
438         }
439
440         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
441         return enqueued_ops;
442 }
443
444 static uint16_t
445 zuc_pmd_dequeue_burst(void *queue_pair,
446                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
447 {
448         struct zuc_qp *qp = queue_pair;
449
450         unsigned nb_dequeued;
451
452         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
453                         (void **)c_ops, nb_ops, NULL);
454         qp->qp_stats.dequeued_count += nb_dequeued;
455
456         return nb_dequeued;
457 }
458
459 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
460
461 static int
462 cryptodev_zuc_create(const char *name,
463                 struct rte_vdev_device *vdev,
464                 struct rte_cryptodev_pmd_init_params *init_params)
465 {
466         struct rte_cryptodev *dev;
467         struct zuc_private *internals;
468         uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
469
470
471         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
472         if (dev == NULL) {
473                 ZUC_LOG_ERR("failed to create cryptodev vdev");
474                 goto init_error;
475         }
476
477         dev->driver_id = cryptodev_driver_id;
478         dev->dev_ops = rte_zuc_pmd_ops;
479
480         /* Register RX/TX burst functions for data path. */
481         dev->dequeue_burst = zuc_pmd_dequeue_burst;
482         dev->enqueue_burst = zuc_pmd_enqueue_burst;
483
484         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
485                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
486                         cpu_flags;
487
488         internals = dev->data->dev_private;
489
490         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
491         internals->max_nb_sessions = init_params->max_nb_sessions;
492
493         return 0;
494 init_error:
495         ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
496                         init_params->name);
497
498         cryptodev_zuc_remove(vdev);
499         return -EFAULT;
500 }
501
502 static int
503 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
504 {
505         struct rte_cryptodev_pmd_init_params init_params = {
506                 "",
507                 sizeof(struct zuc_private),
508                 rte_socket_id(),
509                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
510                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
511         };
512         const char *name;
513         const char *input_args;
514
515         name = rte_vdev_device_name(vdev);
516         if (name == NULL)
517                 return -EINVAL;
518         input_args = rte_vdev_device_args(vdev);
519
520         rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
521
522         return cryptodev_zuc_create(name, vdev, &init_params);
523 }
524
525 static int
526 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
527 {
528
529         struct rte_cryptodev *cryptodev;
530         const char *name;
531
532         name = rte_vdev_device_name(vdev);
533         if (name == NULL)
534                 return -EINVAL;
535
536         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
537         if (cryptodev == NULL)
538                 return -ENODEV;
539
540         return rte_cryptodev_pmd_destroy(cryptodev);
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 static struct cryptodev_driver zuc_crypto_drv;
549
550 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
551 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
552         "max_nb_queue_pairs=<int> "
553         "max_nb_sessions=<int> "
554         "socket_id=<int>");
555 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
556                 cryptodev_driver_id);