8a96fec98dd1d74bfa93ca649ba17425d00eaf03
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 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         /* Set IV parameters */
250         sess->iv.offset = xform->cipher.iv.offset;
251         sess->iv.length = xform->cipher.iv.length;
252
253         /* Expanded cipher keys */
254         (*aes_keyexp_fn)(xform->cipher.key.data,
255                         sess->cipher.expanded_aes_keys.encode,
256                         sess->cipher.expanded_aes_keys.decode);
257
258         return 0;
259 }
260
261 /** Parse crypto xform chain and set private session parameters */
262 int
263 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
264                 struct aesni_mb_session *sess,
265                 const struct rte_crypto_sym_xform *xform)
266 {
267         const struct rte_crypto_sym_xform *auth_xform = NULL;
268         const struct rte_crypto_sym_xform *cipher_xform = NULL;
269
270         /* Select Crypto operation - hash then cipher / cipher then hash */
271         switch (aesni_mb_get_chain_order(xform)) {
272         case AESNI_MB_OP_HASH_CIPHER:
273                 sess->chain_order = HASH_CIPHER;
274                 auth_xform = xform;
275                 cipher_xform = xform->next;
276                 break;
277         case AESNI_MB_OP_CIPHER_HASH:
278                 sess->chain_order = CIPHER_HASH;
279                 auth_xform = xform->next;
280                 cipher_xform = xform;
281                 break;
282         case AESNI_MB_OP_HASH_ONLY:
283                 sess->chain_order = HASH_CIPHER;
284                 auth_xform = xform;
285                 cipher_xform = NULL;
286                 break;
287         case AESNI_MB_OP_CIPHER_ONLY:
288                 /*
289                  * Multi buffer library operates only at two modes,
290                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
291                  * chain order depends on cipher operation: encryption is always
292                  * the first operation and decryption the last one.
293                  */
294                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
295                         sess->chain_order = CIPHER_HASH;
296                 else
297                         sess->chain_order = HASH_CIPHER;
298                 auth_xform = NULL;
299                 cipher_xform = xform;
300                 break;
301         case AESNI_MB_OP_NOT_SUPPORTED:
302         default:
303                 MB_LOG_ERR("Unsupported operation chain order parameter");
304                 return -1;
305         }
306
307         /* Default IV length = 0 */
308         sess->iv.length = 0;
309
310         if (aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform)) {
311                 MB_LOG_ERR("Invalid/unsupported authentication parameters");
312                 return -1;
313         }
314
315         if (aesni_mb_set_session_cipher_parameters(mb_ops, sess,
316                         cipher_xform)) {
317                 MB_LOG_ERR("Invalid/unsupported cipher parameters");
318                 return -1;
319         }
320         return 0;
321 }
322
323 /**
324  * burst enqueue, place crypto operations on ingress queue for processing.
325  *
326  * @param __qp         Queue Pair to process
327  * @param ops          Crypto operations for processing
328  * @param nb_ops       Number of crypto operations for processing
329  *
330  * @return
331  * - Number of crypto operations enqueued
332  */
333 static uint16_t
334 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
335                 uint16_t nb_ops)
336 {
337         struct aesni_mb_qp *qp = __qp;
338
339         unsigned int nb_enqueued;
340
341         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
342                         (void **)ops, nb_ops, NULL);
343
344         qp->stats.enqueued_count += nb_enqueued;
345
346         return nb_enqueued;
347 }
348
349 /** Get multi buffer session */
350 static inline struct aesni_mb_session *
351 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
352 {
353         struct aesni_mb_session *sess = NULL;
354
355         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
356                 if (unlikely(op->sym->session->dev_type !=
357                                 RTE_CRYPTODEV_AESNI_MB_PMD)) {
358                         return NULL;
359                 }
360
361                 sess = (struct aesni_mb_session *)op->sym->session->_private;
362         } else  {
363                 void *_sess = NULL;
364
365                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
366                         return NULL;
367
368                 sess = (struct aesni_mb_session *)
369                         ((struct rte_cryptodev_sym_session *)_sess)->_private;
370
371                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
372                                 sess, op->sym->xform) != 0)) {
373                         rte_mempool_put(qp->sess_mp, _sess);
374                         sess = NULL;
375                 }
376                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
377         }
378
379         return sess;
380 }
381
382 /**
383  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
384  * submission to the multi buffer library for processing.
385  *
386  * @param       qp      queue pair
387  * @param       job     JOB_AES_HMAC structure to fill
388  * @param       m       mbuf to process
389  *
390  * @return
391  * - Completed JOB_AES_HMAC structure pointer on success
392  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
393  */
394 static inline int
395 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
396                 struct rte_crypto_op *op)
397 {
398         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
399         struct aesni_mb_session *session;
400         uint16_t m_offset = 0;
401
402         session = get_session(qp, op);
403         if (session == NULL) {
404                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
405                 return -1;
406         }
407         op->status = RTE_CRYPTO_OP_STATUS_ENQUEUED;
408
409         /* Set crypto operation */
410         job->chain_order = session->chain_order;
411
412         /* Set cipher parameters */
413         job->cipher_direction = session->cipher.direction;
414         job->cipher_mode = session->cipher.mode;
415
416         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
417         job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
418         job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
419
420
421         /* Set authentication parameters */
422         job->hash_alg = session->auth.algo;
423         if (job->hash_alg == AES_XCBC) {
424                 job->_k1_expanded = session->auth.xcbc.k1_expanded;
425                 job->_k2 = session->auth.xcbc.k2;
426                 job->_k3 = session->auth.xcbc.k3;
427         } else {
428                 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
429                 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
430         }
431
432         /* Mutable crypto operation parameters */
433         if (op->sym->m_dst) {
434                 m_src = m_dst = op->sym->m_dst;
435
436                 /* append space for output data to mbuf */
437                 char *odata = rte_pktmbuf_append(m_dst,
438                                 rte_pktmbuf_data_len(op->sym->m_src));
439                 if (odata == NULL) {
440                         MB_LOG_ERR("failed to allocate space in destination "
441                                         "mbuf for source data");
442                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
443                         return -1;
444                 }
445
446                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
447                                 rte_pktmbuf_data_len(op->sym->m_src));
448         } else {
449                 m_dst = m_src;
450                 m_offset = op->sym->cipher.data.offset;
451         }
452
453         /* Set digest output location */
454         if (job->hash_alg != NULL_HASH &&
455                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
456                 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m_dst,
457                                 get_digest_byte_length(job->hash_alg));
458
459                 if (job->auth_tag_output == NULL) {
460                         MB_LOG_ERR("failed to allocate space in output mbuf "
461                                         "for temp digest");
462                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
463                         return -1;
464                 }
465
466                 memset(job->auth_tag_output, 0,
467                                 sizeof(get_digest_byte_length(job->hash_alg)));
468
469         } else {
470                 job->auth_tag_output = op->sym->auth.digest.data;
471         }
472
473         /*
474          * Multi-buffer library current only support returning a truncated
475          * digest length as specified in the relevant IPsec RFCs
476          */
477         job->auth_tag_output_len_in_bytes =
478                         get_truncated_digest_byte_length(job->hash_alg);
479
480         /* Set IV parameters */
481         job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
482                         session->iv.offset);
483         job->iv_len_in_bytes = session->iv.length;
484
485         /* Data  Parameter */
486         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
487         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
488
489         job->cipher_start_src_offset_in_bytes = op->sym->cipher.data.offset;
490         job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
491
492         job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
493         job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
494
495         /* Set user data to be crypto operation data struct */
496         job->user_data = op;
497         job->user_data2 = m_dst;
498
499         return 0;
500 }
501
502 static inline void
503 verify_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op) {
504         struct rte_mbuf *m_dst = (struct rte_mbuf *)job->user_data2;
505
506         /* Verify digest if required */
507         if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
508                         job->auth_tag_output_len_in_bytes) != 0)
509                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
510
511         /* trim area used for digest from mbuf */
512         rte_pktmbuf_trim(m_dst, get_digest_byte_length(job->hash_alg));
513 }
514
515 /**
516  * Process a completed job and return rte_mbuf which job processed
517  *
518  * @param job   JOB_AES_HMAC job to process
519  *
520  * @return
521  * - Returns processed mbuf which is trimmed of output digest used in
522  * verification of supplied digest in the case of a HASH_CIPHER operation
523  * - Returns NULL on invalid job
524  */
525 static inline struct rte_crypto_op *
526 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
527 {
528         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
529
530         struct aesni_mb_session *sess;
531
532         if (unlikely(op->status == RTE_CRYPTO_OP_STATUS_ENQUEUED)) {
533                 switch (job->status) {
534                 case STS_COMPLETED:
535                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
536
537                         if (job->hash_alg != NULL_HASH) {
538                                 sess = (struct aesni_mb_session *)
539                                                 op->sym->session->_private;
540
541                                 if (sess->auth.operation ==
542                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
543                                         verify_digest(job, op);
544                         }
545                         break;
546                 default:
547                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
548                 }
549         }
550
551         /* Free session if a session-less crypto op */
552         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
553                 rte_mempool_put(qp->sess_mp, op->sym->session);
554                 op->sym->session = NULL;
555         }
556
557         return op;
558 }
559
560 /**
561  * Process a completed JOB_AES_HMAC job and keep processing jobs until
562  * get_completed_job return NULL
563  *
564  * @param qp            Queue Pair to process
565  * @param job           JOB_AES_HMAC job
566  *
567  * @return
568  * - Number of processed jobs
569  */
570 static unsigned
571 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
572                 struct rte_crypto_op **ops, uint16_t nb_ops)
573 {
574         struct rte_crypto_op *op = NULL;
575         unsigned processed_jobs = 0;
576
577         while (job != NULL && processed_jobs < nb_ops) {
578                 op = post_process_mb_job(qp, job);
579
580                 if (op) {
581                         ops[processed_jobs++] = op;
582                         qp->stats.dequeued_count++;
583                 } else {
584                         qp->stats.dequeue_err_count++;
585                         break;
586                 }
587
588                 job = (*qp->op_fns->job.get_completed_job)(&qp->mb_mgr);
589         }
590
591         return processed_jobs;
592 }
593
594 static inline uint16_t
595 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
596                 uint16_t nb_ops)
597 {
598         int processed_ops = 0;
599
600         /* Flush the remaining jobs */
601         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(&qp->mb_mgr);
602
603         if (job)
604                 processed_ops += handle_completed_jobs(qp, job,
605                                 &ops[processed_ops], nb_ops - processed_ops);
606
607         return processed_ops;
608 }
609
610 static inline JOB_AES_HMAC *
611 set_job_null_op(JOB_AES_HMAC *job)
612 {
613         job->chain_order = HASH_CIPHER;
614         job->cipher_mode = NULL_CIPHER;
615         job->hash_alg = NULL_HASH;
616         job->cipher_direction = DECRYPT;
617
618         return job;
619 }
620
621 static uint16_t
622 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
623                 uint16_t nb_ops)
624 {
625         struct aesni_mb_qp *qp = queue_pair;
626
627         struct rte_crypto_op *op;
628         JOB_AES_HMAC *job;
629
630         int retval, processed_jobs = 0;
631
632         do {
633                 /* Get next operation to process from ingress queue */
634                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
635                 if (retval < 0)
636                         break;
637
638                 /* Get next free mb job struct from mb manager */
639                 job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
640                 if (unlikely(job == NULL)) {
641                         /* if no free mb job structs we need to flush mb_mgr */
642                         processed_jobs += flush_mb_mgr(qp,
643                                         &ops[processed_jobs],
644                                         (nb_ops - processed_jobs) - 1);
645
646                         job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
647                 }
648
649                 retval = set_mb_job_params(job, qp, op);
650                 if (unlikely(retval != 0)) {
651                         qp->stats.dequeue_err_count++;
652                         set_job_null_op(job);
653                 }
654
655                 /* Submit job to multi-buffer for processing */
656                 job = (*qp->op_fns->job.submit)(&qp->mb_mgr);
657
658                 /*
659                  * If submit returns a processed job then handle it,
660                  * before submitting subsequent jobs
661                  */
662                 if (job)
663                         processed_jobs += handle_completed_jobs(qp, job,
664                                         &ops[processed_jobs],
665                                         nb_ops - processed_jobs);
666
667         } while (processed_jobs < nb_ops);
668
669         if (processed_jobs < 1)
670                 processed_jobs += flush_mb_mgr(qp,
671                                 &ops[processed_jobs],
672                                 nb_ops - processed_jobs);
673
674         return processed_jobs;
675 }
676
677 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
678
679 static int
680 cryptodev_aesni_mb_create(const char *name,
681                         struct rte_vdev_device *vdev,
682                         struct rte_crypto_vdev_init_params *init_params)
683 {
684         struct rte_cryptodev *dev;
685         struct aesni_mb_private *internals;
686         enum aesni_mb_vector_mode vector_mode;
687
688         if (init_params->name[0] == '\0')
689                 snprintf(init_params->name, sizeof(init_params->name),
690                                 "%s", name);
691
692         /* Check CPU for supported vector instruction set */
693         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
694                 vector_mode = RTE_AESNI_MB_AVX512;
695         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
696                 vector_mode = RTE_AESNI_MB_AVX2;
697         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
698                 vector_mode = RTE_AESNI_MB_AVX;
699         else
700                 vector_mode = RTE_AESNI_MB_SSE;
701
702         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
703                         sizeof(struct aesni_mb_private), init_params->socket_id,
704                         vdev);
705         if (dev == NULL) {
706                 MB_LOG_ERR("failed to create cryptodev vdev");
707                 goto init_error;
708         }
709
710         dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
711         dev->dev_ops = rte_aesni_mb_pmd_ops;
712
713         /* register rx/tx burst functions for data path */
714         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
715         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
716
717         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
718                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
719                         RTE_CRYPTODEV_FF_CPU_AESNI;
720
721         switch (vector_mode) {
722         case RTE_AESNI_MB_SSE:
723                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
724                 break;
725         case RTE_AESNI_MB_AVX:
726                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
727                 break;
728         case RTE_AESNI_MB_AVX2:
729                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
730                 break;
731         case RTE_AESNI_MB_AVX512:
732                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
733                 break;
734         default:
735                 break;
736         }
737
738         /* Set vector instructions mode supported */
739         internals = dev->data->dev_private;
740
741         internals->vector_mode = vector_mode;
742         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
743         internals->max_nb_sessions = init_params->max_nb_sessions;
744
745         return 0;
746 init_error:
747         MB_LOG_ERR("driver %s: cryptodev_aesni_create failed",
748                         init_params->name);
749
750         cryptodev_aesni_mb_remove(vdev);
751         return -EFAULT;
752 }
753
754 static int
755 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
756 {
757         struct rte_crypto_vdev_init_params init_params = {
758                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
759                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
760                 rte_socket_id(),
761                 ""
762         };
763         const char *name;
764         const char *input_args;
765
766         name = rte_vdev_device_name(vdev);
767         if (name == NULL)
768                 return -EINVAL;
769         input_args = rte_vdev_device_args(vdev);
770         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
771
772         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
773                         init_params.socket_id);
774         if (init_params.name[0] != '\0')
775                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
776                         init_params.name);
777         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
778                         init_params.max_nb_queue_pairs);
779         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
780                         init_params.max_nb_sessions);
781
782         return cryptodev_aesni_mb_create(name, vdev, &init_params);
783 }
784
785 static int
786 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
787 {
788         const char *name;
789
790         name = rte_vdev_device_name(vdev);
791         if (name == NULL)
792                 return -EINVAL;
793
794         RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
795                         name, rte_socket_id());
796
797         return 0;
798 }
799
800 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
801         .probe = cryptodev_aesni_mb_probe,
802         .remove = cryptodev_aesni_mb_remove
803 };
804
805 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
806 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
807 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
808         "max_nb_queue_pairs=<int> "
809         "max_nb_sessions=<int> "
810         "socket_id=<int>");