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