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