16e145126f02ed5b7aca6a4ea577e0bf3387ad05
[deb_dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <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 -ENOTSUP;
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 -EINVAL;
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("Invalid cipher operation parameter");
213                 return -EINVAL;
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 -ENOTSUP;
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("Invalid cipher key length");
248                 return -EINVAL;
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         int ret;
272
273         /* Select Crypto operation - hash then cipher / cipher then hash */
274         switch (aesni_mb_get_chain_order(xform)) {
275         case AESNI_MB_OP_HASH_CIPHER:
276                 sess->chain_order = HASH_CIPHER;
277                 auth_xform = xform;
278                 cipher_xform = xform->next;
279                 break;
280         case AESNI_MB_OP_CIPHER_HASH:
281                 sess->chain_order = CIPHER_HASH;
282                 auth_xform = xform->next;
283                 cipher_xform = xform;
284                 break;
285         case AESNI_MB_OP_HASH_ONLY:
286                 sess->chain_order = HASH_CIPHER;
287                 auth_xform = xform;
288                 cipher_xform = NULL;
289                 break;
290         case AESNI_MB_OP_CIPHER_ONLY:
291                 /*
292                  * Multi buffer library operates only at two modes,
293                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
294                  * chain order depends on cipher operation: encryption is always
295                  * the first operation and decryption the last one.
296                  */
297                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
298                         sess->chain_order = CIPHER_HASH;
299                 else
300                         sess->chain_order = HASH_CIPHER;
301                 auth_xform = NULL;
302                 cipher_xform = xform;
303                 break;
304         case AESNI_MB_OP_NOT_SUPPORTED:
305         default:
306                 MB_LOG_ERR("Unsupported operation chain order parameter");
307                 return -ENOTSUP;
308         }
309
310         /* Default IV length = 0 */
311         sess->iv.length = 0;
312
313         ret = aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform);
314         if (ret != 0) {
315                 MB_LOG_ERR("Invalid/unsupported authentication parameters");
316                 return ret;
317         }
318
319         ret = aesni_mb_set_session_cipher_parameters(mb_ops, sess,
320                         cipher_xform);
321         if (ret != 0) {
322                 MB_LOG_ERR("Invalid/unsupported cipher parameters");
323                 return ret;
324         }
325
326         return 0;
327 }
328
329 /**
330  * burst enqueue, place crypto operations on ingress queue for processing.
331  *
332  * @param __qp         Queue Pair to process
333  * @param ops          Crypto operations for processing
334  * @param nb_ops       Number of crypto operations for processing
335  *
336  * @return
337  * - Number of crypto operations enqueued
338  */
339 static uint16_t
340 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
341                 uint16_t nb_ops)
342 {
343         struct aesni_mb_qp *qp = __qp;
344
345         unsigned int nb_enqueued;
346
347         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
348                         (void **)ops, nb_ops, NULL);
349
350         qp->stats.enqueued_count += nb_enqueued;
351
352         return nb_enqueued;
353 }
354
355 /** Get multi buffer session */
356 static inline struct aesni_mb_session *
357 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
358 {
359         struct aesni_mb_session *sess = NULL;
360
361         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
362                 if (likely(op->sym->session != NULL))
363                         sess = (struct aesni_mb_session *)
364                                         get_session_private_data(
365                                         op->sym->session,
366                                         cryptodev_driver_id);
367         } else {
368                 void *_sess = NULL;
369                 void *_sess_private_data = NULL;
370
371                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
372                         return NULL;
373
374                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
375                         return NULL;
376
377                 sess = (struct aesni_mb_session *)_sess_private_data;
378
379                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
380                                 sess, op->sym->xform) != 0)) {
381                         rte_mempool_put(qp->sess_mp, _sess);
382                         rte_mempool_put(qp->sess_mp, _sess_private_data);
383                         sess = NULL;
384                 }
385                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
386                 set_session_private_data(op->sym->session, cryptodev_driver_id,
387                         _sess_private_data);
388         }
389
390         if (unlikely(sess == NULL))
391                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
392
393         return sess;
394 }
395
396 /**
397  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
398  * submission to the multi buffer library for processing.
399  *
400  * @param       qp      queue pair
401  * @param       job     JOB_AES_HMAC structure to fill
402  * @param       m       mbuf to process
403  *
404  * @return
405  * - Completed JOB_AES_HMAC structure pointer on success
406  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
407  */
408 static inline int
409 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
410                 struct rte_crypto_op *op)
411 {
412         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
413         struct aesni_mb_session *session;
414         uint16_t m_offset = 0;
415
416         session = get_session(qp, op);
417         if (session == NULL) {
418                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
419                 return -1;
420         }
421
422         /* Set crypto operation */
423         job->chain_order = session->chain_order;
424
425         /* Set cipher parameters */
426         job->cipher_direction = session->cipher.direction;
427         job->cipher_mode = session->cipher.mode;
428
429         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
430         job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
431         job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
432
433
434         /* Set authentication parameters */
435         job->hash_alg = session->auth.algo;
436         if (job->hash_alg == AES_XCBC) {
437                 job->_k1_expanded = session->auth.xcbc.k1_expanded;
438                 job->_k2 = session->auth.xcbc.k2;
439                 job->_k3 = session->auth.xcbc.k3;
440         } else {
441                 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
442                 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
443         }
444
445         /* Mutable crypto operation parameters */
446         if (op->sym->m_dst) {
447                 m_src = m_dst = op->sym->m_dst;
448
449                 /* append space for output data to mbuf */
450                 char *odata = rte_pktmbuf_append(m_dst,
451                                 rte_pktmbuf_data_len(op->sym->m_src));
452                 if (odata == NULL) {
453                         MB_LOG_ERR("failed to allocate space in destination "
454                                         "mbuf for source data");
455                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
456                         return -1;
457                 }
458
459                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
460                                 rte_pktmbuf_data_len(op->sym->m_src));
461         } else {
462                 m_dst = m_src;
463                 m_offset = op->sym->cipher.data.offset;
464         }
465
466         /* Set digest output location */
467         if (job->hash_alg != NULL_HASH &&
468                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
469                 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m_dst,
470                                 get_digest_byte_length(job->hash_alg));
471
472                 if (job->auth_tag_output == NULL) {
473                         MB_LOG_ERR("failed to allocate space in output mbuf "
474                                         "for temp digest");
475                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
476                         return -1;
477                 }
478
479                 memset(job->auth_tag_output, 0,
480                                 sizeof(get_digest_byte_length(job->hash_alg)));
481
482         } else {
483                 job->auth_tag_output = op->sym->auth.digest.data;
484         }
485
486         /*
487          * Multi-buffer library current only support returning a truncated
488          * digest length as specified in the relevant IPsec RFCs
489          */
490         job->auth_tag_output_len_in_bytes =
491                         get_truncated_digest_byte_length(job->hash_alg);
492
493         /* Set IV parameters */
494         job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
495                         session->iv.offset);
496         job->iv_len_in_bytes = session->iv.length;
497
498         /* Data  Parameter */
499         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
500         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
501
502         job->cipher_start_src_offset_in_bytes = op->sym->cipher.data.offset;
503         job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
504
505         job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
506         job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
507
508         /* Set user data to be crypto operation data struct */
509         job->user_data = op;
510         job->user_data2 = m_dst;
511
512         return 0;
513 }
514
515 static inline void
516 verify_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op) {
517         struct rte_mbuf *m_dst = (struct rte_mbuf *)job->user_data2;
518
519         /* Verify digest if required */
520         if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
521                         job->auth_tag_output_len_in_bytes) != 0)
522                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
523
524         /* trim area used for digest from mbuf */
525         rte_pktmbuf_trim(m_dst, get_digest_byte_length(job->hash_alg));
526 }
527
528 /**
529  * Process a completed job and return rte_mbuf which job processed
530  *
531  * @param qp            Queue Pair to process
532  * @param job   JOB_AES_HMAC job to process
533  *
534  * @return
535  * - Returns processed crypto operation which mbuf is trimmed of output digest
536  *   used in verification of supplied digest.
537  * - Returns NULL on invalid job
538  */
539 static inline struct rte_crypto_op *
540 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
541 {
542         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
543         struct aesni_mb_session *sess = get_session_private_data(
544                                                         op->sym->session,
545                                                         cryptodev_driver_id);
546
547         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
548                 switch (job->status) {
549                 case STS_COMPLETED:
550                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
551
552                         if (job->hash_alg != NULL_HASH) {
553                                 if (sess->auth.operation ==
554                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
555                                         verify_digest(job, op);
556                         }
557                         break;
558                 default:
559                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
560                 }
561         }
562
563         /* Free session if a session-less crypto op */
564         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
565                 memset(sess, 0, sizeof(struct aesni_mb_session));
566                 memset(op->sym->session, 0,
567                                 rte_cryptodev_get_header_session_size());
568                 rte_mempool_put(qp->sess_mp, sess);
569                 rte_mempool_put(qp->sess_mp, op->sym->session);
570                 op->sym->session = NULL;
571         }
572
573         return op;
574 }
575
576 /**
577  * Process a completed JOB_AES_HMAC job and keep processing jobs until
578  * get_completed_job return NULL
579  *
580  * @param qp            Queue Pair to process
581  * @param job           JOB_AES_HMAC job
582  *
583  * @return
584  * - Number of processed jobs
585  */
586 static unsigned
587 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
588                 struct rte_crypto_op **ops, uint16_t nb_ops)
589 {
590         struct rte_crypto_op *op = NULL;
591         unsigned processed_jobs = 0;
592
593         while (job != NULL) {
594                 op = post_process_mb_job(qp, job);
595
596                 if (op) {
597                         ops[processed_jobs++] = op;
598                         qp->stats.dequeued_count++;
599                 } else {
600                         qp->stats.dequeue_err_count++;
601                         break;
602                 }
603                 if (processed_jobs == nb_ops)
604                         break;
605
606                 job = (*qp->op_fns->job.get_completed_job)(&qp->mb_mgr);
607         }
608
609         return processed_jobs;
610 }
611
612 static inline uint16_t
613 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
614                 uint16_t nb_ops)
615 {
616         int processed_ops = 0;
617
618         /* Flush the remaining jobs */
619         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(&qp->mb_mgr);
620
621         if (job)
622                 processed_ops += handle_completed_jobs(qp, job,
623                                 &ops[processed_ops], nb_ops - processed_ops);
624
625         return processed_ops;
626 }
627
628 static inline JOB_AES_HMAC *
629 set_job_null_op(JOB_AES_HMAC *job)
630 {
631         job->chain_order = HASH_CIPHER;
632         job->cipher_mode = NULL_CIPHER;
633         job->hash_alg = NULL_HASH;
634         job->cipher_direction = DECRYPT;
635
636         return job;
637 }
638
639 static uint16_t
640 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
641                 uint16_t nb_ops)
642 {
643         struct aesni_mb_qp *qp = queue_pair;
644
645         struct rte_crypto_op *op;
646         JOB_AES_HMAC *job;
647
648         int retval, processed_jobs = 0;
649
650         if (unlikely(nb_ops == 0))
651                 return 0;
652
653         do {
654                 /* Get next operation to process from ingress queue */
655                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
656                 if (retval < 0)
657                         break;
658
659                 /* Get next free mb job struct from mb manager */
660                 job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
661                 if (unlikely(job == NULL)) {
662                         /* if no free mb job structs we need to flush mb_mgr */
663                         processed_jobs += flush_mb_mgr(qp,
664                                         &ops[processed_jobs],
665                                         (nb_ops - processed_jobs) - 1);
666
667                         job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
668                 }
669
670                 retval = set_mb_job_params(job, qp, op);
671                 if (unlikely(retval != 0)) {
672                         qp->stats.dequeue_err_count++;
673                         set_job_null_op(job);
674                 }
675
676                 /* Submit job to multi-buffer for processing */
677                 job = (*qp->op_fns->job.submit)(&qp->mb_mgr);
678
679                 /*
680                  * If submit returns a processed job then handle it,
681                  * before submitting subsequent jobs
682                  */
683                 if (job)
684                         processed_jobs += handle_completed_jobs(qp, job,
685                                         &ops[processed_jobs],
686                                         nb_ops - processed_jobs);
687
688         } while (processed_jobs < nb_ops);
689
690         if (processed_jobs < 1)
691                 processed_jobs += flush_mb_mgr(qp,
692                                 &ops[processed_jobs],
693                                 nb_ops - processed_jobs);
694
695         return processed_jobs;
696 }
697
698 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
699
700 static int
701 cryptodev_aesni_mb_create(const char *name,
702                         struct rte_vdev_device *vdev,
703                         struct rte_crypto_vdev_init_params *init_params)
704 {
705         struct rte_cryptodev *dev;
706         struct aesni_mb_private *internals;
707         enum aesni_mb_vector_mode vector_mode;
708
709         if (init_params->name[0] == '\0')
710                 snprintf(init_params->name, sizeof(init_params->name),
711                                 "%s", name);
712
713         /* Check CPU for supported vector instruction set */
714         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
715                 vector_mode = RTE_AESNI_MB_AVX512;
716         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
717                 vector_mode = RTE_AESNI_MB_AVX2;
718         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
719                 vector_mode = RTE_AESNI_MB_AVX;
720         else
721                 vector_mode = RTE_AESNI_MB_SSE;
722
723         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
724                         sizeof(struct aesni_mb_private), init_params->socket_id,
725                         vdev);
726         if (dev == NULL) {
727                 MB_LOG_ERR("failed to create cryptodev vdev");
728                 goto init_error;
729         }
730
731         dev->driver_id = cryptodev_driver_id;
732         dev->dev_ops = rte_aesni_mb_pmd_ops;
733
734         /* register rx/tx burst functions for data path */
735         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
736         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
737
738         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
739                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
740                         RTE_CRYPTODEV_FF_CPU_AESNI;
741
742         switch (vector_mode) {
743         case RTE_AESNI_MB_SSE:
744                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
745                 break;
746         case RTE_AESNI_MB_AVX:
747                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
748                 break;
749         case RTE_AESNI_MB_AVX2:
750                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
751                 break;
752         case RTE_AESNI_MB_AVX512:
753                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
754                 break;
755         default:
756                 break;
757         }
758
759         /* Set vector instructions mode supported */
760         internals = dev->data->dev_private;
761
762         internals->vector_mode = vector_mode;
763         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
764         internals->max_nb_sessions = init_params->max_nb_sessions;
765
766         return 0;
767 init_error:
768         MB_LOG_ERR("driver %s: cryptodev_aesni_create failed",
769                         init_params->name);
770
771         cryptodev_aesni_mb_remove(vdev);
772         return -EFAULT;
773 }
774
775 static int
776 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
777 {
778         struct rte_crypto_vdev_init_params init_params = {
779                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
780                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
781                 rte_socket_id(),
782                 ""
783         };
784         const char *name;
785         const char *input_args;
786
787         name = rte_vdev_device_name(vdev);
788         if (name == NULL)
789                 return -EINVAL;
790         input_args = rte_vdev_device_args(vdev);
791         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
792
793         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
794                         init_params.socket_id);
795         if (init_params.name[0] != '\0')
796                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
797                         init_params.name);
798         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
799                         init_params.max_nb_queue_pairs);
800         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
801                         init_params.max_nb_sessions);
802
803         return cryptodev_aesni_mb_create(name, vdev, &init_params);
804 }
805
806 static int
807 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
808 {
809         const char *name;
810
811         name = rte_vdev_device_name(vdev);
812         if (name == NULL)
813                 return -EINVAL;
814
815         RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
816                         name, rte_socket_id());
817
818         return 0;
819 }
820
821 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
822         .probe = cryptodev_aesni_mb_probe,
823         .remove = cryptodev_aesni_mb_remove
824 };
825
826 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
827 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
828 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
829         "max_nb_queue_pairs=<int> "
830         "max_nb_sessions=<int> "
831         "socket_id=<int>");
832 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_aesni_mb_pmd_drv, cryptodev_driver_id);