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