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