LCOV - code coverage report
Current view: top level - drivers/crypto/nitrox - nitrox_sym.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 331 0.3 %
Date: 2025-01-02 22:41:34 Functions: 1 28 3.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 174 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2019 Marvell International Ltd.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdbool.h>
       6                 :            : 
       7                 :            : #include <cryptodev_pmd.h>
       8                 :            : #include <rte_crypto.h>
       9                 :            : 
      10                 :            : #include "nitrox_sym.h"
      11                 :            : #include "nitrox_device.h"
      12                 :            : #include "nitrox_sym_capabilities.h"
      13                 :            : #include "nitrox_qp.h"
      14                 :            : #include "nitrox_sym_reqmgr.h"
      15                 :            : #include "nitrox_sym_ctx.h"
      16                 :            : #include "nitrox_logs.h"
      17                 :            : 
      18                 :            : #define CRYPTODEV_NAME_NITROX_PMD crypto_nitrox_sym
      19                 :            : #define MC_MAC_MISMATCH_ERR_CODE 0x4c
      20                 :            : #define NPS_PKT_IN_INSTR_SIZE 64
      21                 :            : #define IV_FROM_DPTR 1
      22                 :            : #define FLEXI_CRYPTO_ENCRYPT_HMAC 0x33
      23                 :            : #define FLEXI_CRYPTO_MAX_AAD_LEN 512
      24                 :            : #define AES_KEYSIZE_128 16
      25                 :            : #define AES_KEYSIZE_192 24
      26                 :            : #define AES_KEYSIZE_256 32
      27                 :            : #define MAX_IV_LEN 16
      28                 :            : 
      29                 :            : struct nitrox_sym_device {
      30                 :            :         struct rte_cryptodev *cdev;
      31                 :            :         struct nitrox_device *ndev;
      32                 :            : };
      33                 :            : 
      34                 :            : /* Cipher opcodes */
      35                 :            : enum flexi_cipher {
      36                 :            :         CIPHER_NULL = 0,
      37                 :            :         CIPHER_3DES_CBC,
      38                 :            :         CIPHER_3DES_ECB,
      39                 :            :         CIPHER_AES_CBC,
      40                 :            :         CIPHER_AES_ECB,
      41                 :            :         CIPHER_AES_CFB,
      42                 :            :         CIPHER_AES_CTR,
      43                 :            :         CIPHER_AES_GCM,
      44                 :            :         CIPHER_AES_XTS,
      45                 :            :         CIPHER_AES_CCM,
      46                 :            :         CIPHER_AES_CBC_CTS,
      47                 :            :         CIPHER_AES_ECB_CTS,
      48                 :            :         CIPHER_INVALID
      49                 :            : };
      50                 :            : 
      51                 :            : /* Auth opcodes */
      52                 :            : enum flexi_auth {
      53                 :            :         AUTH_NULL = 0,
      54                 :            :         AUTH_MD5,
      55                 :            :         AUTH_SHA1,
      56                 :            :         AUTH_SHA2_SHA224,
      57                 :            :         AUTH_SHA2_SHA256,
      58                 :            :         AUTH_SHA2_SHA384,
      59                 :            :         AUTH_SHA2_SHA512,
      60                 :            :         AUTH_GMAC,
      61                 :            :         AUTH_INVALID
      62                 :            : };
      63                 :            : 
      64                 :            : uint8_t nitrox_sym_drv_id;
      65                 :            : static const char nitrox_sym_drv_name[] = RTE_STR(CRYPTODEV_NAME_NITROX_PMD);
      66                 :            : static const struct rte_driver nitrox_rte_sym_drv = {
      67                 :            :         .name = nitrox_sym_drv_name,
      68                 :            :         .alias = nitrox_sym_drv_name
      69                 :            : };
      70                 :            : 
      71                 :            : static int nitrox_sym_dev_qp_release(struct rte_cryptodev *cdev,
      72                 :            :                                      uint16_t qp_id);
      73                 :            : 
      74                 :            : static int
      75                 :          0 : nitrox_sym_dev_config(struct rte_cryptodev *cdev,
      76                 :            :                       struct rte_cryptodev_config *config)
      77                 :            : {
      78                 :          0 :         struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
      79                 :          0 :         struct nitrox_device *ndev = sym_dev->ndev;
      80                 :            : 
      81         [ #  # ]:          0 :         if (config->nb_queue_pairs > ndev->nr_queues) {
      82                 :          0 :                 NITROX_LOG_LINE(ERR, "Invalid queue pairs, max supported %d",
      83                 :            :                            ndev->nr_queues);
      84                 :          0 :                 return -EINVAL;
      85                 :            :         }
      86                 :            : 
      87                 :            :         return 0;
      88                 :            : }
      89                 :            : 
      90                 :            : static int
      91                 :          0 : nitrox_sym_dev_start(struct rte_cryptodev *cdev)
      92                 :            : {
      93                 :            :         /* SE cores initialization is done in PF */
      94                 :            :         RTE_SET_USED(cdev);
      95                 :          0 :         return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : static void
      99                 :          0 : nitrox_sym_dev_stop(struct rte_cryptodev *cdev)
     100                 :            : {
     101                 :            :         /* SE cores cleanup is done in PF */
     102                 :            :         RTE_SET_USED(cdev);
     103                 :          0 : }
     104                 :            : 
     105                 :            : static int
     106                 :          0 : nitrox_sym_dev_close(struct rte_cryptodev *cdev)
     107                 :            : {
     108                 :            :         int i, ret;
     109                 :            : 
     110         [ #  # ]:          0 :         for (i = 0; i < cdev->data->nb_queue_pairs; i++) {
     111                 :          0 :                 ret = nitrox_sym_dev_qp_release(cdev, i);
     112         [ #  # ]:          0 :                 if (ret)
     113                 :          0 :                         return ret;
     114                 :            :         }
     115                 :            : 
     116                 :            :         return 0;
     117                 :            : }
     118                 :            : 
     119                 :            : static void
     120                 :          0 : nitrox_sym_dev_info_get(struct rte_cryptodev *cdev,
     121                 :            :                         struct rte_cryptodev_info *info)
     122                 :            : {
     123                 :          0 :         struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
     124                 :          0 :         struct nitrox_device *ndev = sym_dev->ndev;
     125                 :            : 
     126         [ #  # ]:          0 :         if (!info)
     127                 :            :                 return;
     128                 :            : 
     129                 :          0 :         info->max_nb_queue_pairs = ndev->nr_queues;
     130                 :          0 :         info->feature_flags = cdev->feature_flags;
     131                 :          0 :         info->capabilities = nitrox_get_sym_capabilities();
     132                 :          0 :         info->driver_id = nitrox_sym_drv_id;
     133                 :          0 :         info->sym.max_nb_sessions = 0;
     134                 :            : }
     135                 :            : 
     136                 :            : static void
     137                 :          0 : nitrox_sym_dev_stats_get(struct rte_cryptodev *cdev,
     138                 :            :                          struct rte_cryptodev_stats *stats)
     139                 :            : {
     140                 :            :         int qp_id;
     141                 :            : 
     142         [ #  # ]:          0 :         for (qp_id = 0; qp_id < cdev->data->nb_queue_pairs; qp_id++) {
     143                 :          0 :                 struct nitrox_qp *qp = cdev->data->queue_pairs[qp_id];
     144                 :            : 
     145         [ #  # ]:          0 :                 if (!qp)
     146                 :          0 :                         continue;
     147                 :            : 
     148                 :          0 :                 stats->enqueued_count += qp->stats.enqueued_count;
     149                 :          0 :                 stats->dequeued_count += qp->stats.dequeued_count;
     150                 :          0 :                 stats->enqueue_err_count += qp->stats.enqueue_err_count;
     151                 :          0 :                 stats->dequeue_err_count += qp->stats.dequeue_err_count;
     152                 :            :         }
     153                 :          0 : }
     154                 :            : 
     155                 :            : static void
     156                 :          0 : nitrox_sym_dev_stats_reset(struct rte_cryptodev *cdev)
     157                 :            : {
     158                 :            :         int qp_id;
     159                 :            : 
     160         [ #  # ]:          0 :         for (qp_id = 0; qp_id < cdev->data->nb_queue_pairs; qp_id++) {
     161                 :          0 :                 struct nitrox_qp *qp = cdev->data->queue_pairs[qp_id];
     162                 :            : 
     163         [ #  # ]:          0 :                 if (!qp)
     164                 :          0 :                         continue;
     165                 :            : 
     166                 :          0 :                 memset(&qp->stats, 0, sizeof(qp->stats));
     167                 :            :         }
     168                 :          0 : }
     169                 :            : 
     170                 :            : static int
     171                 :          0 : nitrox_sym_dev_qp_setup(struct rte_cryptodev *cdev, uint16_t qp_id,
     172                 :            :                         const struct rte_cryptodev_qp_conf *qp_conf,
     173                 :            :                         int socket_id)
     174                 :            : {
     175                 :          0 :         struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
     176                 :          0 :         struct nitrox_device *ndev = sym_dev->ndev;
     177                 :            :         struct nitrox_qp *qp = NULL;
     178                 :            :         int err;
     179                 :            : 
     180                 :          0 :         NITROX_LOG_LINE(DEBUG, "queue %d", qp_id);
     181         [ #  # ]:          0 :         if (qp_id >= ndev->nr_queues) {
     182                 :          0 :                 NITROX_LOG_LINE(ERR, "queue %u invalid, max queues supported %d",
     183                 :            :                            qp_id, ndev->nr_queues);
     184                 :          0 :                 return -EINVAL;
     185                 :            :         }
     186                 :            : 
     187         [ #  # ]:          0 :         if (cdev->data->queue_pairs[qp_id]) {
     188                 :          0 :                 err = nitrox_sym_dev_qp_release(cdev, qp_id);
     189         [ #  # ]:          0 :                 if (err)
     190                 :            :                         return err;
     191                 :            :         }
     192                 :            : 
     193                 :          0 :         qp = rte_zmalloc_socket("nitrox PMD qp", sizeof(*qp),
     194                 :            :                                 RTE_CACHE_LINE_SIZE,
     195                 :            :                                 socket_id);
     196         [ #  # ]:          0 :         if (!qp) {
     197                 :          0 :                 NITROX_LOG_LINE(ERR, "Failed to allocate nitrox qp");
     198                 :          0 :                 return -ENOMEM;
     199                 :            :         }
     200                 :            : 
     201                 :          0 :         qp->type = NITROX_QUEUE_SE;
     202                 :          0 :         qp->qno = qp_id;
     203                 :          0 :         err = nitrox_qp_setup(qp, ndev->bar_addr, cdev->data->name,
     204                 :          0 :                               qp_conf->nb_descriptors, NPS_PKT_IN_INSTR_SIZE,
     205                 :            :                               socket_id);
     206         [ #  # ]:          0 :         if (unlikely(err))
     207                 :          0 :                 goto qp_setup_err;
     208                 :            : 
     209                 :          0 :         qp->sr_mp = nitrox_sym_req_pool_create(cdev, qp->count, qp_id,
     210                 :            :                                                socket_id);
     211         [ #  # ]:          0 :         if (unlikely(!qp->sr_mp))
     212                 :          0 :                 goto req_pool_err;
     213                 :            : 
     214                 :          0 :         cdev->data->queue_pairs[qp_id] = qp;
     215                 :          0 :         NITROX_LOG_LINE(DEBUG, "queue %d setup done", qp_id);
     216                 :          0 :         return 0;
     217                 :            : 
     218                 :            : req_pool_err:
     219                 :          0 :         nitrox_qp_release(qp, ndev->bar_addr);
     220                 :          0 : qp_setup_err:
     221                 :          0 :         rte_free(qp);
     222                 :          0 :         return err;
     223                 :            : }
     224                 :            : 
     225                 :            : static int
     226                 :          0 : nitrox_sym_dev_qp_release(struct rte_cryptodev *cdev, uint16_t qp_id)
     227                 :            : {
     228                 :          0 :         struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
     229                 :          0 :         struct nitrox_device *ndev = sym_dev->ndev;
     230                 :            :         struct nitrox_qp *qp;
     231                 :            :         int err;
     232                 :            : 
     233                 :          0 :         NITROX_LOG_LINE(DEBUG, "queue %d", qp_id);
     234         [ #  # ]:          0 :         if (qp_id >= ndev->nr_queues) {
     235                 :          0 :                 NITROX_LOG_LINE(ERR, "queue %u invalid, max queues supported %d",
     236                 :            :                            qp_id, ndev->nr_queues);
     237                 :          0 :                 return -EINVAL;
     238                 :            :         }
     239                 :            : 
     240                 :          0 :         qp = cdev->data->queue_pairs[qp_id];
     241         [ #  # ]:          0 :         if (!qp) {
     242                 :          0 :                 NITROX_LOG_LINE(DEBUG, "queue %u already freed", qp_id);
     243                 :          0 :                 return 0;
     244                 :            :         }
     245                 :            : 
     246         [ #  # ]:          0 :         if (!nitrox_qp_is_empty(qp)) {
     247                 :          0 :                 NITROX_LOG_LINE(ERR, "queue %d not empty", qp_id);
     248                 :          0 :                 return -EAGAIN;
     249                 :            :         }
     250                 :            : 
     251                 :          0 :         cdev->data->queue_pairs[qp_id] = NULL;
     252                 :          0 :         err = nitrox_qp_release(qp, ndev->bar_addr);
     253                 :          0 :         nitrox_sym_req_pool_free(qp->sr_mp);
     254                 :          0 :         rte_free(qp);
     255                 :          0 :         NITROX_LOG_LINE(DEBUG, "queue %d release done", qp_id);
     256                 :          0 :         return err;
     257                 :            : }
     258                 :            : 
     259                 :            : static unsigned int
     260                 :          0 : nitrox_sym_dev_sess_get_size(__rte_unused struct rte_cryptodev *cdev)
     261                 :            : {
     262                 :          0 :         return sizeof(struct nitrox_crypto_ctx);
     263                 :            : }
     264                 :            : 
     265                 :            : static enum nitrox_chain
     266                 :          0 : get_crypto_chain_order(const struct rte_crypto_sym_xform *xform)
     267                 :            : {
     268                 :            :         enum nitrox_chain res = NITROX_CHAIN_NOT_SUPPORTED;
     269                 :            : 
     270         [ #  # ]:          0 :         if (unlikely(xform == NULL))
     271                 :            :                 return res;
     272                 :            : 
     273   [ #  #  #  # ]:          0 :         switch (xform->type) {
     274                 :          0 :         case RTE_CRYPTO_SYM_XFORM_AUTH:
     275         [ #  # ]:          0 :                 if (xform->next == NULL) {
     276                 :            :                         res = NITROX_CHAIN_NOT_SUPPORTED;
     277         [ #  # ]:          0 :                 } else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     278         [ #  # ]:          0 :                         if (xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY &&
     279         [ #  # ]:          0 :                             xform->next->cipher.op ==
     280                 :            :                             RTE_CRYPTO_CIPHER_OP_DECRYPT) {
     281                 :            :                                 res = NITROX_CHAIN_AUTH_CIPHER;
     282                 :            :                         } else {
     283                 :          0 :                                 NITROX_LOG_LINE(ERR, "auth op %d, cipher op %d",
     284                 :            :                                     xform->auth.op, xform->next->cipher.op);
     285                 :            :                         }
     286                 :            :                 }
     287                 :            :                 break;
     288                 :          0 :         case RTE_CRYPTO_SYM_XFORM_CIPHER:
     289         [ #  # ]:          0 :                 if (xform->next == NULL) {
     290                 :            :                         res = NITROX_CHAIN_CIPHER_ONLY;
     291         [ #  # ]:          0 :                 } else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
     292         [ #  # ]:          0 :                         if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
     293         [ #  # ]:          0 :                             xform->next->auth.op ==
     294                 :            :                             RTE_CRYPTO_AUTH_OP_GENERATE) {
     295                 :            :                                 res = NITROX_CHAIN_CIPHER_AUTH;
     296                 :            :                         } else {
     297                 :          0 :                                 NITROX_LOG_LINE(ERR, "cipher op %d, auth op %d",
     298                 :            :                                     xform->cipher.op, xform->next->auth.op);
     299                 :            :                         }
     300                 :            :                 }
     301                 :            :                 break;
     302                 :          0 :         case RTE_CRYPTO_SYM_XFORM_AEAD:
     303                 :            :                 res = NITROX_CHAIN_COMBINED;
     304                 :          0 :                 break;
     305                 :            :         default:
     306                 :            :                 break;
     307                 :            :         }
     308                 :            : 
     309                 :            :         return res;
     310                 :            : }
     311                 :            : 
     312                 :            : static enum flexi_cipher
     313                 :          0 : get_flexi_cipher_type(enum rte_crypto_cipher_algorithm algo, bool *is_aes)
     314                 :            : {
     315                 :            :         enum flexi_cipher type;
     316                 :            : 
     317      [ #  #  # ]:          0 :         switch (algo) {
     318                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
     319                 :            :                 type = CIPHER_AES_CBC;
     320                 :          0 :                 *is_aes = true;
     321                 :          0 :                 break;
     322                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_CBC:
     323                 :            :                 type = CIPHER_3DES_CBC;
     324                 :          0 :                 *is_aes = false;
     325                 :          0 :                 break;
     326                 :          0 :         default:
     327                 :            :                 type = CIPHER_INVALID;
     328                 :          0 :                 NITROX_LOG_LINE(ERR, "Algorithm not supported %d", algo);
     329                 :          0 :                 break;
     330                 :            :         }
     331                 :            : 
     332                 :          0 :         return type;
     333                 :            : }
     334                 :            : 
     335                 :            : static int
     336                 :          0 : flexi_aes_keylen(size_t keylen, bool is_aes)
     337                 :            : {
     338                 :            :         int aes_keylen;
     339                 :            : 
     340         [ #  # ]:          0 :         if (!is_aes)
     341                 :            :                 return 0;
     342                 :            : 
     343   [ #  #  #  # ]:          0 :         switch (keylen) {
     344                 :            :         case AES_KEYSIZE_128:
     345                 :            :                 aes_keylen = 1;
     346                 :            :                 break;
     347                 :          0 :         case AES_KEYSIZE_192:
     348                 :            :                 aes_keylen = 2;
     349                 :          0 :                 break;
     350                 :          0 :         case AES_KEYSIZE_256:
     351                 :            :                 aes_keylen = 3;
     352                 :          0 :                 break;
     353                 :          0 :         default:
     354                 :          0 :                 NITROX_LOG_LINE(ERR, "Invalid keylen %zu", keylen);
     355                 :            :                 aes_keylen = -EINVAL;
     356                 :          0 :                 break;
     357                 :            :         }
     358                 :            : 
     359                 :            :         return aes_keylen;
     360                 :            : }
     361                 :            : 
     362                 :            : static bool
     363                 :          0 : crypto_key_is_valid(struct rte_crypto_cipher_xform *xform,
     364                 :            :                     struct flexi_crypto_context *fctx)
     365                 :            : {
     366         [ #  # ]:          0 :         if (unlikely(xform->key.length > sizeof(fctx->crypto.key))) {
     367                 :          0 :                 NITROX_LOG_LINE(ERR, "Invalid crypto key length %d",
     368                 :            :                            xform->key.length);
     369                 :          0 :                 return false;
     370                 :            :         }
     371                 :            : 
     372                 :            :         return true;
     373                 :            : }
     374                 :            : 
     375                 :            : static int
     376                 :          0 : configure_cipher_ctx(struct rte_crypto_cipher_xform *xform,
     377                 :            :                      struct nitrox_crypto_ctx *ctx)
     378                 :            : {
     379                 :            :         enum flexi_cipher type;
     380                 :          0 :         bool cipher_is_aes = false;
     381                 :            :         int aes_keylen;
     382                 :          0 :         struct flexi_crypto_context *fctx = &ctx->fctx;
     383                 :            : 
     384                 :          0 :         type = get_flexi_cipher_type(xform->algo, &cipher_is_aes);
     385         [ #  # ]:          0 :         if (unlikely(type == CIPHER_INVALID))
     386                 :            :                 return -ENOTSUP;
     387                 :            : 
     388                 :          0 :         aes_keylen = flexi_aes_keylen(xform->key.length, cipher_is_aes);
     389         [ #  # ]:          0 :         if (unlikely(aes_keylen < 0))
     390                 :            :                 return -EINVAL;
     391                 :            : 
     392   [ #  #  #  # ]:          0 :         if (unlikely(!cipher_is_aes && !crypto_key_is_valid(xform, fctx)))
     393                 :            :                 return -EINVAL;
     394                 :            : 
     395         [ #  # ]:          0 :         if (unlikely(xform->iv.length > MAX_IV_LEN))
     396                 :            :                 return -EINVAL;
     397                 :            : 
     398         [ #  # ]:          0 :         fctx->flags = rte_be_to_cpu_64(fctx->flags);
     399                 :          0 :         fctx->w0.cipher_type = type;
     400                 :          0 :         fctx->w0.aes_keylen = aes_keylen;
     401                 :          0 :         fctx->w0.iv_source = IV_FROM_DPTR;
     402         [ #  # ]:          0 :         fctx->flags = rte_cpu_to_be_64(fctx->flags);
     403                 :          0 :         memset(fctx->crypto.key, 0, sizeof(fctx->crypto.key));
     404                 :          0 :         memcpy(fctx->crypto.key, xform->key.data, xform->key.length);
     405                 :            : 
     406                 :          0 :         ctx->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
     407                 :          0 :         ctx->req_op = (xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
     408                 :          0 :                         NITROX_OP_ENCRYPT : NITROX_OP_DECRYPT;
     409                 :          0 :         ctx->iv.offset = xform->iv.offset;
     410                 :          0 :         ctx->iv.length = xform->iv.length;
     411                 :          0 :         return 0;
     412                 :            : }
     413                 :            : 
     414                 :            : static enum flexi_auth
     415                 :          0 : get_flexi_auth_type(enum rte_crypto_auth_algorithm algo)
     416                 :            : {
     417                 :            :         enum flexi_auth type;
     418                 :            : 
     419   [ #  #  #  # ]:          0 :         switch (algo) {
     420                 :            :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
     421                 :            :                 type = AUTH_SHA1;
     422                 :            :                 break;
     423                 :          0 :         case RTE_CRYPTO_AUTH_SHA224_HMAC:
     424                 :            :                 type = AUTH_SHA2_SHA224;
     425                 :          0 :                 break;
     426                 :          0 :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
     427                 :            :                 type = AUTH_SHA2_SHA256;
     428                 :          0 :                 break;
     429                 :          0 :         default:
     430                 :          0 :                 NITROX_LOG_LINE(ERR, "Algorithm not supported %d", algo);
     431                 :            :                 type = AUTH_INVALID;
     432                 :          0 :                 break;
     433                 :            :         }
     434                 :            : 
     435                 :          0 :         return type;
     436                 :            : }
     437                 :            : 
     438                 :            : static bool
     439                 :          0 : auth_key_is_valid(const uint8_t *data, uint16_t length,
     440                 :            :                   struct flexi_crypto_context *fctx)
     441                 :            : {
     442         [ #  # ]:          0 :         if (unlikely(!data && length)) {
     443                 :          0 :                 NITROX_LOG_LINE(ERR, "Invalid auth key");
     444                 :          0 :                 return false;
     445                 :            :         }
     446                 :            : 
     447         [ #  # ]:          0 :         if (unlikely(length > sizeof(fctx->auth.opad))) {
     448                 :          0 :                 NITROX_LOG_LINE(ERR, "Invalid auth key length %d",
     449                 :            :                            length);
     450                 :          0 :                 return false;
     451                 :            :         }
     452                 :            : 
     453                 :            :         return true;
     454                 :            : }
     455                 :            : 
     456                 :            : static int
     457                 :          0 : configure_auth_ctx(struct rte_crypto_auth_xform *xform,
     458                 :            :                    struct nitrox_crypto_ctx *ctx)
     459                 :            : {
     460                 :            :         enum flexi_auth type;
     461                 :          0 :         struct flexi_crypto_context *fctx = &ctx->fctx;
     462                 :            : 
     463                 :          0 :         type = get_flexi_auth_type(xform->algo);
     464         [ #  # ]:          0 :         if (unlikely(type == AUTH_INVALID))
     465                 :            :                 return -ENOTSUP;
     466                 :            : 
     467         [ #  # ]:          0 :         if (unlikely(!auth_key_is_valid(xform->key.data, xform->key.length,
     468                 :            :                                         fctx)))
     469                 :            :                 return -EINVAL;
     470                 :            : 
     471                 :          0 :         ctx->digest_length = xform->digest_length;
     472                 :            : 
     473         [ #  # ]:          0 :         fctx->flags = rte_be_to_cpu_64(fctx->flags);
     474                 :          0 :         fctx->w0.hash_type = type;
     475                 :          0 :         fctx->w0.auth_input_type = 1;
     476                 :          0 :         fctx->w0.mac_len = xform->digest_length;
     477         [ #  # ]:          0 :         fctx->flags = rte_cpu_to_be_64(fctx->flags);
     478                 :          0 :         memset(&fctx->auth, 0, sizeof(fctx->auth));
     479                 :          0 :         memcpy(fctx->auth.opad, xform->key.data, xform->key.length);
     480                 :          0 :         return 0;
     481                 :            : }
     482                 :            : 
     483                 :            : static int
     484                 :          0 : configure_aead_ctx(struct rte_crypto_aead_xform *xform,
     485                 :            :                    struct nitrox_crypto_ctx *ctx)
     486                 :            : {
     487                 :            :         int aes_keylen;
     488                 :          0 :         struct flexi_crypto_context *fctx = &ctx->fctx;
     489                 :            : 
     490         [ #  # ]:          0 :         if (unlikely(xform->aad_length > FLEXI_CRYPTO_MAX_AAD_LEN)) {
     491                 :          0 :                 NITROX_LOG_LINE(ERR, "AAD length %d not supported",
     492                 :            :                            xform->aad_length);
     493                 :          0 :                 return -ENOTSUP;
     494                 :            :         }
     495                 :            : 
     496         [ #  # ]:          0 :         if (unlikely(xform->algo != RTE_CRYPTO_AEAD_AES_GCM &&
     497                 :            :                      xform->algo != RTE_CRYPTO_AEAD_AES_CCM))
     498                 :            :                 return -ENOTSUP;
     499                 :            : 
     500                 :          0 :         aes_keylen = flexi_aes_keylen(xform->key.length, true);
     501         [ #  # ]:          0 :         if (unlikely(aes_keylen < 0))
     502                 :            :                 return -EINVAL;
     503                 :            : 
     504         [ #  # ]:          0 :         if (unlikely(!auth_key_is_valid(xform->key.data, xform->key.length,
     505                 :            :                                         fctx)))
     506                 :            :                 return -EINVAL;
     507                 :            : 
     508         [ #  # ]:          0 :         if (unlikely(xform->iv.length > MAX_IV_LEN))
     509                 :            :                 return -EINVAL;
     510                 :            : 
     511         [ #  # ]:          0 :         if (xform->algo == RTE_CRYPTO_AEAD_AES_CCM) {
     512                 :            :                 int L;
     513                 :            : 
     514                 :            :                 /* digest_length must be 4, 6, 8, 10, 12, 14, 16 bytes */
     515   [ #  #  #  # ]:          0 :                 if (unlikely(xform->digest_length < 4 ||
     516                 :            :                              xform->digest_length > 16 ||
     517                 :            :                              (xform->digest_length & 1) == 1)) {
     518                 :          0 :                         NITROX_LOG_LINE(ERR, "Invalid digest length %d",
     519                 :            :                                    xform->digest_length);
     520                 :          0 :                         return -EINVAL;
     521                 :            :                 }
     522                 :            : 
     523                 :          0 :                 L = 15 - xform->iv.length;
     524         [ #  # ]:          0 :                 if (unlikely(L < 2 || L > 8)) {
     525                 :          0 :                         NITROX_LOG_LINE(ERR, "Invalid iv length %d",
     526                 :            :                                    xform->iv.length);
     527                 :          0 :                         return -EINVAL;
     528                 :            :                 }
     529                 :            :         }
     530                 :            : 
     531         [ #  # ]:          0 :         fctx->flags = rte_be_to_cpu_64(fctx->flags);
     532                 :          0 :         fctx->w0.cipher_type = (xform->algo == RTE_CRYPTO_AEAD_AES_GCM) ?
     533         [ #  # ]:          0 :                                 CIPHER_AES_GCM : CIPHER_AES_CCM;
     534                 :          0 :         fctx->w0.aes_keylen = aes_keylen;
     535                 :          0 :         fctx->w0.iv_source = IV_FROM_DPTR;
     536                 :          0 :         fctx->w0.hash_type = AUTH_NULL;
     537                 :          0 :         fctx->w0.auth_input_type = 1;
     538                 :          0 :         fctx->w0.mac_len = xform->digest_length;
     539         [ #  # ]:          0 :         fctx->flags = rte_cpu_to_be_64(fctx->flags);
     540                 :          0 :         memset(fctx->crypto.key, 0, sizeof(fctx->crypto.key));
     541                 :          0 :         memcpy(fctx->crypto.key, xform->key.data, xform->key.length);
     542                 :          0 :         memset(&fctx->auth, 0, sizeof(fctx->auth));
     543                 :          0 :         memcpy(fctx->auth.opad, xform->key.data, xform->key.length);
     544                 :            : 
     545                 :          0 :         ctx->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
     546                 :          0 :         ctx->req_op = (xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
     547                 :          0 :                         NITROX_OP_ENCRYPT : NITROX_OP_DECRYPT;
     548                 :          0 :         ctx->iv.offset = xform->iv.offset;
     549                 :          0 :         ctx->iv.length = xform->iv.length;
     550                 :          0 :         ctx->digest_length = xform->digest_length;
     551                 :          0 :         ctx->aad_length = xform->aad_length;
     552                 :          0 :         ctx->aead_algo = xform->algo;
     553                 :          0 :         return 0;
     554                 :            : }
     555                 :            : 
     556                 :            : static int
     557                 :          0 : nitrox_sym_dev_sess_configure(struct rte_cryptodev *cdev __rte_unused,
     558                 :            :                               struct rte_crypto_sym_xform *xform,
     559                 :            :                               struct rte_cryptodev_sym_session *sess)
     560                 :            : {
     561                 :          0 :         struct nitrox_crypto_ctx *ctx = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
     562                 :            :         struct rte_crypto_cipher_xform *cipher_xform = NULL;
     563                 :            :         struct rte_crypto_auth_xform *auth_xform = NULL;
     564                 :            :         struct rte_crypto_aead_xform *aead_xform = NULL;
     565                 :            :         int ret = -EINVAL;
     566                 :            : 
     567                 :          0 :         ctx->nitrox_chain = get_crypto_chain_order(xform);
     568   [ #  #  #  #  :          0 :         switch (ctx->nitrox_chain) {
                      # ]
     569                 :          0 :         case NITROX_CHAIN_CIPHER_ONLY:
     570                 :          0 :                 cipher_xform = &xform->cipher;
     571                 :          0 :                 break;
     572                 :          0 :         case NITROX_CHAIN_CIPHER_AUTH:
     573                 :          0 :                 cipher_xform = &xform->cipher;
     574                 :          0 :                 auth_xform = &xform->next->auth;
     575                 :          0 :                 break;
     576                 :          0 :         case NITROX_CHAIN_AUTH_CIPHER:
     577                 :          0 :                 auth_xform = &xform->auth;
     578                 :          0 :                 cipher_xform = &xform->next->cipher;
     579                 :          0 :                 break;
     580                 :          0 :         case NITROX_CHAIN_COMBINED:
     581                 :          0 :                 aead_xform = &xform->aead;
     582                 :          0 :                 break;
     583                 :          0 :         default:
     584                 :          0 :                 NITROX_LOG_LINE(ERR, "Crypto chain not supported");
     585                 :            :                 ret = -ENOTSUP;
     586                 :          0 :                 goto err;
     587                 :            :         }
     588                 :            : 
     589   [ #  #  #  # ]:          0 :         if (cipher_xform && unlikely(configure_cipher_ctx(cipher_xform, ctx))) {
     590                 :          0 :                 NITROX_LOG_LINE(ERR, "Failed to configure cipher ctx");
     591                 :          0 :                 goto err;
     592                 :            :         }
     593                 :            : 
     594   [ #  #  #  # ]:          0 :         if (auth_xform && unlikely(configure_auth_ctx(auth_xform, ctx))) {
     595                 :          0 :                 NITROX_LOG_LINE(ERR, "Failed to configure auth ctx");
     596                 :          0 :                 goto err;
     597                 :            :         }
     598                 :            : 
     599   [ #  #  #  # ]:          0 :         if (aead_xform && unlikely(configure_aead_ctx(aead_xform, ctx))) {
     600                 :          0 :                 NITROX_LOG_LINE(ERR, "Failed to configure aead ctx");
     601                 :          0 :                 goto err;
     602                 :            :         }
     603                 :            : 
     604                 :          0 :         ctx->iova = CRYPTODEV_GET_SYM_SESS_PRIV_IOVA(sess);
     605                 :          0 :         return 0;
     606                 :            : err:
     607                 :            :         return ret;
     608                 :            : }
     609                 :            : 
     610                 :            : static void
     611                 :          0 : nitrox_sym_dev_sess_clear(struct rte_cryptodev *cdev __rte_unused,
     612                 :            :                           struct rte_cryptodev_sym_session *sess __rte_unused)
     613                 :          0 : {}
     614                 :            : 
     615                 :            : static struct nitrox_crypto_ctx *
     616                 :            : get_crypto_ctx(struct rte_crypto_op *op)
     617                 :            : {
     618                 :          0 :         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
     619         [ #  # ]:          0 :                 if (likely(op->sym->session))
     620                 :          0 :                         return CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
     621                 :            :         }
     622                 :            : 
     623                 :            :         return NULL;
     624                 :            : }
     625                 :            : 
     626                 :            : static int
     627                 :          0 : nitrox_enq_single_op(struct nitrox_qp *qp, struct rte_crypto_op *op)
     628                 :            : {
     629                 :            :         struct nitrox_crypto_ctx *ctx;
     630                 :            :         struct nitrox_softreq *sr;
     631                 :            :         int err;
     632                 :            : 
     633         [ #  # ]:          0 :         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
     634                 :            :         ctx = get_crypto_ctx(op);
     635         [ #  # ]:          0 :         if (unlikely(!ctx)) {
     636                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
     637                 :          0 :                 return -EINVAL;
     638                 :            :         }
     639                 :            : 
     640   [ #  #  #  # ]:          0 :         if (unlikely(rte_mempool_get(qp->sr_mp, (void **)&sr)))
     641                 :            :                 return -ENOMEM;
     642                 :            : 
     643                 :          0 :         err = nitrox_process_se_req(qp->qno, op, ctx, sr);
     644         [ #  # ]:          0 :         if (unlikely(err)) {
     645         [ #  # ]:          0 :                 rte_mempool_put(qp->sr_mp, sr);
     646                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
     647                 :          0 :                 return err;
     648                 :            :         }
     649                 :            : 
     650                 :          0 :         nitrox_qp_enqueue(qp, nitrox_sym_instr_addr(sr), sr);
     651                 :          0 :         return 0;
     652                 :            : }
     653                 :            : 
     654                 :            : static uint16_t
     655                 :          0 : nitrox_sym_dev_enq_burst(void *queue_pair, struct rte_crypto_op **ops,
     656                 :            :                          uint16_t nb_ops)
     657                 :            : {
     658                 :            :         struct nitrox_qp *qp = queue_pair;
     659                 :            :         uint16_t free_slots = 0;
     660                 :            :         uint16_t cnt = 0;
     661                 :            :         bool err = false;
     662                 :            : 
     663                 :            :         free_slots = nitrox_qp_free_count(qp);
     664                 :            :         if (nb_ops > free_slots)
     665                 :            :                 nb_ops = free_slots;
     666                 :            : 
     667         [ #  # ]:          0 :         for (cnt = 0; cnt < nb_ops; cnt++) {
     668         [ #  # ]:          0 :                 if (unlikely(nitrox_enq_single_op(qp, ops[cnt]))) {
     669                 :            :                         err = true;
     670                 :            :                         break;
     671                 :            :                 }
     672                 :            :         }
     673                 :            : 
     674                 :            :         nitrox_ring_dbell(qp, cnt);
     675                 :          0 :         qp->stats.enqueued_count += cnt;
     676         [ #  # ]:          0 :         if (unlikely(err))
     677                 :          0 :                 qp->stats.enqueue_err_count++;
     678                 :            : 
     679                 :          0 :         return cnt;
     680                 :            : }
     681                 :            : 
     682                 :            : static int
     683                 :          0 : nitrox_deq_single_op(struct nitrox_qp *qp, struct rte_crypto_op **op_ptr)
     684                 :            : {
     685                 :            :         struct nitrox_softreq *sr;
     686                 :            :         int ret;
     687                 :            :         struct rte_crypto_op *op;
     688                 :            : 
     689                 :            :         sr = nitrox_qp_get_softreq(qp);
     690                 :          0 :         ret = nitrox_check_se_req(sr, op_ptr);
     691         [ #  # ]:          0 :         if (ret < 0)
     692                 :            :                 return -EAGAIN;
     693                 :            : 
     694         [ #  # ]:          0 :         op = *op_ptr;
     695                 :            :         nitrox_qp_dequeue(qp);
     696         [ #  # ]:          0 :         rte_mempool_put(qp->sr_mp, sr);
     697         [ #  # ]:          0 :         if (!ret) {
     698                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
     699                 :          0 :                 qp->stats.dequeued_count++;
     700                 :            : 
     701                 :          0 :                 return 0;
     702                 :            :         }
     703                 :            : 
     704         [ #  # ]:          0 :         if (ret == MC_MAC_MISMATCH_ERR_CODE)
     705                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
     706                 :            :         else
     707                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
     708                 :            : 
     709                 :          0 :         qp->stats.dequeue_err_count++;
     710                 :          0 :         return 0;
     711                 :            : }
     712                 :            : 
     713                 :            : static uint16_t
     714                 :          0 : nitrox_sym_dev_deq_burst(void *queue_pair, struct rte_crypto_op **ops,
     715                 :            :                          uint16_t nb_ops)
     716                 :            : {
     717                 :            :         struct nitrox_qp *qp = queue_pair;
     718                 :            :         uint16_t filled_slots = nitrox_qp_used_count(qp);
     719                 :            :         int cnt = 0;
     720                 :            : 
     721                 :            :         if (nb_ops > filled_slots)
     722                 :            :                 nb_ops = filled_slots;
     723                 :            : 
     724         [ #  # ]:          0 :         for (cnt = 0; cnt < nb_ops; cnt++)
     725         [ #  # ]:          0 :                 if (nitrox_deq_single_op(qp, &ops[cnt]))
     726                 :            :                         break;
     727                 :            : 
     728                 :          0 :         return cnt;
     729                 :            : }
     730                 :            : 
     731                 :            : static struct rte_cryptodev_ops nitrox_cryptodev_ops = {
     732                 :            :         .dev_configure          = nitrox_sym_dev_config,
     733                 :            :         .dev_start              = nitrox_sym_dev_start,
     734                 :            :         .dev_stop               = nitrox_sym_dev_stop,
     735                 :            :         .dev_close              = nitrox_sym_dev_close,
     736                 :            :         .dev_infos_get          = nitrox_sym_dev_info_get,
     737                 :            :         .stats_get              = nitrox_sym_dev_stats_get,
     738                 :            :         .stats_reset            = nitrox_sym_dev_stats_reset,
     739                 :            :         .queue_pair_setup       = nitrox_sym_dev_qp_setup,
     740                 :            :         .queue_pair_release     = nitrox_sym_dev_qp_release,
     741                 :            :         .sym_session_get_size   = nitrox_sym_dev_sess_get_size,
     742                 :            :         .sym_session_configure  = nitrox_sym_dev_sess_configure,
     743                 :            :         .sym_session_clear      = nitrox_sym_dev_sess_clear
     744                 :            : };
     745                 :            : 
     746                 :            : int
     747                 :          0 : nitrox_sym_pmd_create(struct nitrox_device *ndev)
     748                 :            : {
     749                 :            :         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
     750                 :          0 :         struct rte_cryptodev_pmd_init_params init_params = {
     751                 :            :                         .name = "",
     752                 :          0 :                         .socket_id = ndev->pdev->device.numa_node,
     753                 :            :                         .private_data_size = sizeof(struct nitrox_sym_device)
     754                 :            :         };
     755                 :            :         struct rte_cryptodev *cdev;
     756                 :            : 
     757                 :          0 :         rte_pci_device_name(&ndev->pdev->addr, name, sizeof(name));
     758                 :          0 :         snprintf(name + strlen(name), RTE_CRYPTODEV_NAME_MAX_LEN - strlen(name),
     759                 :            :                  "_n5sym");
     760                 :          0 :         ndev->rte_sym_dev.driver = &nitrox_rte_sym_drv;
     761                 :          0 :         ndev->rte_sym_dev.numa_node = ndev->pdev->device.numa_node;
     762                 :          0 :         ndev->rte_sym_dev.devargs = NULL;
     763                 :          0 :         cdev = rte_cryptodev_pmd_create(name, &ndev->rte_sym_dev,
     764                 :            :                                         &init_params);
     765         [ #  # ]:          0 :         if (!cdev) {
     766                 :          0 :                 NITROX_LOG_LINE(ERR, "Cryptodev '%s' creation failed", name);
     767                 :          0 :                 return -ENODEV;
     768                 :            :         }
     769                 :            : 
     770                 :          0 :         ndev->rte_sym_dev.name = cdev->data->name;
     771                 :          0 :         cdev->driver_id = nitrox_sym_drv_id;
     772                 :          0 :         cdev->dev_ops = &nitrox_cryptodev_ops;
     773                 :          0 :         cdev->enqueue_burst = nitrox_sym_dev_enq_burst;
     774                 :          0 :         cdev->dequeue_burst = nitrox_sym_dev_deq_burst;
     775                 :          0 :         cdev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
     776                 :            :                 RTE_CRYPTODEV_FF_HW_ACCELERATED |
     777                 :            :                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
     778                 :            :                 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
     779                 :            :                 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
     780                 :            :                 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
     781                 :            :                 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
     782                 :            :                 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
     783                 :            : 
     784                 :          0 :         ndev->sym_dev = cdev->data->dev_private;
     785                 :          0 :         ndev->sym_dev->cdev = cdev;
     786                 :          0 :         ndev->sym_dev->ndev = ndev;
     787                 :            : 
     788                 :          0 :         rte_cryptodev_pmd_probing_finish(cdev);
     789                 :            : 
     790                 :          0 :         NITROX_LOG_LINE(DEBUG, "Created cryptodev '%s', dev_id %d, drv_id %d",
     791                 :            :                    cdev->data->name, cdev->data->dev_id, nitrox_sym_drv_id);
     792                 :          0 :         return 0;
     793                 :            : }
     794                 :            : 
     795                 :            : int
     796                 :          0 : nitrox_sym_pmd_destroy(struct nitrox_device *ndev)
     797                 :            : {
     798                 :          0 :         return rte_cryptodev_pmd_destroy(ndev->sym_dev->cdev);
     799                 :            : }
     800                 :            : 
     801                 :            : static struct cryptodev_driver nitrox_crypto_drv;
     802                 :        251 : RTE_PMD_REGISTER_CRYPTO_DRIVER(nitrox_crypto_drv,
     803                 :            :                 nitrox_rte_sym_drv,
     804                 :            :                 nitrox_sym_drv_id);

Generated by: LCOV version 1.14