0fab81ef56f70fcb9f8414461a4c7bc1d459b760
[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)(const void *data, void *digest);
44 typedef void (*aes_keyexp_t)(const 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_op_fns *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_op_fns *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_op_fns *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 /**
313  * burst enqueue, place crypto operations on ingress queue for processing.
314  *
315  * @param __qp         Queue Pair to process
316  * @param ops          Crypto operations for processing
317  * @param nb_ops       Number of crypto operations for processing
318  *
319  * @return
320  * - Number of crypto operations enqueued
321  */
322 static uint16_t
323 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
324                 uint16_t nb_ops)
325 {
326         struct aesni_mb_qp *qp = __qp;
327
328         unsigned int nb_enqueued;
329
330         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
331                         (void **)ops, nb_ops, NULL);
332
333         qp->stats.enqueued_count += nb_enqueued;
334
335         return nb_enqueued;
336 }
337
338 /** Get multi buffer session */
339 static inline struct aesni_mb_session *
340 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
341 {
342         struct aesni_mb_session *sess = NULL;
343
344         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
345                 if (unlikely(op->sym->session->dev_type !=
346                                 RTE_CRYPTODEV_AESNI_MB_PMD)) {
347                         return NULL;
348                 }
349
350                 sess = (struct aesni_mb_session *)op->sym->session->_private;
351         } else  {
352                 void *_sess = NULL;
353
354                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
355                         return NULL;
356
357                 sess = (struct aesni_mb_session *)
358                         ((struct rte_cryptodev_sym_session *)_sess)->_private;
359
360                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
361                                 sess, op->sym->xform) != 0)) {
362                         rte_mempool_put(qp->sess_mp, _sess);
363                         sess = NULL;
364                 }
365                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
366         }
367
368         return sess;
369 }
370
371 /**
372  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
373  * submission to the multi buffer library for processing.
374  *
375  * @param       qp      queue pair
376  * @param       job     JOB_AES_HMAC structure to fill
377  * @param       m       mbuf to process
378  *
379  * @return
380  * - Completed JOB_AES_HMAC structure pointer on success
381  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
382  */
383 static inline int
384 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
385                 struct rte_crypto_op *op)
386 {
387         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
388         struct aesni_mb_session *session;
389         uint16_t m_offset = 0;
390
391         session = get_session(qp, op);
392         if (session == NULL) {
393                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
394                 return -1;
395         }
396         op->status = RTE_CRYPTO_OP_STATUS_ENQUEUED;
397
398         /* Set crypto operation */
399         job->chain_order = session->chain_order;
400
401         /* Set cipher parameters */
402         job->cipher_direction = session->cipher.direction;
403         job->cipher_mode = session->cipher.mode;
404
405         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
406         job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
407         job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
408
409
410         /* Set authentication parameters */
411         job->hash_alg = session->auth.algo;
412         if (job->hash_alg == AES_XCBC) {
413                 job->_k1_expanded = session->auth.xcbc.k1_expanded;
414                 job->_k2 = session->auth.xcbc.k2;
415                 job->_k3 = session->auth.xcbc.k3;
416         } else {
417                 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
418                 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
419         }
420
421         /* Mutable crypto operation parameters */
422         if (op->sym->m_dst) {
423                 m_src = m_dst = op->sym->m_dst;
424
425                 /* append space for output data to mbuf */
426                 char *odata = rte_pktmbuf_append(m_dst,
427                                 rte_pktmbuf_data_len(op->sym->m_src));
428                 if (odata == NULL) {
429                         MB_LOG_ERR("failed to allocate space in destination "
430                                         "mbuf for source data");
431                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
432                         return -1;
433                 }
434
435                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
436                                 rte_pktmbuf_data_len(op->sym->m_src));
437         } else {
438                 m_dst = m_src;
439                 m_offset = op->sym->cipher.data.offset;
440         }
441
442         /* Set digest output location */
443         if (job->hash_alg != NULL_HASH &&
444                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
445                 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m_dst,
446                                 get_digest_byte_length(job->hash_alg));
447
448                 if (job->auth_tag_output == NULL) {
449                         MB_LOG_ERR("failed to allocate space in output mbuf "
450                                         "for temp digest");
451                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
452                         return -1;
453                 }
454
455                 memset(job->auth_tag_output, 0,
456                                 sizeof(get_digest_byte_length(job->hash_alg)));
457
458         } else {
459                 job->auth_tag_output = op->sym->auth.digest.data;
460         }
461
462         /*
463          * Multi-buffer library current only support returning a truncated
464          * digest length as specified in the relevant IPsec RFCs
465          */
466         job->auth_tag_output_len_in_bytes =
467                         get_truncated_digest_byte_length(job->hash_alg);
468
469         /* Set IV parameters */
470         job->iv = op->sym->cipher.iv.data;
471         job->iv_len_in_bytes = op->sym->cipher.iv.length;
472
473         /* Data  Parameter */
474         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
475         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
476
477         job->cipher_start_src_offset_in_bytes = op->sym->cipher.data.offset;
478         job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
479
480         job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
481         job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
482
483         /* Set user data to be crypto operation data struct */
484         job->user_data = op;
485         job->user_data2 = m_dst;
486
487         return 0;
488 }
489
490 static inline void
491 verify_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op) {
492         struct rte_mbuf *m_dst = (struct rte_mbuf *)job->user_data2;
493
494         RTE_ASSERT(m_dst == NULL);
495
496         /* Verify digest if required */
497         if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
498                         job->auth_tag_output_len_in_bytes) != 0)
499                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
500
501         /* trim area used for digest from mbuf */
502         rte_pktmbuf_trim(m_dst, get_digest_byte_length(job->hash_alg));
503 }
504
505 /**
506  * Process a completed job and return rte_mbuf which job processed
507  *
508  * @param job   JOB_AES_HMAC job to process
509  *
510  * @return
511  * - Returns processed mbuf which is trimmed of output digest used in
512  * verification of supplied digest in the case of a HASH_CIPHER operation
513  * - Returns NULL on invalid job
514  */
515 static inline struct rte_crypto_op *
516 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
517 {
518         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
519
520         struct aesni_mb_session *sess;
521
522         RTE_ASSERT(op == NULL);
523
524         if (unlikely(op->status == RTE_CRYPTO_OP_STATUS_ENQUEUED)) {
525                 switch (job->status) {
526                 case STS_COMPLETED:
527                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
528
529                         if (job->hash_alg != NULL_HASH) {
530                                 sess = (struct aesni_mb_session *)
531                                                 op->sym->session->_private;
532
533                                 if (sess->auth.operation ==
534                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
535                                         verify_digest(job, op);
536                         }
537                         break;
538                 default:
539                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
540                 }
541         }
542
543         /* Free session if a session-less crypto op */
544         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
545                 rte_mempool_put(qp->sess_mp, op->sym->session);
546                 op->sym->session = NULL;
547         }
548
549         return op;
550 }
551
552 /**
553  * Process a completed JOB_AES_HMAC job and keep processing jobs until
554  * get_completed_job return NULL
555  *
556  * @param qp            Queue Pair to process
557  * @param job           JOB_AES_HMAC job
558  *
559  * @return
560  * - Number of processed jobs
561  */
562 static unsigned
563 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
564                 struct rte_crypto_op **ops, uint16_t nb_ops)
565 {
566         struct rte_crypto_op *op = NULL;
567         unsigned processed_jobs = 0;
568
569         while (job != NULL && processed_jobs < nb_ops) {
570                 op = post_process_mb_job(qp, job);
571
572                 if (op) {
573                         ops[processed_jobs++] = op;
574                         qp->stats.dequeued_count++;
575                 } else {
576                         qp->stats.dequeue_err_count++;
577                         break;
578                 }
579
580                 job = (*qp->op_fns->job.get_completed_job)(&qp->mb_mgr);
581         }
582
583         return processed_jobs;
584 }
585
586 static inline uint16_t
587 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
588                 uint16_t nb_ops)
589 {
590         int processed_ops = 0;
591
592         /* Flush the remaining jobs */
593         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(&qp->mb_mgr);
594
595         if (job)
596                 processed_ops += handle_completed_jobs(qp, job,
597                                 &ops[processed_ops], nb_ops - processed_ops);
598
599         return processed_ops;
600 }
601
602 static inline JOB_AES_HMAC *
603 set_job_null_op(JOB_AES_HMAC *job)
604 {
605         job->chain_order = HASH_CIPHER;
606         job->cipher_mode = NULL_CIPHER;
607         job->hash_alg = NULL_HASH;
608         job->cipher_direction = DECRYPT;
609
610         return job;
611 }
612
613 static uint16_t
614 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
615                 uint16_t nb_ops)
616 {
617         struct aesni_mb_qp *qp = queue_pair;
618
619         struct rte_crypto_op *op;
620         JOB_AES_HMAC *job;
621
622         int retval, processed_jobs = 0;
623
624         do {
625                 /* Get next operation to process from ingress queue */
626                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
627                 if (retval < 0)
628                         break;
629
630                 /* Get next free mb job struct from mb manager */
631                 job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
632                 if (unlikely(job == NULL)) {
633                         /* if no free mb job structs we need to flush mb_mgr */
634                         processed_jobs += flush_mb_mgr(qp,
635                                         &ops[processed_jobs],
636                                         (nb_ops - processed_jobs) - 1);
637
638                         job = (*qp->op_fns->job.get_next)(&qp->mb_mgr);
639                 }
640
641                 retval = set_mb_job_params(job, qp, op);
642                 if (unlikely(retval != 0)) {
643                         qp->stats.dequeue_err_count++;
644                         set_job_null_op(job);
645                 }
646
647                 /* Submit job to multi-buffer for processing */
648                 job = (*qp->op_fns->job.submit)(&qp->mb_mgr);
649
650                 /*
651                  * If submit returns a processed job then handle it,
652                  * before submitting subsequent jobs
653                  */
654                 if (job)
655                         processed_jobs += handle_completed_jobs(qp, job,
656                                         &ops[processed_jobs],
657                                         nb_ops - processed_jobs);
658
659         } while (processed_jobs < nb_ops);
660
661         if (processed_jobs < 1)
662                 processed_jobs += flush_mb_mgr(qp,
663                                 &ops[processed_jobs],
664                                 nb_ops - processed_jobs);
665
666         return processed_jobs;
667 }
668
669 static int cryptodev_aesni_mb_remove(const char *name);
670
671 static int
672 cryptodev_aesni_mb_create(struct rte_crypto_vdev_init_params *init_params)
673 {
674         struct rte_cryptodev *dev;
675         struct aesni_mb_private *internals;
676         enum aesni_mb_vector_mode vector_mode;
677
678         if (init_params->name[0] == '\0') {
679                 int ret = rte_cryptodev_pmd_create_dev_name(
680                                 init_params->name,
681                                 RTE_STR(CRYPTODEV_NAME_AESNI_MB_PMD));
682
683                 if (ret < 0) {
684                         MB_LOG_ERR("failed to create unique name");
685                         return ret;
686                 }
687         }
688
689         /* Check CPU for supported vector instruction set */
690         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
691                 vector_mode = RTE_AESNI_MB_AVX512;
692         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
693                 vector_mode = RTE_AESNI_MB_AVX2;
694         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
695                 vector_mode = RTE_AESNI_MB_AVX;
696         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
697                 vector_mode = RTE_AESNI_MB_SSE;
698         else {
699                 MB_LOG_ERR("Vector instructions are not supported by CPU");
700                 return -EFAULT;
701         }
702
703         dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
704                         sizeof(struct aesni_mb_private), init_params->socket_id);
705         if (dev == NULL) {
706                 MB_LOG_ERR("failed to create cryptodev vdev");
707                 goto init_error;
708         }
709
710         dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
711         dev->dev_ops = rte_aesni_mb_pmd_ops;
712
713         /* register rx/tx burst functions for data path */
714         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
715         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
716
717         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
718                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
719                         RTE_CRYPTODEV_FF_CPU_AESNI;
720
721         switch (vector_mode) {
722         case RTE_AESNI_MB_SSE:
723                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
724                 break;
725         case RTE_AESNI_MB_AVX:
726                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
727                 break;
728         case RTE_AESNI_MB_AVX2:
729                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
730                 break;
731         case RTE_AESNI_MB_AVX512:
732                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
733                 break;
734         default:
735                 break;
736         }
737
738         /* Set vector instructions mode supported */
739         internals = dev->data->dev_private;
740
741         internals->vector_mode = vector_mode;
742         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
743         internals->max_nb_sessions = init_params->max_nb_sessions;
744
745         return 0;
746 init_error:
747         MB_LOG_ERR("driver %s: cryptodev_aesni_create failed",
748                         init_params->name);
749
750         cryptodev_aesni_mb_remove(init_params->name);
751         return -EFAULT;
752 }
753
754 static int
755 cryptodev_aesni_mb_probe(const char *name,
756                 const char *input_args)
757 {
758         struct rte_crypto_vdev_init_params init_params = {
759                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
760                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
761                 rte_socket_id(),
762                 ""
763         };
764
765         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
766
767         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
768                         init_params.socket_id);
769         if (init_params.name[0] != '\0')
770                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
771                         init_params.name);
772         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
773                         init_params.max_nb_queue_pairs);
774         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
775                         init_params.max_nb_sessions);
776
777         return cryptodev_aesni_mb_create(&init_params);
778 }
779
780 static int
781 cryptodev_aesni_mb_remove(const char *name)
782 {
783         if (name == NULL)
784                 return -EINVAL;
785
786         RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
787                         name, rte_socket_id());
788
789         return 0;
790 }
791
792 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
793         .probe = cryptodev_aesni_mb_probe,
794         .remove = cryptodev_aesni_mb_remove
795 };
796
797 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
798 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
799 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
800         "max_nb_queue_pairs=<int> "
801         "max_nb_sessions=<int> "
802         "socket_id=<int>");