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