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