crypto/aesni_mb: support CPU crypto
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2017 Intel Corporation
3  */
4
5 #include <intel-ipsec-mb.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 #include <rte_per_lcore.h>
15
16 #include "aesni_mb_pmd_private.h"
17
18 int aesni_mb_logtype_driver;
19
20 #define AES_CCM_DIGEST_MIN_LEN 4
21 #define AES_CCM_DIGEST_MAX_LEN 16
22 #define HMAC_MAX_BLOCK_SIZE 128
23 static uint8_t cryptodev_driver_id;
24
25 /*
26  * Needed to support CPU-CRYPTO API (rte_cryptodev_sym_cpu_crypto_process),
27  * as we still use JOB based API even for synchronous processing.
28  */
29 static RTE_DEFINE_PER_LCORE(MB_MGR *, sync_mb_mgr);
30
31 typedef void (*hash_one_block_t)(const void *data, void *digest);
32 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
33
34 /**
35  * Calculate the authentication pre-computes
36  *
37  * @param one_block_hash        Function pointer to calculate digest on ipad/opad
38  * @param ipad                  Inner pad output byte array
39  * @param opad                  Outer pad output byte array
40  * @param hkey                  Authentication key
41  * @param hkey_len              Authentication key length
42  * @param blocksize             Block size of selected hash algo
43  */
44 static void
45 calculate_auth_precomputes(hash_one_block_t one_block_hash,
46                 uint8_t *ipad, uint8_t *opad,
47                 const uint8_t *hkey, uint16_t hkey_len,
48                 uint16_t blocksize)
49 {
50         unsigned i, length;
51
52         uint8_t ipad_buf[blocksize] __rte_aligned(16);
53         uint8_t opad_buf[blocksize] __rte_aligned(16);
54
55         /* Setup inner and outer pads */
56         memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
57         memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
58
59         /* XOR hash key with inner and outer pads */
60         length = hkey_len > blocksize ? blocksize : hkey_len;
61
62         for (i = 0; i < length; i++) {
63                 ipad_buf[i] ^= hkey[i];
64                 opad_buf[i] ^= hkey[i];
65         }
66
67         /* Compute partial hashes */
68         (*one_block_hash)(ipad_buf, ipad);
69         (*one_block_hash)(opad_buf, opad);
70
71         /* Clean up stack */
72         memset(ipad_buf, 0, blocksize);
73         memset(opad_buf, 0, blocksize);
74 }
75
76 /** Get xform chain order */
77 static enum aesni_mb_operation
78 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
79 {
80         if (xform == NULL)
81                 return AESNI_MB_OP_NOT_SUPPORTED;
82
83         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
84                 if (xform->next == NULL)
85                         return AESNI_MB_OP_CIPHER_ONLY;
86                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
87                         return AESNI_MB_OP_CIPHER_HASH;
88         }
89
90         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
91                 if (xform->next == NULL)
92                         return AESNI_MB_OP_HASH_ONLY;
93                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
94                         return AESNI_MB_OP_HASH_CIPHER;
95         }
96 #if IMB_VERSION_NUM > IMB_VERSION(0, 52, 0)
97         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
98                 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
99                         /*
100                          * CCM requires to hash first and cipher later
101                          * when encrypting
102                          */
103                         if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
104                                 return AESNI_MB_OP_AEAD_HASH_CIPHER;
105                         else
106                                 return AESNI_MB_OP_AEAD_CIPHER_HASH;
107                 } else {
108                         if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
109                                 return AESNI_MB_OP_AEAD_CIPHER_HASH;
110                         else
111                                 return AESNI_MB_OP_AEAD_HASH_CIPHER;
112                 }
113         }
114 #else
115         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
116                 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM ||
117                                 xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
118                         if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
119                                 return AESNI_MB_OP_AEAD_CIPHER_HASH;
120                         else
121                                 return AESNI_MB_OP_AEAD_HASH_CIPHER;
122                 }
123         }
124 #endif
125
126         return AESNI_MB_OP_NOT_SUPPORTED;
127 }
128
129 /** Set session authentication parameters */
130 static int
131 aesni_mb_set_session_auth_parameters(const MB_MGR *mb_mgr,
132                 struct aesni_mb_session *sess,
133                 const struct rte_crypto_sym_xform *xform)
134 {
135         hash_one_block_t hash_oneblock_fn = NULL;
136         unsigned int key_larger_block_size = 0;
137         uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
138         uint32_t auth_precompute = 1;
139
140         if (xform == NULL) {
141                 sess->auth.algo = NULL_HASH;
142                 return 0;
143         }
144
145         if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
146                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
147                 return -1;
148         }
149
150         /* Set the request digest size */
151         sess->auth.req_digest_len = xform->auth.digest_length;
152
153         /* Select auth generate/verify */
154         sess->auth.operation = xform->auth.op;
155
156         /* Set Authentication Parameters */
157         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
158                 sess->auth.algo = AES_XCBC;
159
160                 uint16_t xcbc_mac_digest_len =
161                         get_truncated_digest_byte_length(AES_XCBC);
162                 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
163                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
164                         return -EINVAL;
165                 }
166                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
167
168                 IMB_AES_XCBC_KEYEXP(mb_mgr, xform->auth.key.data,
169                                 sess->auth.xcbc.k1_expanded,
170                                 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
171                 return 0;
172         }
173
174         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
175                 uint32_t dust[4*15];
176
177                 sess->auth.algo = AES_CMAC;
178
179                 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
180
181                 if (sess->auth.req_digest_len > cmac_digest_len) {
182                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
183                         return -EINVAL;
184                 }
185                 /*
186                  * Multi-buffer lib supports digest sizes from 4 to 16 bytes
187                  * in version 0.50 and sizes of 12 and 16 bytes,
188                  * in version 0.49.
189                  * If size requested is different, generate the full digest
190                  * (16 bytes) in a temporary location and then memcpy
191                  * the requested number of bytes.
192                  */
193                 if (sess->auth.req_digest_len < 4)
194                         sess->auth.gen_digest_len = cmac_digest_len;
195                 else
196                         sess->auth.gen_digest_len = sess->auth.req_digest_len;
197
198                 IMB_AES_KEYEXP_128(mb_mgr, xform->auth.key.data,
199                                 sess->auth.cmac.expkey, dust);
200                 IMB_AES_CMAC_SUBKEY_GEN_128(mb_mgr, sess->auth.cmac.expkey,
201                                 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
202                 return 0;
203         }
204
205         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
206                 if (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) {
207                         sess->cipher.direction = ENCRYPT;
208                         sess->chain_order = CIPHER_HASH;
209                 } else
210                         sess->cipher.direction = DECRYPT;
211
212                 sess->auth.algo = AES_GMAC;
213                 /*
214                  * Multi-buffer lib supports 8, 12 and 16 bytes of digest.
215                  * If size requested is different, generate the full digest
216                  * (16 bytes) in a temporary location and then memcpy
217                  * the requested number of bytes.
218                  */
219                 if (sess->auth.req_digest_len != 16 &&
220                                 sess->auth.req_digest_len != 12 &&
221                                 sess->auth.req_digest_len != 8) {
222                         sess->auth.gen_digest_len = 16;
223                 } else {
224                         sess->auth.gen_digest_len = sess->auth.req_digest_len;
225                 }
226                 sess->iv.length = xform->auth.iv.length;
227                 sess->iv.offset = xform->auth.iv.offset;
228
229                 switch (xform->auth.key.length) {
230                 case AES_128_BYTES:
231                         IMB_AES128_GCM_PRE(mb_mgr, xform->auth.key.data,
232                                 &sess->cipher.gcm_key);
233                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
234                         break;
235                 case AES_192_BYTES:
236                         IMB_AES192_GCM_PRE(mb_mgr, xform->auth.key.data,
237                                 &sess->cipher.gcm_key);
238                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
239                         break;
240                 case AES_256_BYTES:
241                         IMB_AES256_GCM_PRE(mb_mgr, xform->auth.key.data,
242                                 &sess->cipher.gcm_key);
243                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
244                         break;
245                 default:
246                         RTE_LOG(ERR, PMD, "failed to parse test type\n");
247                         return -EINVAL;
248                 }
249
250                 return 0;
251         }
252
253         switch (xform->auth.algo) {
254         case RTE_CRYPTO_AUTH_MD5_HMAC:
255                 sess->auth.algo = MD5;
256                 hash_oneblock_fn = mb_mgr->md5_one_block;
257                 break;
258         case RTE_CRYPTO_AUTH_SHA1_HMAC:
259                 sess->auth.algo = SHA1;
260                 hash_oneblock_fn = mb_mgr->sha1_one_block;
261                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
262                         IMB_SHA1(mb_mgr,
263                                 xform->auth.key.data,
264                                 xform->auth.key.length,
265                                 hashed_key);
266                         key_larger_block_size = 1;
267                 }
268                 break;
269         case RTE_CRYPTO_AUTH_SHA1:
270                 sess->auth.algo = PLAIN_SHA1;
271                 auth_precompute = 0;
272                 break;
273         case RTE_CRYPTO_AUTH_SHA224_HMAC:
274                 sess->auth.algo = SHA_224;
275                 hash_oneblock_fn = mb_mgr->sha224_one_block;
276                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
277                         IMB_SHA224(mb_mgr,
278                                 xform->auth.key.data,
279                                 xform->auth.key.length,
280                                 hashed_key);
281                         key_larger_block_size = 1;
282                 }
283                 break;
284         case RTE_CRYPTO_AUTH_SHA224:
285                 sess->auth.algo = PLAIN_SHA_224;
286                 auth_precompute = 0;
287                 break;
288         case RTE_CRYPTO_AUTH_SHA256_HMAC:
289                 sess->auth.algo = SHA_256;
290                 hash_oneblock_fn = mb_mgr->sha256_one_block;
291                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
292                         IMB_SHA256(mb_mgr,
293                                 xform->auth.key.data,
294                                 xform->auth.key.length,
295                                 hashed_key);
296                         key_larger_block_size = 1;
297                 }
298                 break;
299         case RTE_CRYPTO_AUTH_SHA256:
300                 sess->auth.algo = PLAIN_SHA_256;
301                 auth_precompute = 0;
302                 break;
303         case RTE_CRYPTO_AUTH_SHA384_HMAC:
304                 sess->auth.algo = SHA_384;
305                 hash_oneblock_fn = mb_mgr->sha384_one_block;
306                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
307                         IMB_SHA384(mb_mgr,
308                                 xform->auth.key.data,
309                                 xform->auth.key.length,
310                                 hashed_key);
311                         key_larger_block_size = 1;
312                 }
313                 break;
314         case RTE_CRYPTO_AUTH_SHA384:
315                 sess->auth.algo = PLAIN_SHA_384;
316                 auth_precompute = 0;
317                 break;
318         case RTE_CRYPTO_AUTH_SHA512_HMAC:
319                 sess->auth.algo = SHA_512;
320                 hash_oneblock_fn = mb_mgr->sha512_one_block;
321                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
322                         IMB_SHA512(mb_mgr,
323                                 xform->auth.key.data,
324                                 xform->auth.key.length,
325                                 hashed_key);
326                         key_larger_block_size = 1;
327                 }
328                 break;
329         case RTE_CRYPTO_AUTH_SHA512:
330                 sess->auth.algo = PLAIN_SHA_512;
331                 auth_precompute = 0;
332                 break;
333         default:
334                 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
335                 return -ENOTSUP;
336         }
337         uint16_t trunc_digest_size =
338                         get_truncated_digest_byte_length(sess->auth.algo);
339         uint16_t full_digest_size =
340                         get_digest_byte_length(sess->auth.algo);
341
342         if (sess->auth.req_digest_len > full_digest_size ||
343                         sess->auth.req_digest_len == 0) {
344                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
345                 return -EINVAL;
346         }
347
348         if (sess->auth.req_digest_len != trunc_digest_size &&
349                         sess->auth.req_digest_len != full_digest_size)
350                 sess->auth.gen_digest_len = full_digest_size;
351         else
352                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
353
354         /* Plain SHA does not require precompute key */
355         if (auth_precompute == 0)
356                 return 0;
357
358         /* Calculate Authentication precomputes */
359         if (key_larger_block_size) {
360                 calculate_auth_precomputes(hash_oneblock_fn,
361                         sess->auth.pads.inner, sess->auth.pads.outer,
362                         hashed_key,
363                         xform->auth.key.length,
364                         get_auth_algo_blocksize(sess->auth.algo));
365         } else {
366                 calculate_auth_precomputes(hash_oneblock_fn,
367                         sess->auth.pads.inner, sess->auth.pads.outer,
368                         xform->auth.key.data,
369                         xform->auth.key.length,
370                         get_auth_algo_blocksize(sess->auth.algo));
371         }
372
373         return 0;
374 }
375
376 /** Set session cipher parameters */
377 static int
378 aesni_mb_set_session_cipher_parameters(const MB_MGR *mb_mgr,
379                 struct aesni_mb_session *sess,
380                 const struct rte_crypto_sym_xform *xform)
381 {
382         uint8_t is_aes = 0;
383         uint8_t is_3DES = 0;
384
385         if (xform == NULL) {
386                 sess->cipher.mode = NULL_CIPHER;
387                 return 0;
388         }
389
390         if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
391                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
392                 return -EINVAL;
393         }
394
395         /* Select cipher direction */
396         switch (xform->cipher.op) {
397         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
398                 sess->cipher.direction = ENCRYPT;
399                 break;
400         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
401                 sess->cipher.direction = DECRYPT;
402                 break;
403         default:
404                 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
405                 return -EINVAL;
406         }
407
408         /* Select cipher mode */
409         switch (xform->cipher.algo) {
410         case RTE_CRYPTO_CIPHER_AES_CBC:
411                 sess->cipher.mode = CBC;
412                 is_aes = 1;
413                 break;
414         case RTE_CRYPTO_CIPHER_AES_CTR:
415                 sess->cipher.mode = CNTR;
416                 is_aes = 1;
417                 break;
418         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
419                 sess->cipher.mode = DOCSIS_SEC_BPI;
420                 is_aes = 1;
421                 break;
422         case RTE_CRYPTO_CIPHER_DES_CBC:
423                 sess->cipher.mode = DES;
424                 break;
425         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
426                 sess->cipher.mode = DOCSIS_DES;
427                 break;
428         case RTE_CRYPTO_CIPHER_3DES_CBC:
429                 sess->cipher.mode = DES3;
430                 is_3DES = 1;
431                 break;
432         default:
433                 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
434                 return -ENOTSUP;
435         }
436
437         /* Set IV parameters */
438         sess->iv.offset = xform->cipher.iv.offset;
439         sess->iv.length = xform->cipher.iv.length;
440
441         /* Check key length and choose key expansion function for AES */
442         if (is_aes) {
443                 switch (xform->cipher.key.length) {
444                 case AES_128_BYTES:
445                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
446                         IMB_AES_KEYEXP_128(mb_mgr, xform->cipher.key.data,
447                                         sess->cipher.expanded_aes_keys.encode,
448                                         sess->cipher.expanded_aes_keys.decode);
449                         break;
450                 case AES_192_BYTES:
451                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
452                         IMB_AES_KEYEXP_192(mb_mgr, xform->cipher.key.data,
453                                         sess->cipher.expanded_aes_keys.encode,
454                                         sess->cipher.expanded_aes_keys.decode);
455                         break;
456                 case AES_256_BYTES:
457                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
458                         IMB_AES_KEYEXP_256(mb_mgr, xform->cipher.key.data,
459                                         sess->cipher.expanded_aes_keys.encode,
460                                         sess->cipher.expanded_aes_keys.decode);
461                         break;
462                 default:
463                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
464                         return -EINVAL;
465                 }
466         } else if (is_3DES) {
467                 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
468                                 sess->cipher.exp_3des_keys.key[1],
469                                 sess->cipher.exp_3des_keys.key[2]};
470
471                 switch (xform->cipher.key.length) {
472                 case  24:
473                         IMB_DES_KEYSCHED(mb_mgr, keys[0],
474                                         xform->cipher.key.data);
475                         IMB_DES_KEYSCHED(mb_mgr, keys[1],
476                                         xform->cipher.key.data + 8);
477                         IMB_DES_KEYSCHED(mb_mgr, keys[2],
478                                         xform->cipher.key.data + 16);
479
480                         /* Initialize keys - 24 bytes: [K1-K2-K3] */
481                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
482                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
483                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
484                         break;
485                 case 16:
486                         IMB_DES_KEYSCHED(mb_mgr, keys[0],
487                                         xform->cipher.key.data);
488                         IMB_DES_KEYSCHED(mb_mgr, keys[1],
489                                         xform->cipher.key.data + 8);
490                         /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
491                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
492                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
493                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
494                         break;
495                 case 8:
496                         IMB_DES_KEYSCHED(mb_mgr, keys[0],
497                                         xform->cipher.key.data);
498
499                         /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
500                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
501                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
502                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
503                         break;
504                 default:
505                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
506                         return -EINVAL;
507                 }
508
509                 sess->cipher.key_length_in_bytes = 24;
510         } else {
511                 if (xform->cipher.key.length != 8) {
512                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
513                         return -EINVAL;
514                 }
515                 sess->cipher.key_length_in_bytes = 8;
516
517                 IMB_DES_KEYSCHED(mb_mgr,
518                         (uint64_t *)sess->cipher.expanded_aes_keys.encode,
519                                 xform->cipher.key.data);
520                 IMB_DES_KEYSCHED(mb_mgr,
521                         (uint64_t *)sess->cipher.expanded_aes_keys.decode,
522                                 xform->cipher.key.data);
523         }
524
525         return 0;
526 }
527
528 static int
529 aesni_mb_set_session_aead_parameters(const MB_MGR *mb_mgr,
530                 struct aesni_mb_session *sess,
531                 const struct rte_crypto_sym_xform *xform)
532 {
533         switch (xform->aead.op) {
534         case RTE_CRYPTO_AEAD_OP_ENCRYPT:
535                 sess->cipher.direction = ENCRYPT;
536                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
537                 break;
538         case RTE_CRYPTO_AEAD_OP_DECRYPT:
539                 sess->cipher.direction = DECRYPT;
540                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
541                 break;
542         default:
543                 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
544                 return -EINVAL;
545         }
546
547         switch (xform->aead.algo) {
548         case RTE_CRYPTO_AEAD_AES_CCM:
549                 sess->cipher.mode = CCM;
550                 sess->auth.algo = AES_CCM;
551
552                 /* Check key length and choose key expansion function for AES */
553                 switch (xform->aead.key.length) {
554                 case AES_128_BYTES:
555                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
556                         IMB_AES_KEYEXP_128(mb_mgr, xform->aead.key.data,
557                                         sess->cipher.expanded_aes_keys.encode,
558                                         sess->cipher.expanded_aes_keys.decode);
559                         break;
560                 default:
561                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
562                         return -EINVAL;
563                 }
564
565                 break;
566
567         case RTE_CRYPTO_AEAD_AES_GCM:
568                 sess->cipher.mode = GCM;
569                 sess->auth.algo = AES_GMAC;
570
571                 switch (xform->aead.key.length) {
572                 case AES_128_BYTES:
573                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
574                         IMB_AES128_GCM_PRE(mb_mgr, xform->aead.key.data,
575                                 &sess->cipher.gcm_key);
576                         break;
577                 case AES_192_BYTES:
578                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
579                         IMB_AES192_GCM_PRE(mb_mgr, xform->aead.key.data,
580                                 &sess->cipher.gcm_key);
581                         break;
582                 case AES_256_BYTES:
583                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
584                         IMB_AES256_GCM_PRE(mb_mgr, xform->aead.key.data,
585                                 &sess->cipher.gcm_key);
586                         break;
587                 default:
588                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
589                         return -EINVAL;
590                 }
591
592                 break;
593
594         default:
595                 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
596                 return -ENOTSUP;
597         }
598
599         /* Set IV parameters */
600         sess->iv.offset = xform->aead.iv.offset;
601         sess->iv.length = xform->aead.iv.length;
602
603         sess->auth.req_digest_len = xform->aead.digest_length;
604         /* CCM digests must be between 4 and 16 and an even number */
605         if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
606                         sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
607                         (sess->auth.req_digest_len & 1) == 1) {
608                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
609                 return -EINVAL;
610         }
611         sess->auth.gen_digest_len = sess->auth.req_digest_len;
612
613         return 0;
614 }
615
616 /** Parse crypto xform chain and set private session parameters */
617 int
618 aesni_mb_set_session_parameters(const MB_MGR *mb_mgr,
619                 struct aesni_mb_session *sess,
620                 const struct rte_crypto_sym_xform *xform)
621 {
622         const struct rte_crypto_sym_xform *auth_xform = NULL;
623         const struct rte_crypto_sym_xform *cipher_xform = NULL;
624         const struct rte_crypto_sym_xform *aead_xform = NULL;
625         int ret;
626
627         /* Select Crypto operation - hash then cipher / cipher then hash */
628         switch (aesni_mb_get_chain_order(xform)) {
629         case AESNI_MB_OP_HASH_CIPHER:
630                 sess->chain_order = HASH_CIPHER;
631                 auth_xform = xform;
632                 cipher_xform = xform->next;
633                 break;
634         case AESNI_MB_OP_CIPHER_HASH:
635                 sess->chain_order = CIPHER_HASH;
636                 auth_xform = xform->next;
637                 cipher_xform = xform;
638                 break;
639         case AESNI_MB_OP_HASH_ONLY:
640                 sess->chain_order = HASH_CIPHER;
641                 auth_xform = xform;
642                 cipher_xform = NULL;
643                 break;
644         case AESNI_MB_OP_CIPHER_ONLY:
645                 /*
646                  * Multi buffer library operates only at two modes,
647                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
648                  * chain order depends on cipher operation: encryption is always
649                  * the first operation and decryption the last one.
650                  */
651                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
652                         sess->chain_order = CIPHER_HASH;
653                 else
654                         sess->chain_order = HASH_CIPHER;
655                 auth_xform = NULL;
656                 cipher_xform = xform;
657                 break;
658         case AESNI_MB_OP_AEAD_CIPHER_HASH:
659                 sess->chain_order = CIPHER_HASH;
660                 sess->aead.aad_len = xform->aead.aad_length;
661                 aead_xform = xform;
662                 break;
663         case AESNI_MB_OP_AEAD_HASH_CIPHER:
664                 sess->chain_order = HASH_CIPHER;
665                 sess->aead.aad_len = xform->aead.aad_length;
666                 aead_xform = xform;
667                 break;
668         case AESNI_MB_OP_NOT_SUPPORTED:
669         default:
670                 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
671                 return -ENOTSUP;
672         }
673
674         /* Default IV length = 0 */
675         sess->iv.length = 0;
676
677         ret = aesni_mb_set_session_auth_parameters(mb_mgr, sess, auth_xform);
678         if (ret != 0) {
679                 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
680                 return ret;
681         }
682
683         ret = aesni_mb_set_session_cipher_parameters(mb_mgr, sess,
684                         cipher_xform);
685         if (ret != 0) {
686                 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
687                 return ret;
688         }
689
690         if (aead_xform) {
691                 ret = aesni_mb_set_session_aead_parameters(mb_mgr, sess,
692                                 aead_xform);
693                 if (ret != 0) {
694                         AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
695                         return ret;
696                 }
697         }
698
699         return 0;
700 }
701
702 /**
703  * burst enqueue, place crypto operations on ingress queue for processing.
704  *
705  * @param __qp         Queue Pair to process
706  * @param ops          Crypto operations for processing
707  * @param nb_ops       Number of crypto operations for processing
708  *
709  * @return
710  * - Number of crypto operations enqueued
711  */
712 static uint16_t
713 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
714                 uint16_t nb_ops)
715 {
716         struct aesni_mb_qp *qp = __qp;
717
718         unsigned int nb_enqueued;
719
720         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
721                         (void **)ops, nb_ops, NULL);
722
723         qp->stats.enqueued_count += nb_enqueued;
724
725         return nb_enqueued;
726 }
727
728 /** Get multi buffer session */
729 static inline struct aesni_mb_session *
730 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
731 {
732         struct aesni_mb_session *sess = NULL;
733
734         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
735                 if (likely(op->sym->session != NULL))
736                         sess = (struct aesni_mb_session *)
737                                         get_sym_session_private_data(
738                                         op->sym->session,
739                                         cryptodev_driver_id);
740         } else {
741                 void *_sess = rte_cryptodev_sym_session_create(qp->sess_mp);
742                 void *_sess_private_data = NULL;
743
744                 if (_sess == NULL)
745                         return NULL;
746
747                 if (rte_mempool_get(qp->sess_mp_priv,
748                                 (void **)&_sess_private_data))
749                         return NULL;
750
751                 sess = (struct aesni_mb_session *)_sess_private_data;
752
753                 if (unlikely(aesni_mb_set_session_parameters(qp->mb_mgr,
754                                 sess, op->sym->xform) != 0)) {
755                         rte_mempool_put(qp->sess_mp, _sess);
756                         rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
757                         sess = NULL;
758                 }
759                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
760                 set_sym_session_private_data(op->sym->session,
761                                 cryptodev_driver_id, _sess_private_data);
762         }
763
764         if (unlikely(sess == NULL))
765                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
766
767         return sess;
768 }
769
770 static inline uint64_t
771 auth_start_offset(struct rte_crypto_op *op, struct aesni_mb_session *session,
772                 uint32_t oop)
773 {
774         struct rte_mbuf *m_src, *m_dst;
775         uint8_t *p_src, *p_dst;
776         uintptr_t u_src, u_dst;
777         uint32_t cipher_end, auth_end;
778
779         /* Only cipher then hash needs special calculation. */
780         if (!oop || session->chain_order != CIPHER_HASH)
781                 return op->sym->auth.data.offset;
782
783         m_src = op->sym->m_src;
784         m_dst = op->sym->m_dst;
785
786         p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
787         p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
788         u_src = (uintptr_t)p_src;
789         u_dst = (uintptr_t)p_dst + op->sym->auth.data.offset;
790
791         /**
792          * Copy the content between cipher offset and auth offset for generating
793          * correct digest.
794          */
795         if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
796                 memcpy(p_dst + op->sym->auth.data.offset,
797                                 p_src + op->sym->auth.data.offset,
798                                 op->sym->cipher.data.offset -
799                                 op->sym->auth.data.offset);
800
801         /**
802          * Copy the content between (cipher offset + length) and (auth offset +
803          * length) for generating correct digest
804          */
805         cipher_end = op->sym->cipher.data.offset + op->sym->cipher.data.length;
806         auth_end = op->sym->auth.data.offset + op->sym->auth.data.length;
807         if (cipher_end < auth_end)
808                 memcpy(p_dst + cipher_end, p_src + cipher_end,
809                                 auth_end - cipher_end);
810
811         /**
812          * Since intel-ipsec-mb only supports positive values,
813          * we need to deduct the correct offset between src and dst.
814          */
815
816         return u_src < u_dst ? (u_dst - u_src) :
817                         (UINT64_MAX - u_src + u_dst + 1);
818 }
819
820 static inline void
821 set_cpu_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_session *session,
822                 union rte_crypto_sym_ofs sofs, void *buf, uint32_t len,
823                 void *iv, void *aad, void *digest, void *udata)
824 {
825         /* Set crypto operation */
826         job->chain_order = session->chain_order;
827
828         /* Set cipher parameters */
829         job->cipher_direction = session->cipher.direction;
830         job->cipher_mode = session->cipher.mode;
831
832         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
833
834         /* Set authentication parameters */
835         job->hash_alg = session->auth.algo;
836         job->iv = iv;
837
838         switch (job->hash_alg) {
839         case AES_XCBC:
840                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
841                 job->u.XCBC._k2 = session->auth.xcbc.k2;
842                 job->u.XCBC._k3 = session->auth.xcbc.k3;
843
844                 job->aes_enc_key_expanded =
845                                 session->cipher.expanded_aes_keys.encode;
846                 job->aes_dec_key_expanded =
847                                 session->cipher.expanded_aes_keys.decode;
848                 break;
849
850         case AES_CCM:
851                 job->u.CCM.aad = (uint8_t *)aad + 18;
852                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
853                 job->aes_enc_key_expanded =
854                                 session->cipher.expanded_aes_keys.encode;
855                 job->aes_dec_key_expanded =
856                                 session->cipher.expanded_aes_keys.decode;
857                 job->iv++;
858                 break;
859
860         case AES_CMAC:
861                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
862                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
863                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
864                 job->aes_enc_key_expanded =
865                                 session->cipher.expanded_aes_keys.encode;
866                 job->aes_dec_key_expanded =
867                                 session->cipher.expanded_aes_keys.decode;
868                 break;
869
870         case AES_GMAC:
871                 if (session->cipher.mode == GCM) {
872                         job->u.GCM.aad = aad;
873                         job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
874                 } else {
875                         /* For GMAC */
876                         job->u.GCM.aad = buf;
877                         job->u.GCM.aad_len_in_bytes = len;
878                         job->cipher_mode = GCM;
879                 }
880                 job->aes_enc_key_expanded = &session->cipher.gcm_key;
881                 job->aes_dec_key_expanded = &session->cipher.gcm_key;
882                 break;
883
884         default:
885                 job->u.HMAC._hashed_auth_key_xor_ipad =
886                                 session->auth.pads.inner;
887                 job->u.HMAC._hashed_auth_key_xor_opad =
888                                 session->auth.pads.outer;
889
890                 if (job->cipher_mode == DES3) {
891                         job->aes_enc_key_expanded =
892                                 session->cipher.exp_3des_keys.ks_ptr;
893                         job->aes_dec_key_expanded =
894                                 session->cipher.exp_3des_keys.ks_ptr;
895                 } else {
896                         job->aes_enc_key_expanded =
897                                 session->cipher.expanded_aes_keys.encode;
898                         job->aes_dec_key_expanded =
899                                 session->cipher.expanded_aes_keys.decode;
900                 }
901         }
902
903         /*
904          * Multi-buffer library current only support returning a truncated
905          * digest length as specified in the relevant IPsec RFCs
906          */
907
908         /* Set digest location and length */
909         job->auth_tag_output = digest;
910         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
911
912         /* Set IV parameters */
913         job->iv_len_in_bytes = session->iv.length;
914
915         /* Data Parameters */
916         job->src = buf;
917         job->dst = (uint8_t *)buf + sofs.ofs.cipher.head;
918         job->cipher_start_src_offset_in_bytes = sofs.ofs.cipher.head;
919         job->hash_start_src_offset_in_bytes = sofs.ofs.auth.head;
920         if (job->hash_alg == AES_GMAC && session->cipher.mode != GCM) {
921                 job->msg_len_to_hash_in_bytes = 0;
922                 job->msg_len_to_cipher_in_bytes = 0;
923         } else {
924                 job->msg_len_to_hash_in_bytes = len - sofs.ofs.auth.head -
925                         sofs.ofs.auth.tail;
926                 job->msg_len_to_cipher_in_bytes = len - sofs.ofs.cipher.head -
927                         sofs.ofs.cipher.tail;
928         }
929
930         job->user_data = udata;
931 }
932
933 /**
934  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
935  * submission to the multi buffer library for processing.
936  *
937  * @param       qp      queue pair
938  * @param       job     JOB_AES_HMAC structure to fill
939  * @param       m       mbuf to process
940  *
941  * @return
942  * - Completed JOB_AES_HMAC structure pointer on success
943  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
944  */
945 static inline int
946 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
947                 struct rte_crypto_op *op, uint8_t *digest_idx)
948 {
949         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
950         struct aesni_mb_session *session;
951         uint32_t m_offset, oop;
952
953         session = get_session(qp, op);
954         if (session == NULL) {
955                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
956                 return -1;
957         }
958
959         /* Set crypto operation */
960         job->chain_order = session->chain_order;
961
962         /* Set cipher parameters */
963         job->cipher_direction = session->cipher.direction;
964         job->cipher_mode = session->cipher.mode;
965
966         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
967
968         /* Set authentication parameters */
969         job->hash_alg = session->auth.algo;
970
971         switch (job->hash_alg) {
972         case AES_XCBC:
973                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
974                 job->u.XCBC._k2 = session->auth.xcbc.k2;
975                 job->u.XCBC._k3 = session->auth.xcbc.k3;
976
977                 job->aes_enc_key_expanded =
978                                 session->cipher.expanded_aes_keys.encode;
979                 job->aes_dec_key_expanded =
980                                 session->cipher.expanded_aes_keys.decode;
981                 break;
982
983         case AES_CCM:
984                 job->u.CCM.aad = op->sym->aead.aad.data + 18;
985                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
986                 job->aes_enc_key_expanded =
987                                 session->cipher.expanded_aes_keys.encode;
988                 job->aes_dec_key_expanded =
989                                 session->cipher.expanded_aes_keys.decode;
990                 break;
991
992         case AES_CMAC:
993                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
994                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
995                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
996                 job->aes_enc_key_expanded =
997                                 session->cipher.expanded_aes_keys.encode;
998                 job->aes_dec_key_expanded =
999                                 session->cipher.expanded_aes_keys.decode;
1000                 break;
1001
1002         case AES_GMAC:
1003                 if (session->cipher.mode == GCM) {
1004                         job->u.GCM.aad = op->sym->aead.aad.data;
1005                         job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1006                 } else {
1007                         /* For GMAC */
1008                         job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
1009                                         uint8_t *, op->sym->auth.data.offset);
1010                         job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
1011                         job->cipher_mode = GCM;
1012                 }
1013                 job->aes_enc_key_expanded = &session->cipher.gcm_key;
1014                 job->aes_dec_key_expanded = &session->cipher.gcm_key;
1015                 break;
1016
1017         default:
1018                 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
1019                 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
1020
1021                 if (job->cipher_mode == DES3) {
1022                         job->aes_enc_key_expanded =
1023                                 session->cipher.exp_3des_keys.ks_ptr;
1024                         job->aes_dec_key_expanded =
1025                                 session->cipher.exp_3des_keys.ks_ptr;
1026                 } else {
1027                         job->aes_enc_key_expanded =
1028                                 session->cipher.expanded_aes_keys.encode;
1029                         job->aes_dec_key_expanded =
1030                                 session->cipher.expanded_aes_keys.decode;
1031                 }
1032         }
1033
1034         if (!op->sym->m_dst) {
1035                 /* in-place operation */
1036                 m_dst = m_src;
1037                 oop = 0;
1038         } else if (op->sym->m_dst == op->sym->m_src) {
1039                 /* in-place operation */
1040                 m_dst = m_src;
1041                 oop = 0;
1042         } else {
1043                 /* out-of-place operation */
1044                 m_dst = op->sym->m_dst;
1045                 oop = 1;
1046         }
1047
1048         if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1049                         session->cipher.mode == GCM))
1050                 m_offset = op->sym->aead.data.offset;
1051         else
1052                 m_offset = op->sym->cipher.data.offset;
1053
1054         /* Set digest output location */
1055         if (job->hash_alg != NULL_HASH &&
1056                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1057                 job->auth_tag_output = qp->temp_digests[*digest_idx];
1058                 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1059         } else {
1060                 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1061                                 session->cipher.mode == GCM))
1062                         job->auth_tag_output = op->sym->aead.digest.data;
1063                 else
1064                         job->auth_tag_output = op->sym->auth.digest.data;
1065
1066                 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
1067                         job->auth_tag_output = qp->temp_digests[*digest_idx];
1068                         *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1069                 }
1070         }
1071         /*
1072          * Multi-buffer library current only support returning a truncated
1073          * digest length as specified in the relevant IPsec RFCs
1074          */
1075
1076         /* Set digest length */
1077         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1078
1079         /* Set IV parameters */
1080         job->iv_len_in_bytes = session->iv.length;
1081
1082         /* Data Parameters */
1083         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1084         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
1085
1086         switch (job->hash_alg) {
1087         case AES_CCM:
1088                 job->cipher_start_src_offset_in_bytes =
1089                                 op->sym->aead.data.offset;
1090                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1091                 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
1092                 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
1093
1094                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1095                         session->iv.offset + 1);
1096                 break;
1097
1098         case AES_GMAC:
1099                 if (session->cipher.mode == GCM) {
1100                         job->cipher_start_src_offset_in_bytes =
1101                                         op->sym->aead.data.offset;
1102                         job->hash_start_src_offset_in_bytes =
1103                                         op->sym->aead.data.offset;
1104                         job->msg_len_to_cipher_in_bytes =
1105                                         op->sym->aead.data.length;
1106                         job->msg_len_to_hash_in_bytes =
1107                                         op->sym->aead.data.length;
1108                 } else {
1109                         job->cipher_start_src_offset_in_bytes =
1110                                         op->sym->auth.data.offset;
1111                         job->hash_start_src_offset_in_bytes =
1112                                         op->sym->auth.data.offset;
1113                         job->msg_len_to_cipher_in_bytes = 0;
1114                         job->msg_len_to_hash_in_bytes = 0;
1115                 }
1116
1117                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1118                                 session->iv.offset);
1119                 break;
1120
1121         default:
1122                 job->cipher_start_src_offset_in_bytes =
1123                                 op->sym->cipher.data.offset;
1124                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
1125
1126                 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1127                                 session, oop);
1128                 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
1129
1130                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1131                         session->iv.offset);
1132         }
1133
1134         /* Set user data to be crypto operation data struct */
1135         job->user_data = op;
1136
1137         return 0;
1138 }
1139
1140 static inline void
1141 verify_digest(JOB_AES_HMAC *job, void *digest, uint16_t len, uint8_t *status)
1142 {
1143         /* Verify digest if required */
1144         if (memcmp(job->auth_tag_output, digest, len) != 0)
1145                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1146 }
1147
1148 static inline void
1149 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
1150                 struct aesni_mb_session *sess)
1151 {
1152         /* No extra copy needed */
1153         if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
1154                 return;
1155
1156         /*
1157          * This can only happen for HMAC, so only digest
1158          * for authentication algos is required
1159          */
1160         memcpy(op->sym->auth.digest.data, job->auth_tag_output,
1161                         sess->auth.req_digest_len);
1162 }
1163
1164 /**
1165  * Process a completed job and return rte_mbuf which job processed
1166  *
1167  * @param qp            Queue Pair to process
1168  * @param job   JOB_AES_HMAC job to process
1169  *
1170  * @return
1171  * - Returns processed crypto operation.
1172  * - Returns NULL on invalid job
1173  */
1174 static inline struct rte_crypto_op *
1175 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
1176 {
1177         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
1178         struct aesni_mb_session *sess = get_sym_session_private_data(
1179                                                         op->sym->session,
1180                                                         cryptodev_driver_id);
1181
1182         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
1183                 switch (job->status) {
1184                 case STS_COMPLETED:
1185                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1186
1187                         if (job->hash_alg == NULL_HASH)
1188                                 break;
1189
1190                         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1191                                 if (job->hash_alg == AES_CCM ||
1192                                         (job->hash_alg == AES_GMAC &&
1193                                                 sess->cipher.mode == GCM))
1194                                         verify_digest(job,
1195                                                 op->sym->aead.digest.data,
1196                                                 sess->auth.req_digest_len,
1197                                                 &op->status);
1198                                 else
1199                                         verify_digest(job,
1200                                                 op->sym->auth.digest.data,
1201                                                 sess->auth.req_digest_len,
1202                                                 &op->status);
1203                         } else
1204                                 generate_digest(job, op, sess);
1205                         break;
1206                 default:
1207                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1208                 }
1209         }
1210
1211         /* Free session if a session-less crypto op */
1212         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1213                 memset(sess, 0, sizeof(struct aesni_mb_session));
1214                 memset(op->sym->session, 0,
1215                         rte_cryptodev_sym_get_existing_header_session_size(
1216                                 op->sym->session));
1217                 rte_mempool_put(qp->sess_mp_priv, sess);
1218                 rte_mempool_put(qp->sess_mp, op->sym->session);
1219                 op->sym->session = NULL;
1220         }
1221
1222         return op;
1223 }
1224
1225 static inline void
1226 post_process_mb_sync_job(JOB_AES_HMAC *job)
1227 {
1228         uint32_t *st;
1229
1230         st = job->user_data;
1231         st[0] = (job->status == STS_COMPLETED) ? 0 : EBADMSG;
1232 }
1233
1234 /**
1235  * Process a completed JOB_AES_HMAC job and keep processing jobs until
1236  * get_completed_job return NULL
1237  *
1238  * @param qp            Queue Pair to process
1239  * @param job           JOB_AES_HMAC job
1240  *
1241  * @return
1242  * - Number of processed jobs
1243  */
1244 static unsigned
1245 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
1246                 struct rte_crypto_op **ops, uint16_t nb_ops)
1247 {
1248         struct rte_crypto_op *op = NULL;
1249         unsigned processed_jobs = 0;
1250
1251         while (job != NULL) {
1252                 op = post_process_mb_job(qp, job);
1253
1254                 if (op) {
1255                         ops[processed_jobs++] = op;
1256                         qp->stats.dequeued_count++;
1257                 } else {
1258                         qp->stats.dequeue_err_count++;
1259                         break;
1260                 }
1261                 if (processed_jobs == nb_ops)
1262                         break;
1263
1264                 job = IMB_GET_COMPLETED_JOB(qp->mb_mgr);
1265         }
1266
1267         return processed_jobs;
1268 }
1269
1270 static inline uint32_t
1271 handle_completed_sync_jobs(JOB_AES_HMAC *job, MB_MGR *mb_mgr)
1272 {
1273         uint32_t i;
1274
1275         for (i = 0; job != NULL; i++, job = IMB_GET_COMPLETED_JOB(mb_mgr))
1276                 post_process_mb_sync_job(job);
1277
1278         return i;
1279 }
1280
1281 static inline uint32_t
1282 flush_mb_sync_mgr(MB_MGR *mb_mgr)
1283 {
1284         JOB_AES_HMAC *job;
1285
1286         job = IMB_FLUSH_JOB(mb_mgr);
1287         return handle_completed_sync_jobs(job, mb_mgr);
1288 }
1289
1290 static inline uint16_t
1291 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
1292                 uint16_t nb_ops)
1293 {
1294         int processed_ops = 0;
1295
1296         /* Flush the remaining jobs */
1297         JOB_AES_HMAC *job = IMB_FLUSH_JOB(qp->mb_mgr);
1298
1299         if (job)
1300                 processed_ops += handle_completed_jobs(qp, job,
1301                                 &ops[processed_ops], nb_ops - processed_ops);
1302
1303         return processed_ops;
1304 }
1305
1306 static inline JOB_AES_HMAC *
1307 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
1308 {
1309         job->chain_order = HASH_CIPHER;
1310         job->cipher_mode = NULL_CIPHER;
1311         job->hash_alg = NULL_HASH;
1312         job->cipher_direction = DECRYPT;
1313
1314         /* Set user data to be crypto operation data struct */
1315         job->user_data = op;
1316
1317         return job;
1318 }
1319
1320 static uint16_t
1321 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1322                 uint16_t nb_ops)
1323 {
1324         struct aesni_mb_qp *qp = queue_pair;
1325
1326         struct rte_crypto_op *op;
1327         JOB_AES_HMAC *job;
1328
1329         int retval, processed_jobs = 0;
1330
1331         if (unlikely(nb_ops == 0))
1332                 return 0;
1333
1334         uint8_t digest_idx = qp->digest_idx;
1335         do {
1336                 /* Get next free mb job struct from mb manager */
1337                 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1338                 if (unlikely(job == NULL)) {
1339                         /* if no free mb job structs we need to flush mb_mgr */
1340                         processed_jobs += flush_mb_mgr(qp,
1341                                         &ops[processed_jobs],
1342                                         nb_ops - processed_jobs);
1343
1344                         if (nb_ops == processed_jobs)
1345                                 break;
1346
1347                         job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1348                 }
1349
1350                 /*
1351                  * Get next operation to process from ingress queue.
1352                  * There is no need to return the job to the MB_MGR
1353                  * if there are no more operations to process, since the MB_MGR
1354                  * can use that pointer again in next get_next calls.
1355                  */
1356                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1357                 if (retval < 0)
1358                         break;
1359
1360                 retval = set_mb_job_params(job, qp, op, &digest_idx);
1361                 if (unlikely(retval != 0)) {
1362                         qp->stats.dequeue_err_count++;
1363                         set_job_null_op(job, op);
1364                 }
1365
1366                 /* Submit job to multi-buffer for processing */
1367 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1368                 job = IMB_SUBMIT_JOB(qp->mb_mgr);
1369 #else
1370                 job = IMB_SUBMIT_JOB_NOCHECK(qp->mb_mgr);
1371 #endif
1372                 /*
1373                  * If submit returns a processed job then handle it,
1374                  * before submitting subsequent jobs
1375                  */
1376                 if (job)
1377                         processed_jobs += handle_completed_jobs(qp, job,
1378                                         &ops[processed_jobs],
1379                                         nb_ops - processed_jobs);
1380
1381         } while (processed_jobs < nb_ops);
1382
1383         qp->digest_idx = digest_idx;
1384
1385         if (processed_jobs < 1)
1386                 processed_jobs += flush_mb_mgr(qp,
1387                                 &ops[processed_jobs],
1388                                 nb_ops - processed_jobs);
1389
1390         return processed_jobs;
1391 }
1392
1393 static MB_MGR *
1394 alloc_init_mb_mgr(enum aesni_mb_vector_mode vector_mode)
1395 {
1396         MB_MGR *mb_mgr = alloc_mb_mgr(0);
1397         if (mb_mgr == NULL)
1398                 return NULL;
1399
1400         switch (vector_mode) {
1401         case RTE_AESNI_MB_SSE:
1402                 init_mb_mgr_sse(mb_mgr);
1403                 break;
1404         case RTE_AESNI_MB_AVX:
1405                 init_mb_mgr_avx(mb_mgr);
1406                 break;
1407         case RTE_AESNI_MB_AVX2:
1408                 init_mb_mgr_avx2(mb_mgr);
1409                 break;
1410         case RTE_AESNI_MB_AVX512:
1411                 init_mb_mgr_avx512(mb_mgr);
1412                 break;
1413         default:
1414                 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", vector_mode);
1415                 free_mb_mgr(mb_mgr);
1416                 return NULL;
1417         }
1418
1419         return mb_mgr;
1420 }
1421
1422 static inline void
1423 aesni_mb_fill_error_code(struct rte_crypto_sym_vec *vec, int32_t err)
1424 {
1425         uint32_t i;
1426
1427         for (i = 0; i != vec->num; ++i)
1428                 vec->status[i] = err;
1429 }
1430
1431 static inline int
1432 check_crypto_sgl(union rte_crypto_sym_ofs so, const struct rte_crypto_sgl *sgl)
1433 {
1434         /* no multi-seg support with current AESNI-MB PMD */
1435         if (sgl->num != 1)
1436                 return ENOTSUP;
1437         else if (so.ofs.cipher.head + so.ofs.cipher.tail > sgl->vec[0].len)
1438                 return EINVAL;
1439         return 0;
1440 }
1441
1442 static inline JOB_AES_HMAC *
1443 submit_sync_job(MB_MGR *mb_mgr)
1444 {
1445 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1446         return IMB_SUBMIT_JOB(mb_mgr);
1447 #else
1448         return IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1449 #endif
1450 }
1451
1452 static inline uint32_t
1453 generate_sync_dgst(struct rte_crypto_sym_vec *vec,
1454         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1455 {
1456         uint32_t i, k;
1457
1458         for (i = 0, k = 0; i != vec->num; i++) {
1459                 if (vec->status[i] == 0) {
1460                         memcpy(vec->digest[i], dgst[i], len);
1461                         k++;
1462                 }
1463         }
1464
1465         return k;
1466 }
1467
1468 static inline uint32_t
1469 verify_sync_dgst(struct rte_crypto_sym_vec *vec,
1470         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1471 {
1472         uint32_t i, k;
1473
1474         for (i = 0, k = 0; i != vec->num; i++) {
1475                 if (vec->status[i] == 0) {
1476                         if (memcmp(vec->digest[i], dgst[i], len) != 0)
1477                                 vec->status[i] = EBADMSG;
1478                         else
1479                                 k++;
1480                 }
1481         }
1482
1483         return k;
1484 }
1485
1486 uint32_t
1487 aesni_mb_cpu_crypto_process_bulk(struct rte_cryptodev *dev,
1488         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs sofs,
1489         struct rte_crypto_sym_vec *vec)
1490 {
1491         int32_t ret;
1492         uint32_t i, j, k, len;
1493         void *buf;
1494         JOB_AES_HMAC *job;
1495         MB_MGR *mb_mgr;
1496         struct aesni_mb_private *priv;
1497         struct aesni_mb_session *s;
1498         uint8_t tmp_dgst[vec->num][DIGEST_LENGTH_MAX];
1499
1500         s = get_sym_session_private_data(sess, dev->driver_id);
1501         if (s == NULL) {
1502                 aesni_mb_fill_error_code(vec, EINVAL);
1503                 return 0;
1504         }
1505
1506         /* get per-thread MB MGR, create one if needed */
1507         mb_mgr = RTE_PER_LCORE(sync_mb_mgr);
1508         if (mb_mgr == NULL) {
1509
1510                 priv = dev->data->dev_private;
1511                 mb_mgr = alloc_init_mb_mgr(priv->vector_mode);
1512                 if (mb_mgr == NULL) {
1513                         aesni_mb_fill_error_code(vec, ENOMEM);
1514                         return 0;
1515                 }
1516                 RTE_PER_LCORE(sync_mb_mgr) = mb_mgr;
1517         }
1518
1519         for (i = 0, j = 0, k = 0; i != vec->num; i++) {
1520
1521
1522                 ret = check_crypto_sgl(sofs, vec->sgl + i);
1523                 if (ret != 0) {
1524                         vec->status[i] = ret;
1525                         continue;
1526                 }
1527
1528                 buf = vec->sgl[i].vec[0].base;
1529                 len = vec->sgl[i].vec[0].len;
1530
1531                 job = IMB_GET_NEXT_JOB(mb_mgr);
1532                 if (job == NULL) {
1533                         k += flush_mb_sync_mgr(mb_mgr);
1534                         job = IMB_GET_NEXT_JOB(mb_mgr);
1535                         RTE_ASSERT(job != NULL);
1536                 }
1537
1538                 /* Submit job for processing */
1539                 set_cpu_mb_job_params(job, s, sofs, buf, len,
1540                         vec->iv[i], vec->aad[i], tmp_dgst[i],
1541                         &vec->status[i]);
1542                 job = submit_sync_job(mb_mgr);
1543                 j++;
1544
1545                 /* handle completed jobs */
1546                 k += handle_completed_sync_jobs(job, mb_mgr);
1547         }
1548
1549         /* flush remaining jobs */
1550         while (k != j)
1551                 k += flush_mb_sync_mgr(mb_mgr);
1552
1553         /* finish processing for successful jobs: check/update digest */
1554         if (k != 0) {
1555                 if (s->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1556                         k = verify_sync_dgst(vec,
1557                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1558                                 s->auth.req_digest_len);
1559                 else
1560                         k = generate_sync_dgst(vec,
1561                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1562                                 s->auth.req_digest_len);
1563         }
1564
1565         return k;
1566 }
1567
1568 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1569
1570 static uint64_t
1571 vec_mode_to_flags(enum aesni_mb_vector_mode mode)
1572 {
1573         switch (mode) {
1574         case RTE_AESNI_MB_SSE:
1575                 return RTE_CRYPTODEV_FF_CPU_SSE;
1576         case RTE_AESNI_MB_AVX:
1577                 return RTE_CRYPTODEV_FF_CPU_AVX;
1578         case RTE_AESNI_MB_AVX2:
1579                 return RTE_CRYPTODEV_FF_CPU_AVX2;
1580         case RTE_AESNI_MB_AVX512:
1581                 return RTE_CRYPTODEV_FF_CPU_AVX512;
1582         default:
1583                 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", mode);
1584                 return 0;
1585         }
1586 }
1587
1588 static int
1589 cryptodev_aesni_mb_create(const char *name,
1590                         struct rte_vdev_device *vdev,
1591                         struct rte_cryptodev_pmd_init_params *init_params)
1592 {
1593         struct rte_cryptodev *dev;
1594         struct aesni_mb_private *internals;
1595         enum aesni_mb_vector_mode vector_mode;
1596         MB_MGR *mb_mgr;
1597
1598         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1599         if (dev == NULL) {
1600                 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1601                 return -ENODEV;
1602         }
1603
1604         /* Check CPU for supported vector instruction set */
1605         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1606                 vector_mode = RTE_AESNI_MB_AVX512;
1607         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1608                 vector_mode = RTE_AESNI_MB_AVX2;
1609         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1610                 vector_mode = RTE_AESNI_MB_AVX;
1611         else
1612                 vector_mode = RTE_AESNI_MB_SSE;
1613
1614         dev->driver_id = cryptodev_driver_id;
1615         dev->dev_ops = rte_aesni_mb_pmd_ops;
1616
1617         /* register rx/tx burst functions for data path */
1618         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1619         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1620
1621         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1622                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1623                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1624                         RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO;
1625
1626         /* Check CPU for support for AES instruction set */
1627         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES))
1628                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AESNI;
1629         else
1630                 AESNI_MB_LOG(WARNING, "AES instructions not supported by CPU");
1631
1632         dev->feature_flags |= vec_mode_to_flags(vector_mode);
1633
1634         mb_mgr = alloc_init_mb_mgr(vector_mode);
1635         if (mb_mgr == NULL) {
1636                 rte_cryptodev_pmd_destroy(dev);
1637                 return -ENOMEM;
1638         }
1639
1640         /* Set vector instructions mode supported */
1641         internals = dev->data->dev_private;
1642
1643         internals->vector_mode = vector_mode;
1644         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1645         internals->mb_mgr = mb_mgr;
1646
1647         AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1648                         imb_get_version_str());
1649         return 0;
1650 }
1651
1652 static int
1653 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1654 {
1655         struct rte_cryptodev_pmd_init_params init_params = {
1656                 "",
1657                 sizeof(struct aesni_mb_private),
1658                 rte_socket_id(),
1659                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1660         };
1661         const char *name, *args;
1662         int retval;
1663
1664         name = rte_vdev_device_name(vdev);
1665         if (name == NULL)
1666                 return -EINVAL;
1667
1668         args = rte_vdev_device_args(vdev);
1669
1670         retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1671         if (retval) {
1672                 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1673                                 args);
1674                 return -EINVAL;
1675         }
1676
1677         return cryptodev_aesni_mb_create(name, vdev, &init_params);
1678 }
1679
1680 static int
1681 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1682 {
1683         struct rte_cryptodev *cryptodev;
1684         struct aesni_mb_private *internals;
1685         const char *name;
1686
1687         name = rte_vdev_device_name(vdev);
1688         if (name == NULL)
1689                 return -EINVAL;
1690
1691         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1692         if (cryptodev == NULL)
1693                 return -ENODEV;
1694
1695         internals = cryptodev->data->dev_private;
1696
1697         free_mb_mgr(internals->mb_mgr);
1698         if (RTE_PER_LCORE(sync_mb_mgr)) {
1699                 free_mb_mgr(RTE_PER_LCORE(sync_mb_mgr));
1700                 RTE_PER_LCORE(sync_mb_mgr) = NULL;
1701         }
1702
1703         return rte_cryptodev_pmd_destroy(cryptodev);
1704 }
1705
1706 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
1707         .probe = cryptodev_aesni_mb_probe,
1708         .remove = cryptodev_aesni_mb_remove
1709 };
1710
1711 static struct cryptodev_driver aesni_mb_crypto_drv;
1712
1713 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
1714 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
1715 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
1716         "max_nb_queue_pairs=<int> "
1717         "socket_id=<int>");
1718 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
1719                 cryptodev_aesni_mb_pmd_drv.driver,
1720                 cryptodev_driver_id);
1721
1722 RTE_INIT(aesni_mb_init_log)
1723 {
1724         aesni_mb_logtype_driver = rte_log_register("pmd.crypto.aesni_mb");
1725 }