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