cryptodev: change queue pair configure structure
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd_compat.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2017 Intel Corporation
3  */
4
5 #include <intel-ipsec-mb.h>
6
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
14
15 #include "rte_aesni_mb_pmd_private.h"
16
17 #define AES_CCM_DIGEST_MIN_LEN 4
18 #define AES_CCM_DIGEST_MAX_LEN 16
19 #define HMAC_MAX_BLOCK_SIZE 128
20 static uint8_t cryptodev_driver_id;
21
22 typedef void (*hash_one_block_t)(const void *data, void *digest);
23 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
24
25 /**
26  * Calculate the authentication pre-computes
27  *
28  * @param one_block_hash        Function pointer to calculate digest on ipad/opad
29  * @param ipad                  Inner pad output byte array
30  * @param opad                  Outer pad output byte array
31  * @param hkey                  Authentication key
32  * @param hkey_len              Authentication key length
33  * @param blocksize             Block size of selected hash algo
34  */
35 static void
36 calculate_auth_precomputes(hash_one_block_t one_block_hash,
37                 uint8_t *ipad, uint8_t *opad,
38                 uint8_t *hkey, uint16_t hkey_len,
39                 uint16_t blocksize)
40 {
41         unsigned i, length;
42
43         uint8_t ipad_buf[blocksize] __rte_aligned(16);
44         uint8_t opad_buf[blocksize] __rte_aligned(16);
45
46         /* Setup inner and outer pads */
47         memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
48         memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
49
50         /* XOR hash key with inner and outer pads */
51         length = hkey_len > blocksize ? blocksize : hkey_len;
52
53         for (i = 0; i < length; i++) {
54                 ipad_buf[i] ^= hkey[i];
55                 opad_buf[i] ^= hkey[i];
56         }
57
58         /* Compute partial hashes */
59         (*one_block_hash)(ipad_buf, ipad);
60         (*one_block_hash)(opad_buf, opad);
61
62         /* Clean up stack */
63         memset(ipad_buf, 0, blocksize);
64         memset(opad_buf, 0, blocksize);
65 }
66
67 /** Get xform chain order */
68 static enum aesni_mb_operation
69 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
70 {
71         if (xform == NULL)
72                 return AESNI_MB_OP_NOT_SUPPORTED;
73
74         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
75                 if (xform->next == NULL)
76                         return AESNI_MB_OP_CIPHER_ONLY;
77                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
78                         return AESNI_MB_OP_CIPHER_HASH;
79         }
80
81         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
82                 if (xform->next == NULL)
83                         return AESNI_MB_OP_HASH_ONLY;
84                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
85                         return AESNI_MB_OP_HASH_CIPHER;
86         }
87
88         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
89                 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM ||
90                                 xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
91                         if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
92                                 return AESNI_MB_OP_AEAD_CIPHER_HASH;
93                         else
94                                 return AESNI_MB_OP_AEAD_HASH_CIPHER;
95                 }
96         }
97
98         return AESNI_MB_OP_NOT_SUPPORTED;
99 }
100
101 /** Set session authentication parameters */
102 static int
103 aesni_mb_set_session_auth_parameters(const struct aesni_mb_op_fns *mb_ops,
104                 struct aesni_mb_session *sess,
105                 const struct rte_crypto_sym_xform *xform)
106 {
107         hash_one_block_t hash_oneblock_fn;
108         unsigned int key_larger_block_size = 0;
109         uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
110
111         if (xform == NULL) {
112                 sess->auth.algo = NULL_HASH;
113                 return 0;
114         }
115
116         if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
117                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
118                 return -1;
119         }
120
121         /* Set the request digest size */
122         sess->auth.req_digest_len = xform->auth.digest_length;
123
124         /* Select auth generate/verify */
125         sess->auth.operation = xform->auth.op;
126
127         /* Set Authentication Parameters */
128         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
129                 sess->auth.algo = AES_XCBC;
130
131                 uint16_t xcbc_mac_digest_len =
132                         get_truncated_digest_byte_length(AES_XCBC);
133                 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
134                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
135                         return -EINVAL;
136                 }
137                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
138                 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
139                                 sess->auth.xcbc.k1_expanded,
140                                 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
141                 return 0;
142         }
143
144         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
145                 sess->auth.algo = AES_CMAC;
146
147                 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
148
149                 if (sess->auth.req_digest_len > cmac_digest_len) {
150                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
151                         return -EINVAL;
152                 }
153                 /*
154                  * Multi-buffer lib supports digest sizes from 4 to 16 bytes
155                  * in version 0.50 and sizes of 12 and 16 bytes,
156                  * in version 0.49.
157                  * If size requested is different, generate the full digest
158                  * (16 bytes) in a temporary location and then memcpy
159                  * the requested number of bytes.
160                  */
161 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
162                 if (sess->auth.req_digest_len < 4)
163 #else
164                 uint16_t cmac_trunc_digest_len =
165                                 get_truncated_digest_byte_length(AES_CMAC);
166                 if (sess->auth.req_digest_len != cmac_digest_len &&
167                                 sess->auth.req_digest_len != cmac_trunc_digest_len)
168 #endif
169                         sess->auth.gen_digest_len = cmac_digest_len;
170                 else
171                         sess->auth.gen_digest_len = sess->auth.req_digest_len;
172                 (*mb_ops->aux.keyexp.aes_cmac_expkey)(xform->auth.key.data,
173                                 sess->auth.cmac.expkey);
174
175                 (*mb_ops->aux.keyexp.aes_cmac_subkey)(sess->auth.cmac.expkey,
176                                 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
177                 return 0;
178         }
179
180         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
181                 if (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) {
182                         sess->cipher.direction = ENCRYPT;
183                         sess->chain_order = CIPHER_HASH;
184                 } else
185                         sess->cipher.direction = DECRYPT;
186
187                 sess->auth.algo = AES_GMAC;
188                 /*
189                  * Multi-buffer lib supports 8, 12 and 16 bytes of digest.
190                  * If size requested is different, generate the full digest
191                  * (16 bytes) in a temporary location and then memcpy
192                  * the requested number of bytes.
193                  */
194                 if (sess->auth.req_digest_len != 16 &&
195                                 sess->auth.req_digest_len != 12 &&
196                                 sess->auth.req_digest_len != 8) {
197                         sess->auth.gen_digest_len = 16;
198                 } else {
199                         sess->auth.gen_digest_len = sess->auth.req_digest_len;
200                 }
201                 sess->iv.length = xform->auth.iv.length;
202                 sess->iv.offset = xform->auth.iv.offset;
203
204                 switch (xform->auth.key.length) {
205                 case AES_128_BYTES:
206                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
207                         (mb_ops->aux.keyexp.aes_gcm_128)(xform->auth.key.data,
208                                 &sess->cipher.gcm_key);
209                         break;
210                 case AES_192_BYTES:
211                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
212                         (mb_ops->aux.keyexp.aes_gcm_192)(xform->auth.key.data,
213                                 &sess->cipher.gcm_key);
214                         break;
215                 case AES_256_BYTES:
216                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
217                         (mb_ops->aux.keyexp.aes_gcm_256)(xform->auth.key.data,
218                                 &sess->cipher.gcm_key);
219                         break;
220                 default:
221                         RTE_LOG(ERR, PMD, "failed to parse test type\n");
222                         return -EINVAL;
223                 }
224
225                 return 0;
226         }
227
228         switch (xform->auth.algo) {
229         case RTE_CRYPTO_AUTH_MD5_HMAC:
230                 sess->auth.algo = MD5;
231                 hash_oneblock_fn = mb_ops->aux.one_block.md5;
232                 break;
233         case RTE_CRYPTO_AUTH_SHA1_HMAC:
234                 sess->auth.algo = SHA1;
235                 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
236 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
237                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
238                         mb_ops->aux.multi_block.sha1(
239                                 xform->auth.key.data,
240                                 xform->auth.key.length,
241                                 hashed_key);
242                         key_larger_block_size = 1;
243                 }
244 #endif
245                 break;
246         case RTE_CRYPTO_AUTH_SHA224_HMAC:
247                 sess->auth.algo = SHA_224;
248                 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
249 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
250                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
251                         mb_ops->aux.multi_block.sha224(
252                                 xform->auth.key.data,
253                                 xform->auth.key.length,
254                                 hashed_key);
255                         key_larger_block_size = 1;
256                 }
257 #endif
258                 break;
259         case RTE_CRYPTO_AUTH_SHA256_HMAC:
260                 sess->auth.algo = SHA_256;
261                 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
262 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
263                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
264                         mb_ops->aux.multi_block.sha256(
265                                 xform->auth.key.data,
266                                 xform->auth.key.length,
267                                 hashed_key);
268                         key_larger_block_size = 1;
269                 }
270 #endif
271                 break;
272         case RTE_CRYPTO_AUTH_SHA384_HMAC:
273                 sess->auth.algo = SHA_384;
274                 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
275 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
276                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
277                         mb_ops->aux.multi_block.sha384(
278                                 xform->auth.key.data,
279                                 xform->auth.key.length,
280                                 hashed_key);
281                         key_larger_block_size = 1;
282                 }
283 #endif
284                 break;
285         case RTE_CRYPTO_AUTH_SHA512_HMAC:
286                 sess->auth.algo = SHA_512;
287                 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
288 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
289                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
290                         mb_ops->aux.multi_block.sha512(
291                                 xform->auth.key.data,
292                                 xform->auth.key.length,
293                                 hashed_key);
294                         key_larger_block_size = 1;
295                 }
296 #endif
297                 break;
298         default:
299                 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
300                 return -ENOTSUP;
301         }
302         uint16_t trunc_digest_size =
303                         get_truncated_digest_byte_length(sess->auth.algo);
304         uint16_t full_digest_size =
305                         get_digest_byte_length(sess->auth.algo);
306
307 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
308         if (sess->auth.req_digest_len > full_digest_size ||
309                         sess->auth.req_digest_len == 0) {
310 #else
311         if (sess->auth.req_digest_len != trunc_digest_size) {
312 #endif
313                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
314                 return -EINVAL;
315         }
316
317         if (sess->auth.req_digest_len != trunc_digest_size &&
318                         sess->auth.req_digest_len != full_digest_size)
319                 sess->auth.gen_digest_len = full_digest_size;
320         else
321                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
322
323         /* Calculate Authentication precomputes */
324         if (key_larger_block_size) {
325                 calculate_auth_precomputes(hash_oneblock_fn,
326                         sess->auth.pads.inner, sess->auth.pads.outer,
327                         hashed_key,
328                         xform->auth.key.length,
329                         get_auth_algo_blocksize(sess->auth.algo));
330         } else {
331                 calculate_auth_precomputes(hash_oneblock_fn,
332                         sess->auth.pads.inner, sess->auth.pads.outer,
333                         xform->auth.key.data,
334                         xform->auth.key.length,
335                         get_auth_algo_blocksize(sess->auth.algo));
336         }
337
338         return 0;
339 }
340
341 /** Set session cipher parameters */
342 static int
343 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_op_fns *mb_ops,
344                 struct aesni_mb_session *sess,
345                 const struct rte_crypto_sym_xform *xform)
346 {
347         uint8_t is_aes = 0;
348         uint8_t is_3DES = 0;
349         aes_keyexp_t aes_keyexp_fn;
350
351         if (xform == NULL) {
352                 sess->cipher.mode = NULL_CIPHER;
353                 return 0;
354         }
355
356         if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
357                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
358                 return -EINVAL;
359         }
360
361         /* Select cipher direction */
362         switch (xform->cipher.op) {
363         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
364                 sess->cipher.direction = ENCRYPT;
365                 break;
366         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
367                 sess->cipher.direction = DECRYPT;
368                 break;
369         default:
370                 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
371                 return -EINVAL;
372         }
373
374         /* Select cipher mode */
375         switch (xform->cipher.algo) {
376         case RTE_CRYPTO_CIPHER_AES_CBC:
377                 sess->cipher.mode = CBC;
378                 is_aes = 1;
379                 break;
380         case RTE_CRYPTO_CIPHER_AES_CTR:
381                 sess->cipher.mode = CNTR;
382                 is_aes = 1;
383                 break;
384         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
385                 sess->cipher.mode = DOCSIS_SEC_BPI;
386                 is_aes = 1;
387                 break;
388         case RTE_CRYPTO_CIPHER_DES_CBC:
389                 sess->cipher.mode = DES;
390                 break;
391         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
392                 sess->cipher.mode = DOCSIS_DES;
393                 break;
394         case RTE_CRYPTO_CIPHER_3DES_CBC:
395                 sess->cipher.mode = DES3;
396                 is_3DES = 1;
397                 break;
398         default:
399                 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
400                 return -ENOTSUP;
401         }
402
403         /* Set IV parameters */
404         sess->iv.offset = xform->cipher.iv.offset;
405         sess->iv.length = xform->cipher.iv.length;
406
407         /* Check key length and choose key expansion function for AES */
408         if (is_aes) {
409                 switch (xform->cipher.key.length) {
410                 case AES_128_BYTES:
411                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
412                         aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
413                         break;
414                 case AES_192_BYTES:
415                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
416                         aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
417                         break;
418                 case AES_256_BYTES:
419                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
420                         aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
421                         break;
422                 default:
423                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
424                         return -EINVAL;
425                 }
426
427                 /* Expanded cipher keys */
428                 (*aes_keyexp_fn)(xform->cipher.key.data,
429                                 sess->cipher.expanded_aes_keys.encode,
430                                 sess->cipher.expanded_aes_keys.decode);
431
432         } else if (is_3DES) {
433                 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
434                                 sess->cipher.exp_3des_keys.key[1],
435                                 sess->cipher.exp_3des_keys.key[2]};
436
437                 switch (xform->cipher.key.length) {
438                 case  24:
439                         des_key_schedule(keys[0], xform->cipher.key.data);
440                         des_key_schedule(keys[1], xform->cipher.key.data+8);
441                         des_key_schedule(keys[2], xform->cipher.key.data+16);
442
443                         /* Initialize keys - 24 bytes: [K1-K2-K3] */
444                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
445                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
446                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
447                         break;
448                 case 16:
449                         des_key_schedule(keys[0], xform->cipher.key.data);
450                         des_key_schedule(keys[1], xform->cipher.key.data+8);
451
452                         /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
453                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
454                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
455                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
456                         break;
457                 case 8:
458                         des_key_schedule(keys[0], xform->cipher.key.data);
459
460                         /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
461                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
462                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
463                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
464                         break;
465                 default:
466                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
467                         return -EINVAL;
468                 }
469
470 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
471                 sess->cipher.key_length_in_bytes = 24;
472 #else
473                 sess->cipher.key_length_in_bytes = 8;
474 #endif
475         } else {
476                 if (xform->cipher.key.length != 8) {
477                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
478                         return -EINVAL;
479                 }
480                 sess->cipher.key_length_in_bytes = 8;
481
482                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.encode,
483                                 xform->cipher.key.data);
484                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.decode,
485                                 xform->cipher.key.data);
486         }
487
488         return 0;
489 }
490
491 static int
492 aesni_mb_set_session_aead_parameters(const struct aesni_mb_op_fns *mb_ops,
493                 struct aesni_mb_session *sess,
494                 const struct rte_crypto_sym_xform *xform)
495 {
496         union {
497                 aes_keyexp_t aes_keyexp_fn;
498                 aes_gcm_keyexp_t aes_gcm_keyexp_fn;
499         } keyexp;
500
501         switch (xform->aead.op) {
502         case RTE_CRYPTO_AEAD_OP_ENCRYPT:
503                 sess->cipher.direction = ENCRYPT;
504                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
505                 break;
506         case RTE_CRYPTO_AEAD_OP_DECRYPT:
507                 sess->cipher.direction = DECRYPT;
508                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
509                 break;
510         default:
511                 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
512                 return -EINVAL;
513         }
514
515         switch (xform->aead.algo) {
516         case RTE_CRYPTO_AEAD_AES_CCM:
517                 sess->cipher.mode = CCM;
518                 sess->auth.algo = AES_CCM;
519
520                 /* Check key length and choose key expansion function for AES */
521                 switch (xform->aead.key.length) {
522                 case AES_128_BYTES:
523                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
524                         keyexp.aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
525                         break;
526                 default:
527                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
528                         return -EINVAL;
529                 }
530
531                 /* Expanded cipher keys */
532                 (*keyexp.aes_keyexp_fn)(xform->aead.key.data,
533                                 sess->cipher.expanded_aes_keys.encode,
534                                 sess->cipher.expanded_aes_keys.decode);
535                 break;
536
537         case RTE_CRYPTO_AEAD_AES_GCM:
538                 sess->cipher.mode = GCM;
539                 sess->auth.algo = AES_GMAC;
540
541                 switch (xform->aead.key.length) {
542                 case AES_128_BYTES:
543                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
544                         keyexp.aes_gcm_keyexp_fn =
545                                         mb_ops->aux.keyexp.aes_gcm_128;
546                         break;
547                 case AES_192_BYTES:
548                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
549                         keyexp.aes_gcm_keyexp_fn =
550                                         mb_ops->aux.keyexp.aes_gcm_192;
551                         break;
552                 case AES_256_BYTES:
553                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
554                         keyexp.aes_gcm_keyexp_fn =
555                                         mb_ops->aux.keyexp.aes_gcm_256;
556                         break;
557                 default:
558                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
559                         return -EINVAL;
560                 }
561
562                 (keyexp.aes_gcm_keyexp_fn)(xform->aead.key.data,
563                                 &sess->cipher.gcm_key);
564                 break;
565
566         default:
567                 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
568                 return -ENOTSUP;
569         }
570
571         /* Set IV parameters */
572         sess->iv.offset = xform->aead.iv.offset;
573         sess->iv.length = xform->aead.iv.length;
574
575         sess->auth.req_digest_len = xform->aead.digest_length;
576         /* CCM digests must be between 4 and 16 and an even number */
577         if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
578                         sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
579                         (sess->auth.req_digest_len & 1) == 1) {
580                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
581                 return -EINVAL;
582         }
583         sess->auth.gen_digest_len = sess->auth.req_digest_len;
584
585         return 0;
586 }
587
588 /** Parse crypto xform chain and set private session parameters */
589 int
590 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
591                 struct aesni_mb_session *sess,
592                 const struct rte_crypto_sym_xform *xform)
593 {
594         const struct rte_crypto_sym_xform *auth_xform = NULL;
595         const struct rte_crypto_sym_xform *cipher_xform = NULL;
596         const struct rte_crypto_sym_xform *aead_xform = NULL;
597         int ret;
598
599         /* Select Crypto operation - hash then cipher / cipher then hash */
600         switch (aesni_mb_get_chain_order(xform)) {
601         case AESNI_MB_OP_HASH_CIPHER:
602                 sess->chain_order = HASH_CIPHER;
603                 auth_xform = xform;
604                 cipher_xform = xform->next;
605                 break;
606         case AESNI_MB_OP_CIPHER_HASH:
607                 sess->chain_order = CIPHER_HASH;
608                 auth_xform = xform->next;
609                 cipher_xform = xform;
610                 break;
611         case AESNI_MB_OP_HASH_ONLY:
612                 sess->chain_order = HASH_CIPHER;
613                 auth_xform = xform;
614                 cipher_xform = NULL;
615                 break;
616         case AESNI_MB_OP_CIPHER_ONLY:
617                 /*
618                  * Multi buffer library operates only at two modes,
619                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
620                  * chain order depends on cipher operation: encryption is always
621                  * the first operation and decryption the last one.
622                  */
623                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
624                         sess->chain_order = CIPHER_HASH;
625                 else
626                         sess->chain_order = HASH_CIPHER;
627                 auth_xform = NULL;
628                 cipher_xform = xform;
629                 break;
630         case AESNI_MB_OP_AEAD_CIPHER_HASH:
631                 sess->chain_order = CIPHER_HASH;
632                 sess->aead.aad_len = xform->aead.aad_length;
633                 aead_xform = xform;
634                 break;
635         case AESNI_MB_OP_AEAD_HASH_CIPHER:
636                 sess->chain_order = HASH_CIPHER;
637                 sess->aead.aad_len = xform->aead.aad_length;
638                 aead_xform = xform;
639                 break;
640         case AESNI_MB_OP_NOT_SUPPORTED:
641         default:
642                 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
643                 return -ENOTSUP;
644         }
645
646         /* Default IV length = 0 */
647         sess->iv.length = 0;
648
649         ret = aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform);
650         if (ret != 0) {
651                 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
652                 return ret;
653         }
654
655         ret = aesni_mb_set_session_cipher_parameters(mb_ops, sess,
656                         cipher_xform);
657         if (ret != 0) {
658                 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
659                 return ret;
660         }
661
662         if (aead_xform) {
663                 ret = aesni_mb_set_session_aead_parameters(mb_ops, sess,
664                                 aead_xform);
665                 if (ret != 0) {
666                         AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
667                         return ret;
668                 }
669         }
670
671         return 0;
672 }
673
674 /**
675  * burst enqueue, place crypto operations on ingress queue for processing.
676  *
677  * @param __qp         Queue Pair to process
678  * @param ops          Crypto operations for processing
679  * @param nb_ops       Number of crypto operations for processing
680  *
681  * @return
682  * - Number of crypto operations enqueued
683  */
684 static uint16_t
685 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
686                 uint16_t nb_ops)
687 {
688         struct aesni_mb_qp *qp = __qp;
689
690         unsigned int nb_enqueued;
691
692         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
693                         (void **)ops, nb_ops, NULL);
694
695         qp->stats.enqueued_count += nb_enqueued;
696
697         return nb_enqueued;
698 }
699
700 /** Get multi buffer session */
701 static inline struct aesni_mb_session *
702 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
703 {
704         struct aesni_mb_session *sess = NULL;
705
706         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
707                 if (likely(op->sym->session != NULL))
708                         sess = (struct aesni_mb_session *)
709                                         get_sym_session_private_data(
710                                         op->sym->session,
711                                         cryptodev_driver_id);
712         } else {
713                 void *_sess = NULL;
714                 void *_sess_private_data = NULL;
715
716                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
717                         return NULL;
718
719                 if (rte_mempool_get(qp->sess_mp_priv, (void **)&_sess_private_data))
720                         return NULL;
721
722                 sess = (struct aesni_mb_session *)_sess_private_data;
723
724                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
725                                 sess, op->sym->xform) != 0)) {
726                         rte_mempool_put(qp->sess_mp, _sess);
727                         rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
728                         sess = NULL;
729                 }
730                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
731                 set_sym_session_private_data(op->sym->session,
732                                 cryptodev_driver_id, _sess_private_data);
733         }
734
735         if (unlikely(sess == NULL))
736                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
737
738         return sess;
739 }
740
741 /**
742  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
743  * submission to the multi buffer library for processing.
744  *
745  * @param       qp      queue pair
746  * @param       job     JOB_AES_HMAC structure to fill
747  * @param       m       mbuf to process
748  *
749  * @return
750  * - Completed JOB_AES_HMAC structure pointer on success
751  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
752  */
753 static inline int
754 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
755                 struct rte_crypto_op *op, uint8_t *digest_idx)
756 {
757         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
758         struct aesni_mb_session *session;
759         uint16_t m_offset = 0;
760
761         session = get_session(qp, op);
762         if (session == NULL) {
763                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
764                 return -1;
765         }
766
767         /* Set crypto operation */
768         job->chain_order = session->chain_order;
769
770         /* Set cipher parameters */
771         job->cipher_direction = session->cipher.direction;
772         job->cipher_mode = session->cipher.mode;
773
774         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
775
776         /* Set authentication parameters */
777         job->hash_alg = session->auth.algo;
778
779         switch (job->hash_alg) {
780         case AES_XCBC:
781                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
782                 job->u.XCBC._k2 = session->auth.xcbc.k2;
783                 job->u.XCBC._k3 = session->auth.xcbc.k3;
784
785                 job->aes_enc_key_expanded =
786                                 session->cipher.expanded_aes_keys.encode;
787                 job->aes_dec_key_expanded =
788                                 session->cipher.expanded_aes_keys.decode;
789                 break;
790
791         case AES_CCM:
792                 job->u.CCM.aad = op->sym->aead.aad.data + 18;
793                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
794                 job->aes_enc_key_expanded =
795                                 session->cipher.expanded_aes_keys.encode;
796                 job->aes_dec_key_expanded =
797                                 session->cipher.expanded_aes_keys.decode;
798                 break;
799
800         case AES_CMAC:
801                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
802                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
803                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
804                 job->aes_enc_key_expanded =
805                                 session->cipher.expanded_aes_keys.encode;
806                 job->aes_dec_key_expanded =
807                                 session->cipher.expanded_aes_keys.decode;
808                 break;
809
810         case AES_GMAC:
811                 if (session->cipher.mode == GCM) {
812                         job->u.GCM.aad = op->sym->aead.aad.data;
813                         job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
814                 } else {
815                         /* For GMAC */
816                         job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
817                                         uint8_t *, op->sym->auth.data.offset);
818                         job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
819                         job->cipher_mode = GCM;
820                 }
821                 job->aes_enc_key_expanded = &session->cipher.gcm_key;
822                 job->aes_dec_key_expanded = &session->cipher.gcm_key;
823                 break;
824
825         default:
826                 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
827                 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
828
829                 if (job->cipher_mode == DES3) {
830                         job->aes_enc_key_expanded =
831                                 session->cipher.exp_3des_keys.ks_ptr;
832                         job->aes_dec_key_expanded =
833                                 session->cipher.exp_3des_keys.ks_ptr;
834                 } else {
835                         job->aes_enc_key_expanded =
836                                 session->cipher.expanded_aes_keys.encode;
837                         job->aes_dec_key_expanded =
838                                 session->cipher.expanded_aes_keys.decode;
839                 }
840         }
841
842         /* Mutable crypto operation parameters */
843         if (op->sym->m_dst) {
844                 m_src = m_dst = op->sym->m_dst;
845
846                 /* append space for output data to mbuf */
847                 char *odata = rte_pktmbuf_append(m_dst,
848                                 rte_pktmbuf_data_len(op->sym->m_src));
849                 if (odata == NULL) {
850                         AESNI_MB_LOG(ERR, "failed to allocate space in destination "
851                                         "mbuf for source data");
852                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
853                         return -1;
854                 }
855
856                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
857                                 rte_pktmbuf_data_len(op->sym->m_src));
858         } else {
859                 m_dst = m_src;
860                 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
861                                 session->cipher.mode == GCM))
862                         m_offset = op->sym->aead.data.offset;
863                 else
864                         m_offset = op->sym->cipher.data.offset;
865         }
866
867         /* Set digest output location */
868         if (job->hash_alg != NULL_HASH &&
869                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
870                 job->auth_tag_output = qp->temp_digests[*digest_idx];
871                 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
872         } else {
873                 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
874                                 session->cipher.mode == GCM))
875                         job->auth_tag_output = op->sym->aead.digest.data;
876                 else
877                         job->auth_tag_output = op->sym->auth.digest.data;
878
879                 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
880                         job->auth_tag_output = qp->temp_digests[*digest_idx];
881                         *digest_idx = (*digest_idx + 1) % MAX_JOBS;
882                 }
883         }
884         /*
885          * Multi-buffer library current only support returning a truncated
886          * digest length as specified in the relevant IPsec RFCs
887          */
888
889         /* Set digest length */
890         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
891
892         /* Set IV parameters */
893         job->iv_len_in_bytes = session->iv.length;
894
895         /* Data  Parameter */
896         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
897         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
898
899         switch (job->hash_alg) {
900         case AES_CCM:
901                 job->cipher_start_src_offset_in_bytes =
902                                 op->sym->aead.data.offset;
903                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
904                 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
905                 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
906
907                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
908                         session->iv.offset + 1);
909                 break;
910
911         case AES_GMAC:
912                 if (session->cipher.mode == GCM) {
913                         job->cipher_start_src_offset_in_bytes =
914                                         op->sym->aead.data.offset;
915                         job->hash_start_src_offset_in_bytes =
916                                         op->sym->aead.data.offset;
917                         job->msg_len_to_cipher_in_bytes =
918                                         op->sym->aead.data.length;
919                         job->msg_len_to_hash_in_bytes =
920                                         op->sym->aead.data.length;
921                 } else {
922                         job->cipher_start_src_offset_in_bytes =
923                                         op->sym->auth.data.offset;
924                         job->hash_start_src_offset_in_bytes =
925                                         op->sym->auth.data.offset;
926                         job->msg_len_to_cipher_in_bytes = 0;
927                         job->msg_len_to_hash_in_bytes = 0;
928                 }
929                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
930                                 session->iv.offset);
931
932                 break;
933
934         default:
935                 job->cipher_start_src_offset_in_bytes =
936                                 op->sym->cipher.data.offset;
937                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
938
939                 job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
940                 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
941
942                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
943                         session->iv.offset);
944         }
945
946         /* Set user data to be crypto operation data struct */
947         job->user_data = op;
948
949         return 0;
950 }
951
952 static inline void
953 verify_digest(JOB_AES_HMAC *job, void *digest, uint16_t len, uint8_t *status)
954 {
955         if (memcmp(job->auth_tag_output, digest, len) != 0)
956                 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
957 }
958
959 static inline void
960 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
961                 struct aesni_mb_session *sess)
962 {
963         /* No extra copy neeed */
964         if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
965                 return;
966
967         /*
968          * This can only happen for HMAC, so only digest
969          * for authentication algos is required
970          */
971         memcpy(op->sym->auth.digest.data, job->auth_tag_output,
972                         sess->auth.req_digest_len);
973 }
974
975 /**
976  * Process a completed job and return rte_mbuf which job processed
977  *
978  * @param qp            Queue Pair to process
979  * @param job   JOB_AES_HMAC job to process
980  *
981  * @return
982  * - Returns processed crypto operation.
983  * - Returns NULL on invalid job
984  */
985 static inline struct rte_crypto_op *
986 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
987 {
988         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
989         struct aesni_mb_session *sess = get_sym_session_private_data(
990                                                         op->sym->session,
991                                                         cryptodev_driver_id);
992
993         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
994                 switch (job->status) {
995                 case STS_COMPLETED:
996                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
997
998                         if (job->hash_alg == NULL_HASH)
999                                 break;
1000
1001                         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1002                                 if (job->hash_alg == AES_CCM ||
1003                                         (job->hash_alg == AES_GMAC &&
1004                                                 sess->cipher.mode == GCM))
1005                                         verify_digest(job,
1006                                                 op->sym->aead.digest.data,
1007                                                 sess->auth.req_digest_len,
1008                                                 &op->status);
1009                                 else
1010                                         verify_digest(job,
1011                                                 op->sym->auth.digest.data,
1012                                                 sess->auth.req_digest_len,
1013                                                 &op->status);
1014                         } else
1015                                 generate_digest(job, op, sess);
1016                         break;
1017                 default:
1018                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1019                 }
1020         }
1021
1022         /* Free session if a session-less crypto op */
1023         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1024                 memset(sess, 0, sizeof(struct aesni_mb_session));
1025                 memset(op->sym->session, 0,
1026                                 rte_cryptodev_sym_get_header_session_size());
1027                 rte_mempool_put(qp->sess_mp_priv, sess);
1028                 rte_mempool_put(qp->sess_mp, op->sym->session);
1029                 op->sym->session = NULL;
1030         }
1031
1032         return op;
1033 }
1034
1035 /**
1036  * Process a completed JOB_AES_HMAC job and keep processing jobs until
1037  * get_completed_job return NULL
1038  *
1039  * @param qp            Queue Pair to process
1040  * @param job           JOB_AES_HMAC job
1041  *
1042  * @return
1043  * - Number of processed jobs
1044  */
1045 static unsigned
1046 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
1047                 struct rte_crypto_op **ops, uint16_t nb_ops)
1048 {
1049         struct rte_crypto_op *op = NULL;
1050         unsigned processed_jobs = 0;
1051
1052         while (job != NULL) {
1053                 op = post_process_mb_job(qp, job);
1054
1055                 if (op) {
1056                         ops[processed_jobs++] = op;
1057                         qp->stats.dequeued_count++;
1058                 } else {
1059                         qp->stats.dequeue_err_count++;
1060                         break;
1061                 }
1062                 if (processed_jobs == nb_ops)
1063                         break;
1064
1065                 job = (*qp->op_fns->job.get_completed_job)(qp->mb_mgr);
1066         }
1067
1068         return processed_jobs;
1069 }
1070
1071 static inline uint16_t
1072 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
1073                 uint16_t nb_ops)
1074 {
1075         int processed_ops = 0;
1076
1077         /* Flush the remaining jobs */
1078         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(qp->mb_mgr);
1079
1080         if (job)
1081                 processed_ops += handle_completed_jobs(qp, job,
1082                                 &ops[processed_ops], nb_ops - processed_ops);
1083
1084         return processed_ops;
1085 }
1086
1087 static inline JOB_AES_HMAC *
1088 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
1089 {
1090         job->chain_order = HASH_CIPHER;
1091         job->cipher_mode = NULL_CIPHER;
1092         job->hash_alg = NULL_HASH;
1093         job->cipher_direction = DECRYPT;
1094
1095         /* Set user data to be crypto operation data struct */
1096         job->user_data = op;
1097
1098         return job;
1099 }
1100
1101 static uint16_t
1102 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1103                 uint16_t nb_ops)
1104 {
1105         struct aesni_mb_qp *qp = queue_pair;
1106
1107         struct rte_crypto_op *op;
1108         JOB_AES_HMAC *job;
1109
1110         int retval, processed_jobs = 0;
1111
1112         if (unlikely(nb_ops == 0))
1113                 return 0;
1114
1115         uint8_t digest_idx = qp->digest_idx;
1116         do {
1117                 /* Get next free mb job struct from mb manager */
1118                 job = (*qp->op_fns->job.get_next)(qp->mb_mgr);
1119                 if (unlikely(job == NULL)) {
1120                         /* if no free mb job structs we need to flush mb_mgr */
1121                         processed_jobs += flush_mb_mgr(qp,
1122                                         &ops[processed_jobs],
1123                                         nb_ops - processed_jobs);
1124
1125                         if (nb_ops == processed_jobs)
1126                                 break;
1127
1128                         job = (*qp->op_fns->job.get_next)(qp->mb_mgr);
1129                 }
1130
1131                 /*
1132                  * Get next operation to process from ingress queue.
1133                  * There is no need to return the job to the MB_MGR
1134                  * if there are no more operations to process, since the MB_MGR
1135                  * can use that pointer again in next get_next calls.
1136                  */
1137                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1138                 if (retval < 0)
1139                         break;
1140
1141                 retval = set_mb_job_params(job, qp, op, &digest_idx);
1142                 if (unlikely(retval != 0)) {
1143                         qp->stats.dequeue_err_count++;
1144                         set_job_null_op(job, op);
1145                 }
1146
1147                 /* Submit job to multi-buffer for processing */
1148                 job = (*qp->op_fns->job.submit)(qp->mb_mgr);
1149
1150                 /*
1151                  * If submit returns a processed job then handle it,
1152                  * before submitting subsequent jobs
1153                  */
1154                 if (job)
1155                         processed_jobs += handle_completed_jobs(qp, job,
1156                                         &ops[processed_jobs],
1157                                         nb_ops - processed_jobs);
1158
1159         } while (processed_jobs < nb_ops);
1160
1161         qp->digest_idx = digest_idx;
1162
1163         if (processed_jobs < 1)
1164                 processed_jobs += flush_mb_mgr(qp,
1165                                 &ops[processed_jobs],
1166                                 nb_ops - processed_jobs);
1167
1168         return processed_jobs;
1169 }
1170
1171 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1172
1173 static int
1174 cryptodev_aesni_mb_create(const char *name,
1175                         struct rte_vdev_device *vdev,
1176                         struct rte_cryptodev_pmd_init_params *init_params)
1177 {
1178         struct rte_cryptodev *dev;
1179         struct aesni_mb_private *internals;
1180         enum aesni_mb_vector_mode vector_mode;
1181
1182         /* Check CPU for support for AES instruction set */
1183         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
1184                 AESNI_MB_LOG(ERR, "AES instructions not supported by CPU");
1185                 return -EFAULT;
1186         }
1187
1188         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1189         if (dev == NULL) {
1190                 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1191                 return -ENODEV;
1192         }
1193
1194         /* Check CPU for supported vector instruction set */
1195         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1196                 vector_mode = RTE_AESNI_MB_AVX512;
1197         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1198                 vector_mode = RTE_AESNI_MB_AVX2;
1199         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1200                 vector_mode = RTE_AESNI_MB_AVX;
1201         else
1202                 vector_mode = RTE_AESNI_MB_SSE;
1203
1204         dev->driver_id = cryptodev_driver_id;
1205         dev->dev_ops = rte_aesni_mb_pmd_ops;
1206
1207         /* register rx/tx burst functions for data path */
1208         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1209         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1210
1211         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1212                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1213                         RTE_CRYPTODEV_FF_CPU_AESNI;
1214
1215         switch (vector_mode) {
1216         case RTE_AESNI_MB_SSE:
1217                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
1218                 break;
1219         case RTE_AESNI_MB_AVX:
1220                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
1221                 break;
1222         case RTE_AESNI_MB_AVX2:
1223                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
1224                 break;
1225         case RTE_AESNI_MB_AVX512:
1226                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
1227                 break;
1228         default:
1229                 break;
1230         }
1231
1232         /* Set vector instructions mode supported */
1233         internals = dev->data->dev_private;
1234
1235         internals->vector_mode = vector_mode;
1236         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1237
1238 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
1239         AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1240                         imb_get_version_str());
1241 #else
1242         AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: 0.49.0\n");
1243 #endif
1244
1245         return 0;
1246 }
1247
1248 static int
1249 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1250 {
1251         struct rte_cryptodev_pmd_init_params init_params = {
1252                 "",
1253                 sizeof(struct aesni_mb_private),
1254                 rte_socket_id(),
1255                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1256         };
1257         const char *name, *args;
1258         int retval;
1259
1260         name = rte_vdev_device_name(vdev);
1261         if (name == NULL)
1262                 return -EINVAL;
1263
1264         args = rte_vdev_device_args(vdev);
1265
1266         retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1267         if (retval) {
1268                 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1269                                 args);
1270                 return -EINVAL;
1271         }
1272
1273         return cryptodev_aesni_mb_create(name, vdev, &init_params);
1274 }
1275
1276 static int
1277 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1278 {
1279         struct rte_cryptodev *cryptodev;
1280         const char *name;
1281
1282         name = rte_vdev_device_name(vdev);
1283         if (name == NULL)
1284                 return -EINVAL;
1285
1286         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1287         if (cryptodev == NULL)
1288                 return -ENODEV;
1289
1290         return rte_cryptodev_pmd_destroy(cryptodev);
1291 }
1292
1293 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
1294         .probe = cryptodev_aesni_mb_probe,
1295         .remove = cryptodev_aesni_mb_remove
1296 };
1297
1298 static struct cryptodev_driver aesni_mb_crypto_drv;
1299
1300 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
1301 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
1302 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
1303         "max_nb_queue_pairs=<int> "
1304         "socket_id=<int>");
1305 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
1306                 cryptodev_aesni_mb_pmd_drv.driver,
1307                 cryptodev_driver_id);
1308
1309 RTE_INIT(aesni_mb_init_log)
1310 {
1311         aesni_mb_logtype_driver = rte_log_register("pmd.crypto.aesni_mb");
1312 }