LCOV - code coverage report
Current view: top level - lib/cryptodev - cryptodev_pmd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 53 92 57.6 %
Date: 2025-05-01 17:49:45 Functions: 6 11 54.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 50 28.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2017 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdlib.h>
       6                 :            : #include <sys/queue.h>
       7                 :            : 
       8                 :            : #include <dev_driver.h>
       9                 :            : #include <eal_export.h>
      10                 :            : #include <rte_errno.h>
      11                 :            : #include <rte_string_fns.h>
      12                 :            : #include <rte_malloc.h>
      13                 :            : 
      14                 :            : #include "cryptodev_pmd.h"
      15                 :            : 
      16                 :            : /**
      17                 :            :  * Parse name from argument
      18                 :            :  */
      19                 :            : static int
      20                 :          0 : rte_cryptodev_pmd_parse_name_arg(const char *key __rte_unused,
      21                 :            :                 const char *value, void *extra_args)
      22                 :            : {
      23                 :            :         struct rte_cryptodev_pmd_init_params *params = extra_args;
      24                 :            :         int n;
      25                 :            : 
      26         [ #  # ]:          0 :         if (value == NULL || extra_args == NULL)
      27                 :            :                 return -EINVAL;
      28                 :            : 
      29         [ #  # ]:          0 :         n = strlcpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
      30         [ #  # ]:          0 :         if (n >= RTE_CRYPTODEV_NAME_MAX_LEN)
      31                 :          0 :                 return -EINVAL;
      32                 :            : 
      33                 :            :         return 0;
      34                 :            : }
      35                 :            : 
      36                 :            : /**
      37                 :            :  * Parse unsigned integer from argument
      38                 :            :  */
      39                 :            : static int
      40                 :          0 : rte_cryptodev_pmd_parse_uint_arg(const char *key __rte_unused,
      41                 :            :                 const char *value, void *extra_args)
      42                 :            : {
      43                 :            :         int i;
      44                 :            :         char *end;
      45                 :            : 
      46         [ #  # ]:          0 :         if (value == NULL || extra_args == NULL)
      47                 :            :                 return -EINVAL;
      48                 :            : 
      49                 :          0 :         errno = 0;
      50                 :            : 
      51                 :          0 :         i = strtol(value, &end, 10);
      52   [ #  #  #  #  :          0 :         if (*end != 0 || errno != 0 || i < 0)
                   #  # ]
      53                 :            :                 return -EINVAL;
      54                 :            : 
      55                 :          0 :         *((uint32_t *)extra_args) = i;
      56                 :          0 :         return 0;
      57                 :            : }
      58                 :            : 
      59                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_cryptodev_pmd_parse_input_args)
      60                 :            : int
      61                 :          3 : rte_cryptodev_pmd_parse_input_args(
      62                 :            :                 struct rte_cryptodev_pmd_init_params *params,
      63                 :            :                 const char *args)
      64                 :            : {
      65                 :            :         struct rte_kvargs *kvlist = NULL;
      66                 :            :         int ret = 0;
      67                 :            : 
      68         [ +  - ]:          3 :         if (params == NULL)
      69                 :            :                 return -EINVAL;
      70                 :            : 
      71         [ -  + ]:          3 :         if (args) {
      72                 :          3 :                 kvlist = rte_kvargs_parse(args, cryptodev_pmd_valid_params);
      73         [ +  - ]:          3 :                 if (kvlist == NULL)
      74                 :            :                         return -EINVAL;
      75                 :            : 
      76                 :          3 :                 ret = rte_kvargs_process(kvlist,
      77                 :            :                                 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
      78                 :            :                                 &rte_cryptodev_pmd_parse_uint_arg,
      79                 :          3 :                                 &params->max_nb_queue_pairs);
      80         [ -  + ]:          3 :                 if (ret < 0)
      81                 :          0 :                         goto free_kvlist;
      82                 :            : 
      83                 :          3 :                 ret = rte_kvargs_process(kvlist,
      84                 :            :                                 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
      85                 :            :                                 &rte_cryptodev_pmd_parse_uint_arg,
      86                 :          3 :                                 &params->socket_id);
      87         [ -  + ]:          3 :                 if (ret < 0)
      88                 :          0 :                         goto free_kvlist;
      89                 :            : 
      90                 :          3 :                 ret = rte_kvargs_process(kvlist,
      91                 :            :                                 RTE_CRYPTODEV_PMD_NAME_ARG,
      92                 :            :                                 &rte_cryptodev_pmd_parse_name_arg,
      93                 :            :                                 params);
      94         [ +  - ]:          3 :                 if (ret < 0)
      95                 :          0 :                         goto free_kvlist;
      96                 :            :         }
      97                 :            : 
      98                 :          3 : free_kvlist:
      99                 :          3 :         rte_kvargs_free(kvlist);
     100                 :          3 :         return ret;
     101                 :            : }
     102                 :            : 
     103                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_cryptodev_pmd_create)
     104                 :            : struct rte_cryptodev *
     105                 :          3 : rte_cryptodev_pmd_create(const char *name,
     106                 :            :                 struct rte_device *device,
     107                 :            :                 struct rte_cryptodev_pmd_init_params *params)
     108                 :            : {
     109                 :            :         struct rte_cryptodev *cryptodev;
     110                 :            : 
     111         [ -  + ]:          3 :         if (params->name[0] != '\0') {
     112                 :          0 :                 CDEV_LOG_INFO("User specified device name = %s", params->name);
     113                 :            :                 name = params->name;
     114                 :            :         }
     115                 :            : 
     116                 :          3 :         CDEV_LOG_INFO("Creating cryptodev %s", name);
     117                 :            : 
     118                 :          3 :         CDEV_LOG_INFO("Initialisation parameters - name: %s,"
     119                 :            :                         "socket id: %d, max queue pairs: %u",
     120                 :            :                         name, params->socket_id, params->max_nb_queue_pairs);
     121                 :            : 
     122                 :            :         /* allocate device structure */
     123                 :          3 :         cryptodev = rte_cryptodev_pmd_allocate(name, params->socket_id);
     124         [ -  + ]:          3 :         if (cryptodev == NULL) {
     125                 :          0 :                 CDEV_LOG_ERR("Failed to allocate crypto device for %s", name);
     126                 :          0 :                 return NULL;
     127                 :            :         }
     128                 :            : 
     129                 :            :         /* allocate private device structure */
     130         [ +  - ]:          3 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
     131                 :          6 :                 cryptodev->data->dev_private =
     132                 :          3 :                                 rte_zmalloc_socket("cryptodev device private",
     133                 :            :                                                 params->private_data_size,
     134                 :            :                                                 RTE_CACHE_LINE_SIZE,
     135                 :            :                                                 params->socket_id);
     136                 :            : 
     137         [ -  + ]:          3 :                 if (cryptodev->data->dev_private == NULL) {
     138                 :          0 :                         CDEV_LOG_ERR("Cannot allocate memory for cryptodev %s"
     139                 :            :                                         " private data", name);
     140                 :            : 
     141                 :          0 :                         rte_cryptodev_pmd_release_device(cryptodev);
     142                 :          0 :                         return NULL;
     143                 :            :                 }
     144                 :            :         }
     145                 :            : 
     146                 :          3 :         cryptodev->device = device;
     147                 :            : 
     148                 :            :         /* initialise user call-back tail queue */
     149                 :          3 :         TAILQ_INIT(&(cryptodev->link_intr_cbs));
     150                 :            : 
     151                 :          3 :         return cryptodev;
     152                 :            : }
     153                 :            : 
     154                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_cryptodev_pmd_destroy)
     155                 :            : int
     156                 :          3 : rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
     157                 :            : {
     158                 :            :         int retval;
     159                 :          3 :         void *dev_priv = cryptodev->data->dev_private;
     160                 :            : 
     161                 :          3 :         CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device->name);
     162                 :            : 
     163                 :            :         /* free crypto device */
     164                 :          3 :         retval = rte_cryptodev_pmd_release_device(cryptodev);
     165         [ +  - ]:          3 :         if (retval)
     166                 :            :                 return retval;
     167                 :            : 
     168         [ +  - ]:          3 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
     169                 :          3 :                 rte_free(dev_priv);
     170                 :            : 
     171                 :            : 
     172                 :          3 :         cryptodev->device = NULL;
     173                 :          3 :         cryptodev->data = NULL;
     174                 :            : 
     175                 :          3 :         return 0;
     176                 :            : }
     177                 :            : 
     178                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_cryptodev_pmd_probing_finish)
     179                 :            : void
     180                 :          3 : rte_cryptodev_pmd_probing_finish(struct rte_cryptodev *cryptodev)
     181                 :            : {
     182         [ +  - ]:          3 :         if (cryptodev == NULL)
     183                 :            :                 return;
     184                 :            :         /*
     185                 :            :          * for secondary process, at that point we expect device
     186                 :            :          * to be already 'usable', so shared data and all function
     187                 :            :          * pointers for fast-path devops have to be setup properly
     188                 :            :          * inside rte_cryptodev.
     189                 :            :          */
     190         [ -  + ]:          3 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
     191                 :          0 :                 cryptodev_fp_ops_set(rte_crypto_fp_ops +
     192                 :          0 :                                 cryptodev->data->dev_id, cryptodev);
     193                 :            : }
     194                 :            : 
     195                 :            : static uint16_t
     196                 :          0 : dummy_crypto_enqueue_burst(__rte_unused void *qp,
     197                 :            :                            __rte_unused struct rte_crypto_op **ops,
     198                 :            :                            __rte_unused uint16_t nb_ops)
     199                 :            : {
     200                 :          0 :         CDEV_LOG_ERR(
     201                 :            :                 "crypto enqueue burst requested for unconfigured device");
     202                 :          0 :         rte_errno = ENOTSUP;
     203                 :          0 :         return 0;
     204                 :            : }
     205                 :            : 
     206                 :            : static uint16_t
     207                 :          0 : dummy_crypto_dequeue_burst(__rte_unused void *qp,
     208                 :            :                            __rte_unused struct rte_crypto_op **ops,
     209                 :            :                            __rte_unused uint16_t nb_ops)
     210                 :            : {
     211                 :          0 :         CDEV_LOG_ERR(
     212                 :            :                 "crypto dequeue burst requested for unconfigured device");
     213                 :          0 :         rte_errno = ENOTSUP;
     214                 :          0 :         return 0;
     215                 :            : }
     216                 :            : 
     217                 :            : RTE_EXPORT_INTERNAL_SYMBOL(cryptodev_fp_ops_reset)
     218                 :            : void
     219                 :      16586 : cryptodev_fp_ops_reset(struct rte_crypto_fp_ops *fp_ops)
     220                 :            : {
     221                 :            :         static struct rte_cryptodev_cb_rcu dummy_cb[RTE_MAX_QUEUES_PER_PORT];
     222                 :            :         static void *dummy_data[RTE_MAX_QUEUES_PER_PORT];
     223                 :            :         static const struct rte_crypto_fp_ops dummy = {
     224                 :            :                 .enqueue_burst = dummy_crypto_enqueue_burst,
     225                 :            :                 .dequeue_burst = dummy_crypto_dequeue_burst,
     226                 :            :                 .qp = {
     227                 :            :                         .data = dummy_data,
     228                 :            :                         .enq_cb = dummy_cb,
     229                 :            :                         .deq_cb = dummy_cb,
     230                 :            :                 },
     231                 :            :         };
     232                 :            : 
     233                 :      16586 :         *fp_ops = dummy;
     234                 :      16586 : }
     235                 :            : 
     236                 :            : RTE_EXPORT_INTERNAL_SYMBOL(cryptodev_fp_ops_set)
     237                 :            : void
     238                 :        455 : cryptodev_fp_ops_set(struct rte_crypto_fp_ops *fp_ops,
     239                 :            :                      const struct rte_cryptodev *dev)
     240                 :            : {
     241                 :        455 :         fp_ops->enqueue_burst = dev->enqueue_burst;
     242                 :        455 :         fp_ops->dequeue_burst = dev->dequeue_burst;
     243                 :        455 :         fp_ops->qp.data = dev->data->queue_pairs;
     244                 :        455 :         fp_ops->qp.enq_cb = dev->enq_cbs;
     245                 :        455 :         fp_ops->qp.deq_cb = dev->deq_cbs;
     246                 :        455 :         fp_ops->qp_depth_used = dev->qp_depth_used;
     247                 :        455 : }
     248                 :            : 
     249                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_cryptodev_session_event_mdata_get)
     250                 :            : void *
     251                 :          0 : rte_cryptodev_session_event_mdata_get(struct rte_crypto_op *op)
     252                 :            : {
     253         [ #  # ]:          0 :         if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
     254                 :            :                         op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
     255                 :          0 :                 return rte_cryptodev_sym_session_get_user_data(op->sym->session);
     256         [ #  # ]:          0 :         else if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC &&
     257                 :            :                         op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
     258                 :          0 :                 return op->asym->session->event_mdata;
     259         [ #  # ]:          0 :         else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
     260         [ #  # ]:          0 :                         op->private_data_offset)
     261                 :          0 :                 return ((uint8_t *)op + op->private_data_offset);
     262                 :            :         else
     263                 :            :                 return NULL;
     264                 :            : }

Generated by: LCOV version 1.14