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