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