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