net/mlx5: fix RSS hash types adjustment
[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 { /* AES-GMAC only, only AAD used */
1300                         job->msg_len_to_hash_in_bytes = 0;
1301                         job->hash_start_src_offset_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_GCM:
1382                 if (session->cipher.mode == IMB_CIPHER_NULL) {
1383                         /* AES-GMAC only (only AAD used) */
1384                         job->msg_len_to_cipher_in_bytes = 0;
1385                         job->cipher_start_src_offset_in_bytes = 0;
1386                 } else {
1387                         job->cipher_start_src_offset_in_bytes =
1388                                         op->sym->aead.data.offset;
1389                         job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1390                 }
1391                 break;
1392         case IMB_CIPHER_CCM:
1393         case IMB_CIPHER_CHACHA20_POLY1305:
1394                 job->cipher_start_src_offset_in_bytes =
1395                                 op->sym->aead.data.offset;
1396                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1397                 break;
1398         default:
1399                 job->cipher_start_src_offset_in_bytes =
1400                                         op->sym->cipher.data.offset;
1401                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
1402         }
1403
1404         if (job->cipher_mode == IMB_CIPHER_NULL && oop) {
1405                 memcpy(job->dst + job->cipher_start_src_offset_in_bytes,
1406                         job->src + job->cipher_start_src_offset_in_bytes,
1407                         job->msg_len_to_cipher_in_bytes);
1408         }
1409
1410         /* Set user data to be crypto operation data struct */
1411         job->user_data = op;
1412
1413         return 0;
1414 }
1415
1416 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1417 /**
1418  * Process a crypto operation containing a security op and complete a
1419  * IMB_JOB job structure for submission to the multi buffer library for
1420  * processing.
1421  */
1422 static inline int
1423 set_sec_mb_job_params(IMB_JOB *job, struct ipsec_mb_qp *qp,
1424                         struct rte_crypto_op *op, uint8_t *digest_idx)
1425 {
1426         struct aesni_mb_qp_data *qp_data = ipsec_mb_get_qp_private_data(qp);
1427         struct rte_mbuf *m_src, *m_dst;
1428         struct rte_crypto_sym_op *sym;
1429         struct aesni_mb_session *session = NULL;
1430
1431         if (unlikely(op->sess_type != RTE_CRYPTO_OP_SECURITY_SESSION)) {
1432                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1433                 return -1;
1434         }
1435         session = (struct aesni_mb_session *)
1436                 get_sec_session_private_data(op->sym->sec_session);
1437
1438         if (unlikely(session == NULL)) {
1439                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1440                 return -1;
1441         }
1442         /* Only DOCSIS protocol operations supported now */
1443         if (session->cipher.mode != IMB_CIPHER_DOCSIS_SEC_BPI ||
1444                         session->auth.algo != IMB_AUTH_DOCSIS_CRC32) {
1445                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1446                 return -1;
1447         }
1448
1449         sym = op->sym;
1450         m_src = sym->m_src;
1451
1452         if (likely(sym->m_dst == NULL || sym->m_dst == m_src)) {
1453                 /* in-place operation */
1454                 m_dst = m_src;
1455         } else {
1456                 /* out-of-place operation not supported */
1457                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1458                 return -ENOTSUP;
1459         }
1460
1461         /* Set crypto operation */
1462         job->chain_order = session->chain_order;
1463
1464         /* Set cipher parameters */
1465         job->cipher_direction = session->cipher.direction;
1466         job->cipher_mode = session->cipher.mode;
1467
1468         job->key_len_in_bytes = session->cipher.key_length_in_bytes;
1469         job->enc_keys = session->cipher.expanded_aes_keys.encode;
1470         job->dec_keys = session->cipher.expanded_aes_keys.decode;
1471
1472         /* Set IV parameters */
1473         job->iv_len_in_bytes = session->iv.length;
1474         job->iv = (uint8_t *)op + session->iv.offset;
1475
1476         /* Set authentication parameters */
1477         job->hash_alg = session->auth.algo;
1478
1479         /* Set digest output location */
1480         job->auth_tag_output = qp_data->temp_digests[*digest_idx];
1481         *digest_idx = (*digest_idx + 1) % IMB_MAX_JOBS;
1482
1483         /* Set digest length */
1484         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1485
1486         /* Set data parameters */
1487         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1488         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *,
1489                                                 sym->cipher.data.offset);
1490
1491         job->cipher_start_src_offset_in_bytes = sym->cipher.data.offset;
1492         job->msg_len_to_cipher_in_bytes = sym->cipher.data.length;
1493
1494         job->hash_start_src_offset_in_bytes = sym->auth.data.offset;
1495         job->msg_len_to_hash_in_bytes = sym->auth.data.length;
1496
1497         job->user_data = op;
1498
1499         return 0;
1500 }
1501
1502 static inline void
1503 verify_docsis_sec_crc(IMB_JOB *job, uint8_t *status)
1504 {
1505         uint16_t crc_offset;
1506         uint8_t *crc;
1507
1508         if (!job->msg_len_to_hash_in_bytes)
1509                 return;
1510
1511         crc_offset = job->hash_start_src_offset_in_bytes +
1512                         job->msg_len_to_hash_in_bytes -
1513                         job->cipher_start_src_offset_in_bytes;
1514         crc = job->dst + crc_offset;
1515
1516         /* Verify CRC (at the end of the message) */
1517         if (memcmp(job->auth_tag_output, crc, RTE_ETHER_CRC_LEN) != 0)
1518                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1519 }
1520 #endif
1521
1522 static inline void
1523 verify_digest(IMB_JOB *job, void *digest, uint16_t len, uint8_t *status)
1524 {
1525         /* Verify digest if required */
1526         if (memcmp(job->auth_tag_output, digest, len) != 0)
1527                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1528 }
1529
1530 static inline void
1531 generate_digest(IMB_JOB *job, struct rte_crypto_op *op,
1532                 struct aesni_mb_session *sess)
1533 {
1534         /* No extra copy needed */
1535         if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
1536                 return;
1537
1538         /*
1539          * This can only happen for HMAC, so only digest
1540          * for authentication algos is required
1541          */
1542         memcpy(op->sym->auth.digest.data, job->auth_tag_output,
1543                         sess->auth.req_digest_len);
1544 }
1545
1546 /**
1547  * Process a completed job and return rte_mbuf which job processed
1548  *
1549  * @param qp    Queue Pair to process
1550  * @param job   IMB_JOB job to process
1551  *
1552  * @return
1553  * - Returns processed crypto operation.
1554  * - Returns NULL on invalid job
1555  */
1556 static inline struct rte_crypto_op *
1557 post_process_mb_job(struct ipsec_mb_qp *qp, IMB_JOB *job)
1558 {
1559         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
1560         struct aesni_mb_session *sess = NULL;
1561         uint32_t driver_id = ipsec_mb_get_driver_id(
1562                                                 IPSEC_MB_PMD_TYPE_AESNI_MB);
1563
1564 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1565         uint8_t is_docsis_sec = 0;
1566
1567         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1568                 /*
1569                  * Assuming at this point that if it's a security type op, that
1570                  * this is for DOCSIS
1571                  */
1572                 is_docsis_sec = 1;
1573                 sess = get_sec_session_private_data(op->sym->sec_session);
1574         } else
1575 #endif
1576         {
1577                 sess = get_sym_session_private_data(op->sym->session,
1578                                                 driver_id);
1579         }
1580
1581         if (unlikely(sess == NULL)) {
1582                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1583                 return op;
1584         }
1585
1586         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
1587                 switch (job->status) {
1588                 case IMB_STATUS_COMPLETED:
1589                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1590
1591                         if (job->hash_alg == IMB_AUTH_NULL)
1592                                 break;
1593
1594                         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1595                                 if (is_aead_algo(job->hash_alg,
1596                                                 sess->cipher.mode))
1597                                         verify_digest(job,
1598                                                 op->sym->aead.digest.data,
1599                                                 sess->auth.req_digest_len,
1600                                                 &op->status);
1601 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1602                                 else if (is_docsis_sec)
1603                                         verify_docsis_sec_crc(job,
1604                                                 &op->status);
1605 #endif
1606                                 else
1607                                         verify_digest(job,
1608                                                 op->sym->auth.digest.data,
1609                                                 sess->auth.req_digest_len,
1610                                                 &op->status);
1611                         } else
1612                                 generate_digest(job, op, sess);
1613                         break;
1614                 default:
1615                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1616                 }
1617         }
1618
1619         /* Free session if a session-less crypto op */
1620         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1621                 memset(sess, 0, sizeof(struct aesni_mb_session));
1622                 memset(op->sym->session, 0,
1623                         rte_cryptodev_sym_get_existing_header_session_size(
1624                                 op->sym->session));
1625                 rte_mempool_put(qp->sess_mp_priv, sess);
1626                 rte_mempool_put(qp->sess_mp, op->sym->session);
1627                 op->sym->session = NULL;
1628         }
1629
1630         return op;
1631 }
1632
1633 static inline void
1634 post_process_mb_sync_job(IMB_JOB *job)
1635 {
1636         uint32_t *st;
1637
1638         st = job->user_data;
1639         st[0] = (job->status == IMB_STATUS_COMPLETED) ? 0 : EBADMSG;
1640 }
1641
1642 /**
1643  * Process a completed IMB_JOB job and keep processing jobs until
1644  * get_completed_job return NULL
1645  *
1646  * @param qp            Queue Pair to process
1647  * @param mb_mgr        IMB_MGR to use
1648  * @param job           IMB_JOB job
1649  * @param ops           crypto ops to fill
1650  * @param nb_ops        number of crypto ops
1651  *
1652  * @return
1653  * - Number of processed jobs
1654  */
1655 static unsigned
1656 handle_completed_jobs(struct ipsec_mb_qp *qp, IMB_MGR *mb_mgr,
1657                 IMB_JOB *job, struct rte_crypto_op **ops,
1658                 uint16_t nb_ops)
1659 {
1660         struct rte_crypto_op *op = NULL;
1661         uint16_t processed_jobs = 0;
1662
1663         while (job != NULL) {
1664                 op = post_process_mb_job(qp, job);
1665
1666                 if (op) {
1667                         ops[processed_jobs++] = op;
1668                         qp->stats.dequeued_count++;
1669                 } else {
1670                         qp->stats.dequeue_err_count++;
1671                         break;
1672                 }
1673                 if (processed_jobs == nb_ops)
1674                         break;
1675
1676                 job = IMB_GET_COMPLETED_JOB(mb_mgr);
1677         }
1678
1679         return processed_jobs;
1680 }
1681
1682 static inline uint32_t
1683 handle_completed_sync_jobs(IMB_JOB *job, IMB_MGR *mb_mgr)
1684 {
1685         uint32_t i;
1686
1687         for (i = 0; job != NULL; i++, job = IMB_GET_COMPLETED_JOB(mb_mgr))
1688                 post_process_mb_sync_job(job);
1689
1690         return i;
1691 }
1692
1693 static inline uint32_t
1694 flush_mb_sync_mgr(IMB_MGR *mb_mgr)
1695 {
1696         IMB_JOB *job;
1697
1698         job = IMB_FLUSH_JOB(mb_mgr);
1699         return handle_completed_sync_jobs(job, mb_mgr);
1700 }
1701
1702 static inline uint16_t
1703 flush_mb_mgr(struct ipsec_mb_qp *qp, IMB_MGR *mb_mgr,
1704                 struct rte_crypto_op **ops, uint16_t nb_ops)
1705 {
1706         int processed_ops = 0;
1707
1708         /* Flush the remaining jobs */
1709         IMB_JOB *job = IMB_FLUSH_JOB(mb_mgr);
1710
1711         if (job)
1712                 processed_ops += handle_completed_jobs(qp, mb_mgr, job,
1713                                 &ops[processed_ops], nb_ops - processed_ops);
1714
1715         return processed_ops;
1716 }
1717
1718 static inline IMB_JOB *
1719 set_job_null_op(IMB_JOB *job, struct rte_crypto_op *op)
1720 {
1721         job->chain_order = IMB_ORDER_HASH_CIPHER;
1722         job->cipher_mode = IMB_CIPHER_NULL;
1723         job->hash_alg = IMB_AUTH_NULL;
1724         job->cipher_direction = IMB_DIR_DECRYPT;
1725
1726         /* Set user data to be crypto operation data struct */
1727         job->user_data = op;
1728
1729         return job;
1730 }
1731
1732 static uint16_t
1733 aesni_mb_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1734                 uint16_t nb_ops)
1735 {
1736         struct ipsec_mb_qp *qp = queue_pair;
1737         IMB_MGR *mb_mgr = qp->mb_mgr;
1738         struct rte_crypto_op *op;
1739         IMB_JOB *job;
1740         int retval, processed_jobs = 0;
1741
1742         if (unlikely(nb_ops == 0 || mb_mgr == NULL))
1743                 return 0;
1744
1745         uint8_t digest_idx = qp->digest_idx;
1746
1747         do {
1748                 /* Get next free mb job struct from mb manager */
1749                 job = IMB_GET_NEXT_JOB(mb_mgr);
1750                 if (unlikely(job == NULL)) {
1751                         /* if no free mb job structs we need to flush mb_mgr */
1752                         processed_jobs += flush_mb_mgr(qp, mb_mgr,
1753                                         &ops[processed_jobs],
1754                                         nb_ops - processed_jobs);
1755
1756                         if (nb_ops == processed_jobs)
1757                                 break;
1758
1759                         job = IMB_GET_NEXT_JOB(mb_mgr);
1760                 }
1761
1762                 /*
1763                  * Get next operation to process from ingress queue.
1764                  * There is no need to return the job to the IMB_MGR
1765                  * if there are no more operations to process, since the IMB_MGR
1766                  * can use that pointer again in next get_next calls.
1767                  */
1768                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1769                 if (retval < 0)
1770                         break;
1771
1772 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1773                 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1774                         retval = set_sec_mb_job_params(job, qp, op,
1775                                                 &digest_idx);
1776                 else
1777 #endif
1778                         retval = set_mb_job_params(job, qp, op,
1779                                 &digest_idx);
1780
1781                 if (unlikely(retval != 0)) {
1782                         qp->stats.dequeue_err_count++;
1783                         set_job_null_op(job, op);
1784                 }
1785
1786                 /* Submit job to multi-buffer for processing */
1787 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1788                 job = IMB_SUBMIT_JOB(mb_mgr);
1789 #else
1790                 job = IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1791 #endif
1792                 /*
1793                  * If submit returns a processed job then handle it,
1794                  * before submitting subsequent jobs
1795                  */
1796                 if (job)
1797                         processed_jobs += handle_completed_jobs(qp, mb_mgr,
1798                                         job, &ops[processed_jobs],
1799                                         nb_ops - processed_jobs);
1800
1801         } while (processed_jobs < nb_ops);
1802
1803         qp->digest_idx = digest_idx;
1804
1805         if (processed_jobs < 1)
1806                 processed_jobs += flush_mb_mgr(qp, mb_mgr,
1807                                 &ops[processed_jobs],
1808                                 nb_ops - processed_jobs);
1809
1810         return processed_jobs;
1811 }
1812
1813
1814 static inline void
1815 ipsec_mb_fill_error_code(struct rte_crypto_sym_vec *vec, int32_t err)
1816 {
1817         uint32_t i;
1818
1819         for (i = 0; i != vec->num; ++i)
1820                 vec->status[i] = err;
1821 }
1822
1823 static inline int
1824 check_crypto_sgl(union rte_crypto_sym_ofs so, const struct rte_crypto_sgl *sgl)
1825 {
1826         /* no multi-seg support with current AESNI-MB PMD */
1827         if (sgl->num != 1)
1828                 return -ENOTSUP;
1829         else if (so.ofs.cipher.head + so.ofs.cipher.tail > sgl->vec[0].len)
1830                 return -EINVAL;
1831         return 0;
1832 }
1833
1834 static inline IMB_JOB *
1835 submit_sync_job(IMB_MGR *mb_mgr)
1836 {
1837 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1838         return IMB_SUBMIT_JOB(mb_mgr);
1839 #else
1840         return IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1841 #endif
1842 }
1843
1844 static inline uint32_t
1845 generate_sync_dgst(struct rte_crypto_sym_vec *vec,
1846         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1847 {
1848         uint32_t i, k;
1849
1850         for (i = 0, k = 0; i != vec->num; i++) {
1851                 if (vec->status[i] == 0) {
1852                         memcpy(vec->digest[i].va, dgst[i], len);
1853                         k++;
1854                 }
1855         }
1856
1857         return k;
1858 }
1859
1860 static inline uint32_t
1861 verify_sync_dgst(struct rte_crypto_sym_vec *vec,
1862         const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1863 {
1864         uint32_t i, k;
1865
1866         for (i = 0, k = 0; i != vec->num; i++) {
1867                 if (vec->status[i] == 0) {
1868                         if (memcmp(vec->digest[i].va, dgst[i], len) != 0)
1869                                 vec->status[i] = EBADMSG;
1870                         else
1871                                 k++;
1872                 }
1873         }
1874
1875         return k;
1876 }
1877
1878 static uint32_t
1879 aesni_mb_process_bulk(struct rte_cryptodev *dev,
1880         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs sofs,
1881         struct rte_crypto_sym_vec *vec)
1882 {
1883         int32_t ret;
1884         uint32_t i, j, k, len;
1885         void *buf;
1886         IMB_JOB *job;
1887         IMB_MGR *mb_mgr;
1888         struct aesni_mb_session *s;
1889         uint8_t tmp_dgst[vec->num][DIGEST_LENGTH_MAX];
1890
1891         s = get_sym_session_private_data(sess, dev->driver_id);
1892         if (s == NULL) {
1893                 ipsec_mb_fill_error_code(vec, EINVAL);
1894                 return 0;
1895         }
1896
1897         /* get per-thread MB MGR, create one if needed */
1898         mb_mgr = get_per_thread_mb_mgr();
1899         if (unlikely(mb_mgr == NULL))
1900                 return 0;
1901
1902         for (i = 0, j = 0, k = 0; i != vec->num; i++) {
1903                 ret = check_crypto_sgl(sofs, vec->src_sgl + i);
1904                 if (ret != 0) {
1905                         vec->status[i] = ret;
1906                         continue;
1907                 }
1908
1909                 buf = vec->src_sgl[i].vec[0].base;
1910                 len = vec->src_sgl[i].vec[0].len;
1911
1912                 job = IMB_GET_NEXT_JOB(mb_mgr);
1913                 if (job == NULL) {
1914                         k += flush_mb_sync_mgr(mb_mgr);
1915                         job = IMB_GET_NEXT_JOB(mb_mgr);
1916                         RTE_ASSERT(job != NULL);
1917                 }
1918
1919                 /* Submit job for processing */
1920                 set_cpu_mb_job_params(job, s, sofs, buf, len, &vec->iv[i],
1921                         &vec->aad[i], tmp_dgst[i], &vec->status[i]);
1922                 job = submit_sync_job(mb_mgr);
1923                 j++;
1924
1925                 /* handle completed jobs */
1926                 k += handle_completed_sync_jobs(job, mb_mgr);
1927         }
1928
1929         /* flush remaining jobs */
1930         while (k != j)
1931                 k += flush_mb_sync_mgr(mb_mgr);
1932
1933         /* finish processing for successful jobs: check/update digest */
1934         if (k != 0) {
1935                 if (s->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1936                         k = verify_sync_dgst(vec,
1937                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1938                                 s->auth.req_digest_len);
1939                 else
1940                         k = generate_sync_dgst(vec,
1941                                 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1942                                 s->auth.req_digest_len);
1943         }
1944
1945         return k;
1946 }
1947
1948 struct rte_cryptodev_ops aesni_mb_pmd_ops = {
1949         .dev_configure = ipsec_mb_config,
1950         .dev_start = ipsec_mb_start,
1951         .dev_stop = ipsec_mb_stop,
1952         .dev_close = ipsec_mb_close,
1953
1954         .stats_get = ipsec_mb_stats_get,
1955         .stats_reset = ipsec_mb_stats_reset,
1956
1957         .dev_infos_get = ipsec_mb_info_get,
1958
1959         .queue_pair_setup = ipsec_mb_qp_setup,
1960         .queue_pair_release = ipsec_mb_qp_release,
1961
1962         .sym_cpu_process = aesni_mb_process_bulk,
1963
1964         .sym_session_get_size = ipsec_mb_sym_session_get_size,
1965         .sym_session_configure = ipsec_mb_sym_session_configure,
1966         .sym_session_clear = ipsec_mb_sym_session_clear
1967 };
1968
1969 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1970 /**
1971  * Configure a aesni multi-buffer session from a security session
1972  * configuration
1973  */
1974 static int
1975 aesni_mb_pmd_sec_sess_create(void *dev, struct rte_security_session_conf *conf,
1976                 struct rte_security_session *sess,
1977                 struct rte_mempool *mempool)
1978 {
1979         void *sess_private_data;
1980         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
1981         int ret;
1982
1983         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL ||
1984                         conf->protocol != RTE_SECURITY_PROTOCOL_DOCSIS) {
1985                 IPSEC_MB_LOG(ERR, "Invalid security protocol");
1986                 return -EINVAL;
1987         }
1988
1989         if (rte_mempool_get(mempool, &sess_private_data)) {
1990                 IPSEC_MB_LOG(ERR, "Couldn't get object from session mempool");
1991                 return -ENOMEM;
1992         }
1993
1994         ret = aesni_mb_set_docsis_sec_session_parameters(cdev, conf,
1995                         sess_private_data);
1996
1997         if (ret != 0) {
1998                 IPSEC_MB_LOG(ERR, "Failed to configure session parameters");
1999
2000                 /* Return session to mempool */
2001                 rte_mempool_put(mempool, sess_private_data);
2002                 return ret;
2003         }
2004
2005         set_sec_session_private_data(sess, sess_private_data);
2006
2007         return ret;
2008 }
2009
2010 /** Clear the memory of session so it does not leave key material behind */
2011 static int
2012 aesni_mb_pmd_sec_sess_destroy(void *dev __rte_unused,
2013                 struct rte_security_session *sess)
2014 {
2015         void *sess_priv = get_sec_session_private_data(sess);
2016
2017         if (sess_priv) {
2018                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
2019
2020                 memset(sess_priv, 0, sizeof(struct aesni_mb_session));
2021                 set_sec_session_private_data(sess, NULL);
2022                 rte_mempool_put(sess_mp, sess_priv);
2023         }
2024         return 0;
2025 }
2026
2027 /** Get security capabilities for aesni multi-buffer */
2028 static const struct rte_security_capability *
2029 aesni_mb_pmd_sec_capa_get(void *device __rte_unused)
2030 {
2031         return aesni_mb_pmd_security_cap;
2032 }
2033
2034 static struct rte_security_ops aesni_mb_pmd_sec_ops = {
2035                 .session_create = aesni_mb_pmd_sec_sess_create,
2036                 .session_update = NULL,
2037                 .session_stats_get = NULL,
2038                 .session_destroy = aesni_mb_pmd_sec_sess_destroy,
2039                 .set_pkt_metadata = NULL,
2040                 .capabilities_get = aesni_mb_pmd_sec_capa_get
2041 };
2042
2043 struct rte_security_ops *rte_aesni_mb_pmd_sec_ops = &aesni_mb_pmd_sec_ops;
2044
2045 static int
2046 aesni_mb_configure_dev(struct rte_cryptodev *dev)
2047 {
2048         struct rte_security_ctx *security_instance;
2049
2050         security_instance = rte_malloc("aesni_mb_sec",
2051                                 sizeof(struct rte_security_ctx),
2052                                 RTE_CACHE_LINE_SIZE);
2053         if (security_instance != NULL) {
2054                 security_instance->device = (void *)dev;
2055                 security_instance->ops = rte_aesni_mb_pmd_sec_ops;
2056                 security_instance->sess_cnt = 0;
2057                 dev->security_ctx = security_instance;
2058
2059                 return 0;
2060         }
2061
2062         return -ENOMEM;
2063 }
2064
2065 #endif
2066
2067 static int
2068 aesni_mb_probe(struct rte_vdev_device *vdev)
2069 {
2070         return ipsec_mb_create(vdev, IPSEC_MB_PMD_TYPE_AESNI_MB);
2071 }
2072
2073 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
2074         .probe = aesni_mb_probe,
2075         .remove = ipsec_mb_remove
2076 };
2077
2078 static struct cryptodev_driver aesni_mb_crypto_drv;
2079
2080 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD,
2081         cryptodev_aesni_mb_pmd_drv);
2082 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
2083 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
2084                         "max_nb_queue_pairs=<int> socket_id=<int>");
2085 RTE_PMD_REGISTER_CRYPTO_DRIVER(
2086         aesni_mb_crypto_drv,
2087         cryptodev_aesni_mb_pmd_drv.driver,
2088         pmd_driver_id_aesni_mb);
2089
2090 /* Constructor function to register aesni-mb PMD */
2091 RTE_INIT(ipsec_mb_register_aesni_mb)
2092 {
2093         struct ipsec_mb_internals *aesni_mb_data =
2094                 &ipsec_mb_pmds[IPSEC_MB_PMD_TYPE_AESNI_MB];
2095
2096         aesni_mb_data->caps = aesni_mb_capabilities;
2097         aesni_mb_data->dequeue_burst = aesni_mb_dequeue_burst;
2098         aesni_mb_data->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2099                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2100                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
2101                         RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO |
2102                         RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA |
2103                         RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
2104
2105         aesni_mb_data->internals_priv_size = 0;
2106         aesni_mb_data->ops = &aesni_mb_pmd_ops;
2107         aesni_mb_data->qp_priv_size = sizeof(struct aesni_mb_qp_data);
2108         aesni_mb_data->queue_pair_configure = NULL;
2109 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
2110         aesni_mb_data->security_ops = &aesni_mb_pmd_sec_ops;
2111         aesni_mb_data->dev_config = aesni_mb_configure_dev;
2112         aesni_mb_data->feature_flags |= RTE_CRYPTODEV_FF_SECURITY;
2113 #endif
2114         aesni_mb_data->session_configure = aesni_mb_session_configure;
2115         aesni_mb_data->session_priv_size = sizeof(struct aesni_mb_session);
2116 }