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