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