cryptodev: rename functions to get session size
[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                 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                 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         aes_keyexp_t aes_keyexp_fn;
186
187         if (xform == NULL) {
188                 sess->cipher.mode = NULL_CIPHER;
189                 return 0;
190         }
191
192         if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
193                 MB_LOG_ERR("Crypto xform struct not of type cipher");
194                 return -EINVAL;
195         }
196
197         /* Select cipher direction */
198         switch (xform->cipher.op) {
199         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
200                 sess->cipher.direction = ENCRYPT;
201                 break;
202         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
203                 sess->cipher.direction = DECRYPT;
204                 break;
205         default:
206                 MB_LOG_ERR("Invalid cipher operation parameter");
207                 return -EINVAL;
208         }
209
210         /* Select cipher mode */
211         switch (xform->cipher.algo) {
212         case RTE_CRYPTO_CIPHER_AES_CBC:
213                 sess->cipher.mode = CBC;
214                 is_aes = 1;
215                 break;
216         case RTE_CRYPTO_CIPHER_AES_CTR:
217                 sess->cipher.mode = CNTR;
218                 is_aes = 1;
219                 break;
220         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
221                 sess->cipher.mode = DOCSIS_SEC_BPI;
222                 is_aes = 1;
223                 break;
224         case RTE_CRYPTO_CIPHER_DES_CBC:
225                 sess->cipher.mode = DES;
226                 break;
227         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
228                 sess->cipher.mode = DOCSIS_DES;
229                 break;
230         default:
231                 MB_LOG_ERR("Unsupported cipher mode parameter");
232                 return -ENOTSUP;
233         }
234
235         /* Set IV parameters */
236         sess->iv.offset = xform->cipher.iv.offset;
237         sess->iv.length = xform->cipher.iv.length;
238
239         /* Check key length and choose key expansion function for AES */
240         if (is_aes) {
241                 switch (xform->cipher.key.length) {
242                 case AES_128_BYTES:
243                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
244                         aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
245                         break;
246                 case AES_192_BYTES:
247                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
248                         aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
249                         break;
250                 case AES_256_BYTES:
251                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
252                         aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
253                         break;
254                 default:
255                         MB_LOG_ERR("Invalid cipher key length");
256                         return -EINVAL;
257                 }
258
259                 /* Expanded cipher keys */
260                 (*aes_keyexp_fn)(xform->cipher.key.data,
261                                 sess->cipher.expanded_aes_keys.encode,
262                                 sess->cipher.expanded_aes_keys.decode);
263
264         } else {
265                 if (xform->cipher.key.length != 8) {
266                         MB_LOG_ERR("Invalid cipher key length");
267                         return -EINVAL;
268                 }
269                 sess->cipher.key_length_in_bytes = 8;
270
271                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.encode,
272                                 xform->cipher.key.data);
273                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.decode,
274                                 xform->cipher.key.data);
275         }
276
277         return 0;
278 }
279
280 static int
281 aesni_mb_set_session_aead_parameters(const struct aesni_mb_op_fns *mb_ops,
282                 struct aesni_mb_session *sess,
283                 const struct rte_crypto_sym_xform *xform)
284 {
285         aes_keyexp_t aes_keyexp_fn;
286
287         switch (xform->aead.op) {
288         case RTE_CRYPTO_AEAD_OP_ENCRYPT:
289                 sess->cipher.direction = ENCRYPT;
290                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
291                 break;
292         case RTE_CRYPTO_AEAD_OP_DECRYPT:
293                 sess->cipher.direction = DECRYPT;
294                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
295                 break;
296         default:
297                 MB_LOG_ERR("Invalid aead operation parameter");
298                 return -EINVAL;
299         }
300
301         switch (xform->aead.algo) {
302         case RTE_CRYPTO_AEAD_AES_CCM:
303                 sess->cipher.mode = CCM;
304                 sess->auth.algo = AES_CCM;
305                 break;
306         default:
307                 MB_LOG_ERR("Unsupported aead mode parameter");
308                 return -ENOTSUP;
309         }
310
311         /* Set IV parameters */
312         sess->iv.offset = xform->aead.iv.offset;
313         sess->iv.length = xform->aead.iv.length;
314
315         /* Check key length and choose key expansion function for AES */
316
317         switch (xform->aead.key.length) {
318         case AES_128_BYTES:
319                 sess->cipher.key_length_in_bytes = AES_128_BYTES;
320                 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
321                 break;
322         default:
323                 MB_LOG_ERR("Invalid cipher key length");
324                 return -EINVAL;
325         }
326
327         /* Expanded cipher keys */
328         (*aes_keyexp_fn)(xform->aead.key.data,
329                         sess->cipher.expanded_aes_keys.encode,
330                         sess->cipher.expanded_aes_keys.decode);
331
332         return 0;
333 }
334
335 /** Parse crypto xform chain and set private session parameters */
336 int
337 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
338                 struct aesni_mb_session *sess,
339                 const struct rte_crypto_sym_xform *xform)
340 {
341         const struct rte_crypto_sym_xform *auth_xform = NULL;
342         const struct rte_crypto_sym_xform *cipher_xform = NULL;
343         const struct rte_crypto_sym_xform *aead_xform = NULL;
344         int ret;
345
346         /* Select Crypto operation - hash then cipher / cipher then hash */
347         switch (aesni_mb_get_chain_order(xform)) {
348         case AESNI_MB_OP_HASH_CIPHER:
349                 sess->chain_order = HASH_CIPHER;
350                 auth_xform = xform;
351                 cipher_xform = xform->next;
352                 sess->auth.digest_len = xform->auth.digest_length;
353                 break;
354         case AESNI_MB_OP_CIPHER_HASH:
355                 sess->chain_order = CIPHER_HASH;
356                 auth_xform = xform->next;
357                 cipher_xform = xform;
358                 sess->auth.digest_len = xform->auth.digest_length;
359                 break;
360         case AESNI_MB_OP_HASH_ONLY:
361                 sess->chain_order = HASH_CIPHER;
362                 auth_xform = xform;
363                 cipher_xform = NULL;
364                 sess->auth.digest_len = xform->auth.digest_length;
365                 break;
366         case AESNI_MB_OP_CIPHER_ONLY:
367                 /*
368                  * Multi buffer library operates only at two modes,
369                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
370                  * chain order depends on cipher operation: encryption is always
371                  * the first operation and decryption the last one.
372                  */
373                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
374                         sess->chain_order = CIPHER_HASH;
375                 else
376                         sess->chain_order = HASH_CIPHER;
377                 auth_xform = NULL;
378                 cipher_xform = xform;
379                 break;
380         case AESNI_MB_OP_AEAD_CIPHER_HASH:
381                 sess->chain_order = CIPHER_HASH;
382                 sess->aead.aad_len = xform->aead.aad_length;
383                 sess->auth.digest_len = xform->aead.digest_length;
384                 aead_xform = xform;
385                 break;
386         case AESNI_MB_OP_AEAD_HASH_CIPHER:
387                 sess->chain_order = HASH_CIPHER;
388                 sess->aead.aad_len = xform->aead.aad_length;
389                 sess->auth.digest_len = xform->aead.digest_length;
390                 aead_xform = xform;
391                 break;
392         case AESNI_MB_OP_NOT_SUPPORTED:
393         default:
394                 MB_LOG_ERR("Unsupported operation chain order parameter");
395                 return -ENOTSUP;
396         }
397
398         /* Default IV length = 0 */
399         sess->iv.length = 0;
400
401         ret = aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform);
402         if (ret != 0) {
403                 MB_LOG_ERR("Invalid/unsupported authentication parameters");
404                 return ret;
405         }
406
407         ret = aesni_mb_set_session_cipher_parameters(mb_ops, sess,
408                         cipher_xform);
409         if (ret != 0) {
410                 MB_LOG_ERR("Invalid/unsupported cipher parameters");
411                 return ret;
412         }
413
414         if (aead_xform) {
415                 ret = aesni_mb_set_session_aead_parameters(mb_ops, sess,
416                                 aead_xform);
417                 if (ret != 0) {
418                         MB_LOG_ERR("Invalid/unsupported aead parameters");
419                         return ret;
420                 }
421         }
422
423         return 0;
424 }
425
426 /**
427  * burst enqueue, place crypto operations on ingress queue for processing.
428  *
429  * @param __qp         Queue Pair to process
430  * @param ops          Crypto operations for processing
431  * @param nb_ops       Number of crypto operations for processing
432  *
433  * @return
434  * - Number of crypto operations enqueued
435  */
436 static uint16_t
437 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
438                 uint16_t nb_ops)
439 {
440         struct aesni_mb_qp *qp = __qp;
441
442         unsigned int nb_enqueued;
443
444         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
445                         (void **)ops, nb_ops, NULL);
446
447         qp->stats.enqueued_count += nb_enqueued;
448
449         return nb_enqueued;
450 }
451
452 /** Get multi buffer session */
453 static inline struct aesni_mb_session *
454 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
455 {
456         struct aesni_mb_session *sess = NULL;
457
458         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
459                 if (likely(op->sym->session != NULL))
460                         sess = (struct aesni_mb_session *)
461                                         get_session_private_data(
462                                         op->sym->session,
463                                         cryptodev_driver_id);
464         } else {
465                 void *_sess = NULL;
466                 void *_sess_private_data = NULL;
467
468                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
469                         return NULL;
470
471                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
472                         return NULL;
473
474                 sess = (struct aesni_mb_session *)_sess_private_data;
475
476                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
477                                 sess, op->sym->xform) != 0)) {
478                         rte_mempool_put(qp->sess_mp, _sess);
479                         rte_mempool_put(qp->sess_mp, _sess_private_data);
480                         sess = NULL;
481                 }
482                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
483                 set_session_private_data(op->sym->session, cryptodev_driver_id,
484                         _sess_private_data);
485         }
486
487         if (unlikely(sess == NULL))
488                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
489
490         return sess;
491 }
492
493 /**
494  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
495  * submission to the multi buffer library for processing.
496  *
497  * @param       qp      queue pair
498  * @param       job     JOB_AES_HMAC structure to fill
499  * @param       m       mbuf to process
500  *
501  * @return
502  * - Completed JOB_AES_HMAC structure pointer on success
503  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
504  */
505 static inline int
506 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
507                 struct rte_crypto_op *op, uint8_t *digest_idx)
508 {
509         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
510         struct aesni_mb_session *session;
511         uint16_t m_offset = 0;
512
513         session = get_session(qp, op);
514         if (session == NULL) {
515                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
516                 return -1;
517         }
518
519         /* Set crypto operation */
520         job->chain_order = session->chain_order;
521
522         /* Set cipher parameters */
523         job->cipher_direction = session->cipher.direction;
524         job->cipher_mode = session->cipher.mode;
525
526         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
527         job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
528         job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
529
530
531         /* Set authentication parameters */
532         job->hash_alg = session->auth.algo;
533         if (job->hash_alg == AES_XCBC) {
534                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
535                 job->u.XCBC._k2 = session->auth.xcbc.k2;
536                 job->u.XCBC._k3 = session->auth.xcbc.k3;
537         } else if (job->hash_alg == AES_CCM) {
538                 job->u.CCM.aad = op->sym->aead.aad.data + 18;
539                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
540         } else if (job->hash_alg == AES_CMAC) {
541                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
542                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
543                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
544
545         } else {
546                 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
547                 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
548         }
549
550         /* Mutable crypto operation parameters */
551         if (op->sym->m_dst) {
552                 m_src = m_dst = op->sym->m_dst;
553
554                 /* append space for output data to mbuf */
555                 char *odata = rte_pktmbuf_append(m_dst,
556                                 rte_pktmbuf_data_len(op->sym->m_src));
557                 if (odata == NULL) {
558                         MB_LOG_ERR("failed to allocate space in destination "
559                                         "mbuf for source data");
560                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
561                         return -1;
562                 }
563
564                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
565                                 rte_pktmbuf_data_len(op->sym->m_src));
566         } else {
567                 m_dst = m_src;
568                 if (job->hash_alg == AES_CCM)
569                         m_offset = op->sym->aead.data.offset;
570                 else
571                         m_offset = op->sym->cipher.data.offset;
572         }
573
574         /* Set digest output location */
575         if (job->hash_alg != NULL_HASH &&
576                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
577                 job->auth_tag_output = qp->temp_digests[*digest_idx];
578                 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
579         } else {
580                 if (job->hash_alg == AES_CCM)
581                         job->auth_tag_output = op->sym->aead.digest.data;
582                 else
583                         job->auth_tag_output = op->sym->auth.digest.data;
584         }
585
586         /*
587          * Multi-buffer library current only support returning a truncated
588          * digest length as specified in the relevant IPsec RFCs
589          */
590         if (job->hash_alg != AES_CCM && job->hash_alg != AES_CMAC)
591                 job->auth_tag_output_len_in_bytes =
592                                 get_truncated_digest_byte_length(job->hash_alg);
593         else
594                 job->auth_tag_output_len_in_bytes = session->auth.digest_len;
595
596
597         /* Set IV parameters */
598
599         job->iv_len_in_bytes = session->iv.length;
600
601         /* Data  Parameter */
602         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
603         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
604
605         if (job->hash_alg == AES_CCM) {
606                 job->cipher_start_src_offset_in_bytes =
607                                 op->sym->aead.data.offset;
608                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
609                 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
610                 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
611
612                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
613                         session->iv.offset + 1);
614         } else {
615                 job->cipher_start_src_offset_in_bytes =
616                                 op->sym->cipher.data.offset;
617                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
618
619                 job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
620                 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
621
622                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
623                         session->iv.offset);
624         }
625
626         /* Set user data to be crypto operation data struct */
627         job->user_data = op;
628
629         return 0;
630 }
631
632 static inline void
633 verify_digest(struct aesni_mb_qp *qp __rte_unused, JOB_AES_HMAC *job,
634                 struct rte_crypto_op *op) {
635         /* Verify digest if required */
636         if (job->hash_alg == AES_CCM) {
637                 if (memcmp(job->auth_tag_output, op->sym->aead.digest.data,
638                                 job->auth_tag_output_len_in_bytes) != 0)
639                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
640         } else {
641                 if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
642                                 job->auth_tag_output_len_in_bytes) != 0)
643                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
644         }
645 }
646
647 /**
648  * Process a completed job and return rte_mbuf which job processed
649  *
650  * @param qp            Queue Pair to process
651  * @param job   JOB_AES_HMAC job to process
652  *
653  * @return
654  * - Returns processed crypto operation.
655  * - Returns NULL on invalid job
656  */
657 static inline struct rte_crypto_op *
658 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
659 {
660         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
661         struct aesni_mb_session *sess = get_session_private_data(
662                                                         op->sym->session,
663                                                         cryptodev_driver_id);
664
665         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
666                 switch (job->status) {
667                 case STS_COMPLETED:
668                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
669
670                         if (job->hash_alg != NULL_HASH) {
671                                 if (sess->auth.operation ==
672                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
673                                         verify_digest(qp, job, op);
674                         }
675                         break;
676                 default:
677                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
678                 }
679         }
680
681         /* Free session if a session-less crypto op */
682         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
683                 memset(sess, 0, sizeof(struct aesni_mb_session));
684                 memset(op->sym->session, 0,
685                                 rte_cryptodev_sym_get_header_session_size());
686                 rte_mempool_put(qp->sess_mp, sess);
687                 rte_mempool_put(qp->sess_mp, op->sym->session);
688                 op->sym->session = NULL;
689         }
690
691         return op;
692 }
693
694 /**
695  * Process a completed JOB_AES_HMAC job and keep processing jobs until
696  * get_completed_job return NULL
697  *
698  * @param qp            Queue Pair to process
699  * @param job           JOB_AES_HMAC job
700  *
701  * @return
702  * - Number of processed jobs
703  */
704 static unsigned
705 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
706                 struct rte_crypto_op **ops, uint16_t nb_ops)
707 {
708         struct rte_crypto_op *op = NULL;
709         unsigned processed_jobs = 0;
710
711         while (job != NULL) {
712                 op = post_process_mb_job(qp, job);
713
714                 if (op) {
715                         ops[processed_jobs++] = op;
716                         qp->stats.dequeued_count++;
717                 } else {
718                         qp->stats.dequeue_err_count++;
719                         break;
720                 }
721                 if (processed_jobs == nb_ops)
722                         break;
723
724                 job = (*qp->op_fns->job.get_completed_job)(&qp->mb_mgr);
725         }
726
727         return processed_jobs;
728 }
729
730 static inline uint16_t
731 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
732                 uint16_t nb_ops)
733 {
734         int processed_ops = 0;
735
736         /* Flush the remaining jobs */
737         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(&qp->mb_mgr);
738
739         if (job)
740                 processed_ops += handle_completed_jobs(qp, job,
741                                 &ops[processed_ops], nb_ops - processed_ops);
742
743         return processed_ops;
744 }
745
746 static inline JOB_AES_HMAC *
747 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
748 {
749         job->chain_order = HASH_CIPHER;
750         job->cipher_mode = NULL_CIPHER;
751         job->hash_alg = NULL_HASH;
752         job->cipher_direction = DECRYPT;
753
754         /* Set user data to be crypto operation data struct */
755         job->user_data = op;
756
757         return job;
758 }
759
760 static uint16_t
761 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
762                 uint16_t nb_ops)
763 {
764         struct aesni_mb_qp *qp = queue_pair;
765
766         struct rte_crypto_op *op;
767         JOB_AES_HMAC *job;
768
769         int retval, processed_jobs = 0;
770
771         if (unlikely(nb_ops == 0))
772                 return 0;
773
774         uint8_t digest_idx = qp->digest_idx;
775         do {
776                 /* Get next operation to process from ingress queue */
777                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
778                 if (retval < 0)
779                         break;
780
781                 /* Get next free mb job struct from mb manager */
782                 job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
783                 if (unlikely(job == NULL)) {
784                         /* if no free mb job structs we need to flush mb_mgr */
785                         processed_jobs += flush_mb_mgr(qp,
786                                         &ops[processed_jobs],
787                                         (nb_ops - processed_jobs) - 1);
788
789                         job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
790                 }
791
792                 retval = set_mb_job_params(job, qp, op, &digest_idx);
793                 if (unlikely(retval != 0)) {
794                         qp->stats.dequeue_err_count++;
795                         set_job_null_op(job, op);
796                 }
797
798                 /* Submit job to multi-buffer for processing */
799                 job = (*qp->op_fns->job.submit)(&qp->mb_mgr);
800
801                 /*
802                  * If submit returns a processed job then handle it,
803                  * before submitting subsequent jobs
804                  */
805                 if (job)
806                         processed_jobs += handle_completed_jobs(qp, job,
807                                         &ops[processed_jobs],
808                                         nb_ops - processed_jobs);
809
810         } while (processed_jobs < nb_ops);
811
812         qp->digest_idx = digest_idx;
813
814         if (processed_jobs < 1)
815                 processed_jobs += flush_mb_mgr(qp,
816                                 &ops[processed_jobs],
817                                 nb_ops - processed_jobs);
818
819         return processed_jobs;
820 }
821
822 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
823
824 static int
825 cryptodev_aesni_mb_create(const char *name,
826                         struct rte_vdev_device *vdev,
827                         struct rte_cryptodev_pmd_init_params *init_params)
828 {
829         struct rte_cryptodev *dev;
830         struct aesni_mb_private *internals;
831         enum aesni_mb_vector_mode vector_mode;
832
833         /* Check CPU for support for AES instruction set */
834         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
835                 MB_LOG_ERR("AES instructions not supported by CPU");
836                 return -EFAULT;
837         }
838
839         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
840         if (dev == NULL) {
841                 MB_LOG_ERR("failed to create cryptodev vdev");
842                 return -ENODEV;
843         }
844
845         /* Check CPU for supported vector instruction set */
846         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
847                 vector_mode = RTE_AESNI_MB_AVX512;
848         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
849                 vector_mode = RTE_AESNI_MB_AVX2;
850         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
851                 vector_mode = RTE_AESNI_MB_AVX;
852         else
853                 vector_mode = RTE_AESNI_MB_SSE;
854
855         dev->driver_id = cryptodev_driver_id;
856         dev->dev_ops = rte_aesni_mb_pmd_ops;
857
858         /* register rx/tx burst functions for data path */
859         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
860         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
861
862         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
863                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
864                         RTE_CRYPTODEV_FF_CPU_AESNI;
865
866         switch (vector_mode) {
867         case RTE_AESNI_MB_SSE:
868                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
869                 break;
870         case RTE_AESNI_MB_AVX:
871                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
872                 break;
873         case RTE_AESNI_MB_AVX2:
874                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
875                 break;
876         case RTE_AESNI_MB_AVX512:
877                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
878                 break;
879         default:
880                 break;
881         }
882
883         /* Set vector instructions mode supported */
884         internals = dev->data->dev_private;
885
886         internals->vector_mode = vector_mode;
887         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
888         internals->max_nb_sessions = init_params->max_nb_sessions;
889
890         return 0;
891 }
892
893 static int
894 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
895 {
896         struct rte_cryptodev_pmd_init_params init_params = {
897                 "",
898                 sizeof(struct aesni_mb_private),
899                 rte_socket_id(),
900                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
901                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
902         };
903         const char *name, *args;
904         int retval;
905
906         name = rte_vdev_device_name(vdev);
907         if (name == NULL)
908                 return -EINVAL;
909
910         args = rte_vdev_device_args(vdev);
911
912         retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
913         if (retval) {
914                 MB_LOG_ERR("Failed to parse initialisation arguments[%s]\n",
915                                 args);
916                 return -EINVAL;
917         }
918
919         return cryptodev_aesni_mb_create(name, vdev, &init_params);
920 }
921
922 static int
923 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
924 {
925         struct rte_cryptodev *cryptodev;
926         const char *name;
927
928         name = rte_vdev_device_name(vdev);
929         if (name == NULL)
930                 return -EINVAL;
931
932         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
933         if (cryptodev == NULL)
934                 return -ENODEV;
935
936         return rte_cryptodev_pmd_destroy(cryptodev);
937 }
938
939 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
940         .probe = cryptodev_aesni_mb_probe,
941         .remove = cryptodev_aesni_mb_remove
942 };
943
944 static struct cryptodev_driver aesni_mb_crypto_drv;
945
946 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
947 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
948 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
949         "max_nb_queue_pairs=<int> "
950         "max_nb_sessions=<int> "
951         "socket_id=<int>");
952 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
953                 cryptodev_aesni_mb_pmd_drv.driver,
954                 cryptodev_driver_id);