New upstream version 18.08
[deb_dpdk.git] / drivers / crypto / zuc / rte_zuc_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation
3  */
4
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_bus_vdev.h>
10 #include <rte_malloc.h>
11 #include <rte_cpuflags.h>
12
13 #include "rte_zuc_pmd_private.h"
14 #define ZUC_MAX_BURST 4
15 #define BYTE_LEN 8
16
17 static uint8_t cryptodev_driver_id;
18
19 /** Get xform chain order. */
20 static enum zuc_operation
21 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
22 {
23         if (xform == NULL)
24                 return ZUC_OP_NOT_SUPPORTED;
25
26         if (xform->next)
27                 if (xform->next->next != NULL)
28                         return ZUC_OP_NOT_SUPPORTED;
29
30         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
31                 if (xform->next == NULL)
32                         return ZUC_OP_ONLY_AUTH;
33                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
34                         return ZUC_OP_AUTH_CIPHER;
35                 else
36                         return ZUC_OP_NOT_SUPPORTED;
37         }
38
39         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
40                 if (xform->next == NULL)
41                         return ZUC_OP_ONLY_CIPHER;
42                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
43                         return ZUC_OP_CIPHER_AUTH;
44                 else
45                         return ZUC_OP_NOT_SUPPORTED;
46         }
47
48         return ZUC_OP_NOT_SUPPORTED;
49 }
50
51
52 /** Parse crypto xform chain and set private session parameters. */
53 int
54 zuc_set_session_parameters(struct zuc_session *sess,
55                 const struct rte_crypto_sym_xform *xform)
56 {
57         const struct rte_crypto_sym_xform *auth_xform = NULL;
58         const struct rte_crypto_sym_xform *cipher_xform = NULL;
59         enum zuc_operation mode;
60
61         /* Select Crypto operation - hash then cipher / cipher then hash */
62         mode = zuc_get_mode(xform);
63
64         switch (mode) {
65         case ZUC_OP_CIPHER_AUTH:
66                 auth_xform = xform->next;
67
68                 /* Fall-through */
69         case ZUC_OP_ONLY_CIPHER:
70                 cipher_xform = xform;
71                 break;
72         case ZUC_OP_AUTH_CIPHER:
73                 cipher_xform = xform->next;
74                 /* Fall-through */
75         case ZUC_OP_ONLY_AUTH:
76                 auth_xform = xform;
77                 break;
78         case ZUC_OP_NOT_SUPPORTED:
79         default:
80                 ZUC_LOG(ERR, "Unsupported operation chain order parameter");
81                 return -ENOTSUP;
82         }
83
84         if (cipher_xform) {
85                 /* Only ZUC EEA3 supported */
86                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
87                         return -ENOTSUP;
88
89                 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
90                         ZUC_LOG(ERR, "Wrong IV length");
91                         return -EINVAL;
92                 }
93                 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
94
95                 /* Copy the key */
96                 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
97                                 ZUC_IV_KEY_LENGTH);
98         }
99
100         if (auth_xform) {
101                 /* Only ZUC EIA3 supported */
102                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
103                         return -ENOTSUP;
104
105                 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
106                         ZUC_LOG(ERR, "Wrong digest length");
107                         return -EINVAL;
108                 }
109
110                 sess->auth_op = auth_xform->auth.op;
111
112                 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
113                         ZUC_LOG(ERR, "Wrong IV length");
114                         return -EINVAL;
115                 }
116                 sess->auth_iv_offset = auth_xform->auth.iv.offset;
117
118                 /* Copy the key */
119                 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
120                                 ZUC_IV_KEY_LENGTH);
121         }
122
123
124         sess->op = mode;
125
126         return 0;
127 }
128
129 /** Get ZUC session. */
130 static struct zuc_session *
131 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
132 {
133         struct zuc_session *sess = NULL;
134
135         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
136                 if (likely(op->sym->session != NULL))
137                         sess = (struct zuc_session *)get_sym_session_private_data(
138                                         op->sym->session,
139                                         cryptodev_driver_id);
140         } else {
141                 void *_sess = NULL;
142                 void *_sess_private_data = NULL;
143
144                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
145                         return NULL;
146
147                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
148                         return NULL;
149
150                 sess = (struct zuc_session *)_sess_private_data;
151
152                 if (unlikely(zuc_set_session_parameters(sess,
153                                 op->sym->xform) != 0)) {
154                         rte_mempool_put(qp->sess_mp, _sess);
155                         rte_mempool_put(qp->sess_mp, _sess_private_data);
156                         sess = NULL;
157                 }
158                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
159                 set_sym_session_private_data(op->sym->session,
160                                 cryptodev_driver_id, _sess_private_data);
161         }
162
163         if (unlikely(sess == NULL))
164                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
165
166
167         return sess;
168 }
169
170 /** Encrypt/decrypt mbufs. */
171 static uint8_t
172 process_zuc_cipher_op(struct rte_crypto_op **ops,
173                 struct zuc_session **sessions,
174                 uint8_t num_ops)
175 {
176         unsigned i;
177         uint8_t processed_ops = 0;
178         uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
179         uint8_t *iv[ZUC_MAX_BURST];
180         uint32_t num_bytes[ZUC_MAX_BURST];
181         uint8_t *cipher_keys[ZUC_MAX_BURST];
182         struct zuc_session *sess;
183
184         for (i = 0; i < num_ops; i++) {
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                 sess = sessions[i];
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] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
216                                 sess->cipher_iv_offset);
217                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
218
219                 cipher_keys[i] = sess->pKey_cipher;
220
221                 processed_ops++;
222         }
223
224         sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
225                         num_bytes, processed_ops);
226
227         return processed_ops;
228 }
229
230 /** Generate/verify hash from mbufs. */
231 static int
232 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
233                 struct zuc_session **sessions,
234                 uint8_t num_ops)
235 {
236         unsigned i;
237         uint8_t processed_ops = 0;
238         uint8_t *src;
239         uint32_t *dst;
240         uint32_t length_in_bits;
241         uint8_t *iv;
242         struct zuc_session *sess;
243
244         for (i = 0; i < num_ops; i++) {
245                 /* Data must be byte aligned */
246                 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
247                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248                         ZUC_LOG(ERR, "Offset");
249                         break;
250                 }
251
252                 sess = sessions[i];
253
254                 length_in_bits = ops[i]->sym->auth.data.length;
255
256                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
257                                 (ops[i]->sym->auth.data.offset >> 3);
258                 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
259                                 sess->auth_iv_offset);
260
261                 if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
262                         dst = (uint32_t *)qp->temp_digest;
263
264                         sso_zuc_eia3_1_buffer(sess->pKey_hash,
265                                         iv, src,
266                                         length_in_bits, dst);
267                         /* Verify digest. */
268                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
269                                         ZUC_DIGEST_LENGTH) != 0)
270                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
271                 } else  {
272                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
273
274                         sso_zuc_eia3_1_buffer(sess->pKey_hash,
275                                         iv, src,
276                                         length_in_bits, dst);
277                 }
278                 processed_ops++;
279         }
280
281         return processed_ops;
282 }
283
284 /** Process a batch of crypto ops which shares the same operation type. */
285 static int
286 process_ops(struct rte_crypto_op **ops, enum zuc_operation op_type,
287                 struct zuc_session **sessions,
288                 struct zuc_qp *qp, uint8_t num_ops,
289                 uint16_t *accumulated_enqueued_ops)
290 {
291         unsigned i;
292         unsigned enqueued_ops, processed_ops;
293
294         switch (op_type) {
295         case ZUC_OP_ONLY_CIPHER:
296                 processed_ops = process_zuc_cipher_op(ops,
297                                 sessions, num_ops);
298                 break;
299         case ZUC_OP_ONLY_AUTH:
300                 processed_ops = process_zuc_hash_op(qp, ops, sessions,
301                                 num_ops);
302                 break;
303         case ZUC_OP_CIPHER_AUTH:
304                 processed_ops = process_zuc_cipher_op(ops, sessions,
305                                 num_ops);
306                 process_zuc_hash_op(qp, ops, sessions, processed_ops);
307                 break;
308         case ZUC_OP_AUTH_CIPHER:
309                 processed_ops = process_zuc_hash_op(qp, ops, sessions,
310                                 num_ops);
311                 process_zuc_cipher_op(ops, sessions, processed_ops);
312                 break;
313         default:
314                 /* Operation not supported. */
315                 processed_ops = 0;
316         }
317
318         for (i = 0; i < num_ops; i++) {
319                 /*
320                  * If there was no error/authentication failure,
321                  * change status to successful.
322                  */
323                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
324                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
325                 /* Free session if a session-less crypto op. */
326                 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
327                         memset(sessions[i], 0, sizeof(struct zuc_session));
328                         memset(ops[i]->sym->session, 0,
329                                         rte_cryptodev_sym_get_header_session_size());
330                         rte_mempool_put(qp->sess_mp, sessions[i]);
331                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
332                         ops[i]->sym->session = NULL;
333                 }
334         }
335
336         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
337                         (void **)ops, processed_ops, NULL);
338         qp->qp_stats.enqueued_count += enqueued_ops;
339         *accumulated_enqueued_ops += enqueued_ops;
340
341         return enqueued_ops;
342 }
343
344 static uint16_t
345 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
346                 uint16_t nb_ops)
347 {
348         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
349         struct rte_crypto_op *curr_c_op;
350
351         struct zuc_session *curr_sess;
352         struct zuc_session *sessions[ZUC_MAX_BURST];
353         enum zuc_operation prev_zuc_op = ZUC_OP_NOT_SUPPORTED;
354         enum zuc_operation curr_zuc_op;
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                 curr_sess = zuc_get_session(qp, curr_c_op);
365                 if (unlikely(curr_sess == NULL)) {
366                         curr_c_op->status =
367                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
368                         break;
369                 }
370
371                 curr_zuc_op = curr_sess->op;
372
373                 /*
374                  * Batch ops that share the same operation type
375                  * (cipher only, auth only...).
376                  */
377                 if (burst_size == 0) {
378                         prev_zuc_op = curr_zuc_op;
379                         c_ops[0] = curr_c_op;
380                         sessions[0] = curr_sess;
381                         burst_size++;
382                 } else if (curr_zuc_op == prev_zuc_op) {
383                         c_ops[burst_size] = curr_c_op;
384                         sessions[burst_size] = curr_sess;
385                         burst_size++;
386                         /*
387                          * When there are enough ops to process in a batch,
388                          * process them, and start a new batch.
389                          */
390                         if (burst_size == ZUC_MAX_BURST) {
391                                 processed_ops = process_ops(c_ops, curr_zuc_op,
392                                                 sessions, qp, burst_size,
393                                                 &enqueued_ops);
394                                 if (processed_ops < burst_size) {
395                                         burst_size = 0;
396                                         break;
397                                 }
398
399                                 burst_size = 0;
400                         }
401                 } else {
402                         /*
403                          * Different operation type, process the ops
404                          * of the previous type.
405                          */
406                         processed_ops = process_ops(c_ops, prev_zuc_op,
407                                         sessions, qp, burst_size,
408                                         &enqueued_ops);
409                         if (processed_ops < burst_size) {
410                                 burst_size = 0;
411                                 break;
412                         }
413
414                         burst_size = 0;
415                         prev_zuc_op = curr_zuc_op;
416
417                         c_ops[0] = curr_c_op;
418                         sessions[0] = curr_sess;
419                         burst_size++;
420                 }
421         }
422
423         if (burst_size != 0) {
424                 /* Process the crypto ops of the last operation type. */
425                 processed_ops = process_ops(c_ops, prev_zuc_op,
426                                 sessions, qp, burst_size,
427                                 &enqueued_ops);
428         }
429
430         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
431         return enqueued_ops;
432 }
433
434 static uint16_t
435 zuc_pmd_dequeue_burst(void *queue_pair,
436                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
437 {
438         struct zuc_qp *qp = queue_pair;
439
440         unsigned nb_dequeued;
441
442         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
443                         (void **)c_ops, nb_ops, NULL);
444         qp->qp_stats.dequeued_count += nb_dequeued;
445
446         return nb_dequeued;
447 }
448
449 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
450
451 static int
452 cryptodev_zuc_create(const char *name,
453                 struct rte_vdev_device *vdev,
454                 struct rte_cryptodev_pmd_init_params *init_params)
455 {
456         struct rte_cryptodev *dev;
457         struct zuc_private *internals;
458         uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
459
460
461         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
462         if (dev == NULL) {
463                 ZUC_LOG(ERR, "failed to create cryptodev vdev");
464                 goto init_error;
465         }
466
467         dev->driver_id = cryptodev_driver_id;
468         dev->dev_ops = rte_zuc_pmd_ops;
469
470         /* Register RX/TX burst functions for data path. */
471         dev->dequeue_burst = zuc_pmd_dequeue_burst;
472         dev->enqueue_burst = zuc_pmd_enqueue_burst;
473
474         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
475                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
476                         cpu_flags;
477
478         internals = dev->data->dev_private;
479
480         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
481
482         return 0;
483 init_error:
484         ZUC_LOG(ERR, "driver %s: failed",
485                         init_params->name);
486
487         cryptodev_zuc_remove(vdev);
488         return -EFAULT;
489 }
490
491 static int
492 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
493 {
494         struct rte_cryptodev_pmd_init_params init_params = {
495                 "",
496                 sizeof(struct zuc_private),
497                 rte_socket_id(),
498                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
499         };
500         const char *name;
501         const char *input_args;
502
503         name = rte_vdev_device_name(vdev);
504         if (name == NULL)
505                 return -EINVAL;
506         input_args = rte_vdev_device_args(vdev);
507
508         rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
509
510         return cryptodev_zuc_create(name, vdev, &init_params);
511 }
512
513 static int
514 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
515 {
516
517         struct rte_cryptodev *cryptodev;
518         const char *name;
519
520         name = rte_vdev_device_name(vdev);
521         if (name == NULL)
522                 return -EINVAL;
523
524         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
525         if (cryptodev == NULL)
526                 return -ENODEV;
527
528         return rte_cryptodev_pmd_destroy(cryptodev);
529 }
530
531 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
532         .probe = cryptodev_zuc_probe,
533         .remove = cryptodev_zuc_remove
534 };
535
536 static struct cryptodev_driver zuc_crypto_drv;
537
538 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
539 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
540         "max_nb_queue_pairs=<int> "
541         "socket_id=<int>");
542 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv.driver,
543                 cryptodev_driver_id);
544
545 RTE_INIT(zuc_init_log)
546 {
547         zuc_logtype_driver = rte_log_register("pmd.crypto.zuc");
548 }