45b25c9d59051a3a49636edb0fe34e78ad5f1126
[deb_dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_vdev.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
40
41 #include "rte_aesni_mb_pmd_private.h"
42
43 typedef void (*hash_one_block_t)(const void *data, void *digest);
44 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
45
46 /**
47  * Calculate the authentication pre-computes
48  *
49  * @param one_block_hash        Function pointer to calculate digest on ipad/opad
50  * @param ipad                  Inner pad output byte array
51  * @param opad                  Outer pad output byte array
52  * @param hkey                  Authentication key
53  * @param hkey_len              Authentication key length
54  * @param blocksize             Block size of selected hash algo
55  */
56 static void
57 calculate_auth_precomputes(hash_one_block_t one_block_hash,
58                 uint8_t *ipad, uint8_t *opad,
59                 uint8_t *hkey, uint16_t hkey_len,
60                 uint16_t blocksize)
61 {
62         unsigned i, length;
63
64         uint8_t ipad_buf[blocksize] __rte_aligned(16);
65         uint8_t opad_buf[blocksize] __rte_aligned(16);
66
67         /* Setup inner and outer pads */
68         memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
69         memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
70
71         /* XOR hash key with inner and outer pads */
72         length = hkey_len > blocksize ? blocksize : hkey_len;
73
74         for (i = 0; i < length; i++) {
75                 ipad_buf[i] ^= hkey[i];
76                 opad_buf[i] ^= hkey[i];
77         }
78
79         /* Compute partial hashes */
80         (*one_block_hash)(ipad_buf, ipad);
81         (*one_block_hash)(opad_buf, opad);
82
83         /* Clean up stack */
84         memset(ipad_buf, 0, blocksize);
85         memset(opad_buf, 0, blocksize);
86 }
87
88 /** Get xform chain order */
89 static enum aesni_mb_operation
90 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
91 {
92         if (xform == NULL)
93                 return AESNI_MB_OP_NOT_SUPPORTED;
94
95         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
96                 if (xform->next == NULL)
97                         return AESNI_MB_OP_CIPHER_ONLY;
98                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
99                         return AESNI_MB_OP_CIPHER_HASH;
100         }
101
102         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
103                 if (xform->next == NULL)
104                         return AESNI_MB_OP_HASH_ONLY;
105                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
106                         return AESNI_MB_OP_HASH_CIPHER;
107         }
108
109         return AESNI_MB_OP_NOT_SUPPORTED;
110 }
111
112 /** Set session authentication parameters */
113 static int
114 aesni_mb_set_session_auth_parameters(const struct aesni_mb_op_fns *mb_ops,
115                 struct aesni_mb_session *sess,
116                 const struct rte_crypto_sym_xform *xform)
117 {
118         hash_one_block_t hash_oneblock_fn;
119
120         if (xform == NULL) {
121                 sess->auth.algo = NULL_HASH;
122                 return 0;
123         }
124
125         if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
126                 MB_LOG_ERR("Crypto xform struct not of type auth");
127                 return -1;
128         }
129
130         /* Select auth generate/verify */
131         sess->auth.operation = xform->auth.op;
132
133         /* Set Authentication Parameters */
134         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
135                 sess->auth.algo = AES_XCBC;
136                 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
137                                 sess->auth.xcbc.k1_expanded,
138                                 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
139                 return 0;
140         }
141
142         switch (xform->auth.algo) {
143         case RTE_CRYPTO_AUTH_MD5_HMAC:
144                 sess->auth.algo = MD5;
145                 hash_oneblock_fn = mb_ops->aux.one_block.md5;
146                 break;
147         case RTE_CRYPTO_AUTH_SHA1_HMAC:
148                 sess->auth.algo = SHA1;
149                 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
150                 break;
151         case RTE_CRYPTO_AUTH_SHA224_HMAC:
152                 sess->auth.algo = SHA_224;
153                 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
154                 break;
155         case RTE_CRYPTO_AUTH_SHA256_HMAC:
156                 sess->auth.algo = SHA_256;
157                 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
158                 break;
159         case RTE_CRYPTO_AUTH_SHA384_HMAC:
160                 sess->auth.algo = SHA_384;
161                 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
162                 break;
163         case RTE_CRYPTO_AUTH_SHA512_HMAC:
164                 sess->auth.algo = SHA_512;
165                 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
166                 break;
167         default:
168                 MB_LOG_ERR("Unsupported authentication algorithm selection");
169                 return -1;
170         }
171
172         /* Calculate Authentication precomputes */
173         calculate_auth_precomputes(hash_oneblock_fn,
174                         sess->auth.pads.inner, sess->auth.pads.outer,
175                         xform->auth.key.data,
176                         xform->auth.key.length,
177                         get_auth_algo_blocksize(sess->auth.algo));
178
179         return 0;
180 }
181
182 /** Set session cipher parameters */
183 static int
184 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_op_fns *mb_ops,
185                 struct aesni_mb_session *sess,
186                 const struct rte_crypto_sym_xform *xform)
187 {
188         aes_keyexp_t aes_keyexp_fn;
189
190         if (xform == NULL) {
191                 sess->cipher.mode = NULL_CIPHER;
192                 return 0;
193         }
194
195         if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
196                 MB_LOG_ERR("Crypto xform struct not of type cipher");
197                 return -1;
198         }
199
200         /* Select cipher direction */
201         switch (xform->cipher.op) {
202         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
203                 sess->cipher.direction = ENCRYPT;
204                 break;
205         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
206                 sess->cipher.direction = DECRYPT;
207                 break;
208         default:
209                 MB_LOG_ERR("Unsupported cipher operation parameter");
210                 return -1;
211         }
212
213         /* Select cipher mode */
214         switch (xform->cipher.algo) {
215         case RTE_CRYPTO_CIPHER_AES_CBC:
216                 sess->cipher.mode = CBC;
217                 break;
218         case RTE_CRYPTO_CIPHER_AES_CTR:
219                 sess->cipher.mode = CNTR;
220                 break;
221         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
222                 sess->cipher.mode = DOCSIS_SEC_BPI;
223                 break;
224         default:
225                 MB_LOG_ERR("Unsupported cipher mode parameter");
226                 return -1;
227         }
228
229         /* Check key length and choose key expansion function */
230         switch (xform->cipher.key.length) {
231         case AES_128_BYTES:
232                 sess->cipher.key_length_in_bytes = AES_128_BYTES;
233                 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
234                 break;
235         case AES_192_BYTES:
236                 sess->cipher.key_length_in_bytes = AES_192_BYTES;
237                 aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
238                 break;
239         case AES_256_BYTES:
240                 sess->cipher.key_length_in_bytes = AES_256_BYTES;
241                 aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
242                 break;
243         default:
244                 MB_LOG_ERR("Unsupported cipher key length");
245                 return -1;
246         }
247
248         /* Expanded cipher keys */
249         (*aes_keyexp_fn)(xform->cipher.key.data,
250                         sess->cipher.expanded_aes_keys.encode,
251                         sess->cipher.expanded_aes_keys.decode);
252
253         return 0;
254 }
255
256 /** Parse crypto xform chain and set private session parameters */
257 int
258 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
259                 struct aesni_mb_session *sess,
260                 const struct rte_crypto_sym_xform *xform)
261 {
262         const struct rte_crypto_sym_xform *auth_xform = NULL;
263         const struct rte_crypto_sym_xform *cipher_xform = NULL;
264
265         /* Select Crypto operation - hash then cipher / cipher then hash */
266         switch (aesni_mb_get_chain_order(xform)) {
267         case AESNI_MB_OP_HASH_CIPHER:
268                 sess->chain_order = HASH_CIPHER;
269                 auth_xform = xform;
270                 cipher_xform = xform->next;
271                 break;
272         case AESNI_MB_OP_CIPHER_HASH:
273                 sess->chain_order = CIPHER_HASH;
274                 auth_xform = xform->next;
275                 cipher_xform = xform;
276                 break;
277         case AESNI_MB_OP_HASH_ONLY:
278                 sess->chain_order = HASH_CIPHER;
279                 auth_xform = xform;
280                 cipher_xform = NULL;
281                 break;
282         case AESNI_MB_OP_CIPHER_ONLY:
283                 /*
284                  * Multi buffer library operates only at two modes,
285                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
286                  * chain order depends on cipher operation: encryption is always
287                  * the first operation and decryption the last one.
288                  */
289                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
290                         sess->chain_order = CIPHER_HASH;
291                 else
292                         sess->chain_order = HASH_CIPHER;
293                 auth_xform = NULL;
294                 cipher_xform = xform;
295                 break;
296         case AESNI_MB_OP_NOT_SUPPORTED:
297         default:
298                 MB_LOG_ERR("Unsupported operation chain order parameter");
299                 return -1;
300         }
301
302         if (aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform)) {
303                 MB_LOG_ERR("Invalid/unsupported authentication parameters");
304                 return -1;
305         }
306
307         if (aesni_mb_set_session_cipher_parameters(mb_ops, sess,
308                         cipher_xform)) {
309                 MB_LOG_ERR("Invalid/unsupported cipher parameters");
310                 return -1;
311         }
312         return 0;
313 }
314
315 /**
316  * burst enqueue, place crypto operations on ingress queue for processing.
317  *
318  * @param __qp         Queue Pair to process
319  * @param ops          Crypto operations for processing
320  * @param nb_ops       Number of crypto operations for processing
321  *
322  * @return
323  * - Number of crypto operations enqueued
324  */
325 static uint16_t
326 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
327                 uint16_t nb_ops)
328 {
329         struct aesni_mb_qp *qp = __qp;
330
331         unsigned int nb_enqueued;
332
333         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
334                         (void **)ops, nb_ops, NULL);
335
336         qp->stats.enqueued_count += nb_enqueued;
337
338         return nb_enqueued;
339 }
340
341 /** Get multi buffer session */
342 static inline struct aesni_mb_session *
343 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
344 {
345         struct aesni_mb_session *sess = NULL;
346
347         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
348                 if (unlikely(op->sym->session->dev_type !=
349                                 RTE_CRYPTODEV_AESNI_MB_PMD)) {
350                         return NULL;
351                 }
352
353                 sess = (struct aesni_mb_session *)op->sym->session->_private;
354         } else  {
355                 void *_sess = NULL;
356
357                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
358                         return NULL;
359
360                 sess = (struct aesni_mb_session *)
361                         ((struct rte_cryptodev_sym_session *)_sess)->_private;
362
363                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
364                                 sess, op->sym->xform) != 0)) {
365                         rte_mempool_put(qp->sess_mp, _sess);
366                         sess = NULL;
367                 }
368                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
369         }
370
371         return sess;
372 }
373
374 /**
375  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
376  * submission to the multi buffer library for processing.
377  *
378  * @param       qp      queue pair
379  * @param       job     JOB_AES_HMAC structure to fill
380  * @param       m       mbuf to process
381  *
382  * @return
383  * - Completed JOB_AES_HMAC structure pointer on success
384  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
385  */
386 static inline int
387 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
388                 struct rte_crypto_op *op)
389 {
390         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
391         struct aesni_mb_session *session;
392         uint16_t m_offset = 0;
393
394         session = get_session(qp, op);
395         if (session == NULL) {
396                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
397                 return -1;
398         }
399         op->status = RTE_CRYPTO_OP_STATUS_ENQUEUED;
400
401         /* Set crypto operation */
402         job->chain_order = session->chain_order;
403
404         /* Set cipher parameters */
405         job->cipher_direction = session->cipher.direction;
406         job->cipher_mode = session->cipher.mode;
407
408         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
409         job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
410         job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
411
412
413         /* Set authentication parameters */
414         job->hash_alg = session->auth.algo;
415         if (job->hash_alg == AES_XCBC) {
416                 job->_k1_expanded = session->auth.xcbc.k1_expanded;
417                 job->_k2 = session->auth.xcbc.k2;
418                 job->_k3 = session->auth.xcbc.k3;
419         } else {
420                 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
421                 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
422         }
423
424         /* Mutable crypto operation parameters */
425         if (op->sym->m_dst) {
426                 m_src = m_dst = op->sym->m_dst;
427
428                 /* append space for output data to mbuf */
429                 char *odata = rte_pktmbuf_append(m_dst,
430                                 rte_pktmbuf_data_len(op->sym->m_src));
431                 if (odata == NULL) {
432                         MB_LOG_ERR("failed to allocate space in destination "
433                                         "mbuf for source data");
434                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
435                         return -1;
436                 }
437
438                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
439                                 rte_pktmbuf_data_len(op->sym->m_src));
440         } else {
441                 m_dst = m_src;
442                 m_offset = op->sym->cipher.data.offset;
443         }
444
445         /* Set digest output location */
446         if (job->hash_alg != NULL_HASH &&
447                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
448                 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m_dst,
449                                 get_digest_byte_length(job->hash_alg));
450
451                 if (job->auth_tag_output == NULL) {
452                         MB_LOG_ERR("failed to allocate space in output mbuf "
453                                         "for temp digest");
454                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
455                         return -1;
456                 }
457
458                 memset(job->auth_tag_output, 0,
459                                 sizeof(get_digest_byte_length(job->hash_alg)));
460
461         } else {
462                 job->auth_tag_output = op->sym->auth.digest.data;
463         }
464
465         /*
466          * Multi-buffer library current only support returning a truncated
467          * digest length as specified in the relevant IPsec RFCs
468          */
469         job->auth_tag_output_len_in_bytes =
470                         get_truncated_digest_byte_length(job->hash_alg);
471
472         /* Set IV parameters */
473         job->iv = op->sym->cipher.iv.data;
474         job->iv_len_in_bytes = op->sym->cipher.iv.length;
475
476         /* Data  Parameter */
477         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
478         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
479
480         job->cipher_start_src_offset_in_bytes = op->sym->cipher.data.offset;
481         job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
482
483         job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
484         job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
485
486         /* Set user data to be crypto operation data struct */
487         job->user_data = op;
488         job->user_data2 = m_dst;
489
490         return 0;
491 }
492
493 static inline void
494 verify_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op) {
495         struct rte_mbuf *m_dst = (struct rte_mbuf *)job->user_data2;
496
497         RTE_ASSERT(m_dst == NULL);
498
499         /* Verify digest if required */
500         if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
501                         job->auth_tag_output_len_in_bytes) != 0)
502                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
503
504         /* trim area used for digest from mbuf */
505         rte_pktmbuf_trim(m_dst, get_digest_byte_length(job->hash_alg));
506 }
507
508 /**
509  * Process a completed job and return rte_mbuf which job processed
510  *
511  * @param job   JOB_AES_HMAC job to process
512  *
513  * @return
514  * - Returns processed mbuf which is trimmed of output digest used in
515  * verification of supplied digest in the case of a HASH_CIPHER operation
516  * - Returns NULL on invalid job
517  */
518 static inline struct rte_crypto_op *
519 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
520 {
521         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
522
523         struct aesni_mb_session *sess;
524
525         RTE_ASSERT(op == NULL);
526
527         if (unlikely(op->status == RTE_CRYPTO_OP_STATUS_ENQUEUED)) {
528                 switch (job->status) {
529                 case STS_COMPLETED:
530                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
531
532                         if (job->hash_alg != NULL_HASH) {
533                                 sess = (struct aesni_mb_session *)
534                                                 op->sym->session->_private;
535
536                                 if (sess->auth.operation ==
537                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
538                                         verify_digest(job, op);
539                         }
540                         break;
541                 default:
542                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
543                 }
544         }
545
546         /* Free session if a session-less crypto op */
547         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
548                 rte_mempool_put(qp->sess_mp, op->sym->session);
549                 op->sym->session = NULL;
550         }
551
552         return op;
553 }
554
555 /**
556  * Process a completed JOB_AES_HMAC job and keep processing jobs until
557  * get_completed_job return NULL
558  *
559  * @param qp            Queue Pair to process
560  * @param job           JOB_AES_HMAC job
561  *
562  * @return
563  * - Number of processed jobs
564  */
565 static unsigned
566 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
567                 struct rte_crypto_op **ops, uint16_t nb_ops)
568 {
569         struct rte_crypto_op *op = NULL;
570         unsigned processed_jobs = 0;
571
572         while (job != NULL && processed_jobs < nb_ops) {
573                 op = post_process_mb_job(qp, job);
574
575                 if (op) {
576                         ops[processed_jobs++] = op;
577                         qp->stats.dequeued_count++;
578                 } else {
579                         qp->stats.dequeue_err_count++;
580                         break;
581                 }
582
583                 job = (*qp->op_fns->job.get_completed_job)(&qp->mb_mgr);
584         }
585
586         return processed_jobs;
587 }
588
589 static inline uint16_t
590 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
591                 uint16_t nb_ops)
592 {
593         int processed_ops = 0;
594
595         /* Flush the remaining jobs */
596         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(&qp->mb_mgr);
597
598         if (job)
599                 processed_ops += handle_completed_jobs(qp, job,
600                                 &ops[processed_ops], nb_ops - processed_ops);
601
602         return processed_ops;
603 }
604
605 static inline JOB_AES_HMAC *
606 set_job_null_op(JOB_AES_HMAC *job)
607 {
608         job->chain_order = HASH_CIPHER;
609         job->cipher_mode = NULL_CIPHER;
610         job->hash_alg = NULL_HASH;
611         job->cipher_direction = DECRYPT;
612
613         return job;
614 }
615
616 static uint16_t
617 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
618                 uint16_t nb_ops)
619 {
620         struct aesni_mb_qp *qp = queue_pair;
621
622         struct rte_crypto_op *op;
623         JOB_AES_HMAC *job;
624
625         int retval, processed_jobs = 0;
626
627         do {
628                 /* Get next operation to process from ingress queue */
629                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
630                 if (retval < 0)
631                         break;
632
633                 /* Get next free mb job struct from mb manager */
634                 job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
635                 if (unlikely(job == NULL)) {
636                         /* if no free mb job structs we need to flush mb_mgr */
637                         processed_jobs += flush_mb_mgr(qp,
638                                         &ops[processed_jobs],
639                                         (nb_ops - processed_jobs) - 1);
640
641                         job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
642                 }
643
644                 retval = set_mb_job_params(job, qp, op);
645                 if (unlikely(retval != 0)) {
646                         qp->stats.dequeue_err_count++;
647                         set_job_null_op(job);
648                 }
649
650                 /* Submit job to multi-buffer for processing */
651                 job = (*qp->op_fns->job.submit)(&qp->mb_mgr);
652
653                 /*
654                  * If submit returns a processed job then handle it,
655                  * before submitting subsequent jobs
656                  */
657                 if (job)
658                         processed_jobs += handle_completed_jobs(qp, job,
659                                         &ops[processed_jobs],
660                                         nb_ops - processed_jobs);
661
662         } while (processed_jobs < nb_ops);
663
664         if (processed_jobs < 1)
665                 processed_jobs += flush_mb_mgr(qp,
666                                 &ops[processed_jobs],
667                                 nb_ops - processed_jobs);
668
669         return processed_jobs;
670 }
671
672 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
673
674 static int
675 cryptodev_aesni_mb_create(const char *name,
676                         struct rte_vdev_device *vdev,
677                         struct rte_crypto_vdev_init_params *init_params)
678 {
679         struct rte_cryptodev *dev;
680         struct aesni_mb_private *internals;
681         enum aesni_mb_vector_mode vector_mode;
682
683         if (init_params->name[0] == '\0')
684                 snprintf(init_params->name, sizeof(init_params->name),
685                                 "%s", name);
686
687         /* Check CPU for supported vector instruction set */
688         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
689                 vector_mode = RTE_AESNI_MB_AVX512;
690         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
691                 vector_mode = RTE_AESNI_MB_AVX2;
692         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
693                 vector_mode = RTE_AESNI_MB_AVX;
694         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
695                 vector_mode = RTE_AESNI_MB_SSE;
696         else {
697                 MB_LOG_ERR("Vector instructions are not supported by CPU");
698                 return -EFAULT;
699         }
700
701         dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
702                         sizeof(struct aesni_mb_private), init_params->socket_id);
703         if (dev == NULL) {
704                 MB_LOG_ERR("failed to create cryptodev vdev");
705                 goto init_error;
706         }
707
708         dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
709         dev->dev_ops = rte_aesni_mb_pmd_ops;
710
711         /* register rx/tx burst functions for data path */
712         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
713         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
714
715         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
716                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
717                         RTE_CRYPTODEV_FF_CPU_AESNI;
718
719         switch (vector_mode) {
720         case RTE_AESNI_MB_SSE:
721                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
722                 break;
723         case RTE_AESNI_MB_AVX:
724                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
725                 break;
726         case RTE_AESNI_MB_AVX2:
727                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
728                 break;
729         case RTE_AESNI_MB_AVX512:
730                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
731                 break;
732         default:
733                 break;
734         }
735
736         /* Set vector instructions mode supported */
737         internals = dev->data->dev_private;
738
739         internals->vector_mode = vector_mode;
740         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
741         internals->max_nb_sessions = init_params->max_nb_sessions;
742
743         return 0;
744 init_error:
745         MB_LOG_ERR("driver %s: cryptodev_aesni_create failed",
746                         init_params->name);
747
748         cryptodev_aesni_mb_remove(vdev);
749         return -EFAULT;
750 }
751
752 static int
753 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
754 {
755         struct rte_crypto_vdev_init_params init_params = {
756                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
757                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
758                 rte_socket_id(),
759                 ""
760         };
761         const char *name;
762         const char *input_args;
763
764         name = rte_vdev_device_name(vdev);
765         if (name == NULL)
766                 return -EINVAL;
767         input_args = rte_vdev_device_args(vdev);
768         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
769
770         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
771                         init_params.socket_id);
772         if (init_params.name[0] != '\0')
773                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
774                         init_params.name);
775         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
776                         init_params.max_nb_queue_pairs);
777         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
778                         init_params.max_nb_sessions);
779
780         return cryptodev_aesni_mb_create(name, vdev, &init_params);
781 }
782
783 static int
784 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
785 {
786         const char *name;
787
788         name = rte_vdev_device_name(vdev);
789         if (name == NULL)
790                 return -EINVAL;
791
792         RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
793                         name, rte_socket_id());
794
795         return 0;
796 }
797
798 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
799         .probe = cryptodev_aesni_mb_probe,
800         .remove = cryptodev_aesni_mb_remove
801 };
802
803 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
804 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
805 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
806         "max_nb_queue_pairs=<int> "
807         "max_nb_sessions=<int> "
808         "socket_id=<int>");