bb199ae4c6ffca1935cfaa2ed64c294ebe304cc8
[dpdk.git] / drivers / crypto / qat / qat_crypto.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *       * Redistributions of source code must retain the above copyright
12  *         notice, this list of conditions and the following disclaimer.
13  *       * Redistributions in binary form must reproduce the above copyright
14  *         notice, this list of conditions and the following disclaimer in
15  *         the documentation and/or other materials provided with the
16  *         distribution.
17  *       * Neither the name of Intel Corporation nor the names of its
18  *         contributors may be used to endorse or promote products derived
19  *         from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <string.h>
38 #include <inttypes.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_tailq.h>
49 #include <rte_malloc.h>
50 #include <rte_launch.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_mempool.h>
56 #include <rte_mbuf.h>
57 #include <rte_string_fns.h>
58 #include <rte_spinlock.h>
59 #include <rte_hexdump.h>
60 #include <rte_crypto_sym.h>
61 #include <rte_cryptodev_pci.h>
62 #include <openssl/evp.h>
63
64 #include "qat_logs.h"
65 #include "qat_algs.h"
66 #include "qat_crypto.h"
67 #include "adf_transport_access_macros.h"
68
69 #define BYTE_LENGTH    8
70
71 static int
72 qat_is_cipher_alg_supported(enum rte_crypto_cipher_algorithm algo,
73                 struct qat_pmd_private *internals) {
74         int i = 0;
75         const struct rte_cryptodev_capabilities *capability;
76
77         while ((capability = &(internals->qat_dev_capabilities[i++]))->op !=
78                         RTE_CRYPTO_OP_TYPE_UNDEFINED) {
79                 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
80                         continue;
81
82                 if (capability->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
83                         continue;
84
85                 if (capability->sym.cipher.algo == algo)
86                         return 1;
87         }
88         return 0;
89 }
90
91 static int
92 qat_is_auth_alg_supported(enum rte_crypto_auth_algorithm algo,
93                 struct qat_pmd_private *internals) {
94         int i = 0;
95         const struct rte_cryptodev_capabilities *capability;
96
97         while ((capability = &(internals->qat_dev_capabilities[i++]))->op !=
98                         RTE_CRYPTO_OP_TYPE_UNDEFINED) {
99                 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
100                         continue;
101
102                 if (capability->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
103                         continue;
104
105                 if (capability->sym.auth.algo == algo)
106                         return 1;
107         }
108         return 0;
109 }
110
111 /** Encrypt a single partial block
112  *  Depends on openssl libcrypto
113  *  Uses ECB+XOR to do CFB encryption, same result, more performant
114  */
115 static inline int
116 bpi_cipher_encrypt(uint8_t *src, uint8_t *dst,
117                 uint8_t *iv, int ivlen, int srclen,
118                 void *bpi_ctx)
119 {
120         EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
121         int encrypted_ivlen;
122         uint8_t encrypted_iv[16];
123         int i;
124
125         /* ECB method: encrypt the IV, then XOR this with plaintext */
126         if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
127                                                                 <= 0)
128                 goto cipher_encrypt_err;
129
130         for (i = 0; i < srclen; i++)
131                 *(dst+i) = *(src+i)^(encrypted_iv[i]);
132
133         return 0;
134
135 cipher_encrypt_err:
136         PMD_DRV_LOG(ERR, "libcrypto ECB cipher encrypt failed");
137         return -EINVAL;
138 }
139
140 /** Decrypt a single partial block
141  *  Depends on openssl libcrypto
142  *  Uses ECB+XOR to do CFB encryption, same result, more performant
143  */
144 static inline int
145 bpi_cipher_decrypt(uint8_t *src, uint8_t *dst,
146                 uint8_t *iv, int ivlen, int srclen,
147                 void *bpi_ctx)
148 {
149         EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
150         int encrypted_ivlen;
151         uint8_t encrypted_iv[16];
152         int i;
153
154         /* ECB method: encrypt (not decrypt!) the IV, then XOR with plaintext */
155         if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
156                                                                 <= 0)
157                 goto cipher_decrypt_err;
158
159         for (i = 0; i < srclen; i++)
160                 *(dst+i) = *(src+i)^(encrypted_iv[i]);
161
162         return 0;
163
164 cipher_decrypt_err:
165         PMD_DRV_LOG(ERR, "libcrypto ECB cipher encrypt for BPI IV failed");
166         return -EINVAL;
167 }
168
169 /** Creates a context in either AES or DES in ECB mode
170  *  Depends on openssl libcrypto
171  */
172 static int
173 bpi_cipher_ctx_init(enum rte_crypto_cipher_algorithm cryptodev_algo,
174                 enum rte_crypto_cipher_operation direction __rte_unused,
175                 uint8_t *key, void **ctx)
176 {
177         const EVP_CIPHER *algo = NULL;
178         int ret;
179         *ctx = EVP_CIPHER_CTX_new();
180
181         if (*ctx == NULL) {
182                 ret = -ENOMEM;
183                 goto ctx_init_err;
184         }
185
186         if (cryptodev_algo == RTE_CRYPTO_CIPHER_DES_DOCSISBPI)
187                 algo = EVP_des_ecb();
188         else
189                 algo = EVP_aes_128_ecb();
190
191         /* IV will be ECB encrypted whether direction is encrypt or decrypt*/
192         if (EVP_EncryptInit_ex(*ctx, algo, NULL, key, 0) != 1) {
193                 ret = -EINVAL;
194                 goto ctx_init_err;
195         }
196
197         return 0;
198
199 ctx_init_err:
200         if (*ctx != NULL)
201                 EVP_CIPHER_CTX_free(*ctx);
202         return ret;
203 }
204
205 /** Frees a context previously created
206  *  Depends on openssl libcrypto
207  */
208 static void
209 bpi_cipher_ctx_free(void *bpi_ctx)
210 {
211         if (bpi_ctx != NULL)
212                 EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)bpi_ctx);
213 }
214
215 static inline uint32_t
216 adf_modulo(uint32_t data, uint32_t shift);
217
218 static inline int
219 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
220                 struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp);
221
222 void
223 qat_crypto_sym_clear_session(struct rte_cryptodev *dev,
224                 struct rte_cryptodev_sym_session *sess)
225 {
226         PMD_INIT_FUNC_TRACE();
227         uint8_t index = dev->driver_id;
228         void *sess_priv = get_session_private_data(sess, index);
229         struct qat_session *s = (struct qat_session *)sess_priv;
230
231         if (sess_priv) {
232                 if (s->bpi_ctx)
233                         bpi_cipher_ctx_free(s->bpi_ctx);
234                 memset(s, 0, qat_crypto_sym_get_session_private_size(dev));
235                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
236                 set_session_private_data(sess, index, NULL);
237                 rte_mempool_put(sess_mp, sess_priv);
238         }
239 }
240
241 static int
242 qat_get_cmd_id(const struct rte_crypto_sym_xform *xform)
243 {
244         /* Cipher Only */
245         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL)
246                 return ICP_QAT_FW_LA_CMD_CIPHER;
247
248         /* Authentication Only */
249         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && xform->next == NULL)
250                 return ICP_QAT_FW_LA_CMD_AUTH;
251
252         /* AEAD */
253         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
254                 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
255                         return ICP_QAT_FW_LA_CMD_CIPHER_HASH;
256                 else
257                         return ICP_QAT_FW_LA_CMD_HASH_CIPHER;
258         }
259
260         if (xform->next == NULL)
261                 return -1;
262
263         /* Cipher then Authenticate */
264         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
265                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
266                 return ICP_QAT_FW_LA_CMD_CIPHER_HASH;
267
268         /* Authenticate then Cipher */
269         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
270                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
271                 return ICP_QAT_FW_LA_CMD_HASH_CIPHER;
272
273         return -1;
274 }
275
276 static struct rte_crypto_auth_xform *
277 qat_get_auth_xform(struct rte_crypto_sym_xform *xform)
278 {
279         do {
280                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
281                         return &xform->auth;
282
283                 xform = xform->next;
284         } while (xform);
285
286         return NULL;
287 }
288
289 static struct rte_crypto_cipher_xform *
290 qat_get_cipher_xform(struct rte_crypto_sym_xform *xform)
291 {
292         do {
293                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
294                         return &xform->cipher;
295
296                 xform = xform->next;
297         } while (xform);
298
299         return NULL;
300 }
301
302 int
303 qat_crypto_sym_configure_session_cipher(struct rte_cryptodev *dev,
304                 struct rte_crypto_sym_xform *xform,
305                 struct qat_session *session)
306 {
307         struct qat_pmd_private *internals = dev->data->dev_private;
308         struct rte_crypto_cipher_xform *cipher_xform = NULL;
309         int ret;
310
311         /* Get cipher xform from crypto xform chain */
312         cipher_xform = qat_get_cipher_xform(xform);
313
314         session->cipher_iv.offset = cipher_xform->iv.offset;
315         session->cipher_iv.length = cipher_xform->iv.length;
316
317         switch (cipher_xform->algo) {
318         case RTE_CRYPTO_CIPHER_AES_CBC:
319                 if (qat_alg_validate_aes_key(cipher_xform->key.length,
320                                 &session->qat_cipher_alg) != 0) {
321                         PMD_DRV_LOG(ERR, "Invalid AES cipher key size");
322                         ret = -EINVAL;
323                         goto error_out;
324                 }
325                 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
326                 break;
327         case RTE_CRYPTO_CIPHER_AES_CTR:
328                 if (qat_alg_validate_aes_key(cipher_xform->key.length,
329                                 &session->qat_cipher_alg) != 0) {
330                         PMD_DRV_LOG(ERR, "Invalid AES cipher key size");
331                         ret = -EINVAL;
332                         goto error_out;
333                 }
334                 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
335                 break;
336         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
337                 if (qat_alg_validate_snow3g_key(cipher_xform->key.length,
338                                         &session->qat_cipher_alg) != 0) {
339                         PMD_DRV_LOG(ERR, "Invalid SNOW 3G cipher key size");
340                         ret = -EINVAL;
341                         goto error_out;
342                 }
343                 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
344                 break;
345         case RTE_CRYPTO_CIPHER_NULL:
346                 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
347                 break;
348         case RTE_CRYPTO_CIPHER_KASUMI_F8:
349                 if (qat_alg_validate_kasumi_key(cipher_xform->key.length,
350                                         &session->qat_cipher_alg) != 0) {
351                         PMD_DRV_LOG(ERR, "Invalid KASUMI cipher key size");
352                         ret = -EINVAL;
353                         goto error_out;
354                 }
355                 session->qat_mode = ICP_QAT_HW_CIPHER_F8_MODE;
356                 break;
357         case RTE_CRYPTO_CIPHER_3DES_CBC:
358                 if (qat_alg_validate_3des_key(cipher_xform->key.length,
359                                 &session->qat_cipher_alg) != 0) {
360                         PMD_DRV_LOG(ERR, "Invalid 3DES cipher key size");
361                         ret = -EINVAL;
362                         goto error_out;
363                 }
364                 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
365                 break;
366         case RTE_CRYPTO_CIPHER_DES_CBC:
367                 if (qat_alg_validate_des_key(cipher_xform->key.length,
368                                 &session->qat_cipher_alg) != 0) {
369                         PMD_DRV_LOG(ERR, "Invalid DES cipher key size");
370                         ret = -EINVAL;
371                         goto error_out;
372                 }
373                 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
374                 break;
375         case RTE_CRYPTO_CIPHER_3DES_CTR:
376                 if (qat_alg_validate_3des_key(cipher_xform->key.length,
377                                 &session->qat_cipher_alg) != 0) {
378                         PMD_DRV_LOG(ERR, "Invalid 3DES cipher key size");
379                         ret = -EINVAL;
380                         goto error_out;
381                 }
382                 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
383                 break;
384         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
385                 ret = bpi_cipher_ctx_init(
386                                         cipher_xform->algo,
387                                         cipher_xform->op,
388                                         cipher_xform->key.data,
389                                         &session->bpi_ctx);
390                 if (ret != 0) {
391                         PMD_DRV_LOG(ERR, "failed to create DES BPI ctx");
392                         goto error_out;
393                 }
394                 if (qat_alg_validate_des_key(cipher_xform->key.length,
395                                 &session->qat_cipher_alg) != 0) {
396                         PMD_DRV_LOG(ERR, "Invalid DES cipher key size");
397                         ret = -EINVAL;
398                         goto error_out;
399                 }
400                 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
401                 break;
402         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
403                 ret = bpi_cipher_ctx_init(
404                                         cipher_xform->algo,
405                                         cipher_xform->op,
406                                         cipher_xform->key.data,
407                                         &session->bpi_ctx);
408                 if (ret != 0) {
409                         PMD_DRV_LOG(ERR, "failed to create AES BPI ctx");
410                         goto error_out;
411                 }
412                 if (qat_alg_validate_aes_docsisbpi_key(cipher_xform->key.length,
413                                 &session->qat_cipher_alg) != 0) {
414                         PMD_DRV_LOG(ERR, "Invalid AES DOCSISBPI key size");
415                         ret = -EINVAL;
416                         goto error_out;
417                 }
418                 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
419                 break;
420         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
421                 if (!qat_is_cipher_alg_supported(
422                         cipher_xform->algo, internals)) {
423                         PMD_DRV_LOG(ERR, "%s not supported on this device",
424                                 rte_crypto_cipher_algorithm_strings
425                                         [cipher_xform->algo]);
426                         ret = -ENOTSUP;
427                         goto error_out;
428                 }
429                 if (qat_alg_validate_zuc_key(cipher_xform->key.length,
430                                 &session->qat_cipher_alg) != 0) {
431                         PMD_DRV_LOG(ERR, "Invalid ZUC cipher key size");
432                         ret = -EINVAL;
433                         goto error_out;
434                 }
435                 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
436                 break;
437         case RTE_CRYPTO_CIPHER_3DES_ECB:
438         case RTE_CRYPTO_CIPHER_AES_ECB:
439         case RTE_CRYPTO_CIPHER_AES_F8:
440         case RTE_CRYPTO_CIPHER_AES_XTS:
441         case RTE_CRYPTO_CIPHER_ARC4:
442                 PMD_DRV_LOG(ERR, "Crypto QAT PMD: Unsupported Cipher alg %u",
443                                 cipher_xform->algo);
444                 ret = -ENOTSUP;
445                 goto error_out;
446         default:
447                 PMD_DRV_LOG(ERR, "Crypto: Undefined Cipher specified %u\n",
448                                 cipher_xform->algo);
449                 ret = -EINVAL;
450                 goto error_out;
451         }
452
453         if (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
454                 session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
455         else
456                 session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
457
458         if (qat_alg_aead_session_create_content_desc_cipher(session,
459                                                 cipher_xform->key.data,
460                                                 cipher_xform->key.length)) {
461                 ret = -EINVAL;
462                 goto error_out;
463         }
464
465         return 0;
466
467 error_out:
468         if (session->bpi_ctx) {
469                 bpi_cipher_ctx_free(session->bpi_ctx);
470                 session->bpi_ctx = NULL;
471         }
472         return ret;
473 }
474
475 int
476 qat_crypto_sym_configure_session(struct rte_cryptodev *dev,
477                 struct rte_crypto_sym_xform *xform,
478                 struct rte_cryptodev_sym_session *sess,
479                 struct rte_mempool *mempool)
480 {
481         void *sess_private_data;
482         int ret;
483
484         if (rte_mempool_get(mempool, &sess_private_data)) {
485                 CDEV_LOG_ERR(
486                         "Couldn't get object from session mempool");
487                 return -ENOMEM;
488         }
489
490         ret = qat_crypto_set_session_parameters(dev, xform, sess_private_data);
491         if (ret != 0) {
492                 PMD_DRV_LOG(ERR, "Crypto QAT PMD: failed to configure "
493                                 "session parameters");
494
495                 /* Return session to mempool */
496                 rte_mempool_put(mempool, sess_private_data);
497                 return ret;
498         }
499
500         set_session_private_data(sess, dev->driver_id,
501                 sess_private_data);
502
503         return 0;
504 }
505
506 int
507 qat_crypto_set_session_parameters(struct rte_cryptodev *dev,
508                 struct rte_crypto_sym_xform *xform, void *session_private)
509 {
510         struct qat_session *session = session_private;
511         int ret;
512
513         int qat_cmd_id;
514         PMD_INIT_FUNC_TRACE();
515
516         /* Set context descriptor physical address */
517         session->cd_paddr = rte_mempool_virt2phy(NULL, session) +
518                         offsetof(struct qat_session, cd);
519
520         session->min_qat_dev_gen = QAT_GEN1;
521
522         /* Get requested QAT command id */
523         qat_cmd_id = qat_get_cmd_id(xform);
524         if (qat_cmd_id < 0 || qat_cmd_id >= ICP_QAT_FW_LA_CMD_DELIMITER) {
525                 PMD_DRV_LOG(ERR, "Unsupported xform chain requested");
526                 return -ENOTSUP;
527         }
528         session->qat_cmd = (enum icp_qat_fw_la_cmd_id)qat_cmd_id;
529         switch (session->qat_cmd) {
530         case ICP_QAT_FW_LA_CMD_CIPHER:
531                 ret = qat_crypto_sym_configure_session_cipher(dev, xform, session);
532                 if (ret < 0)
533                         return ret;
534                 break;
535         case ICP_QAT_FW_LA_CMD_AUTH:
536                 ret = qat_crypto_sym_configure_session_auth(dev, xform, session);
537                 if (ret < 0)
538                         return ret;
539                 break;
540         case ICP_QAT_FW_LA_CMD_CIPHER_HASH:
541                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
542                         ret = qat_crypto_sym_configure_session_aead(xform,
543                                         session);
544                         if (ret < 0)
545                                 return ret;
546                 } else {
547                         ret = qat_crypto_sym_configure_session_cipher(dev,
548                                         xform, session);
549                         if (ret < 0)
550                                 return ret;
551                         ret = qat_crypto_sym_configure_session_auth(dev,
552                                         xform, session);
553                         if (ret < 0)
554                                 return ret;
555                 }
556                 break;
557         case ICP_QAT_FW_LA_CMD_HASH_CIPHER:
558                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
559                         ret = qat_crypto_sym_configure_session_aead(xform,
560                                         session);
561                         if (ret < 0)
562                                 return ret;
563                 } else {
564                         ret = qat_crypto_sym_configure_session_auth(dev,
565                                         xform, session);
566                         if (ret < 0)
567                                 return ret;
568                         ret = qat_crypto_sym_configure_session_cipher(dev,
569                                         xform, session);
570                         if (ret < 0)
571                                 return ret;
572                 }
573                 break;
574         case ICP_QAT_FW_LA_CMD_TRNG_GET_RANDOM:
575         case ICP_QAT_FW_LA_CMD_TRNG_TEST:
576         case ICP_QAT_FW_LA_CMD_SSL3_KEY_DERIVE:
577         case ICP_QAT_FW_LA_CMD_TLS_V1_1_KEY_DERIVE:
578         case ICP_QAT_FW_LA_CMD_TLS_V1_2_KEY_DERIVE:
579         case ICP_QAT_FW_LA_CMD_MGF1:
580         case ICP_QAT_FW_LA_CMD_AUTH_PRE_COMP:
581         case ICP_QAT_FW_LA_CMD_CIPHER_PRE_COMP:
582         case ICP_QAT_FW_LA_CMD_DELIMITER:
583         PMD_DRV_LOG(ERR, "Unsupported Service %u",
584                 session->qat_cmd);
585                 return -ENOTSUP;
586         default:
587         PMD_DRV_LOG(ERR, "Unsupported Service %u",
588                 session->qat_cmd);
589                 return -ENOTSUP;
590         }
591
592         return 0;
593 }
594
595 int
596 qat_crypto_sym_configure_session_auth(struct rte_cryptodev *dev,
597                                 struct rte_crypto_sym_xform *xform,
598                                 struct qat_session *session)
599 {
600         struct rte_crypto_auth_xform *auth_xform = NULL;
601         struct qat_pmd_private *internals = dev->data->dev_private;
602         auth_xform = qat_get_auth_xform(xform);
603         uint8_t *key_data = auth_xform->key.data;
604         uint8_t key_length = auth_xform->key.length;
605
606         switch (auth_xform->algo) {
607         case RTE_CRYPTO_AUTH_SHA1_HMAC:
608                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA1;
609                 break;
610         case RTE_CRYPTO_AUTH_SHA224_HMAC:
611                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA224;
612                 break;
613         case RTE_CRYPTO_AUTH_SHA256_HMAC:
614                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA256;
615                 break;
616         case RTE_CRYPTO_AUTH_SHA384_HMAC:
617                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA384;
618                 break;
619         case RTE_CRYPTO_AUTH_SHA512_HMAC:
620                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA512;
621                 break;
622         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
623                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_AES_XCBC_MAC;
624                 break;
625         case RTE_CRYPTO_AUTH_AES_GMAC:
626                 if (qat_alg_validate_aes_key(auth_xform->key.length,
627                                 &session->qat_cipher_alg) != 0) {
628                         PMD_DRV_LOG(ERR, "Invalid AES key size");
629                         return -EINVAL;
630                 }
631                 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
632                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_GALOIS_128;
633
634                 break;
635         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
636                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SNOW_3G_UIA2;
637                 break;
638         case RTE_CRYPTO_AUTH_MD5_HMAC:
639                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_MD5;
640                 break;
641         case RTE_CRYPTO_AUTH_NULL:
642                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_NULL;
643                 break;
644         case RTE_CRYPTO_AUTH_KASUMI_F9:
645                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_KASUMI_F9;
646                 break;
647         case RTE_CRYPTO_AUTH_ZUC_EIA3:
648                 if (!qat_is_auth_alg_supported(auth_xform->algo, internals)) {
649                         PMD_DRV_LOG(ERR, "%s not supported on this device",
650                                 rte_crypto_auth_algorithm_strings
651                                 [auth_xform->algo]);
652                         return -ENOTSUP;
653                 }
654                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_ZUC_3G_128_EIA3;
655                 break;
656         case RTE_CRYPTO_AUTH_SHA1:
657         case RTE_CRYPTO_AUTH_SHA256:
658         case RTE_CRYPTO_AUTH_SHA512:
659         case RTE_CRYPTO_AUTH_SHA224:
660         case RTE_CRYPTO_AUTH_SHA384:
661         case RTE_CRYPTO_AUTH_MD5:
662         case RTE_CRYPTO_AUTH_AES_CMAC:
663         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
664                 PMD_DRV_LOG(ERR, "Crypto: Unsupported hash alg %u",
665                                 auth_xform->algo);
666                 return -ENOTSUP;
667         default:
668                 PMD_DRV_LOG(ERR, "Crypto: Undefined Hash algo %u specified",
669                                 auth_xform->algo);
670                 return -EINVAL;
671         }
672
673         session->auth_iv.offset = auth_xform->iv.offset;
674         session->auth_iv.length = auth_xform->iv.length;
675
676         if (auth_xform->algo == RTE_CRYPTO_AUTH_AES_GMAC) {
677                 if (auth_xform->op == RTE_CRYPTO_AUTH_OP_GENERATE) {
678                         session->qat_cmd = ICP_QAT_FW_LA_CMD_CIPHER_HASH;
679                         session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
680                         /*
681                          * It needs to create cipher desc content first,
682                          * then authentication
683                          */
684                         if (qat_alg_aead_session_create_content_desc_cipher(session,
685                                                 auth_xform->key.data,
686                                                 auth_xform->key.length))
687                                 return -EINVAL;
688
689                         if (qat_alg_aead_session_create_content_desc_auth(session,
690                                                 key_data,
691                                                 key_length,
692                                                 0,
693                                                 auth_xform->digest_length,
694                                                 auth_xform->op))
695                                 return -EINVAL;
696                 } else {
697                         session->qat_cmd = ICP_QAT_FW_LA_CMD_HASH_CIPHER;
698                         session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
699                         /*
700                          * It needs to create authentication desc content first,
701                          * then cipher
702                          */
703                         if (qat_alg_aead_session_create_content_desc_auth(session,
704                                         key_data,
705                                         key_length,
706                                         0,
707                                         auth_xform->digest_length,
708                                         auth_xform->op))
709                                 return -EINVAL;
710
711                         if (qat_alg_aead_session_create_content_desc_cipher(session,
712                                                 auth_xform->key.data,
713                                                 auth_xform->key.length))
714                                 return -EINVAL;
715                 }
716                 /* Restore to authentication only only */
717                 session->qat_cmd = ICP_QAT_FW_LA_CMD_AUTH;
718         } else {
719                 if (qat_alg_aead_session_create_content_desc_auth(session,
720                                 key_data,
721                                 key_length,
722                                 0,
723                                 auth_xform->digest_length,
724                                 auth_xform->op))
725                         return -EINVAL;
726         }
727
728         session->digest_length = auth_xform->digest_length;
729         return 0;
730 }
731
732 int
733 qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
734                                 struct qat_session *session)
735 {
736         struct rte_crypto_aead_xform *aead_xform = &xform->aead;
737
738         /*
739          * Store AEAD IV parameters as cipher IV,
740          * to avoid unnecessary memory usage
741          */
742         session->cipher_iv.offset = xform->aead.iv.offset;
743         session->cipher_iv.length = xform->aead.iv.length;
744
745         switch (aead_xform->algo) {
746         case RTE_CRYPTO_AEAD_AES_GCM:
747                 if (qat_alg_validate_aes_key(aead_xform->key.length,
748                                 &session->qat_cipher_alg) != 0) {
749                         PMD_DRV_LOG(ERR, "Invalid AES key size");
750                         return -EINVAL;
751                 }
752                 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
753                 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_GALOIS_128;
754                 break;
755         case RTE_CRYPTO_AEAD_AES_CCM:
756                 PMD_DRV_LOG(ERR, "Crypto QAT PMD: Unsupported AEAD alg %u",
757                                 aead_xform->algo);
758                 return -ENOTSUP;
759         default:
760                 PMD_DRV_LOG(ERR, "Crypto: Undefined AEAD specified %u\n",
761                                 aead_xform->algo);
762                 return -EINVAL;
763         }
764
765         if (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
766                 session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
767                 /*
768                  * It needs to create cipher desc content first,
769                  * then authentication
770                  */
771                 if (qat_alg_aead_session_create_content_desc_cipher(session,
772                                         aead_xform->key.data,
773                                         aead_xform->key.length))
774                         return -EINVAL;
775
776                 if (qat_alg_aead_session_create_content_desc_auth(session,
777                                         aead_xform->key.data,
778                                         aead_xform->key.length,
779                                         aead_xform->aad_length,
780                                         aead_xform->digest_length,
781                                         RTE_CRYPTO_AUTH_OP_GENERATE))
782                         return -EINVAL;
783         } else {
784                 session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
785                 /*
786                  * It needs to create authentication desc content first,
787                  * then cipher
788                  */
789                 if (qat_alg_aead_session_create_content_desc_auth(session,
790                                         aead_xform->key.data,
791                                         aead_xform->key.length,
792                                         aead_xform->aad_length,
793                                         aead_xform->digest_length,
794                                         RTE_CRYPTO_AUTH_OP_VERIFY))
795                         return -EINVAL;
796
797                 if (qat_alg_aead_session_create_content_desc_cipher(session,
798                                         aead_xform->key.data,
799                                         aead_xform->key.length))
800                         return -EINVAL;
801         }
802
803         session->digest_length = aead_xform->digest_length;
804         return 0;
805 }
806
807 unsigned qat_crypto_sym_get_session_private_size(
808                 struct rte_cryptodev *dev __rte_unused)
809 {
810         return RTE_ALIGN_CEIL(sizeof(struct qat_session), 8);
811 }
812
813 static inline uint32_t
814 qat_bpicipher_preprocess(struct qat_session *ctx,
815                                 struct rte_crypto_op *op)
816 {
817         uint8_t block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
818         struct rte_crypto_sym_op *sym_op = op->sym;
819         uint8_t last_block_len = block_len > 0 ?
820                         sym_op->cipher.data.length % block_len : 0;
821
822         if (last_block_len &&
823                         ctx->qat_dir == ICP_QAT_HW_CIPHER_DECRYPT) {
824
825                 /* Decrypt last block */
826                 uint8_t *last_block, *dst, *iv;
827                 uint32_t last_block_offset = sym_op->cipher.data.offset +
828                                 sym_op->cipher.data.length - last_block_len;
829                 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
830                                 uint8_t *, last_block_offset);
831
832                 if (unlikely(sym_op->m_dst != NULL))
833                         /* out-of-place operation (OOP) */
834                         dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
835                                                 uint8_t *, last_block_offset);
836                 else
837                         dst = last_block;
838
839                 if (last_block_len < sym_op->cipher.data.length)
840                         /* use previous block ciphertext as IV */
841                         iv = last_block - block_len;
842                 else
843                         /* runt block, i.e. less than one full block */
844                         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
845                                         ctx->cipher_iv.offset);
846
847 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
848                 rte_hexdump(stdout, "BPI: src before pre-process:", last_block,
849                         last_block_len);
850                 if (sym_op->m_dst != NULL)
851                         rte_hexdump(stdout, "BPI: dst before pre-process:", dst,
852                                 last_block_len);
853 #endif
854                 bpi_cipher_decrypt(last_block, dst, iv, block_len,
855                                 last_block_len, ctx->bpi_ctx);
856 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
857                 rte_hexdump(stdout, "BPI: src after pre-process:", last_block,
858                         last_block_len);
859                 if (sym_op->m_dst != NULL)
860                         rte_hexdump(stdout, "BPI: dst after pre-process:", dst,
861                                 last_block_len);
862 #endif
863         }
864
865         return sym_op->cipher.data.length - last_block_len;
866 }
867
868 static inline uint32_t
869 qat_bpicipher_postprocess(struct qat_session *ctx,
870                                 struct rte_crypto_op *op)
871 {
872         uint8_t block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
873         struct rte_crypto_sym_op *sym_op = op->sym;
874         uint8_t last_block_len = block_len > 0 ?
875                         sym_op->cipher.data.length % block_len : 0;
876
877         if (last_block_len > 0 &&
878                         ctx->qat_dir == ICP_QAT_HW_CIPHER_ENCRYPT) {
879
880                 /* Encrypt last block */
881                 uint8_t *last_block, *dst, *iv;
882                 uint32_t last_block_offset;
883
884                 last_block_offset = sym_op->cipher.data.offset +
885                                 sym_op->cipher.data.length - last_block_len;
886                 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
887                                 uint8_t *, last_block_offset);
888
889                 if (unlikely(sym_op->m_dst != NULL))
890                         /* out-of-place operation (OOP) */
891                         dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
892                                                 uint8_t *, last_block_offset);
893                 else
894                         dst = last_block;
895
896                 if (last_block_len < sym_op->cipher.data.length)
897                         /* use previous block ciphertext as IV */
898                         iv = dst - block_len;
899                 else
900                         /* runt block, i.e. less than one full block */
901                         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
902                                         ctx->cipher_iv.offset);
903
904 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
905                 rte_hexdump(stdout, "BPI: src before post-process:", last_block,
906                         last_block_len);
907                 if (sym_op->m_dst != NULL)
908                         rte_hexdump(stdout, "BPI: dst before post-process:",
909                                         dst, last_block_len);
910 #endif
911                 bpi_cipher_encrypt(last_block, dst, iv, block_len,
912                                 last_block_len, ctx->bpi_ctx);
913 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
914                 rte_hexdump(stdout, "BPI: src after post-process:", last_block,
915                         last_block_len);
916                 if (sym_op->m_dst != NULL)
917                         rte_hexdump(stdout, "BPI: dst after post-process:", dst,
918                                 last_block_len);
919 #endif
920         }
921         return sym_op->cipher.data.length - last_block_len;
922 }
923
924 uint16_t
925 qat_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
926                 uint16_t nb_ops)
927 {
928         register struct qat_queue *queue;
929         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
930         register uint32_t nb_ops_sent = 0;
931         register struct rte_crypto_op **cur_op = ops;
932         register int ret;
933         uint16_t nb_ops_possible = nb_ops;
934         register uint8_t *base_addr;
935         register uint32_t tail;
936         int overflow;
937
938         if (unlikely(nb_ops == 0))
939                 return 0;
940
941         /* read params used a lot in main loop into registers */
942         queue = &(tmp_qp->tx_q);
943         base_addr = (uint8_t *)queue->base_addr;
944         tail = queue->tail;
945
946         /* Find how many can actually fit on the ring */
947         tmp_qp->inflights16 += nb_ops;
948         overflow = tmp_qp->inflights16 - queue->max_inflights;
949         if (overflow > 0) {
950                 tmp_qp->inflights16 -= overflow;
951                 nb_ops_possible = nb_ops - overflow;
952                 if (nb_ops_possible == 0)
953                         return 0;
954         }
955
956         while (nb_ops_sent != nb_ops_possible) {
957                 ret = qat_write_hw_desc_entry(*cur_op, base_addr + tail,
958                         tmp_qp->op_cookies[tail / queue->msg_size], tmp_qp);
959                 if (ret != 0) {
960                         tmp_qp->stats.enqueue_err_count++;
961                         /*
962                          * This message cannot be enqueued,
963                          * decrease number of ops that wasn't sent
964                          */
965                         tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
966                         if (nb_ops_sent == 0)
967                                 return 0;
968                         goto kick_tail;
969                 }
970
971                 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
972                 nb_ops_sent++;
973                 cur_op++;
974         }
975 kick_tail:
976         WRITE_CSR_RING_TAIL(tmp_qp->mmap_bar_addr, queue->hw_bundle_number,
977                         queue->hw_queue_number, tail);
978         queue->tail = tail;
979         tmp_qp->stats.enqueued_count += nb_ops_sent;
980         return nb_ops_sent;
981 }
982
983 uint16_t
984 qat_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
985                 uint16_t nb_ops)
986 {
987         struct qat_queue *queue;
988         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
989         uint32_t msg_counter = 0;
990         struct rte_crypto_op *rx_op;
991         struct icp_qat_fw_comn_resp *resp_msg;
992
993         queue = &(tmp_qp->rx_q);
994         resp_msg = (struct icp_qat_fw_comn_resp *)
995                         ((uint8_t *)queue->base_addr + queue->head);
996
997         while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
998                         msg_counter != nb_ops) {
999                 rx_op = (struct rte_crypto_op *)(uintptr_t)
1000                                 (resp_msg->opaque_data);
1001
1002 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
1003                 rte_hexdump(stdout, "qat_response:", (uint8_t *)resp_msg,
1004                         sizeof(struct icp_qat_fw_comn_resp));
1005
1006 #endif
1007                 if (ICP_QAT_FW_COMN_STATUS_FLAG_OK !=
1008                                 ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
1009                                         resp_msg->comn_hdr.comn_status)) {
1010                         rx_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1011                 } else {
1012                         struct qat_session *sess = (struct qat_session *)
1013                                         get_session_private_data(
1014                                         rx_op->sym->session,
1015                                         cryptodev_qat_driver_id);
1016
1017                         if (sess->bpi_ctx)
1018                                 qat_bpicipher_postprocess(sess, rx_op);
1019                         rx_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1020                 }
1021
1022                 *(uint32_t *)resp_msg = ADF_RING_EMPTY_SIG;
1023                 queue->head = adf_modulo(queue->head +
1024                                 queue->msg_size,
1025                                 ADF_RING_SIZE_MODULO(queue->queue_size));
1026                 resp_msg = (struct icp_qat_fw_comn_resp *)
1027                                         ((uint8_t *)queue->base_addr +
1028                                                         queue->head);
1029                 *ops = rx_op;
1030                 ops++;
1031                 msg_counter++;
1032         }
1033         if (msg_counter > 0) {
1034                 WRITE_CSR_RING_HEAD(tmp_qp->mmap_bar_addr,
1035                                         queue->hw_bundle_number,
1036                                         queue->hw_queue_number, queue->head);
1037                 tmp_qp->inflights16 -= msg_counter;
1038                 tmp_qp->stats.dequeued_count += msg_counter;
1039         }
1040         return msg_counter;
1041 }
1042
1043 static inline int
1044 qat_sgl_fill_array(struct rte_mbuf *buf, uint64_t buff_start,
1045                 struct qat_alg_buf_list *list, uint32_t data_len)
1046 {
1047         int nr = 1;
1048
1049         uint32_t buf_len = rte_pktmbuf_mtophys(buf) -
1050                         buff_start + rte_pktmbuf_data_len(buf);
1051
1052         list->bufers[0].addr = buff_start;
1053         list->bufers[0].resrvd = 0;
1054         list->bufers[0].len = buf_len;
1055
1056         if (data_len <= buf_len) {
1057                 list->num_bufs = nr;
1058                 list->bufers[0].len = data_len;
1059                 return 0;
1060         }
1061
1062         buf = buf->next;
1063         while (buf) {
1064                 if (unlikely(nr == QAT_SGL_MAX_NUMBER)) {
1065                         PMD_DRV_LOG(ERR, "QAT PMD exceeded size of QAT SGL"
1066                                         " entry(%u)",
1067                                         QAT_SGL_MAX_NUMBER);
1068                         return -EINVAL;
1069                 }
1070
1071                 list->bufers[nr].len = rte_pktmbuf_data_len(buf);
1072                 list->bufers[nr].resrvd = 0;
1073                 list->bufers[nr].addr = rte_pktmbuf_mtophys(buf);
1074
1075                 buf_len += list->bufers[nr].len;
1076                 buf = buf->next;
1077
1078                 if (buf_len > data_len) {
1079                         list->bufers[nr].len -=
1080                                 buf_len - data_len;
1081                         buf = NULL;
1082                 }
1083                 ++nr;
1084         }
1085         list->num_bufs = nr;
1086
1087         return 0;
1088 }
1089
1090 static inline void
1091 set_cipher_iv(uint16_t iv_length, uint16_t iv_offset,
1092                 struct icp_qat_fw_la_cipher_req_params *cipher_param,
1093                 struct rte_crypto_op *op,
1094                 struct icp_qat_fw_la_bulk_req *qat_req)
1095 {
1096         /* copy IV into request if it fits */
1097         if (iv_length <= sizeof(cipher_param->u.cipher_IV_array)) {
1098                 rte_memcpy(cipher_param->u.cipher_IV_array,
1099                                 rte_crypto_op_ctod_offset(op, uint8_t *,
1100                                         iv_offset),
1101                                 iv_length);
1102         } else {
1103                 ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(
1104                                 qat_req->comn_hdr.serv_specif_flags,
1105                                 ICP_QAT_FW_CIPH_IV_64BIT_PTR);
1106                 cipher_param->u.s.cipher_IV_ptr =
1107                                 rte_crypto_op_ctophys_offset(op,
1108                                         iv_offset);
1109         }
1110 }
1111
1112 static inline int
1113 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
1114                 struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp)
1115 {
1116         int ret = 0;
1117         struct qat_session *ctx;
1118         struct icp_qat_fw_la_cipher_req_params *cipher_param;
1119         struct icp_qat_fw_la_auth_req_params *auth_param;
1120         register struct icp_qat_fw_la_bulk_req *qat_req;
1121         uint8_t do_auth = 0, do_cipher = 0, do_aead = 0;
1122         uint32_t cipher_len = 0, cipher_ofs = 0;
1123         uint32_t auth_len = 0, auth_ofs = 0;
1124         uint32_t min_ofs = 0;
1125         uint64_t src_buf_start = 0, dst_buf_start = 0;
1126         uint8_t do_sgl = 0;
1127
1128 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
1129         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
1130                 PMD_DRV_LOG(ERR, "QAT PMD only supports symmetric crypto "
1131                                 "operation requests, op (%p) is not a "
1132                                 "symmetric operation.", op);
1133                 return -EINVAL;
1134         }
1135 #endif
1136         if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
1137                 PMD_DRV_LOG(ERR, "QAT PMD only supports session oriented"
1138                                 " requests, op (%p) is sessionless.", op);
1139                 return -EINVAL;
1140         }
1141
1142         ctx = (struct qat_session *)get_session_private_data(
1143                         op->sym->session, cryptodev_qat_driver_id);
1144
1145         if (unlikely(ctx == NULL)) {
1146                 PMD_DRV_LOG(ERR, "Session was not created for this device");
1147                 return -EINVAL;
1148         }
1149
1150         if (unlikely(ctx->min_qat_dev_gen > qp->qat_dev_gen)) {
1151                 PMD_DRV_LOG(ERR, "Session alg not supported on this device gen");
1152                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1153                 return -EINVAL;
1154         }
1155
1156         qat_req = (struct icp_qat_fw_la_bulk_req *)out_msg;
1157         rte_mov128((uint8_t *)qat_req, (const uint8_t *)&(ctx->fw_req));
1158         qat_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
1159         cipher_param = (void *)&qat_req->serv_specif_rqpars;
1160         auth_param = (void *)((uint8_t *)cipher_param + sizeof(*cipher_param));
1161
1162         if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_HASH_CIPHER ||
1163                         ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER_HASH) {
1164                 /* AES-GCM */
1165                 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1166                                 ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
1167                         do_aead = 1;
1168                 } else {
1169                         do_auth = 1;
1170                         do_cipher = 1;
1171                 }
1172         } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_AUTH) {
1173                 do_auth = 1;
1174                 do_cipher = 0;
1175         } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER) {
1176                 do_auth = 0;
1177                 do_cipher = 1;
1178         }
1179
1180         if (do_cipher) {
1181
1182                 if (ctx->qat_cipher_alg ==
1183                                          ICP_QAT_HW_CIPHER_ALGO_SNOW_3G_UEA2 ||
1184                         ctx->qat_cipher_alg == ICP_QAT_HW_CIPHER_ALGO_KASUMI ||
1185                         ctx->qat_cipher_alg ==
1186                                 ICP_QAT_HW_CIPHER_ALGO_ZUC_3G_128_EEA3) {
1187
1188                         if (unlikely(
1189                                 (cipher_param->cipher_length % BYTE_LENGTH != 0)
1190                                  || (cipher_param->cipher_offset
1191                                                         % BYTE_LENGTH != 0))) {
1192                                 PMD_DRV_LOG(ERR,
1193                   "SNOW3G/KASUMI/ZUC in QAT PMD only supports byte aligned values");
1194                                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1195                                 return -EINVAL;
1196                         }
1197                         cipher_len = op->sym->cipher.data.length >> 3;
1198                         cipher_ofs = op->sym->cipher.data.offset >> 3;
1199
1200                 } else if (ctx->bpi_ctx) {
1201                         /* DOCSIS - only send complete blocks to device
1202                          * Process any partial block using CFB mode.
1203                          * Even if 0 complete blocks, still send this to device
1204                          * to get into rx queue for post-process and dequeuing
1205                          */
1206                         cipher_len = qat_bpicipher_preprocess(ctx, op);
1207                         cipher_ofs = op->sym->cipher.data.offset;
1208                 } else {
1209                         cipher_len = op->sym->cipher.data.length;
1210                         cipher_ofs = op->sym->cipher.data.offset;
1211                 }
1212
1213                 set_cipher_iv(ctx->cipher_iv.length, ctx->cipher_iv.offset,
1214                                 cipher_param, op, qat_req);
1215                 min_ofs = cipher_ofs;
1216         }
1217
1218         if (do_auth) {
1219
1220                 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_SNOW_3G_UIA2 ||
1221                         ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_KASUMI_F9 ||
1222                         ctx->qat_hash_alg ==
1223                                 ICP_QAT_HW_AUTH_ALGO_ZUC_3G_128_EIA3) {
1224                         if (unlikely((auth_param->auth_off % BYTE_LENGTH != 0)
1225                                 || (auth_param->auth_len % BYTE_LENGTH != 0))) {
1226                                 PMD_DRV_LOG(ERR,
1227                 "For SNOW3G/KASUMI/ZUC, QAT PMD only supports byte aligned values");
1228                                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1229                                 return -EINVAL;
1230                         }
1231                         auth_ofs = op->sym->auth.data.offset >> 3;
1232                         auth_len = op->sym->auth.data.length >> 3;
1233
1234                         auth_param->u1.aad_adr =
1235                                         rte_crypto_op_ctophys_offset(op,
1236                                                         ctx->auth_iv.offset);
1237
1238                 } else if (ctx->qat_hash_alg ==
1239                                         ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1240                                 ctx->qat_hash_alg ==
1241                                         ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
1242                         /* AES-GMAC */
1243                         set_cipher_iv(ctx->auth_iv.length,
1244                                 ctx->auth_iv.offset,
1245                                 cipher_param, op, qat_req);
1246                         auth_ofs = op->sym->auth.data.offset;
1247                         auth_len = op->sym->auth.data.length;
1248
1249                         auth_param->u1.aad_adr = 0;
1250                         auth_param->u2.aad_sz = 0;
1251
1252                         /*
1253                          * If len(iv)==12B fw computes J0
1254                          */
1255                         if (ctx->auth_iv.length == 12) {
1256                                 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
1257                                         qat_req->comn_hdr.serv_specif_flags,
1258                                         ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
1259
1260                         }
1261                 } else {
1262                         auth_ofs = op->sym->auth.data.offset;
1263                         auth_len = op->sym->auth.data.length;
1264
1265                 }
1266                 min_ofs = auth_ofs;
1267
1268                 auth_param->auth_res_addr = op->sym->auth.digest.phys_addr;
1269
1270         }
1271
1272         if (do_aead) {
1273                 if (ctx->qat_hash_alg ==
1274                                 ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1275                                 ctx->qat_hash_alg ==
1276                                         ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
1277                         /*
1278                          * If len(iv)==12B fw computes J0
1279                          */
1280                         if (ctx->cipher_iv.length == 12) {
1281                                 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
1282                                         qat_req->comn_hdr.serv_specif_flags,
1283                                         ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
1284                         }
1285
1286                 }
1287
1288                 cipher_len = op->sym->aead.data.length;
1289                 cipher_ofs = op->sym->aead.data.offset;
1290                 auth_len = op->sym->aead.data.length;
1291                 auth_ofs = op->sym->aead.data.offset;
1292
1293                 auth_param->u1.aad_adr = op->sym->aead.aad.phys_addr;
1294                 auth_param->auth_res_addr = op->sym->aead.digest.phys_addr;
1295                 set_cipher_iv(ctx->cipher_iv.length, ctx->cipher_iv.offset,
1296                                 cipher_param, op, qat_req);
1297                 min_ofs = op->sym->aead.data.offset;
1298         }
1299
1300         if (op->sym->m_src->next || (op->sym->m_dst && op->sym->m_dst->next))
1301                 do_sgl = 1;
1302
1303         /* adjust for chain case */
1304         if (do_cipher && do_auth)
1305                 min_ofs = cipher_ofs < auth_ofs ? cipher_ofs : auth_ofs;
1306
1307         if (unlikely(min_ofs >= rte_pktmbuf_data_len(op->sym->m_src) && do_sgl))
1308                 min_ofs = 0;
1309
1310         if (unlikely(op->sym->m_dst != NULL)) {
1311                 /* Out-of-place operation (OOP)
1312                  * Don't align DMA start. DMA the minimum data-set
1313                  * so as not to overwrite data in dest buffer
1314                  */
1315                 src_buf_start =
1316                         rte_pktmbuf_mtophys_offset(op->sym->m_src, min_ofs);
1317                 dst_buf_start =
1318                         rte_pktmbuf_mtophys_offset(op->sym->m_dst, min_ofs);
1319
1320         } else {
1321                 /* In-place operation
1322                  * Start DMA at nearest aligned address below min_ofs
1323                  */
1324                 src_buf_start =
1325                         rte_pktmbuf_mtophys_offset(op->sym->m_src, min_ofs)
1326                                                 & QAT_64_BTYE_ALIGN_MASK;
1327
1328                 if (unlikely((rte_pktmbuf_mtophys(op->sym->m_src) -
1329                                         rte_pktmbuf_headroom(op->sym->m_src))
1330                                                         > src_buf_start)) {
1331                         /* alignment has pushed addr ahead of start of mbuf
1332                          * so revert and take the performance hit
1333                          */
1334                         src_buf_start =
1335                                 rte_pktmbuf_mtophys_offset(op->sym->m_src,
1336                                                                 min_ofs);
1337                 }
1338                 dst_buf_start = src_buf_start;
1339         }
1340
1341         if (do_cipher || do_aead) {
1342                 cipher_param->cipher_offset =
1343                                 (uint32_t)rte_pktmbuf_mtophys_offset(
1344                                 op->sym->m_src, cipher_ofs) - src_buf_start;
1345                 cipher_param->cipher_length = cipher_len;
1346         } else {
1347                 cipher_param->cipher_offset = 0;
1348                 cipher_param->cipher_length = 0;
1349         }
1350
1351         if (do_auth || do_aead) {
1352                 auth_param->auth_off = (uint32_t)rte_pktmbuf_mtophys_offset(
1353                                 op->sym->m_src, auth_ofs) - src_buf_start;
1354                 auth_param->auth_len = auth_len;
1355         } else {
1356                 auth_param->auth_off = 0;
1357                 auth_param->auth_len = 0;
1358         }
1359
1360         qat_req->comn_mid.dst_length =
1361                 qat_req->comn_mid.src_length =
1362                 (cipher_param->cipher_offset + cipher_param->cipher_length)
1363                 > (auth_param->auth_off + auth_param->auth_len) ?
1364                 (cipher_param->cipher_offset + cipher_param->cipher_length)
1365                 : (auth_param->auth_off + auth_param->auth_len);
1366
1367         if (do_sgl) {
1368
1369                 ICP_QAT_FW_COMN_PTR_TYPE_SET(qat_req->comn_hdr.comn_req_flags,
1370                                 QAT_COMN_PTR_TYPE_SGL);
1371                 ret = qat_sgl_fill_array(op->sym->m_src, src_buf_start,
1372                                 &qat_op_cookie->qat_sgl_list_src,
1373                                 qat_req->comn_mid.src_length);
1374                 if (ret) {
1375                         PMD_DRV_LOG(ERR, "QAT PMD Cannot fill sgl array");
1376                         return ret;
1377                 }
1378
1379                 if (likely(op->sym->m_dst == NULL))
1380                         qat_req->comn_mid.dest_data_addr =
1381                                 qat_req->comn_mid.src_data_addr =
1382                                 qat_op_cookie->qat_sgl_src_phys_addr;
1383                 else {
1384                         ret = qat_sgl_fill_array(op->sym->m_dst,
1385                                         dst_buf_start,
1386                                         &qat_op_cookie->qat_sgl_list_dst,
1387                                                 qat_req->comn_mid.dst_length);
1388
1389                         if (ret) {
1390                                 PMD_DRV_LOG(ERR, "QAT PMD Cannot "
1391                                                 "fill sgl array");
1392                                 return ret;
1393                         }
1394
1395                         qat_req->comn_mid.src_data_addr =
1396                                 qat_op_cookie->qat_sgl_src_phys_addr;
1397                         qat_req->comn_mid.dest_data_addr =
1398                                         qat_op_cookie->qat_sgl_dst_phys_addr;
1399                 }
1400         } else {
1401                 qat_req->comn_mid.src_data_addr = src_buf_start;
1402                 qat_req->comn_mid.dest_data_addr = dst_buf_start;
1403         }
1404
1405 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
1406         rte_hexdump(stdout, "qat_req:", qat_req,
1407                         sizeof(struct icp_qat_fw_la_bulk_req));
1408         rte_hexdump(stdout, "src_data:",
1409                         rte_pktmbuf_mtod(op->sym->m_src, uint8_t*),
1410                         rte_pktmbuf_data_len(op->sym->m_src));
1411         if (do_cipher) {
1412                 uint8_t *cipher_iv_ptr = rte_crypto_op_ctod_offset(op,
1413                                                 uint8_t *,
1414                                                 ctx->cipher_iv.offset);
1415                 rte_hexdump(stdout, "cipher iv:", cipher_iv_ptr,
1416                                 ctx->cipher_iv.length);
1417         }
1418
1419         if (do_auth) {
1420                 if (ctx->auth_iv.length) {
1421                         uint8_t *auth_iv_ptr = rte_crypto_op_ctod_offset(op,
1422                                                         uint8_t *,
1423                                                         ctx->auth_iv.offset);
1424                         rte_hexdump(stdout, "auth iv:", auth_iv_ptr,
1425                                                 ctx->auth_iv.length);
1426                 }
1427                 rte_hexdump(stdout, "digest:", op->sym->auth.digest.data,
1428                                 ctx->digest_length);
1429         }
1430
1431         if (do_aead) {
1432                 rte_hexdump(stdout, "digest:", op->sym->aead.digest.data,
1433                                 ctx->digest_length);
1434                 rte_hexdump(stdout, "aad:", op->sym->aead.aad.data,
1435                                 ctx->aad_len);
1436         }
1437 #endif
1438         return 0;
1439 }
1440
1441 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
1442 {
1443         uint32_t div = data >> shift;
1444         uint32_t mult = div << shift;
1445
1446         return data - mult;
1447 }
1448
1449 int qat_dev_config(__rte_unused struct rte_cryptodev *dev,
1450                 __rte_unused struct rte_cryptodev_config *config)
1451 {
1452         PMD_INIT_FUNC_TRACE();
1453         return 0;
1454 }
1455
1456 int qat_dev_start(__rte_unused struct rte_cryptodev *dev)
1457 {
1458         PMD_INIT_FUNC_TRACE();
1459         return 0;
1460 }
1461
1462 void qat_dev_stop(__rte_unused struct rte_cryptodev *dev)
1463 {
1464         PMD_INIT_FUNC_TRACE();
1465 }
1466
1467 int qat_dev_close(struct rte_cryptodev *dev)
1468 {
1469         int i, ret;
1470
1471         PMD_INIT_FUNC_TRACE();
1472
1473         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1474                 ret = qat_crypto_sym_qp_release(dev, i);
1475                 if (ret < 0)
1476                         return ret;
1477         }
1478
1479         return 0;
1480 }
1481
1482 void qat_dev_info_get(struct rte_cryptodev *dev,
1483                         struct rte_cryptodev_info *info)
1484 {
1485         struct qat_pmd_private *internals = dev->data->dev_private;
1486
1487         PMD_INIT_FUNC_TRACE();
1488         if (info != NULL) {
1489                 info->max_nb_queue_pairs =
1490                                 ADF_NUM_SYM_QPS_PER_BUNDLE *
1491                                 ADF_NUM_BUNDLES_PER_DEV;
1492                 info->feature_flags = dev->feature_flags;
1493                 info->capabilities = internals->qat_dev_capabilities;
1494                 info->sym.max_nb_sessions = internals->max_nb_sessions;
1495                 info->driver_id = cryptodev_qat_driver_id;
1496                 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
1497         }
1498 }
1499
1500 void qat_crypto_sym_stats_get(struct rte_cryptodev *dev,
1501                 struct rte_cryptodev_stats *stats)
1502 {
1503         int i;
1504         struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
1505
1506         PMD_INIT_FUNC_TRACE();
1507         if (stats == NULL) {
1508                 PMD_DRV_LOG(ERR, "invalid stats ptr NULL");
1509                 return;
1510         }
1511         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1512                 if (qp[i] == NULL) {
1513                         PMD_DRV_LOG(DEBUG, "Uninitialised queue pair");
1514                         continue;
1515                 }
1516
1517                 stats->enqueued_count += qp[i]->stats.enqueued_count;
1518                 stats->dequeued_count += qp[i]->stats.dequeued_count;
1519                 stats->enqueue_err_count += qp[i]->stats.enqueue_err_count;
1520                 stats->dequeue_err_count += qp[i]->stats.dequeue_err_count;
1521         }
1522 }
1523
1524 void qat_crypto_sym_stats_reset(struct rte_cryptodev *dev)
1525 {
1526         int i;
1527         struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
1528
1529         PMD_INIT_FUNC_TRACE();
1530         for (i = 0; i < dev->data->nb_queue_pairs; i++)
1531                 memset(&(qp[i]->stats), 0, sizeof(qp[i]->stats));
1532         PMD_DRV_LOG(DEBUG, "QAT crypto: stats cleared");
1533 }