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