crypto/zuc: fix init function names
[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 /**
49  * Global static parameter used to create a unique name for each ZUC
50  * crypto device.
51  */
52 static unsigned unique_name_id;
53
54 static inline int
55 create_unique_device_name(char *name, size_t size)
56 {
57         int ret;
58
59         if (name == NULL)
60                 return -EINVAL;
61
62         ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_ZUC_PMD),
63                         unique_name_id++);
64         if (ret < 0)
65                 return ret;
66         return 0;
67 }
68
69 /** Get xform chain order. */
70 static enum zuc_operation
71 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
72 {
73         if (xform == NULL)
74                 return ZUC_OP_NOT_SUPPORTED;
75
76         if (xform->next)
77                 if (xform->next->next != NULL)
78                         return ZUC_OP_NOT_SUPPORTED;
79
80         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
81                 if (xform->next == NULL)
82                         return ZUC_OP_ONLY_AUTH;
83                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
84                         return ZUC_OP_AUTH_CIPHER;
85                 else
86                         return ZUC_OP_NOT_SUPPORTED;
87         }
88
89         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
90                 if (xform->next == NULL)
91                         return ZUC_OP_ONLY_CIPHER;
92                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
93                         return ZUC_OP_CIPHER_AUTH;
94                 else
95                         return ZUC_OP_NOT_SUPPORTED;
96         }
97
98         return ZUC_OP_NOT_SUPPORTED;
99 }
100
101
102 /** Parse crypto xform chain and set private session parameters. */
103 int
104 zuc_set_session_parameters(struct zuc_session *sess,
105                 const struct rte_crypto_sym_xform *xform)
106 {
107         const struct rte_crypto_sym_xform *auth_xform = NULL;
108         const struct rte_crypto_sym_xform *cipher_xform = NULL;
109         enum zuc_operation mode;
110
111         /* Select Crypto operation - hash then cipher / cipher then hash */
112         mode = zuc_get_mode(xform);
113
114         switch (mode) {
115         case ZUC_OP_CIPHER_AUTH:
116                 auth_xform = xform->next;
117
118                 /* Fall-through */
119         case ZUC_OP_ONLY_CIPHER:
120                 cipher_xform = xform;
121                 break;
122         case ZUC_OP_AUTH_CIPHER:
123                 cipher_xform = xform->next;
124                 /* Fall-through */
125         case ZUC_OP_ONLY_AUTH:
126                 auth_xform = xform;
127                 break;
128         case ZUC_OP_NOT_SUPPORTED:
129         default:
130                 ZUC_LOG_ERR("Unsupported operation chain order parameter");
131                 return -EINVAL;
132         }
133
134         if (cipher_xform) {
135                 /* Only ZUC EEA3 supported */
136                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
137                         return -EINVAL;
138                 /* Copy the key */
139                 memcpy(sess->pKey_cipher, xform->cipher.key.data, ZUC_IV_KEY_LENGTH);
140         }
141
142         if (auth_xform) {
143                 /* Only ZUC EIA3 supported */
144                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
145                         return -EINVAL;
146                 sess->auth_op = auth_xform->auth.op;
147                 /* Copy the key */
148                 memcpy(sess->pKey_hash, xform->auth.key.data, ZUC_IV_KEY_LENGTH);
149         }
150
151
152         sess->op = mode;
153
154         return 0;
155 }
156
157 /** Get ZUC session. */
158 static struct zuc_session *
159 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
160 {
161         struct zuc_session *sess;
162
163         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
164                 if (unlikely(op->sym->session->dev_type !=
165                                 RTE_CRYPTODEV_ZUC_PMD))
166                         return NULL;
167
168                 sess = (struct zuc_session *)op->sym->session->_private;
169         } else  {
170                 struct rte_cryptodev_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                 /* Sanity checks. */
200                 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
201                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
202                         ZUC_LOG_ERR("iv");
203                         break;
204                 }
205
206                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
207                                 || ((ops[i]->sym->cipher.data.offset
208                                         % BYTE_LEN) != 0)) {
209                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
210                         ZUC_LOG_ERR("Data Length or offset");
211                         break;
212                 }
213
214                 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
215                                 (ops[i]->sym->cipher.data.offset >> 3);
216                 dst[i] = ops[i]->sym->m_dst ?
217                         rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
218                                 (ops[i]->sym->cipher.data.offset >> 3) :
219                         rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
220                                 (ops[i]->sym->cipher.data.offset >> 3);
221                 IV[i] = ops[i]->sym->cipher.iv.data;
222                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
223
224                 cipher_keys[i] = session->pKey_cipher;
225
226                 processed_ops++;
227         }
228
229         sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
230                         num_bytes, processed_ops);
231
232         return processed_ops;
233 }
234
235 /** Generate/verify hash from mbufs with same hash key. */
236 static int
237 process_zuc_hash_op(struct rte_crypto_op **ops,
238                 struct zuc_session *session,
239                 uint8_t num_ops)
240 {
241         unsigned i;
242         uint8_t processed_ops = 0;
243         uint8_t *src;
244         uint32_t *dst;
245         uint32_t length_in_bits;
246
247         for (i = 0; i < num_ops; i++) {
248                 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
249                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
250                         ZUC_LOG_ERR("aad");
251                         break;
252                 }
253
254                 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
255                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
256                         ZUC_LOG_ERR("digest");
257                         break;
258                 }
259
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
272                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
273                         dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
274                                         ops[i]->sym->auth.digest.length);
275
276                         sso_zuc_eia3_1_buffer(session->pKey_hash,
277                                         ops[i]->sym->auth.aad.data, src,
278                                         length_in_bits, dst);
279                         /* Verify digest. */
280                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
281                                         ops[i]->sym->auth.digest.length) != 0)
282                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
283
284                         /* Trim area used for digest from mbuf. */
285                         rte_pktmbuf_trim(ops[i]->sym->m_src,
286                                         ops[i]->sym->auth.digest.length);
287                 } else  {
288                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
289
290                         sso_zuc_eia3_1_buffer(session->pKey_hash,
291                                         ops[i]->sym->auth.aad.data, src,
292                                         length_in_bits, dst);
293                 }
294                 processed_ops++;
295         }
296
297         return processed_ops;
298 }
299
300 /** Process a batch of crypto ops which shares the same session. */
301 static int
302 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
303                 struct zuc_qp *qp, uint8_t num_ops,
304                 uint16_t *accumulated_enqueued_ops)
305 {
306         unsigned i;
307         unsigned enqueued_ops, processed_ops;
308
309         switch (session->op) {
310         case ZUC_OP_ONLY_CIPHER:
311                 processed_ops = process_zuc_cipher_op(ops,
312                                 session, num_ops);
313                 break;
314         case ZUC_OP_ONLY_AUTH:
315                 processed_ops = process_zuc_hash_op(ops, session,
316                                 num_ops);
317                 break;
318         case ZUC_OP_CIPHER_AUTH:
319                 processed_ops = process_zuc_cipher_op(ops, session,
320                                 num_ops);
321                 process_zuc_hash_op(ops, session, processed_ops);
322                 break;
323         case ZUC_OP_AUTH_CIPHER:
324                 processed_ops = process_zuc_hash_op(ops, session,
325                                 num_ops);
326                 process_zuc_cipher_op(ops, session, processed_ops);
327                 break;
328         default:
329                 /* Operation not supported. */
330                 processed_ops = 0;
331         }
332
333         for (i = 0; i < num_ops; i++) {
334                 /*
335                  * If there was no error/authentication failure,
336                  * change status to successful.
337                  */
338                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
339                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
340                 /* Free session if a session-less crypto op. */
341                 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
342                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
343                         ops[i]->sym->session = NULL;
344                 }
345         }
346
347         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
348                         (void **)ops, processed_ops);
349         qp->qp_stats.enqueued_count += enqueued_ops;
350         *accumulated_enqueued_ops += enqueued_ops;
351
352         return enqueued_ops;
353 }
354
355 static uint16_t
356 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
357                 uint16_t nb_ops)
358 {
359         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
360         struct rte_crypto_op *curr_c_op;
361
362         struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
363         struct zuc_qp *qp = queue_pair;
364         unsigned i;
365         uint8_t burst_size = 0;
366         uint16_t enqueued_ops = 0;
367         uint8_t processed_ops;
368
369         for (i = 0; i < nb_ops; i++) {
370                 curr_c_op = ops[i];
371
372                 /* Set status as enqueued (not processed yet) by default. */
373                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
374
375                 curr_sess = zuc_get_session(qp, curr_c_op);
376                 if (unlikely(curr_sess == NULL ||
377                                 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
378                         curr_c_op->status =
379                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
380                         break;
381                 }
382
383                 /* Batch ops that share the same session. */
384                 if (prev_sess == NULL) {
385                         prev_sess = curr_sess;
386                         c_ops[burst_size++] = curr_c_op;
387                 } else if (curr_sess == prev_sess) {
388                         c_ops[burst_size++] = curr_c_op;
389                         /*
390                          * When there are enough ops to process in a batch,
391                          * process them, and start a new batch.
392                          */
393                         if (burst_size == ZUC_MAX_BURST) {
394                                 processed_ops = process_ops(c_ops, prev_sess,
395                                                 qp, burst_size, &enqueued_ops);
396                                 if (processed_ops < burst_size) {
397                                         burst_size = 0;
398                                         break;
399                                 }
400
401                                 burst_size = 0;
402                                 prev_sess = NULL;
403                         }
404                 } else {
405                         /*
406                          * Different session, process the ops
407                          * of the previous session.
408                          */
409                         processed_ops = process_ops(c_ops, prev_sess,
410                                         qp, burst_size, &enqueued_ops);
411                         if (processed_ops < burst_size) {
412                                 burst_size = 0;
413                                 break;
414                         }
415
416                         burst_size = 0;
417                         prev_sess = curr_sess;
418
419                         c_ops[burst_size++] = curr_c_op;
420                 }
421         }
422
423         if (burst_size != 0) {
424                 /* Process the crypto ops of the last session. */
425                 processed_ops = process_ops(c_ops, prev_sess,
426                                 qp, burst_size, &enqueued_ops);
427         }
428
429         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
430         return enqueued_ops;
431 }
432
433 static uint16_t
434 zuc_pmd_dequeue_burst(void *queue_pair,
435                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
436 {
437         struct zuc_qp *qp = queue_pair;
438
439         unsigned nb_dequeued;
440
441         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
442                         (void **)c_ops, nb_ops);
443         qp->qp_stats.dequeued_count += nb_dequeued;
444
445         return nb_dequeued;
446 }
447
448 static int cryptodev_zuc_remove(const char *name);
449
450 static int
451 cryptodev_zuc_create(const char *name,
452                 struct rte_crypto_vdev_init_params *init_params)
453 {
454         struct rte_cryptodev *dev;
455         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
456         struct zuc_private *internals;
457         uint64_t cpu_flags = 0;
458
459         /* Check CPU for supported vector instruction set */
460         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
461                 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
462         else {
463                 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
464                 return -EFAULT;
465         }
466
467
468         /* Create a unique device name. */
469         if (create_unique_device_name(crypto_dev_name,
470                         RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
471                 ZUC_LOG_ERR("failed to create unique cryptodev name");
472                 return -EINVAL;
473         }
474
475         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
476                         sizeof(struct zuc_private), init_params->socket_id);
477         if (dev == NULL) {
478                 ZUC_LOG_ERR("failed to create cryptodev vdev");
479                 goto init_error;
480         }
481
482         dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
483         dev->dev_ops = rte_zuc_pmd_ops;
484
485         /* Register RX/TX burst functions for data path. */
486         dev->dequeue_burst = zuc_pmd_dequeue_burst;
487         dev->enqueue_burst = zuc_pmd_enqueue_burst;
488
489         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
490                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
491                         cpu_flags;
492
493         internals = dev->data->dev_private;
494
495         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
496         internals->max_nb_sessions = init_params->max_nb_sessions;
497
498         return 0;
499 init_error:
500         ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed", name);
501
502         cryptodev_zuc_remove(crypto_dev_name);
503         return -EFAULT;
504 }
505
506 static int
507 cryptodev_zuc_probe(const char *name,
508                 const char *input_args)
509 {
510         struct rte_crypto_vdev_init_params init_params = {
511                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
512                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
513                 rte_socket_id()
514         };
515
516         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
517
518         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
519                         init_params.socket_id);
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(name, &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>");