net/mlx5: share counter config function
[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, const uint32_t auth_offset,
934                 const uint32_t cipher_offset, const uint32_t auth_length,
935                 const uint32_t cipher_length)
936 {
937         struct rte_mbuf *m_src, *m_dst;
938         uint8_t *p_src, *p_dst;
939         uintptr_t u_src, u_dst;
940         uint32_t cipher_end, auth_end;
941
942         /* Only cipher then hash needs special calculation. */
943         if (!oop || session->chain_order != IMB_ORDER_CIPHER_HASH)
944                 return auth_offset;
945
946         m_src = op->sym->m_src;
947         m_dst = op->sym->m_dst;
948
949         p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
950         p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
951         u_src = (uintptr_t)p_src;
952         u_dst = (uintptr_t)p_dst + auth_offset;
953
954         /**
955          * Copy the content between cipher offset and auth offset for generating
956          * correct digest.
957          */
958         if (cipher_offset > auth_offset)
959                 memcpy(p_dst + auth_offset,
960                                 p_src + auth_offset,
961                                 cipher_offset -
962                                 auth_offset);
963
964         /**
965          * Copy the content between (cipher offset + length) and (auth offset +
966          * length) for generating correct digest
967          */
968         cipher_end = cipher_offset + cipher_length;
969         auth_end = auth_offset + auth_length;
970         if (cipher_end < auth_end)
971                 memcpy(p_dst + cipher_end, p_src + cipher_end,
972                                 auth_end - cipher_end);
973
974         /**
975          * Since intel-ipsec-mb only supports positive values,
976          * we need to deduct the correct offset between src and dst.
977          */
978
979         return u_src < u_dst ? (u_dst - u_src) :
980                         (UINT64_MAX - u_src + u_dst + 1);
981 }
982
983 static inline void
984 set_cpu_mb_job_params(IMB_JOB *job, struct aesni_mb_session *session,
985                 union rte_crypto_sym_ofs sofs, void *buf, uint32_t len,
986                 struct rte_crypto_va_iova_ptr *iv,
987                 struct rte_crypto_va_iova_ptr *aad, void *digest, void *udata)
988 {
989         /* Set crypto operation */
990         job->chain_order = session->chain_order;
991
992         /* Set cipher parameters */
993         job->cipher_direction = session->cipher.direction;
994         job->cipher_mode = session->cipher.mode;
995
996         job->key_len_in_bytes = session->cipher.key_length_in_bytes;
997
998         /* Set authentication parameters */
999         job->hash_alg = session->auth.algo;
1000         job->iv = iv->va;
1001
1002         switch (job->hash_alg) {
1003         case IMB_AUTH_AES_XCBC:
1004                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
1005                 job->u.XCBC._k2 = session->auth.xcbc.k2;
1006                 job->u.XCBC._k3 = session->auth.xcbc.k3;
1007
1008                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1009                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1010                 break;
1011
1012         case IMB_AUTH_AES_CCM:
1013                 job->u.CCM.aad = (uint8_t *)aad->va + 18;
1014                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
1015                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1016                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1017                 job->iv++;
1018                 break;
1019
1020         case IMB_AUTH_AES_CMAC:
1021                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
1022                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
1023                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
1024                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1025                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1026                 break;
1027
1028         case IMB_AUTH_AES_GMAC:
1029                 if (session->cipher.mode == IMB_CIPHER_GCM) {
1030                         job->u.GCM.aad = aad->va;
1031                         job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1032                 } else {
1033                         /* For GMAC */
1034                         job->u.GCM.aad = buf;
1035                         job->u.GCM.aad_len_in_bytes = len;
1036                         job->cipher_mode = IMB_CIPHER_GCM;
1037                 }
1038                 job->enc_keys = &session->cipher.gcm_key;
1039                 job->dec_keys = &session->cipher.gcm_key;
1040                 break;
1041
1042         case IMB_AUTH_CHACHA20_POLY1305:
1043                 job->u.CHACHA20_POLY1305.aad = aad->va;
1044                 job->u.CHACHA20_POLY1305.aad_len_in_bytes =
1045                         session->aead.aad_len;
1046                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1047                 job->dec_keys = session->cipher.expanded_aes_keys.encode;
1048                 break;
1049         default:
1050                 job->u.HMAC._hashed_auth_key_xor_ipad =
1051                                 session->auth.pads.inner;
1052                 job->u.HMAC._hashed_auth_key_xor_opad =
1053                                 session->auth.pads.outer;
1054
1055                 if (job->cipher_mode == IMB_CIPHER_DES3) {
1056                         job->enc_keys = session->cipher.exp_3des_keys.ks_ptr;
1057                         job->dec_keys = session->cipher.exp_3des_keys.ks_ptr;
1058                 } else {
1059                         job->enc_keys = session->cipher.expanded_aes_keys.encode;
1060                         job->dec_keys = session->cipher.expanded_aes_keys.decode;
1061                 }
1062         }
1063
1064         /*
1065          * Multi-buffer library current only support returning a truncated
1066          * digest length as specified in the relevant IPsec RFCs
1067          */
1068
1069         /* Set digest location and length */
1070         job->auth_tag_output = digest;
1071         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1072
1073         /* Set IV parameters */
1074         job->iv_len_in_bytes = session->iv.length;
1075
1076         /* Data Parameters */
1077         job->src = buf;
1078         job->dst = (uint8_t *)buf + sofs.ofs.cipher.head;
1079         job->cipher_start_src_offset_in_bytes = sofs.ofs.cipher.head;
1080         job->hash_start_src_offset_in_bytes = sofs.ofs.auth.head;
1081         if (job->hash_alg == IMB_AUTH_AES_GMAC &&
1082                         session->cipher.mode != IMB_CIPHER_GCM) {
1083                 job->msg_len_to_hash_in_bytes = 0;
1084                 job->msg_len_to_cipher_in_bytes = 0;
1085         } else {
1086                 job->msg_len_to_hash_in_bytes = len - sofs.ofs.auth.head -
1087                         sofs.ofs.auth.tail;
1088                 job->msg_len_to_cipher_in_bytes = len - sofs.ofs.cipher.head -
1089                         sofs.ofs.cipher.tail;
1090         }
1091
1092         job->user_data = udata;
1093 }
1094
1095 /**
1096  * Process a crypto operation and complete a IMB_JOB job structure for
1097  * submission to the multi buffer library for processing.
1098  *
1099  * @param       qp              queue pair
1100  * @param       job             IMB_JOB structure to fill
1101  * @param       op              crypto op to process
1102  * @param       digest_idx      ID for digest to use
1103  *
1104  * @return
1105  * - 0 on success, the IMB_JOB will be filled
1106  * - -1 if invalid session, IMB_JOB will not be filled
1107  */
1108 static inline int
1109 set_mb_job_params(IMB_JOB *job, struct ipsec_mb_qp *qp,
1110                 struct rte_crypto_op *op, uint8_t *digest_idx)
1111 {
1112         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
1113         struct aesni_mb_qp_data *qp_data = ipsec_mb_get_qp_private_data(qp);
1114         struct aesni_mb_session *session;
1115         uint32_t m_offset, oop;
1116         uint32_t auth_off_in_bytes;
1117         uint32_t ciph_off_in_bytes;
1118         uint32_t auth_len_in_bytes;
1119         uint32_t ciph_len_in_bytes;
1120
1121         session = ipsec_mb_get_session_private(qp, op);
1122         if (session == NULL) {
1123                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1124                 return -1;
1125         }
1126
1127         /* Set crypto operation */
1128         job->chain_order = session->chain_order;
1129
1130         /* Set cipher parameters */
1131         job->cipher_direction = session->cipher.direction;
1132         job->cipher_mode = session->cipher.mode;
1133
1134         job->key_len_in_bytes = session->cipher.key_length_in_bytes;
1135
1136         /* Set authentication parameters */
1137         job->hash_alg = session->auth.algo;
1138
1139         const int aead = is_aead_algo(job->hash_alg, job->cipher_mode);
1140
1141         if (job->cipher_mode == IMB_CIPHER_DES3) {
1142                 job->enc_keys = session->cipher.exp_3des_keys.ks_ptr;
1143                 job->dec_keys = session->cipher.exp_3des_keys.ks_ptr;
1144         } else {
1145                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1146                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1147         }
1148
1149         switch (job->hash_alg) {
1150         case IMB_AUTH_AES_XCBC:
1151                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
1152                 job->u.XCBC._k2 = session->auth.xcbc.k2;
1153                 job->u.XCBC._k3 = session->auth.xcbc.k3;
1154
1155                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1156                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1157                 break;
1158
1159         case IMB_AUTH_AES_CCM:
1160                 job->u.CCM.aad = op->sym->aead.aad.data + 18;
1161                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
1162                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1163                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1164                 break;
1165
1166         case IMB_AUTH_AES_CMAC:
1167                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
1168                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
1169                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
1170                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1171                 job->dec_keys = session->cipher.expanded_aes_keys.decode;
1172                 break;
1173
1174         case IMB_AUTH_AES_GMAC:
1175                 if (session->cipher.mode == IMB_CIPHER_GCM) {
1176                         job->u.GCM.aad = op->sym->aead.aad.data;
1177                         job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1178                 } else {
1179                         /* For GMAC */
1180                         job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
1181                                         uint8_t *, op->sym->auth.data.offset);
1182                         job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
1183                         job->cipher_mode = IMB_CIPHER_GCM;
1184                 }
1185                 job->enc_keys = &session->cipher.gcm_key;
1186                 job->dec_keys = &session->cipher.gcm_key;
1187                 break;
1188         case IMB_AUTH_ZUC_EIA3_BITLEN:
1189         case IMB_AUTH_ZUC256_EIA3_BITLEN:
1190                 job->u.ZUC_EIA3._key = session->auth.zuc_auth_key;
1191                 job->u.ZUC_EIA3._iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1192                                                 session->auth_iv.offset);
1193                 break;
1194         case IMB_AUTH_SNOW3G_UIA2_BITLEN:
1195                 job->u.SNOW3G_UIA2._key = (void *)
1196                         &session->auth.pKeySched_snow3g_auth;
1197                 job->u.SNOW3G_UIA2._iv =
1198                         rte_crypto_op_ctod_offset(op, uint8_t *,
1199                                                 session->auth_iv.offset);
1200                 break;
1201         case IMB_AUTH_KASUMI_UIA1:
1202                 job->u.KASUMI_UIA1._key = (void *)
1203                         &session->auth.pKeySched_kasumi_auth;
1204                 break;
1205         case IMB_AUTH_CHACHA20_POLY1305:
1206                 job->u.CHACHA20_POLY1305.aad = op->sym->aead.aad.data;
1207                 job->u.CHACHA20_POLY1305.aad_len_in_bytes =
1208                         session->aead.aad_len;
1209                 job->enc_keys = session->cipher.expanded_aes_keys.encode;
1210                 job->dec_keys = session->cipher.expanded_aes_keys.encode;
1211                 break;
1212         default:
1213                 job->u.HMAC._hashed_auth_key_xor_ipad =
1214                         session->auth.pads.inner;
1215                 job->u.HMAC._hashed_auth_key_xor_opad =
1216                         session->auth.pads.outer;
1217
1218         }
1219
1220         if (aead)
1221                 m_offset = op->sym->aead.data.offset;
1222         else
1223                 m_offset = op->sym->cipher.data.offset;
1224
1225         if (job->cipher_mode == IMB_CIPHER_ZUC_EEA3) {
1226                 job->enc_keys = session->cipher.zuc_cipher_key;
1227                 job->dec_keys = session->cipher.zuc_cipher_key;
1228                 m_offset >>= 3;
1229         } else if (job->cipher_mode == IMB_CIPHER_SNOW3G_UEA2_BITLEN) {
1230                 job->enc_keys = &session->cipher.pKeySched_snow3g_cipher;
1231                 m_offset = 0;
1232         } else if (job->cipher_mode == IMB_CIPHER_KASUMI_UEA1_BITLEN) {
1233                 job->enc_keys = &session->cipher.pKeySched_kasumi_cipher;
1234                 m_offset = 0;
1235         }
1236
1237         if (!op->sym->m_dst) {
1238                 /* in-place operation */
1239                 m_dst = m_src;
1240                 oop = 0;
1241         } else if (op->sym->m_dst == op->sym->m_src) {
1242                 /* in-place operation */
1243                 m_dst = m_src;
1244                 oop = 0;
1245         } else {
1246                 /* out-of-place operation */
1247                 m_dst = op->sym->m_dst;
1248                 oop = 1;
1249         }
1250
1251         /* Set digest output location */
1252         if (job->hash_alg != IMB_AUTH_NULL &&
1253                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1254                 job->auth_tag_output = qp_data->temp_digests[*digest_idx];
1255                 *digest_idx = (*digest_idx + 1) % IMB_MAX_JOBS;
1256         } else {
1257                 if (aead)
1258                         job->auth_tag_output = op->sym->aead.digest.data;
1259                 else
1260                         job->auth_tag_output = op->sym->auth.digest.data;
1261
1262                 if (session->auth.req_digest_len !=
1263                                 session->auth.gen_digest_len) {
1264                         job->auth_tag_output =
1265                                 qp_data->temp_digests[*digest_idx];
1266                         *digest_idx = (*digest_idx + 1) % IMB_MAX_JOBS;
1267                 }
1268         }
1269         /*
1270          * Multi-buffer library current only support returning a truncated
1271          * digest length as specified in the relevant IPsec RFCs
1272          */
1273
1274         /* Set digest length */
1275         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1276
1277         /* Set IV parameters */
1278         job->iv_len_in_bytes = session->iv.length;
1279
1280         /* Data Parameters */
1281         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1282         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
1283
1284         switch (job->hash_alg) {
1285         case IMB_AUTH_AES_CCM:
1286                 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
1287                 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
1288
1289                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1290                         session->iv.offset + 1);
1291                 break;
1292
1293         case IMB_AUTH_AES_GMAC:
1294                 if (session->cipher.mode == IMB_CIPHER_GCM) {
1295                         job->hash_start_src_offset_in_bytes =
1296                                         op->sym->aead.data.offset;
1297                         job->msg_len_to_hash_in_bytes =
1298                                         op->sym->aead.data.length;
1299                 } else {
1300                         job->msg_len_to_cipher_in_bytes = 0;
1301                         job->msg_len_to_hash_in_bytes = 0;
1302                 }
1303
1304                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1305                                 session->iv.offset);
1306                 break;
1307
1308         case IMB_AUTH_CHACHA20_POLY1305:
1309                 job->hash_start_src_offset_in_bytes =
1310                         op->sym->aead.data.offset;
1311                 job->msg_len_to_hash_in_bytes =
1312                                         op->sym->aead.data.length;
1313
1314                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1315                                 session->iv.offset);
1316                 break;
1317         /* ZUC and SNOW3G require length in bits and offset in bytes */
1318         case IMB_AUTH_ZUC_EIA3_BITLEN:
1319         case IMB_AUTH_ZUC256_EIA3_BITLEN:
1320         case IMB_AUTH_SNOW3G_UIA2_BITLEN:
1321                 auth_off_in_bytes = op->sym->auth.data.offset >> 3;
1322                 ciph_off_in_bytes = op->sym->cipher.data.offset >> 3;
1323                 auth_len_in_bytes = op->sym->auth.data.length >> 3;
1324                 ciph_len_in_bytes = op->sym->cipher.data.length >> 3;
1325
1326                 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1327                                 session, oop, auth_off_in_bytes,
1328                                 ciph_off_in_bytes, auth_len_in_bytes,
1329                                 ciph_len_in_bytes);
1330                 job->msg_len_to_hash_in_bits = op->sym->auth.data.length;
1331
1332                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1333                         session->iv.offset);
1334                 break;
1335
1336         /* KASUMI requires lengths and offset in bytes */
1337         case IMB_AUTH_KASUMI_UIA1:
1338                 auth_off_in_bytes = op->sym->auth.data.offset >> 3;
1339                 ciph_off_in_bytes = op->sym->cipher.data.offset >> 3;
1340                 auth_len_in_bytes = op->sym->auth.data.length >> 3;
1341                 ciph_len_in_bytes = op->sym->cipher.data.length >> 3;
1342
1343                 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1344                                 session, oop, auth_off_in_bytes,
1345                                 ciph_off_in_bytes, auth_len_in_bytes,
1346                                 ciph_len_in_bytes);
1347                 job->msg_len_to_hash_in_bytes = auth_len_in_bytes;
1348
1349                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1350                         session->iv.offset);
1351                 break;
1352
1353         default:
1354                 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1355                                 session, oop, op->sym->auth.data.offset,
1356                                 op->sym->cipher.data.offset,
1357                                 op->sym->auth.data.length,
1358                                 op->sym->cipher.data.length);
1359                 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
1360
1361                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1362                         session->iv.offset);
1363         }
1364
1365         switch (job->cipher_mode) {
1366         /* ZUC requires length and offset in bytes */
1367         case IMB_CIPHER_ZUC_EEA3:
1368                 job->cipher_start_src_offset_in_bytes =
1369                                         op->sym->cipher.data.offset >> 3;
1370                 job->msg_len_to_cipher_in_bytes =
1371                                         op->sym->cipher.data.length >> 3;
1372                 break;
1373         /* ZUC and SNOW3G require length and offset in bits */
1374         case IMB_CIPHER_SNOW3G_UEA2_BITLEN:
1375         case IMB_CIPHER_KASUMI_UEA1_BITLEN:
1376                 job->cipher_start_src_offset_in_bits =
1377                                         op->sym->cipher.data.offset;
1378                 job->msg_len_to_cipher_in_bits =
1379                                         op->sym->cipher.data.length;
1380                 break;
1381         case IMB_CIPHER_CCM:
1382         case IMB_CIPHER_GCM:
1383         case IMB_CIPHER_CHACHA20_POLY1305:
1384                 job->cipher_start_src_offset_in_bytes =
1385                                 op->sym->aead.data.offset;
1386                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1387                 break;
1388         default:
1389                 job->cipher_start_src_offset_in_bytes =
1390                                         op->sym->cipher.data.offset;
1391                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
1392         }
1393
1394         if (job->cipher_mode == IMB_CIPHER_NULL && oop) {
1395                 memcpy(job->dst + job->cipher_start_src_offset_in_bytes,
1396                         job->src + job->cipher_start_src_offset_in_bytes,
1397                         job->msg_len_to_cipher_in_bytes);
1398         }
1399
1400         /* Set user data to be crypto operation data struct */
1401         job->user_data = op;
1402
1403         return 0;
1404 }
1405
1406 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1407 /**
1408  * Process a crypto operation containing a security op and complete a
1409  * IMB_JOB job structure for submission to the multi buffer library for
1410  * processing.
1411  */
1412 static inline int
1413 set_sec_mb_job_params(IMB_JOB *job, struct ipsec_mb_qp *qp,
1414                         struct rte_crypto_op *op, uint8_t *digest_idx)
1415 {
1416         struct aesni_mb_qp_data *qp_data = ipsec_mb_get_qp_private_data(qp);
1417         struct rte_mbuf *m_src, *m_dst;
1418         struct rte_crypto_sym_op *sym;
1419         struct aesni_mb_session *session = NULL;
1420
1421         if (unlikely(op->sess_type != RTE_CRYPTO_OP_SECURITY_SESSION)) {
1422                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1423                 return -1;
1424         }
1425         session = (struct aesni_mb_session *)
1426                 get_sec_session_private_data(op->sym->sec_session);
1427
1428         if (unlikely(session == NULL)) {
1429                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1430                 return -1;
1431         }
1432         /* Only DOCSIS protocol operations supported now */
1433         if (session->cipher.mode != IMB_CIPHER_DOCSIS_SEC_BPI ||
1434                         session->auth.algo != IMB_AUTH_DOCSIS_CRC32) {
1435                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1436                 return -1;
1437         }
1438
1439         sym = op->sym;
1440         m_src = sym->m_src;
1441
1442         if (likely(sym->m_dst == NULL || sym->m_dst == m_src)) {
1443                 /* in-place operation */
1444                 m_dst = m_src;
1445         } else {
1446                 /* out-of-place operation not supported */
1447                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1448                 return -ENOTSUP;
1449         }
1450
1451         /* Set crypto operation */
1452         job->chain_order = session->chain_order;
1453
1454         /* Set cipher parameters */
1455         job->cipher_direction = session->cipher.direction;
1456         job->cipher_mode = session->cipher.mode;
1457
1458         job->key_len_in_bytes = session->cipher.key_length_in_bytes;
1459         job->enc_keys = session->cipher.expanded_aes_keys.encode;
1460         job->dec_keys = session->cipher.expanded_aes_keys.decode;
1461
1462         /* Set IV parameters */
1463         job->iv_len_in_bytes = session->iv.length;
1464         job->iv = (uint8_t *)op + session->iv.offset;
1465
1466         /* Set authentication parameters */
1467         job->hash_alg = session->auth.algo;
1468
1469         /* Set digest output location */
1470         job->auth_tag_output = qp_data->temp_digests[*digest_idx];
1471         *digest_idx = (*digest_idx + 1) % IMB_MAX_JOBS;
1472
1473         /* Set digest length */
1474         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1475
1476         /* Set data parameters */
1477         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1478         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *,
1479                                                 sym->cipher.data.offset);
1480
1481         job->cipher_start_src_offset_in_bytes = sym->cipher.data.offset;
1482         job->msg_len_to_cipher_in_bytes = sym->cipher.data.length;
1483
1484         job->hash_start_src_offset_in_bytes = sym->auth.data.offset;
1485         job->msg_len_to_hash_in_bytes = sym->auth.data.length;
1486
1487         job->user_data = op;
1488
1489         return 0;
1490 }
1491
1492 static inline void
1493 verify_docsis_sec_crc(IMB_JOB *job, uint8_t *status)
1494 {
1495         uint16_t crc_offset;
1496         uint8_t *crc;
1497
1498         if (!job->msg_len_to_hash_in_bytes)
1499                 return;
1500
1501         crc_offset = job->hash_start_src_offset_in_bytes +
1502                         job->msg_len_to_hash_in_bytes -
1503                         job->cipher_start_src_offset_in_bytes;
1504         crc = job->dst + crc_offset;
1505
1506         /* Verify CRC (at the end of the message) */
1507         if (memcmp(job->auth_tag_output, crc, RTE_ETHER_CRC_LEN) != 0)
1508                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1509 }
1510 #endif
1511
1512 static inline void
1513 verify_digest(IMB_JOB *job, void *digest, uint16_t len, uint8_t *status)
1514 {
1515         /* Verify digest if required */
1516         if (memcmp(job->auth_tag_output, digest, len) != 0)
1517                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1518 }
1519
1520 static inline void
1521 generate_digest(IMB_JOB *job, struct rte_crypto_op *op,
1522                 struct aesni_mb_session *sess)
1523 {
1524         /* No extra copy needed */
1525         if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
1526                 return;
1527
1528         /*
1529          * This can only happen for HMAC, so only digest
1530          * for authentication algos is required
1531          */
1532         memcpy(op->sym->auth.digest.data, job->auth_tag_output,
1533                         sess->auth.req_digest_len);
1534 }
1535
1536 /**
1537  * Process a completed job and return rte_mbuf which job processed
1538  *
1539  * @param qp    Queue Pair to process
1540  * @param job   IMB_JOB job to process
1541  *
1542  * @return
1543  * - Returns processed crypto operation.
1544  * - Returns NULL on invalid job
1545  */
1546 static inline struct rte_crypto_op *
1547 post_process_mb_job(struct ipsec_mb_qp *qp, IMB_JOB *job)
1548 {
1549         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
1550         struct aesni_mb_session *sess = NULL;
1551         uint32_t driver_id = ipsec_mb_get_driver_id(
1552                                                 IPSEC_MB_PMD_TYPE_AESNI_MB);
1553
1554 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1555         uint8_t is_docsis_sec = 0;
1556
1557         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1558                 /*
1559                  * Assuming at this point that if it's a security type op, that
1560                  * this is for DOCSIS
1561                  */
1562                 is_docsis_sec = 1;
1563                 sess = get_sec_session_private_data(op->sym->sec_session);
1564         } else
1565 #endif
1566         {
1567                 sess = get_sym_session_private_data(op->sym->session,
1568                                                 driver_id);
1569         }
1570
1571         if (unlikely(sess == NULL)) {
1572                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1573                 return op;
1574         }
1575
1576         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
1577                 switch (job->status) {
1578                 case IMB_STATUS_COMPLETED:
1579                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1580
1581                         if (job->hash_alg == IMB_AUTH_NULL)
1582                                 break;
1583
1584                         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1585                                 if (is_aead_algo(job->hash_alg,
1586                                                 sess->cipher.mode))
1587                                         verify_digest(job,
1588                                                 op->sym->aead.digest.data,
1589                                                 sess->auth.req_digest_len,
1590                                                 &op->status);
1591 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1592                                 else if (is_docsis_sec)
1593                                         verify_docsis_sec_crc(job,
1594                                                 &op->status);
1595 #endif
1596                                 else
1597                                         verify_digest(job,
1598                                                 op->sym->auth.digest.data,
1599                                                 sess->auth.req_digest_len,
1600                                                 &op->status);
1601                         } else
1602                                 generate_digest(job, op, sess);
1603                         break;
1604                 default:
1605                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1606                 }
1607         }
1608
1609         /* Free session if a session-less crypto op */
1610         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1611                 memset(sess, 0, sizeof(struct aesni_mb_session));
1612                 memset(op->sym->session, 0,
1613                         rte_cryptodev_sym_get_existing_header_session_size(
1614                                 op->sym->session));
1615                 rte_mempool_put(qp->sess_mp_priv, sess);
1616                 rte_mempool_put(qp->sess_mp, op->sym->session);
1617                 op->sym->session = NULL;
1618         }
1619
1620         return op;
1621 }
1622
1623 static inline void
1624 post_process_mb_sync_job(IMB_JOB *job)
1625 {
1626         uint32_t *st;
1627
1628         st = job->user_data;
1629         st[0] = (job->status == IMB_STATUS_COMPLETED) ? 0 : EBADMSG;
1630 }
1631
1632 /**
1633  * Process a completed IMB_JOB job and keep processing jobs until
1634  * get_completed_job return NULL
1635  *
1636  * @param qp            Queue Pair to process
1637  * @param mb_mgr        IMB_MGR to use
1638  * @param job           IMB_JOB job
1639  * @param ops           crypto ops to fill
1640  * @param nb_ops        number of crypto ops
1641  *
1642  * @return
1643  * - Number of processed jobs
1644  */
1645 static unsigned
1646 handle_completed_jobs(struct ipsec_mb_qp *qp, IMB_MGR *mb_mgr,
1647                 IMB_JOB *job, struct rte_crypto_op **ops,
1648                 uint16_t nb_ops)
1649 {
1650         struct rte_crypto_op *op = NULL;
1651         uint16_t processed_jobs = 0;
1652
1653         while (job != NULL) {
1654                 op = post_process_mb_job(qp, job);
1655
1656                 if (op) {
1657                         ops[processed_jobs++] = op;
1658                         qp->stats.dequeued_count++;
1659                 } else {
1660                         qp->stats.dequeue_err_count++;
1661                         break;
1662                 }
1663                 if (processed_jobs == nb_ops)
1664                         break;
1665
1666                 job = IMB_GET_COMPLETED_JOB(mb_mgr);
1667         }
1668
1669         return processed_jobs;
1670 }
1671
1672 static inline uint32_t
1673 handle_completed_sync_jobs(IMB_JOB *job, IMB_MGR *mb_mgr)
1674 {
1675         uint32_t i;
1676
1677         for (i = 0; job != NULL; i++, job = IMB_GET_COMPLETED_JOB(mb_mgr))
1678                 post_process_mb_sync_job(job);
1679
1680         return i;
1681 }
1682
1683 static inline uint32_t
1684 flush_mb_sync_mgr(IMB_MGR *mb_mgr)
1685 {
1686         IMB_JOB *job;
1687
1688         job = IMB_FLUSH_JOB(mb_mgr);
1689         return handle_completed_sync_jobs(job, mb_mgr);
1690 }
1691
1692 static inline uint16_t
1693 flush_mb_mgr(struct ipsec_mb_qp *qp, IMB_MGR *mb_mgr,
1694                 struct rte_crypto_op **ops, uint16_t nb_ops)
1695 {
1696         int processed_ops = 0;
1697
1698         /* Flush the remaining jobs */
1699         IMB_JOB *job = IMB_FLUSH_JOB(mb_mgr);
1700
1701         if (job)
1702                 processed_ops += handle_completed_jobs(qp, mb_mgr, job,
1703                                 &ops[processed_ops], nb_ops - processed_ops);
1704
1705         return processed_ops;
1706 }
1707
1708 static inline IMB_JOB *
1709 set_job_null_op(IMB_JOB *job, struct rte_crypto_op *op)
1710 {
1711         job->chain_order = IMB_ORDER_HASH_CIPHER;
1712         job->cipher_mode = IMB_CIPHER_NULL;
1713         job->hash_alg = IMB_AUTH_NULL;
1714         job->cipher_direction = IMB_DIR_DECRYPT;
1715
1716         /* Set user data to be crypto operation data struct */
1717         job->user_data = op;
1718
1719         return job;
1720 }
1721
1722 static uint16_t
1723 aesni_mb_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1724                 uint16_t nb_ops)
1725 {
1726         struct ipsec_mb_qp *qp = queue_pair;
1727         IMB_MGR *mb_mgr = qp->mb_mgr;
1728         struct rte_crypto_op *op;
1729         IMB_JOB *job;
1730         int retval, processed_jobs = 0;
1731
1732         if (unlikely(nb_ops == 0 || mb_mgr == NULL))
1733                 return 0;
1734
1735         uint8_t digest_idx = qp->digest_idx;
1736
1737         do {
1738                 /* Get next free mb job struct from mb manager */
1739                 job = IMB_GET_NEXT_JOB(mb_mgr);
1740                 if (unlikely(job == NULL)) {
1741                         /* if no free mb job structs we need to flush mb_mgr */
1742                         processed_jobs += flush_mb_mgr(qp, mb_mgr,
1743                                         &ops[processed_jobs],
1744                                         nb_ops - processed_jobs);
1745
1746                         if (nb_ops == processed_jobs)
1747                                 break;
1748
1749                         job = IMB_GET_NEXT_JOB(mb_mgr);
1750                 }
1751
1752                 /*
1753                  * Get next operation to process from ingress queue.
1754                  * There is no need to return the job to the IMB_MGR
1755                  * if there are no more operations to process, since the IMB_MGR
1756                  * can use that pointer again in next get_next calls.
1757                  */
1758                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1759                 if (retval < 0)
1760                         break;
1761
1762 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1763                 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1764                         retval = set_sec_mb_job_params(job, qp, op,
1765                                                 &digest_idx);
1766                 else
1767 #endif
1768                         retval = set_mb_job_params(job, qp, op,
1769                                 &digest_idx);
1770
1771                 if (unlikely(retval != 0)) {
1772                         qp->stats.dequeue_err_count++;
1773                         set_job_null_op(job, op);
1774                 }
1775
1776                 /* Submit job to multi-buffer for processing */
1777 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1778                 job = IMB_SUBMIT_JOB(mb_mgr);
1779 #else
1780                 job = IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1781 #endif
1782                 /*
1783                  * If submit returns a processed job then handle it,
1784                  * before submitting subsequent jobs
1785                  */
1786                 if (job)
1787                         processed_jobs += handle_completed_jobs(qp, mb_mgr,
1788                                         job, &ops[processed_jobs],
1789                                         nb_ops - processed_jobs);
1790
1791         } while (processed_jobs < nb_ops);
1792
1793         qp->digest_idx = digest_idx;
1794
1795         if (processed_jobs < 1)
1796                 processed_jobs += flush_mb_mgr(qp, mb_mgr,
1797                                 &ops[processed_jobs],
1798                                 nb_ops - processed_jobs);
1799
1800         return processed_jobs;
1801 }
1802
1803
1804 static inline void
1805 ipsec_mb_fill_error_code(struct rte_crypto_sym_vec *vec, int32_t err)
1806 {
1807         uint32_t i;
1808
1809         for (i = 0; i != vec->num; ++i)
1810                 vec->status[i] = err;
1811 }
1812
1813 static inline int
1814 check_crypto_sgl(union rte_crypto_sym_ofs so, const struct rte_crypto_sgl *sgl)
1815 {
1816         /* no multi-seg support with current AESNI-MB PMD */
1817         if (sgl->num != 1)
1818                 return -ENOTSUP;
1819         else if (so.ofs.cipher.head + so.ofs.cipher.tail > sgl->vec[0].len)
1820                 return -EINVAL;
1821         return 0;
1822 }
1823
1824 static inline IMB_JOB *
1825 submit_sync_job(IMB_MGR *mb_mgr)
1826 {
1827 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1828         return IMB_SUBMIT_JOB(mb_mgr);
1829 #else
1830         return IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1831 #endif
1832 }
1833
1834 static inline uint32_t
1835 generate_sync_dgst(struct rte_crypto_sym_vec *vec,
1836         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1837 {
1838         uint32_t i, k;
1839
1840         for (i = 0, k = 0; i != vec->num; i++) {
1841                 if (vec->status[i] == 0) {
1842                         memcpy(vec->digest[i].va, dgst[i], len);
1843                         k++;
1844                 }
1845         }
1846
1847         return k;
1848 }
1849
1850 static inline uint32_t
1851 verify_sync_dgst(struct rte_crypto_sym_vec *vec,
1852         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1853 {
1854         uint32_t i, k;
1855
1856         for (i = 0, k = 0; i != vec->num; i++) {
1857                 if (vec->status[i] == 0) {
1858                         if (memcmp(vec->digest[i].va, dgst[i], len) != 0)
1859                                 vec->status[i] = EBADMSG;
1860                         else
1861                                 k++;
1862                 }
1863         }
1864
1865         return k;
1866 }
1867
1868 static uint32_t
1869 aesni_mb_process_bulk(struct rte_cryptodev *dev,
1870         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs sofs,
1871         struct rte_crypto_sym_vec *vec)
1872 {
1873         int32_t ret;
1874         uint32_t i, j, k, len;
1875         void *buf;
1876         IMB_JOB *job;
1877         IMB_MGR *mb_mgr;
1878         struct aesni_mb_session *s;
1879         uint8_t tmp_dgst[vec->num][DIGEST_LENGTH_MAX];
1880
1881         s = get_sym_session_private_data(sess, dev->driver_id);
1882         if (s == NULL) {
1883                 ipsec_mb_fill_error_code(vec, EINVAL);
1884                 return 0;
1885         }
1886
1887         /* get per-thread MB MGR, create one if needed */
1888         mb_mgr = get_per_thread_mb_mgr();
1889         if (unlikely(mb_mgr == NULL))
1890                 return 0;
1891
1892         for (i = 0, j = 0, k = 0; i != vec->num; i++) {
1893                 ret = check_crypto_sgl(sofs, vec->src_sgl + i);
1894                 if (ret != 0) {
1895                         vec->status[i] = ret;
1896                         continue;
1897                 }
1898
1899                 buf = vec->src_sgl[i].vec[0].base;
1900                 len = vec->src_sgl[i].vec[0].len;
1901
1902                 job = IMB_GET_NEXT_JOB(mb_mgr);
1903                 if (job == NULL) {
1904                         k += flush_mb_sync_mgr(mb_mgr);
1905                         job = IMB_GET_NEXT_JOB(mb_mgr);
1906                         RTE_ASSERT(job != NULL);
1907                 }
1908
1909                 /* Submit job for processing */
1910                 set_cpu_mb_job_params(job, s, sofs, buf, len, &vec->iv[i],
1911                         &vec->aad[i], tmp_dgst[i], &vec->status[i]);
1912                 job = submit_sync_job(mb_mgr);
1913                 j++;
1914
1915                 /* handle completed jobs */
1916                 k += handle_completed_sync_jobs(job, mb_mgr);
1917         }
1918
1919         /* flush remaining jobs */
1920         while (k != j)
1921                 k += flush_mb_sync_mgr(mb_mgr);
1922
1923         /* finish processing for successful jobs: check/update digest */
1924         if (k != 0) {
1925                 if (s->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1926                         k = verify_sync_dgst(vec,
1927                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1928                                 s->auth.req_digest_len);
1929                 else
1930                         k = generate_sync_dgst(vec,
1931                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1932                                 s->auth.req_digest_len);
1933         }
1934
1935         return k;
1936 }
1937
1938 struct rte_cryptodev_ops aesni_mb_pmd_ops = {
1939         .dev_configure = ipsec_mb_config,
1940         .dev_start = ipsec_mb_start,
1941         .dev_stop = ipsec_mb_stop,
1942         .dev_close = ipsec_mb_close,
1943
1944         .stats_get = ipsec_mb_stats_get,
1945         .stats_reset = ipsec_mb_stats_reset,
1946
1947         .dev_infos_get = ipsec_mb_info_get,
1948
1949         .queue_pair_setup = ipsec_mb_qp_setup,
1950         .queue_pair_release = ipsec_mb_qp_release,
1951
1952         .sym_cpu_process = aesni_mb_process_bulk,
1953
1954         .sym_session_get_size = ipsec_mb_sym_session_get_size,
1955         .sym_session_configure = ipsec_mb_sym_session_configure,
1956         .sym_session_clear = ipsec_mb_sym_session_clear
1957 };
1958
1959 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1960 /**
1961  * Configure a aesni multi-buffer session from a security session
1962  * configuration
1963  */
1964 static int
1965 aesni_mb_pmd_sec_sess_create(void *dev, struct rte_security_session_conf *conf,
1966                 struct rte_security_session *sess,
1967                 struct rte_mempool *mempool)
1968 {
1969         void *sess_private_data;
1970         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
1971         int ret;
1972
1973         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL ||
1974                         conf->protocol != RTE_SECURITY_PROTOCOL_DOCSIS) {
1975                 IPSEC_MB_LOG(ERR, "Invalid security protocol");
1976                 return -EINVAL;
1977         }
1978
1979         if (rte_mempool_get(mempool, &sess_private_data)) {
1980                 IPSEC_MB_LOG(ERR, "Couldn't get object from session mempool");
1981                 return -ENOMEM;
1982         }
1983
1984         ret = aesni_mb_set_docsis_sec_session_parameters(cdev, conf,
1985                         sess_private_data);
1986
1987         if (ret != 0) {
1988                 IPSEC_MB_LOG(ERR, "Failed to configure session parameters");
1989
1990                 /* Return session to mempool */
1991                 rte_mempool_put(mempool, sess_private_data);
1992                 return ret;
1993         }
1994
1995         set_sec_session_private_data(sess, sess_private_data);
1996
1997         return ret;
1998 }
1999
2000 /** Clear the memory of session so it does not leave key material behind */
2001 static int
2002 aesni_mb_pmd_sec_sess_destroy(void *dev __rte_unused,
2003                 struct rte_security_session *sess)
2004 {
2005         void *sess_priv = get_sec_session_private_data(sess);
2006
2007         if (sess_priv) {
2008                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
2009
2010                 memset(sess_priv, 0, sizeof(struct aesni_mb_session));
2011                 set_sec_session_private_data(sess, NULL);
2012                 rte_mempool_put(sess_mp, sess_priv);
2013         }
2014         return 0;
2015 }
2016
2017 /** Get security capabilities for aesni multi-buffer */
2018 static const struct rte_security_capability *
2019 aesni_mb_pmd_sec_capa_get(void *device __rte_unused)
2020 {
2021         return aesni_mb_pmd_security_cap;
2022 }
2023
2024 static struct rte_security_ops aesni_mb_pmd_sec_ops = {
2025                 .session_create = aesni_mb_pmd_sec_sess_create,
2026                 .session_update = NULL,
2027                 .session_stats_get = NULL,
2028                 .session_destroy = aesni_mb_pmd_sec_sess_destroy,
2029                 .set_pkt_metadata = NULL,
2030                 .capabilities_get = aesni_mb_pmd_sec_capa_get
2031 };
2032
2033 struct rte_security_ops *rte_aesni_mb_pmd_sec_ops = &aesni_mb_pmd_sec_ops;
2034
2035 static int
2036 aesni_mb_configure_dev(struct rte_cryptodev *dev)
2037 {
2038         struct rte_security_ctx *security_instance;
2039
2040         security_instance = rte_malloc("aesni_mb_sec",
2041                                 sizeof(struct rte_security_ctx),
2042                                 RTE_CACHE_LINE_SIZE);
2043         if (security_instance != NULL) {
2044                 security_instance->device = (void *)dev;
2045                 security_instance->ops = rte_aesni_mb_pmd_sec_ops;
2046                 security_instance->sess_cnt = 0;
2047                 dev->security_ctx = security_instance;
2048
2049                 return 0;
2050         }
2051
2052         return -ENOMEM;
2053 }
2054
2055 #endif
2056
2057 static int
2058 aesni_mb_probe(struct rte_vdev_device *vdev)
2059 {
2060         return ipsec_mb_create(vdev, IPSEC_MB_PMD_TYPE_AESNI_MB);
2061 }
2062
2063 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
2064         .probe = aesni_mb_probe,
2065         .remove = ipsec_mb_remove
2066 };
2067
2068 static struct cryptodev_driver aesni_mb_crypto_drv;
2069
2070 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD,
2071         cryptodev_aesni_mb_pmd_drv);
2072 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
2073 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
2074                         "max_nb_queue_pairs=<int> socket_id=<int>");
2075 RTE_PMD_REGISTER_CRYPTO_DRIVER(
2076         aesni_mb_crypto_drv,
2077         cryptodev_aesni_mb_pmd_drv.driver,
2078         pmd_driver_id_aesni_mb);
2079
2080 /* Constructor function to register aesni-mb PMD */
2081 RTE_INIT(ipsec_mb_register_aesni_mb)
2082 {
2083         struct ipsec_mb_internals *aesni_mb_data =
2084                 &ipsec_mb_pmds[IPSEC_MB_PMD_TYPE_AESNI_MB];
2085
2086         aesni_mb_data->caps = aesni_mb_capabilities;
2087         aesni_mb_data->dequeue_burst = aesni_mb_dequeue_burst;
2088         aesni_mb_data->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2089                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2090                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
2091                         RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO |
2092                         RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA |
2093                         RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
2094
2095         aesni_mb_data->internals_priv_size = 0;
2096         aesni_mb_data->ops = &aesni_mb_pmd_ops;
2097         aesni_mb_data->qp_priv_size = sizeof(struct aesni_mb_qp_data);
2098         aesni_mb_data->queue_pair_configure = NULL;
2099 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
2100         aesni_mb_data->security_ops = &aesni_mb_pmd_sec_ops;
2101         aesni_mb_data->dev_config = aesni_mb_configure_dev;
2102         aesni_mb_data->feature_flags |= RTE_CRYPTODEV_FF_SECURITY;
2103 #endif
2104         aesni_mb_data->session_configure = aesni_mb_session_configure;
2105         aesni_mb_data->session_priv_size = sizeof(struct aesni_mb_session);
2106 }