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