a5c39c9b7ffd84370a25205e158dbe44ba19b068
[deb_dpdk.git] / drivers / crypto / armv8 / rte_armv8_pmd.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2017.
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 Cavium, Inc 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 <stdbool.h>
34
35 #include <rte_common.h>
36 #include <rte_hexdump.h>
37 #include <rte_cryptodev.h>
38 #include <rte_cryptodev_pmd.h>
39 #include <rte_cryptodev_vdev.h>
40 #include <rte_vdev.h>
41 #include <rte_malloc.h>
42 #include <rte_cpuflags.h>
43
44 #include "armv8_crypto_defs.h"
45
46 #include "rte_armv8_pmd_private.h"
47
48 static uint8_t cryptodev_driver_id;
49
50 static int cryptodev_armv8_crypto_uninit(struct rte_vdev_device *vdev);
51
52 /**
53  * Pointers to the supported combined mode crypto functions are stored
54  * in the static tables. Each combined (chained) cryptographic operation
55  * can be described by a set of numbers:
56  * - order:     order of operations (cipher, auth) or (auth, cipher)
57  * - direction: encryption or decryption
58  * - calg:      cipher algorithm such as AES_CBC, AES_CTR, etc.
59  * - aalg:      authentication algorithm such as SHA1, SHA256, etc.
60  * - keyl:      cipher key length, for example 128, 192, 256 bits
61  *
62  * In order to quickly acquire each function pointer based on those numbers,
63  * a hierarchy of arrays is maintained. The final level, 3D array is indexed
64  * by the combined mode function parameters only (cipher algorithm,
65  * authentication algorithm and key length).
66  *
67  * This gives 3 memory accesses to obtain a function pointer instead of
68  * traversing the array manually and comparing function parameters on each loop.
69  *
70  *                   +--+CRYPTO_FUNC
71  *            +--+ENC|
72  *      +--+CA|
73  *      |     +--+DEC
74  * ORDER|
75  *      |     +--+ENC
76  *      +--+AC|
77  *            +--+DEC
78  *
79  */
80
81 /**
82  * 3D array type for ARM Combined Mode crypto functions pointers.
83  * CRYPTO_CIPHER_MAX:                   max cipher ID number
84  * CRYPTO_AUTH_MAX:                     max auth ID number
85  * CRYPTO_CIPHER_KEYLEN_MAX:            max key length ID number
86  */
87 typedef const crypto_func_t
88 crypto_func_tbl_t[CRYPTO_CIPHER_MAX][CRYPTO_AUTH_MAX][CRYPTO_CIPHER_KEYLEN_MAX];
89
90 /* Evaluate to key length definition */
91 #define KEYL(keyl)              (ARMV8_CRYPTO_CIPHER_KEYLEN_ ## keyl)
92
93 /* Local aliases for supported ciphers */
94 #define CIPH_AES_CBC            RTE_CRYPTO_CIPHER_AES_CBC
95 /* Local aliases for supported hashes */
96 #define AUTH_SHA1_HMAC          RTE_CRYPTO_AUTH_SHA1_HMAC
97 #define AUTH_SHA256_HMAC        RTE_CRYPTO_AUTH_SHA256_HMAC
98
99 /**
100  * Arrays containing pointers to particular cryptographic,
101  * combined mode functions.
102  * crypto_op_ca_encrypt:        cipher (encrypt), authenticate
103  * crypto_op_ca_decrypt:        cipher (decrypt), authenticate
104  * crypto_op_ac_encrypt:        authenticate, cipher (encrypt)
105  * crypto_op_ac_decrypt:        authenticate, cipher (decrypt)
106  */
107 static const crypto_func_tbl_t
108 crypto_op_ca_encrypt = {
109         /* [cipher alg][auth alg][key length] = crypto_function, */
110         [CIPH_AES_CBC][AUTH_SHA1_HMAC][KEYL(128)] = aes128cbc_sha1_hmac,
111         [CIPH_AES_CBC][AUTH_SHA256_HMAC][KEYL(128)] = aes128cbc_sha256_hmac,
112 };
113
114 static const crypto_func_tbl_t
115 crypto_op_ca_decrypt = {
116         NULL
117 };
118
119 static const crypto_func_tbl_t
120 crypto_op_ac_encrypt = {
121         NULL
122 };
123
124 static const crypto_func_tbl_t
125 crypto_op_ac_decrypt = {
126         /* [cipher alg][auth alg][key length] = crypto_function, */
127         [CIPH_AES_CBC][AUTH_SHA1_HMAC][KEYL(128)] = sha1_hmac_aes128cbc_dec,
128         [CIPH_AES_CBC][AUTH_SHA256_HMAC][KEYL(128)] = sha256_hmac_aes128cbc_dec,
129 };
130
131 /**
132  * Arrays containing pointers to particular cryptographic function sets,
133  * covering given cipher operation directions (encrypt, decrypt)
134  * for each order of cipher and authentication pairs.
135  */
136 static const crypto_func_tbl_t *
137 crypto_cipher_auth[] = {
138         &crypto_op_ca_encrypt,
139         &crypto_op_ca_decrypt,
140         NULL
141 };
142
143 static const crypto_func_tbl_t *
144 crypto_auth_cipher[] = {
145         &crypto_op_ac_encrypt,
146         &crypto_op_ac_decrypt,
147         NULL
148 };
149
150 /**
151  * Top level array containing pointers to particular cryptographic
152  * function sets, covering given order of chained operations.
153  * crypto_cipher_auth:  cipher first, authenticate after
154  * crypto_auth_cipher:  authenticate first, cipher after
155  */
156 static const crypto_func_tbl_t **
157 crypto_chain_order[] = {
158         crypto_cipher_auth,
159         crypto_auth_cipher,
160         NULL
161 };
162
163 /**
164  * Extract particular combined mode crypto function from the 3D array.
165  */
166 #define CRYPTO_GET_ALGO(order, cop, calg, aalg, keyl)                   \
167 ({                                                                      \
168         crypto_func_tbl_t *func_tbl =                                   \
169                                 (crypto_chain_order[(order)])[(cop)];   \
170                                                                         \
171         ((*func_tbl)[(calg)][(aalg)][KEYL(keyl)]);              \
172 })
173
174 /*----------------------------------------------------------------------------*/
175
176 /**
177  * 2D array type for ARM key schedule functions pointers.
178  * CRYPTO_CIPHER_MAX:                   max cipher ID number
179  * CRYPTO_CIPHER_KEYLEN_MAX:            max key length ID number
180  */
181 typedef const crypto_key_sched_t
182 crypto_key_sched_tbl_t[CRYPTO_CIPHER_MAX][CRYPTO_CIPHER_KEYLEN_MAX];
183
184 static const crypto_key_sched_tbl_t
185 crypto_key_sched_encrypt = {
186         /* [cipher alg][key length] = key_expand_func, */
187         [CIPH_AES_CBC][KEYL(128)] = aes128_key_sched_enc,
188 };
189
190 static const crypto_key_sched_tbl_t
191 crypto_key_sched_decrypt = {
192         /* [cipher alg][key length] = key_expand_func, */
193         [CIPH_AES_CBC][KEYL(128)] = aes128_key_sched_dec,
194 };
195
196 /**
197  * Top level array containing pointers to particular key generation
198  * function sets, covering given operation direction.
199  * crypto_key_sched_encrypt:    keys for encryption
200  * crypto_key_sched_decrypt:    keys for decryption
201  */
202 static const crypto_key_sched_tbl_t *
203 crypto_key_sched_dir[] = {
204         &crypto_key_sched_encrypt,
205         &crypto_key_sched_decrypt,
206         NULL
207 };
208
209 /**
210  * Extract particular combined mode crypto function from the 3D array.
211  */
212 #define CRYPTO_GET_KEY_SCHED(cop, calg, keyl)                           \
213 ({                                                                      \
214         crypto_key_sched_tbl_t *ks_tbl = crypto_key_sched_dir[(cop)];   \
215                                                                         \
216         ((*ks_tbl)[(calg)][KEYL(keyl)]);                                \
217 })
218
219 /*----------------------------------------------------------------------------*/
220
221 /*
222  *------------------------------------------------------------------------------
223  * Session Prepare
224  *------------------------------------------------------------------------------
225  */
226
227 /** Get xform chain order */
228 static enum armv8_crypto_chain_order
229 armv8_crypto_get_chain_order(const struct rte_crypto_sym_xform *xform)
230 {
231
232         /*
233          * This driver currently covers only chained operations.
234          * Ignore only cipher or only authentication operations
235          * or chains longer than 2 xform structures.
236          */
237         if (xform->next == NULL || xform->next->next != NULL)
238                 return ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED;
239
240         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
241                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
242                         return ARMV8_CRYPTO_CHAIN_AUTH_CIPHER;
243         }
244
245         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
246                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
247                         return ARMV8_CRYPTO_CHAIN_CIPHER_AUTH;
248         }
249
250         return ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED;
251 }
252
253 static inline void
254 auth_hmac_pad_prepare(struct armv8_crypto_session *sess,
255                                 const struct rte_crypto_sym_xform *xform)
256 {
257         size_t i;
258
259         /* Generate i_key_pad and o_key_pad */
260         memset(sess->auth.hmac.i_key_pad, 0, sizeof(sess->auth.hmac.i_key_pad));
261         rte_memcpy(sess->auth.hmac.i_key_pad, sess->auth.hmac.key,
262                                                         xform->auth.key.length);
263         memset(sess->auth.hmac.o_key_pad, 0, sizeof(sess->auth.hmac.o_key_pad));
264         rte_memcpy(sess->auth.hmac.o_key_pad, sess->auth.hmac.key,
265                                                         xform->auth.key.length);
266         /*
267          * XOR key with IPAD/OPAD values to obtain i_key_pad
268          * and o_key_pad.
269          * Byte-by-byte operation may seem to be the less efficient
270          * here but in fact it's the opposite.
271          * The result ASM code is likely operate on NEON registers
272          * (load auth key to Qx, load IPAD/OPAD to multiple
273          * elements of Qy, eor 128 bits at once).
274          */
275         for (i = 0; i < SHA_BLOCK_MAX; i++) {
276                 sess->auth.hmac.i_key_pad[i] ^= HMAC_IPAD_VALUE;
277                 sess->auth.hmac.o_key_pad[i] ^= HMAC_OPAD_VALUE;
278         }
279 }
280
281 static inline int
282 auth_set_prerequisites(struct armv8_crypto_session *sess,
283                         const struct rte_crypto_sym_xform *xform)
284 {
285         uint8_t partial[64] = { 0 };
286         int error;
287
288         switch (xform->auth.algo) {
289         case RTE_CRYPTO_AUTH_SHA1_HMAC:
290                 /*
291                  * Generate authentication key, i_key_pad and o_key_pad.
292                  */
293                 /* Zero memory under key */
294                 memset(sess->auth.hmac.key, 0, SHA1_BLOCK_SIZE);
295
296                 /*
297                  * Now copy the given authentication key to the session
298                  * key.
299                  */
300                 rte_memcpy(sess->auth.hmac.key, xform->auth.key.data,
301                                                 xform->auth.key.length);
302
303                 /* Prepare HMAC padding: key|pattern */
304                 auth_hmac_pad_prepare(sess, xform);
305                 /*
306                  * Calculate partial hash values for i_key_pad and o_key_pad.
307                  * Will be used as initialization state for final HMAC.
308                  */
309                 error = sha1_block_partial(NULL, sess->auth.hmac.i_key_pad,
310                     partial, SHA1_BLOCK_SIZE);
311                 if (error != 0)
312                         return -1;
313                 memcpy(sess->auth.hmac.i_key_pad, partial, SHA1_BLOCK_SIZE);
314
315                 error = sha1_block_partial(NULL, sess->auth.hmac.o_key_pad,
316                     partial, SHA1_BLOCK_SIZE);
317                 if (error != 0)
318                         return -1;
319                 memcpy(sess->auth.hmac.o_key_pad, partial, SHA1_BLOCK_SIZE);
320
321                 break;
322         case RTE_CRYPTO_AUTH_SHA256_HMAC:
323                 /*
324                  * Generate authentication key, i_key_pad and o_key_pad.
325                  */
326                 /* Zero memory under key */
327                 memset(sess->auth.hmac.key, 0, SHA256_BLOCK_SIZE);
328
329                 /*
330                  * Now copy the given authentication key to the session
331                  * key.
332                  */
333                 rte_memcpy(sess->auth.hmac.key, xform->auth.key.data,
334                                                 xform->auth.key.length);
335
336                 /* Prepare HMAC padding: key|pattern */
337                 auth_hmac_pad_prepare(sess, xform);
338                 /*
339                  * Calculate partial hash values for i_key_pad and o_key_pad.
340                  * Will be used as initialization state for final HMAC.
341                  */
342                 error = sha256_block_partial(NULL, sess->auth.hmac.i_key_pad,
343                     partial, SHA256_BLOCK_SIZE);
344                 if (error != 0)
345                         return -1;
346                 memcpy(sess->auth.hmac.i_key_pad, partial, SHA256_BLOCK_SIZE);
347
348                 error = sha256_block_partial(NULL, sess->auth.hmac.o_key_pad,
349                     partial, SHA256_BLOCK_SIZE);
350                 if (error != 0)
351                         return -1;
352                 memcpy(sess->auth.hmac.o_key_pad, partial, SHA256_BLOCK_SIZE);
353
354                 break;
355         default:
356                 break;
357         }
358
359         return 0;
360 }
361
362 static inline int
363 cipher_set_prerequisites(struct armv8_crypto_session *sess,
364                         const struct rte_crypto_sym_xform *xform)
365 {
366         crypto_key_sched_t cipher_key_sched;
367
368         cipher_key_sched = sess->cipher.key_sched;
369         if (likely(cipher_key_sched != NULL)) {
370                 /* Set up cipher session key */
371                 cipher_key_sched(sess->cipher.key.data, xform->cipher.key.data);
372         }
373
374         return 0;
375 }
376
377 static int
378 armv8_crypto_set_session_chained_parameters(struct armv8_crypto_session *sess,
379                 const struct rte_crypto_sym_xform *cipher_xform,
380                 const struct rte_crypto_sym_xform *auth_xform)
381 {
382         enum armv8_crypto_chain_order order;
383         enum armv8_crypto_cipher_operation cop;
384         enum rte_crypto_cipher_algorithm calg;
385         enum rte_crypto_auth_algorithm aalg;
386
387         /* Validate and prepare scratch order of combined operations */
388         switch (sess->chain_order) {
389         case ARMV8_CRYPTO_CHAIN_CIPHER_AUTH:
390         case ARMV8_CRYPTO_CHAIN_AUTH_CIPHER:
391                 order = sess->chain_order;
392                 break;
393         default:
394                 return -ENOTSUP;
395         }
396         /* Select cipher direction */
397         sess->cipher.direction = cipher_xform->cipher.op;
398         /* Select cipher key */
399         sess->cipher.key.length = cipher_xform->cipher.key.length;
400         /* Set cipher direction */
401         cop = sess->cipher.direction;
402         /* Set cipher algorithm */
403         calg = cipher_xform->cipher.algo;
404
405         /* Select cipher algo */
406         switch (calg) {
407         /* Cover supported cipher algorithms */
408         case RTE_CRYPTO_CIPHER_AES_CBC:
409                 sess->cipher.algo = calg;
410                 /* IV len is always 16 bytes (block size) for AES CBC */
411                 sess->cipher.iv.length = 16;
412                 break;
413         default:
414                 return -ENOTSUP;
415         }
416         /* Select auth generate/verify */
417         sess->auth.operation = auth_xform->auth.op;
418
419         /* Select auth algo */
420         switch (auth_xform->auth.algo) {
421         /* Cover supported hash algorithms */
422         case RTE_CRYPTO_AUTH_SHA1_HMAC:
423         case RTE_CRYPTO_AUTH_SHA256_HMAC: /* Fall through */
424                 aalg = auth_xform->auth.algo;
425                 sess->auth.mode = ARMV8_CRYPTO_AUTH_AS_HMAC;
426                 break;
427         default:
428                 return -ENOTSUP;
429         }
430
431         /* Set the digest length */
432         sess->auth.digest_length = auth_xform->auth.digest_length;
433
434         /* Verify supported key lengths and extract proper algorithm */
435         switch (cipher_xform->cipher.key.length << 3) {
436         case 128:
437                 sess->crypto_func =
438                                 CRYPTO_GET_ALGO(order, cop, calg, aalg, 128);
439                 sess->cipher.key_sched =
440                                 CRYPTO_GET_KEY_SCHED(cop, calg, 128);
441                 break;
442         case 192:
443         case 256:
444                 /* These key lengths are not supported yet */
445         default: /* Fall through */
446                 sess->crypto_func = NULL;
447                 sess->cipher.key_sched = NULL;
448                 return -ENOTSUP;
449         }
450
451         if (unlikely(sess->crypto_func == NULL)) {
452                 /*
453                  * If we got here that means that there must be a bug
454                  * in the algorithms selection above. Nevertheless keep
455                  * it here to catch bug immediately and avoid NULL pointer
456                  * dereference in OPs processing.
457                  */
458                 ARMV8_CRYPTO_LOG_ERR(
459                         "No appropriate crypto function for given parameters");
460                 return -EINVAL;
461         }
462
463         /* Set up cipher session prerequisites */
464         if (cipher_set_prerequisites(sess, cipher_xform) != 0)
465                 return -EINVAL;
466
467         /* Set up authentication session prerequisites */
468         if (auth_set_prerequisites(sess, auth_xform) != 0)
469                 return -EINVAL;
470
471         return 0;
472 }
473
474 /** Parse crypto xform chain and set private session parameters */
475 int
476 armv8_crypto_set_session_parameters(struct armv8_crypto_session *sess,
477                 const struct rte_crypto_sym_xform *xform)
478 {
479         const struct rte_crypto_sym_xform *cipher_xform = NULL;
480         const struct rte_crypto_sym_xform *auth_xform = NULL;
481         bool is_chained_op;
482         int ret;
483
484         /* Filter out spurious/broken requests */
485         if (xform == NULL)
486                 return -EINVAL;
487
488         sess->chain_order = armv8_crypto_get_chain_order(xform);
489         switch (sess->chain_order) {
490         case ARMV8_CRYPTO_CHAIN_CIPHER_AUTH:
491                 cipher_xform = xform;
492                 auth_xform = xform->next;
493                 is_chained_op = true;
494                 break;
495         case ARMV8_CRYPTO_CHAIN_AUTH_CIPHER:
496                 auth_xform = xform;
497                 cipher_xform = xform->next;
498                 is_chained_op = true;
499                 break;
500         default:
501                 is_chained_op = false;
502                 return -ENOTSUP;
503         }
504
505         /* Set IV offset */
506         sess->cipher.iv.offset = cipher_xform->cipher.iv.offset;
507
508         if (is_chained_op) {
509                 ret = armv8_crypto_set_session_chained_parameters(sess,
510                                                 cipher_xform, auth_xform);
511                 if (unlikely(ret != 0)) {
512                         ARMV8_CRYPTO_LOG_ERR(
513                         "Invalid/unsupported chained (cipher/auth) parameters");
514                         return ret;
515                 }
516         } else {
517                 ARMV8_CRYPTO_LOG_ERR("Invalid/unsupported operation");
518                 return -ENOTSUP;
519         }
520
521         return 0;
522 }
523
524 /** Provide session for operation */
525 static inline struct armv8_crypto_session *
526 get_session(struct armv8_crypto_qp *qp, struct rte_crypto_op *op)
527 {
528         struct armv8_crypto_session *sess = NULL;
529
530         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
531                 /* get existing session */
532                 if (likely(op->sym->session != NULL)) {
533                         sess = (struct armv8_crypto_session *)
534                                         get_session_private_data(
535                                         op->sym->session,
536                                         cryptodev_driver_id);
537                 }
538         } else {
539                 /* provide internal session */
540                 void *_sess = NULL;
541                 void *_sess_private_data = NULL;
542
543                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
544                         return NULL;
545
546                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
547                         return NULL;
548
549                 sess = (struct armv8_crypto_session *)_sess_private_data;
550
551                 if (unlikely(armv8_crypto_set_session_parameters(sess,
552                                 op->sym->xform) != 0)) {
553                         rte_mempool_put(qp->sess_mp, _sess);
554                         rte_mempool_put(qp->sess_mp, _sess_private_data);
555                         sess = NULL;
556                 }
557                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
558                 set_session_private_data(op->sym->session, cryptodev_driver_id,
559                         _sess_private_data);
560         }
561
562         if (unlikely(sess == NULL))
563                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
564
565         return sess;
566 }
567
568 /*
569  *------------------------------------------------------------------------------
570  * Process Operations
571  *------------------------------------------------------------------------------
572  */
573
574 /*----------------------------------------------------------------------------*/
575
576 /** Process cipher operation */
577 static inline void
578 process_armv8_chained_op
579                 (struct rte_crypto_op *op, struct armv8_crypto_session *sess,
580                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
581 {
582         crypto_func_t crypto_func;
583         crypto_arg_t arg;
584         struct rte_mbuf *m_asrc, *m_adst;
585         uint8_t *csrc, *cdst;
586         uint8_t *adst, *asrc;
587         uint64_t clen, alen;
588         int error;
589
590         clen = op->sym->cipher.data.length;
591         alen = op->sym->auth.data.length;
592
593         csrc = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
594                         op->sym->cipher.data.offset);
595         cdst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
596                         op->sym->cipher.data.offset);
597
598         switch (sess->chain_order) {
599         case ARMV8_CRYPTO_CHAIN_CIPHER_AUTH:
600                 m_asrc = m_adst = mbuf_dst;
601                 break;
602         case ARMV8_CRYPTO_CHAIN_AUTH_CIPHER:
603                 m_asrc = mbuf_src;
604                 m_adst = mbuf_dst;
605                 break;
606         default:
607                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
608                 return;
609         }
610         asrc = rte_pktmbuf_mtod_offset(m_asrc, uint8_t *,
611                                 op->sym->auth.data.offset);
612
613         switch (sess->auth.mode) {
614         case ARMV8_CRYPTO_AUTH_AS_AUTH:
615                 /* Nothing to do here, just verify correct option */
616                 break;
617         case ARMV8_CRYPTO_AUTH_AS_HMAC:
618                 arg.digest.hmac.key = sess->auth.hmac.key;
619                 arg.digest.hmac.i_key_pad = sess->auth.hmac.i_key_pad;
620                 arg.digest.hmac.o_key_pad = sess->auth.hmac.o_key_pad;
621                 break;
622         default:
623                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
624                 return;
625         }
626
627         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE) {
628                 adst = op->sym->auth.digest.data;
629                 if (adst == NULL) {
630                         adst = rte_pktmbuf_mtod_offset(m_adst,
631                                         uint8_t *,
632                                         op->sym->auth.data.offset +
633                                         op->sym->auth.data.length);
634                 }
635         } else {
636                 adst = (uint8_t *)rte_pktmbuf_append(m_asrc,
637                                 sess->auth.digest_length);
638         }
639
640         arg.cipher.iv = rte_crypto_op_ctod_offset(op, uint8_t *,
641                                         sess->cipher.iv.offset);
642         arg.cipher.key = sess->cipher.key.data;
643         /* Acquire combined mode function */
644         crypto_func = sess->crypto_func;
645         ARMV8_CRYPTO_ASSERT(crypto_func != NULL);
646         error = crypto_func(csrc, cdst, clen, asrc, adst, alen, &arg);
647         if (error != 0) {
648                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
649                 return;
650         }
651
652         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
653         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
654                 if (memcmp(adst, op->sym->auth.digest.data,
655                                 sess->auth.digest_length) != 0) {
656                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
657                 }
658                 /* Trim area used for digest from mbuf. */
659                 rte_pktmbuf_trim(m_asrc,
660                                 sess->auth.digest_length);
661         }
662 }
663
664 /** Process crypto operation for mbuf */
665 static inline int
666 process_op(const struct armv8_crypto_qp *qp, struct rte_crypto_op *op,
667                 struct armv8_crypto_session *sess)
668 {
669         struct rte_mbuf *msrc, *mdst;
670
671         msrc = op->sym->m_src;
672         mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
673
674         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
675
676         switch (sess->chain_order) {
677         case ARMV8_CRYPTO_CHAIN_CIPHER_AUTH:
678         case ARMV8_CRYPTO_CHAIN_AUTH_CIPHER: /* Fall through */
679                 process_armv8_chained_op(op, sess, msrc, mdst);
680                 break;
681         default:
682                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
683                 break;
684         }
685
686         /* Free session if a session-less crypto op */
687         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
688                 memset(sess, 0, sizeof(struct armv8_crypto_session));
689                 memset(op->sym->session, 0,
690                                 rte_cryptodev_get_header_session_size());
691                 rte_mempool_put(qp->sess_mp, sess);
692                 rte_mempool_put(qp->sess_mp, op->sym->session);
693                 op->sym->session = NULL;
694         }
695
696         if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
697                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
698
699         if (unlikely(op->status == RTE_CRYPTO_OP_STATUS_ERROR))
700                 return -1;
701
702         return 0;
703 }
704
705 /*
706  *------------------------------------------------------------------------------
707  * PMD Framework
708  *------------------------------------------------------------------------------
709  */
710
711 /** Enqueue burst */
712 static uint16_t
713 armv8_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
714                 uint16_t nb_ops)
715 {
716         struct armv8_crypto_session *sess;
717         struct armv8_crypto_qp *qp = queue_pair;
718         int i, retval;
719
720         for (i = 0; i < nb_ops; i++) {
721                 sess = get_session(qp, ops[i]);
722                 if (unlikely(sess == NULL))
723                         goto enqueue_err;
724
725                 retval = process_op(qp, ops[i], sess);
726                 if (unlikely(retval < 0))
727                         goto enqueue_err;
728         }
729
730         retval = rte_ring_enqueue_burst(qp->processed_ops, (void *)ops, i,
731                         NULL);
732         qp->stats.enqueued_count += retval;
733
734         return retval;
735
736 enqueue_err:
737         retval = rte_ring_enqueue_burst(qp->processed_ops, (void *)ops, i,
738                         NULL);
739         if (ops[i] != NULL)
740                 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
741
742         qp->stats.enqueue_err_count++;
743         return retval;
744 }
745
746 /** Dequeue burst */
747 static uint16_t
748 armv8_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
749                 uint16_t nb_ops)
750 {
751         struct armv8_crypto_qp *qp = queue_pair;
752
753         unsigned int nb_dequeued = 0;
754
755         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
756                         (void **)ops, nb_ops, NULL);
757         qp->stats.dequeued_count += nb_dequeued;
758
759         return nb_dequeued;
760 }
761
762 /** Create ARMv8 crypto device */
763 static int
764 cryptodev_armv8_crypto_create(const char *name,
765                         struct rte_vdev_device *vdev,
766                         struct rte_crypto_vdev_init_params *init_params)
767 {
768         struct rte_cryptodev *dev;
769         struct armv8_crypto_private *internals;
770
771         /* Check CPU for support for AES instruction set */
772         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
773                 ARMV8_CRYPTO_LOG_ERR(
774                         "AES instructions not supported by CPU");
775                 return -EFAULT;
776         }
777
778         /* Check CPU for support for SHA instruction set */
779         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_SHA1) ||
780             !rte_cpu_get_flag_enabled(RTE_CPUFLAG_SHA2)) {
781                 ARMV8_CRYPTO_LOG_ERR(
782                         "SHA1/SHA2 instructions not supported by CPU");
783                 return -EFAULT;
784         }
785
786         /* Check CPU for support for Advance SIMD instruction set */
787         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
788                 ARMV8_CRYPTO_LOG_ERR(
789                         "Advanced SIMD instructions not supported by CPU");
790                 return -EFAULT;
791         }
792
793         if (init_params->name[0] == '\0')
794                 snprintf(init_params->name, sizeof(init_params->name),
795                                 "%s", name);
796
797         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
798                                 sizeof(struct armv8_crypto_private),
799                                 init_params->socket_id,
800                                 vdev);
801         if (dev == NULL) {
802                 ARMV8_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
803                 goto init_error;
804         }
805
806         dev->driver_id = cryptodev_driver_id;
807         dev->dev_ops = rte_armv8_crypto_pmd_ops;
808
809         /* register rx/tx burst functions for data path */
810         dev->dequeue_burst = armv8_crypto_pmd_dequeue_burst;
811         dev->enqueue_burst = armv8_crypto_pmd_enqueue_burst;
812
813         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
814                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
815                         RTE_CRYPTODEV_FF_CPU_NEON |
816                         RTE_CRYPTODEV_FF_CPU_ARM_CE;
817
818         /* Set vector instructions mode supported */
819         internals = dev->data->dev_private;
820
821         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
822         internals->max_nb_sessions = init_params->max_nb_sessions;
823
824         return 0;
825
826 init_error:
827         ARMV8_CRYPTO_LOG_ERR(
828                 "driver %s: cryptodev_armv8_crypto_create failed",
829                 init_params->name);
830
831         cryptodev_armv8_crypto_uninit(vdev);
832         return -EFAULT;
833 }
834
835 /** Initialise ARMv8 crypto device */
836 static int
837 cryptodev_armv8_crypto_init(struct rte_vdev_device *vdev)
838 {
839         struct rte_crypto_vdev_init_params init_params = {
840                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
841                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
842                 rte_socket_id(),
843                 {0}
844         };
845         const char *name;
846         const char *input_args;
847
848         name = rte_vdev_device_name(vdev);
849         if (name == NULL)
850                 return -EINVAL;
851         input_args = rte_vdev_device_args(vdev);
852         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
853
854         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
855                         init_params.socket_id);
856         if (init_params.name[0] != '\0') {
857                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
858                         init_params.name);
859         }
860         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
861                         init_params.max_nb_queue_pairs);
862         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
863                         init_params.max_nb_sessions);
864
865         return cryptodev_armv8_crypto_create(name, vdev, &init_params);
866 }
867
868 /** Uninitialise ARMv8 crypto device */
869 static int
870 cryptodev_armv8_crypto_uninit(struct rte_vdev_device *vdev)
871 {
872         const char *name;
873
874         name = rte_vdev_device_name(vdev);
875         if (name == NULL)
876                 return -EINVAL;
877
878         RTE_LOG(INFO, PMD,
879                 "Closing ARMv8 crypto device %s on numa socket %u\n",
880                 name, rte_socket_id());
881
882         return 0;
883 }
884
885 static struct rte_vdev_driver armv8_crypto_drv = {
886         .probe = cryptodev_armv8_crypto_init,
887         .remove = cryptodev_armv8_crypto_uninit
888 };
889
890 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ARMV8_PMD, armv8_crypto_drv);
891 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_ARMV8_PMD, cryptodev_armv8_pmd);
892 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ARMV8_PMD,
893         "max_nb_queue_pairs=<int> "
894         "max_nb_sessions=<int> "
895         "socket_id=<int>");
896 RTE_PMD_REGISTER_CRYPTO_DRIVER(armv8_crypto_drv, cryptodev_driver_id);