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