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