LCOV - code coverage report
Current view: top level - drivers/crypto/virtio - virtio_cryptodev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 7 584 1.2 %
Date: 2025-02-01 18:54:23 Functions: 7 39 17.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 5 240 2.1 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 HUAWEI TECHNOLOGIES CO., LTD.
       3                 :            :  */
       4                 :            : #include <stdbool.h>
       5                 :            : #include <unistd.h>
       6                 :            : 
       7                 :            : #include <rte_common.h>
       8                 :            : #include <rte_errno.h>
       9                 :            : #include <rte_pci.h>
      10                 :            : #include <bus_pci_driver.h>
      11                 :            : #include <rte_cryptodev.h>
      12                 :            : #include <cryptodev_pmd.h>
      13                 :            : #include <rte_eal.h>
      14                 :            : 
      15                 :            : #include "virtio_cryptodev.h"
      16                 :            : #include "virtqueue.h"
      17                 :            : #include "virtio_crypto_algs.h"
      18                 :            : #include "virtio_crypto_capabilities.h"
      19                 :            : 
      20                 :            : static int virtio_crypto_dev_configure(struct rte_cryptodev *dev,
      21                 :            :                 struct rte_cryptodev_config *config);
      22                 :            : static int virtio_crypto_dev_start(struct rte_cryptodev *dev);
      23                 :            : static void virtio_crypto_dev_stop(struct rte_cryptodev *dev);
      24                 :            : static int virtio_crypto_dev_close(struct rte_cryptodev *dev);
      25                 :            : static void virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
      26                 :            :                 struct rte_cryptodev_info *dev_info);
      27                 :            : static void virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
      28                 :            :                 struct rte_cryptodev_stats *stats);
      29                 :            : static void virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev);
      30                 :            : static int virtio_crypto_qp_setup(struct rte_cryptodev *dev,
      31                 :            :                 uint16_t queue_pair_id,
      32                 :            :                 const struct rte_cryptodev_qp_conf *qp_conf,
      33                 :            :                 int socket_id);
      34                 :            : static int virtio_crypto_qp_release(struct rte_cryptodev *dev,
      35                 :            :                 uint16_t queue_pair_id);
      36                 :            : static void virtio_crypto_dev_free_mbufs(struct rte_cryptodev *dev);
      37                 :            : static unsigned int virtio_crypto_sym_get_session_private_size(
      38                 :            :                 struct rte_cryptodev *dev);
      39                 :            : static void virtio_crypto_sym_clear_session(struct rte_cryptodev *dev,
      40                 :            :                 struct rte_cryptodev_sym_session *sess);
      41                 :            : static int virtio_crypto_sym_configure_session(struct rte_cryptodev *dev,
      42                 :            :                 struct rte_crypto_sym_xform *xform,
      43                 :            :                 struct rte_cryptodev_sym_session *session);
      44                 :            : 
      45                 :            : /*
      46                 :            :  * The set of PCI devices this driver supports
      47                 :            :  */
      48                 :            : static const struct rte_pci_id pci_id_virtio_crypto_map[] = {
      49                 :            :         { RTE_PCI_DEVICE(VIRTIO_CRYPTO_PCI_VENDORID,
      50                 :            :                                 VIRTIO_CRYPTO_PCI_DEVICEID) },
      51                 :            :         { .vendor_id = 0, /* sentinel */ },
      52                 :            : };
      53                 :            : 
      54                 :            : static const struct rte_cryptodev_capabilities virtio_capabilities[] = {
      55                 :            :         VIRTIO_SYM_CAPABILITIES,
      56                 :            :         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
      57                 :            : };
      58                 :            : 
      59                 :            : uint8_t cryptodev_virtio_driver_id;
      60                 :            : 
      61                 :            : #define NUM_ENTRY_SYM_CREATE_SESSION 4
      62                 :            : 
      63                 :            : static int
      64                 :          0 : virtio_crypto_send_command(struct virtqueue *vq,
      65                 :            :                 struct virtio_crypto_op_ctrl_req *ctrl, uint8_t *cipher_key,
      66                 :            :                 uint8_t *auth_key, struct virtio_crypto_session *session)
      67                 :            : {
      68                 :            :         uint8_t idx = 0;
      69                 :            :         uint8_t needed = 1;
      70                 :            :         uint32_t head = 0;
      71                 :            :         uint32_t len_cipher_key = 0;
      72                 :            :         uint32_t len_auth_key = 0;
      73                 :            :         uint32_t len_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
      74                 :            :         uint32_t len_session_input = sizeof(struct virtio_crypto_session_input);
      75                 :            :         uint32_t len_total = 0;
      76                 :            :         uint32_t input_offset = 0;
      77                 :            :         void *virt_addr_started = NULL;
      78                 :            :         phys_addr_t phys_addr_started;
      79                 :            :         struct vring_desc *desc;
      80                 :            :         uint32_t desc_offset;
      81                 :            :         struct virtio_crypto_session_input *input;
      82                 :            :         int ret;
      83                 :            : 
      84                 :          0 :         PMD_INIT_FUNC_TRACE();
      85                 :            : 
      86         [ #  # ]:          0 :         if (session == NULL) {
      87                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("session is NULL.");
      88                 :          0 :                 return -EINVAL;
      89                 :            :         }
      90                 :            :         /* cipher only is supported, it is available if auth_key is NULL */
      91         [ #  # ]:          0 :         if (!cipher_key) {
      92                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("cipher key is NULL.");
      93                 :          0 :                 return -EINVAL;
      94                 :            :         }
      95                 :            : 
      96                 :          0 :         head = vq->vq_desc_head_idx;
      97                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_desc_head_idx = %d, vq = %p",
      98                 :            :                                         head, vq);
      99                 :            : 
     100         [ #  # ]:          0 :         if (vq->vq_free_cnt < needed) {
     101                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Not enough entry");
     102                 :          0 :                 return -ENOSPC;
     103                 :            :         }
     104                 :            : 
     105                 :            :         /* calculate the length of cipher key */
     106                 :            :         if (cipher_key) {
     107      [ #  #  # ]:          0 :                 switch (ctrl->u.sym_create_session.op_type) {
     108                 :          0 :                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
     109                 :            :                         len_cipher_key
     110                 :          0 :                                 = ctrl->u.sym_create_session.u.cipher
     111                 :            :                                                         .para.keylen;
     112                 :          0 :                         break;
     113                 :          0 :                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
     114                 :            :                         len_cipher_key
     115                 :          0 :                                 = ctrl->u.sym_create_session.u.chain
     116                 :            :                                         .para.cipher_param.keylen;
     117                 :          0 :                         break;
     118                 :          0 :                 default:
     119                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR("invalid op type");
     120                 :          0 :                         return -EINVAL;
     121                 :            :                 }
     122                 :            :         }
     123                 :            : 
     124                 :            :         /* calculate the length of auth key */
     125         [ #  # ]:          0 :         if (auth_key) {
     126                 :          0 :                 len_auth_key =
     127                 :            :                         ctrl->u.sym_create_session.u.chain.para.u.mac_param
     128                 :            :                                 .auth_key_len;
     129                 :            :         }
     130                 :            : 
     131                 :            :         /*
     132                 :            :          * malloc memory to store indirect vring_desc entries, including
     133                 :            :          * ctrl request, cipher key, auth key, session input and desc vring
     134                 :            :          */
     135                 :          0 :         desc_offset = len_ctrl_req + len_cipher_key + len_auth_key
     136                 :            :                 + len_session_input;
     137                 :          0 :         virt_addr_started = rte_malloc(NULL,
     138                 :            :                 desc_offset + NUM_ENTRY_SYM_CREATE_SESSION
     139                 :            :                         * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
     140         [ #  # ]:          0 :         if (virt_addr_started == NULL) {
     141                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap memory");
     142                 :          0 :                 return -ENOSPC;
     143                 :            :         }
     144                 :          0 :         phys_addr_started = rte_malloc_virt2iova(virt_addr_started);
     145                 :            : 
     146                 :            :         /* address to store indirect vring desc entries */
     147         [ #  # ]:          0 :         desc = (struct vring_desc *)
     148                 :            :                 ((uint8_t *)virt_addr_started + desc_offset);
     149                 :            : 
     150                 :            :         /*  ctrl req part */
     151                 :            :         memcpy(virt_addr_started, ctrl, len_ctrl_req);
     152                 :          0 :         desc[idx].addr = phys_addr_started;
     153                 :          0 :         desc[idx].len = len_ctrl_req;
     154                 :          0 :         desc[idx].flags = VRING_DESC_F_NEXT;
     155                 :          0 :         desc[idx].next = idx + 1;
     156                 :            :         idx++;
     157                 :            :         len_total += len_ctrl_req;
     158                 :            :         input_offset += len_ctrl_req;
     159                 :            : 
     160                 :            :         /* cipher key part */
     161         [ #  # ]:          0 :         if (len_cipher_key > 0) {
     162                 :          0 :                 memcpy((uint8_t *)virt_addr_started + len_total,
     163                 :            :                         cipher_key, len_cipher_key);
     164                 :            : 
     165                 :          0 :                 desc[idx].addr = phys_addr_started + len_total;
     166                 :          0 :                 desc[idx].len = len_cipher_key;
     167                 :          0 :                 desc[idx].flags = VRING_DESC_F_NEXT;
     168                 :          0 :                 desc[idx].next = idx + 1;
     169                 :            :                 idx++;
     170                 :            :                 len_total += len_cipher_key;
     171                 :            :                 input_offset += len_cipher_key;
     172                 :            :         }
     173                 :            : 
     174                 :            :         /* auth key part */
     175         [ #  # ]:          0 :         if (len_auth_key > 0) {
     176                 :          0 :                 memcpy((uint8_t *)virt_addr_started + len_total,
     177                 :            :                         auth_key, len_auth_key);
     178                 :            : 
     179                 :          0 :                 desc[idx].addr = phys_addr_started + len_total;
     180                 :          0 :                 desc[idx].len = len_auth_key;
     181                 :          0 :                 desc[idx].flags = VRING_DESC_F_NEXT;
     182                 :          0 :                 desc[idx].next = idx + 1;
     183                 :          0 :                 idx++;
     184                 :          0 :                 len_total += len_auth_key;
     185                 :            :                 input_offset += len_auth_key;
     186                 :            :         }
     187                 :            : 
     188                 :            :         /* input part */
     189                 :          0 :         input = (struct virtio_crypto_session_input *)
     190                 :            :                 ((uint8_t *)virt_addr_started + input_offset);
     191                 :          0 :         input->status = VIRTIO_CRYPTO_ERR;
     192                 :          0 :         input->session_id = ~0ULL;
     193                 :          0 :         desc[idx].addr = phys_addr_started + len_total;
     194                 :          0 :         desc[idx].len = len_session_input;
     195                 :          0 :         desc[idx].flags = VRING_DESC_F_WRITE;
     196                 :          0 :         idx++;
     197                 :            : 
     198                 :            :         /* use a single desc entry */
     199                 :          0 :         vq->vq_ring.desc[head].addr = phys_addr_started + desc_offset;
     200                 :          0 :         vq->vq_ring.desc[head].len = idx * sizeof(struct vring_desc);
     201                 :          0 :         vq->vq_ring.desc[head].flags = VRING_DESC_F_INDIRECT;
     202                 :          0 :         vq->vq_free_cnt--;
     203                 :            : 
     204         [ #  # ]:          0 :         vq->vq_desc_head_idx = vq->vq_ring.desc[head].next;
     205                 :            : 
     206                 :            :         vq_update_avail_ring(vq, head);
     207                 :            :         vq_update_avail_idx(vq);
     208                 :            : 
     209                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_queue_index = %d",
     210                 :            :                                         vq->vq_queue_index);
     211                 :            : 
     212                 :            :         virtqueue_notify(vq);
     213                 :            : 
     214                 :            :         rte_rmb();
     215         [ #  # ]:          0 :         while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
     216                 :            :                 rte_rmb();
     217                 :          0 :                 usleep(100);
     218                 :            :         }
     219                 :            : 
     220         [ #  # ]:          0 :         while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
     221                 :            :                 uint32_t idx, desc_idx, used_idx;
     222                 :            :                 struct vring_used_elem *uep;
     223                 :            : 
     224                 :          0 :                 used_idx = (uint32_t)(vq->vq_used_cons_idx
     225                 :          0 :                                 & (vq->vq_nentries - 1));
     226                 :            :                 uep = &vq->vq_ring.used->ring[used_idx];
     227                 :          0 :                 idx = (uint32_t) uep->id;
     228                 :            :                 desc_idx = idx;
     229                 :            : 
     230         [ #  # ]:          0 :                 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
     231                 :          0 :                         desc_idx = vq->vq_ring.desc[desc_idx].next;
     232                 :          0 :                         vq->vq_free_cnt++;
     233                 :            :                 }
     234                 :            : 
     235                 :          0 :                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
     236                 :          0 :                 vq->vq_desc_head_idx = idx;
     237                 :            : 
     238                 :          0 :                 vq->vq_used_cons_idx++;
     239                 :          0 :                 vq->vq_free_cnt++;
     240                 :            :         }
     241                 :            : 
     242                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d", vq->vq_free_cnt);
     243                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_desc_head_idx=%d", vq->vq_desc_head_idx);
     244                 :            : 
     245                 :            :         /* get the result */
     246         [ #  # ]:          0 :         if (input->status != VIRTIO_CRYPTO_OK) {
     247                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Something wrong on backend! "
     248                 :            :                                 "status=%u, session_id=%" PRIu64 "",
     249                 :            :                                 input->status, input->session_id);
     250                 :          0 :                 rte_free(virt_addr_started);
     251                 :            :                 ret = -1;
     252                 :            :         } else {
     253                 :          0 :                 session->session_id = input->session_id;
     254                 :            : 
     255                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_INFO("Create session successfully, "
     256                 :            :                                 "session_id=%" PRIu64 "", input->session_id);
     257                 :          0 :                 rte_free(virt_addr_started);
     258                 :            :                 ret = 0;
     259                 :            :         }
     260                 :            : 
     261                 :            :         return ret;
     262                 :            : }
     263                 :            : 
     264                 :            : void
     265                 :          0 : virtio_crypto_queue_release(struct virtqueue *vq)
     266                 :            : {
     267                 :            :         struct virtio_crypto_hw *hw;
     268                 :            : 
     269                 :          0 :         PMD_INIT_FUNC_TRACE();
     270                 :            : 
     271         [ #  # ]:          0 :         if (vq) {
     272                 :          0 :                 hw = vq->hw;
     273                 :            :                 /* Select and deactivate the queue */
     274                 :          0 :                 VTPCI_OPS(hw)->del_queue(hw, vq);
     275                 :            : 
     276                 :          0 :                 rte_memzone_free(vq->mz);
     277                 :          0 :                 rte_mempool_free(vq->mpool);
     278                 :          0 :                 rte_free(vq);
     279                 :            :         }
     280                 :          0 : }
     281                 :            : 
     282                 :            : #define MPOOL_MAX_NAME_SZ 32
     283                 :            : 
     284                 :            : int
     285                 :          0 : virtio_crypto_queue_setup(struct rte_cryptodev *dev,
     286                 :            :                 int queue_type,
     287                 :            :                 uint16_t vtpci_queue_idx,
     288                 :            :                 uint16_t nb_desc,
     289                 :            :                 int socket_id,
     290                 :            :                 struct virtqueue **pvq)
     291                 :            : {
     292                 :            :         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
     293                 :            :         char mpool_name[MPOOL_MAX_NAME_SZ];
     294                 :            :         const struct rte_memzone *mz;
     295                 :            :         unsigned int vq_size, size;
     296                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     297                 :            :         struct virtqueue *vq = NULL;
     298                 :            :         uint32_t i = 0;
     299                 :            :         uint32_t j;
     300                 :            : 
     301                 :          0 :         PMD_INIT_FUNC_TRACE();
     302                 :            : 
     303                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("setting up queue: %u", vtpci_queue_idx);
     304                 :            : 
     305                 :            :         /*
     306                 :            :          * Read the virtqueue size from the Queue Size field
     307                 :            :          * Always power of 2 and if 0 virtqueue does not exist
     308                 :            :          */
     309                 :          0 :         vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
     310         [ #  # ]:          0 :         if (vq_size == 0) {
     311                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("virtqueue does not exist");
     312                 :          0 :                 return -EINVAL;
     313                 :            :         }
     314                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq_size: %u", vq_size);
     315                 :            : 
     316                 :            :         if (!rte_is_power_of_2(vq_size)) {
     317                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("virtqueue size is not powerof 2");
     318                 :          0 :                 return -EINVAL;
     319                 :            :         }
     320                 :            : 
     321         [ #  # ]:          0 :         if (queue_type == VTCRYPTO_DATAQ) {
     322                 :          0 :                 snprintf(vq_name, sizeof(vq_name), "dev%d_dataqueue%d",
     323                 :          0 :                                 dev->data->dev_id, vtpci_queue_idx);
     324                 :          0 :                 snprintf(mpool_name, sizeof(mpool_name),
     325                 :            :                                 "dev%d_dataqueue%d_mpool",
     326                 :          0 :                                 dev->data->dev_id, vtpci_queue_idx);
     327         [ #  # ]:          0 :         } else if (queue_type == VTCRYPTO_CTRLQ) {
     328                 :          0 :                 snprintf(vq_name, sizeof(vq_name), "dev%d_controlqueue",
     329                 :          0 :                                 dev->data->dev_id);
     330                 :          0 :                 snprintf(mpool_name, sizeof(mpool_name),
     331                 :            :                                 "dev%d_controlqueue_mpool",
     332                 :          0 :                                 dev->data->dev_id);
     333                 :            :         }
     334                 :          0 :         size = RTE_ALIGN_CEIL(sizeof(*vq) +
     335                 :            :                                 vq_size * sizeof(struct vq_desc_extra),
     336                 :            :                                 RTE_CACHE_LINE_SIZE);
     337                 :          0 :         vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
     338                 :            :                                 socket_id);
     339         [ #  # ]:          0 :         if (vq == NULL) {
     340                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("Can not allocate virtqueue");
     341                 :          0 :                 return -ENOMEM;
     342                 :            :         }
     343                 :            : 
     344         [ #  # ]:          0 :         if (queue_type == VTCRYPTO_DATAQ) {
     345                 :            :                 /* pre-allocate a mempool and use it in the data plane to
     346                 :            :                  * improve performance
     347                 :            :                  */
     348                 :          0 :                 vq->mpool = rte_mempool_lookup(mpool_name);
     349         [ #  # ]:          0 :                 if (vq->mpool == NULL)
     350                 :          0 :                         vq->mpool = rte_mempool_create(mpool_name,
     351                 :            :                                         vq_size,
     352                 :            :                                         sizeof(struct virtio_crypto_op_cookie),
     353                 :            :                                         RTE_CACHE_LINE_SIZE, 0,
     354                 :            :                                         NULL, NULL, NULL, NULL, socket_id,
     355                 :            :                                         0);
     356         [ #  # ]:          0 :                 if (!vq->mpool) {
     357                 :          0 :                         VIRTIO_CRYPTO_DRV_LOG_ERR("Virtio Crypto PMD "
     358                 :            :                                         "Cannot create mempool");
     359                 :          0 :                         goto mpool_create_err;
     360                 :            :                 }
     361         [ #  # ]:          0 :                 for (i = 0; i < vq_size; i++) {
     362                 :          0 :                         vq->vq_descx[i].cookie =
     363                 :          0 :                                 rte_zmalloc("crypto PMD op cookie pointer",
     364                 :            :                                         sizeof(struct virtio_crypto_op_cookie),
     365                 :            :                                         RTE_CACHE_LINE_SIZE);
     366         [ #  # ]:          0 :                         if (vq->vq_descx[i].cookie == NULL) {
     367                 :          0 :                                 VIRTIO_CRYPTO_DRV_LOG_ERR("Failed to "
     368                 :            :                                                 "alloc mem for cookie");
     369                 :          0 :                                 goto cookie_alloc_err;
     370                 :            :                         }
     371                 :            :                 }
     372                 :            :         }
     373                 :            : 
     374                 :          0 :         vq->hw = hw;
     375                 :          0 :         vq->dev_id = dev->data->dev_id;
     376                 :          0 :         vq->vq_queue_index = vtpci_queue_idx;
     377                 :          0 :         vq->vq_nentries = vq_size;
     378                 :            : 
     379                 :            :         /*
     380                 :            :          * Using part of the vring entries is permitted, but the maximum
     381                 :            :          * is vq_size
     382                 :            :          */
     383   [ #  #  #  # ]:          0 :         if (nb_desc == 0 || nb_desc > vq_size)
     384                 :            :                 nb_desc = vq_size;
     385         [ #  # ]:          0 :         vq->vq_free_cnt = nb_desc;
     386                 :            : 
     387                 :            :         /*
     388                 :            :          * Reserve a memzone for vring elements
     389                 :            :          */
     390                 :          0 :         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
     391                 :          0 :         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
     392         [ #  # ]:          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("%s vring_size: %d, rounded_vring_size: %d",
     393                 :            :                         (queue_type == VTCRYPTO_DATAQ) ? "dataq" : "ctrlq",
     394                 :            :                         size, vq->vq_ring_size);
     395                 :            : 
     396                 :          0 :         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
     397                 :            :                         socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
     398         [ #  # ]:          0 :         if (mz == NULL) {
     399         [ #  # ]:          0 :                 if (rte_errno == EEXIST)
     400                 :          0 :                         mz = rte_memzone_lookup(vq_name);
     401         [ #  # ]:          0 :                 if (mz == NULL) {
     402                 :          0 :                         VIRTIO_CRYPTO_INIT_LOG_ERR("not enough memory");
     403                 :          0 :                         goto mz_reserve_err;
     404                 :            :                 }
     405                 :            :         }
     406                 :            : 
     407                 :            :         /*
     408                 :            :          * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
     409                 :            :          * and only accepts 32 bit page frame number.
     410                 :            :          * Check if the allocated physical memory exceeds 16TB.
     411                 :            :          */
     412                 :          0 :         if ((mz->iova + vq->vq_ring_size - 1)
     413         [ #  # ]:          0 :                                 >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
     414                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("vring address shouldn't be "
     415                 :            :                                         "above 16TB!");
     416                 :          0 :                 goto vring_addr_err;
     417                 :            :         }
     418                 :            : 
     419                 :          0 :         memset(mz->addr, 0, sizeof(mz->len));
     420                 :          0 :         vq->mz = mz;
     421                 :          0 :         vq->vq_ring_mem = mz->iova;
     422                 :          0 :         vq->vq_ring_virt_mem = mz->addr;
     423                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_ring_mem(physical): 0x%"PRIx64,
     424                 :            :                                         (uint64_t)mz->iova);
     425                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_ring_virt_mem: 0x%"PRIx64,
     426                 :            :                                         (uint64_t)(uintptr_t)mz->addr);
     427                 :            : 
     428                 :          0 :         *pvq = vq;
     429                 :            : 
     430                 :          0 :         return 0;
     431                 :            : 
     432                 :            : vring_addr_err:
     433                 :          0 :         rte_memzone_free(mz);
     434                 :          0 : mz_reserve_err:
     435                 :          0 : cookie_alloc_err:
     436                 :          0 :         rte_mempool_free(vq->mpool);
     437         [ #  # ]:          0 :         if (i != 0) {
     438         [ #  # ]:          0 :                 for (j = 0; j < i; j++)
     439                 :          0 :                         rte_free(vq->vq_descx[j].cookie);
     440                 :            :         }
     441                 :          0 : mpool_create_err:
     442                 :          0 :         rte_free(vq);
     443                 :          0 :         return -ENOMEM;
     444                 :            : }
     445                 :            : 
     446                 :            : static int
     447                 :          0 : virtio_crypto_ctrlq_setup(struct rte_cryptodev *dev, uint16_t queue_idx)
     448                 :            : {
     449                 :            :         int ret;
     450                 :            :         struct virtqueue *vq;
     451                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     452                 :            : 
     453                 :            :         /* if virtio device has started, do not touch the virtqueues */
     454         [ #  # ]:          0 :         if (dev->data->dev_started)
     455                 :            :                 return 0;
     456                 :            : 
     457                 :          0 :         PMD_INIT_FUNC_TRACE();
     458                 :            : 
     459                 :          0 :         ret = virtio_crypto_queue_setup(dev, VTCRYPTO_CTRLQ, queue_idx,
     460                 :            :                         0, SOCKET_ID_ANY, &vq);
     461         [ #  # ]:          0 :         if (ret < 0) {
     462                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("control vq initialization failed");
     463                 :          0 :                 return ret;
     464                 :            :         }
     465                 :            : 
     466                 :          0 :         hw->cvq = vq;
     467                 :            : 
     468                 :          0 :         return 0;
     469                 :            : }
     470                 :            : 
     471                 :            : static void
     472                 :          0 : virtio_crypto_free_queues(struct rte_cryptodev *dev)
     473                 :            : {
     474                 :            :         unsigned int i;
     475                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     476                 :            : 
     477                 :          0 :         PMD_INIT_FUNC_TRACE();
     478                 :            : 
     479                 :            :         /* control queue release */
     480                 :          0 :         virtio_crypto_queue_release(hw->cvq);
     481                 :          0 :         hw->cvq = NULL;
     482                 :            : 
     483                 :            :         /* data queue release */
     484         [ #  # ]:          0 :         for (i = 0; i < hw->max_dataqueues; i++) {
     485                 :          0 :                 virtio_crypto_queue_release(dev->data->queue_pairs[i]);
     486                 :          0 :                 dev->data->queue_pairs[i] = NULL;
     487                 :            :         }
     488                 :          0 : }
     489                 :            : 
     490                 :            : static int
     491                 :          0 : virtio_crypto_dev_close(struct rte_cryptodev *dev __rte_unused)
     492                 :            : {
     493                 :          0 :         return 0;
     494                 :            : }
     495                 :            : 
     496                 :            : /*
     497                 :            :  * dev_ops for virtio, bare necessities for basic operation
     498                 :            :  */
     499                 :            : static struct rte_cryptodev_ops virtio_crypto_dev_ops = {
     500                 :            :         /* Device related operations */
     501                 :            :         .dev_configure                   = virtio_crypto_dev_configure,
     502                 :            :         .dev_start                       = virtio_crypto_dev_start,
     503                 :            :         .dev_stop                        = virtio_crypto_dev_stop,
     504                 :            :         .dev_close                       = virtio_crypto_dev_close,
     505                 :            :         .dev_infos_get                   = virtio_crypto_dev_info_get,
     506                 :            : 
     507                 :            :         .stats_get                       = virtio_crypto_dev_stats_get,
     508                 :            :         .stats_reset                     = virtio_crypto_dev_stats_reset,
     509                 :            : 
     510                 :            :         .queue_pair_setup                = virtio_crypto_qp_setup,
     511                 :            :         .queue_pair_release              = virtio_crypto_qp_release,
     512                 :            : 
     513                 :            :         /* Crypto related operations */
     514                 :            :         .sym_session_get_size           = virtio_crypto_sym_get_session_private_size,
     515                 :            :         .sym_session_configure          = virtio_crypto_sym_configure_session,
     516                 :            :         .sym_session_clear              = virtio_crypto_sym_clear_session
     517                 :            : };
     518                 :            : 
     519                 :            : static void
     520                 :          0 : virtio_crypto_update_stats(struct rte_cryptodev *dev,
     521                 :            :                 struct rte_cryptodev_stats *stats)
     522                 :            : {
     523                 :            :         unsigned int i;
     524                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     525                 :            : 
     526                 :          0 :         PMD_INIT_FUNC_TRACE();
     527                 :            : 
     528         [ #  # ]:          0 :         if (stats == NULL) {
     529                 :          0 :                 VIRTIO_CRYPTO_DRV_LOG_ERR("invalid pointer");
     530                 :          0 :                 return;
     531                 :            :         }
     532                 :            : 
     533         [ #  # ]:          0 :         for (i = 0; i < hw->max_dataqueues; i++) {
     534                 :          0 :                 const struct virtqueue *data_queue
     535                 :          0 :                         = dev->data->queue_pairs[i];
     536         [ #  # ]:          0 :                 if (data_queue == NULL)
     537                 :          0 :                         continue;
     538                 :            : 
     539                 :          0 :                 stats->enqueued_count += data_queue->packets_sent_total;
     540                 :          0 :                 stats->enqueue_err_count += data_queue->packets_sent_failed;
     541                 :            : 
     542                 :          0 :                 stats->dequeued_count += data_queue->packets_received_total;
     543                 :            :                 stats->dequeue_err_count
     544                 :          0 :                         += data_queue->packets_received_failed;
     545                 :            :         }
     546                 :            : }
     547                 :            : 
     548                 :            : static void
     549                 :          0 : virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
     550                 :            :                 struct rte_cryptodev_stats *stats)
     551                 :            : {
     552                 :          0 :         PMD_INIT_FUNC_TRACE();
     553                 :            : 
     554                 :          0 :         virtio_crypto_update_stats(dev, stats);
     555                 :          0 : }
     556                 :            : 
     557                 :            : static void
     558                 :          0 : virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev)
     559                 :            : {
     560                 :            :         unsigned int i;
     561                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     562                 :            : 
     563                 :          0 :         PMD_INIT_FUNC_TRACE();
     564                 :            : 
     565         [ #  # ]:          0 :         for (i = 0; i < hw->max_dataqueues; i++) {
     566                 :          0 :                 struct virtqueue *data_queue = dev->data->queue_pairs[i];
     567         [ #  # ]:          0 :                 if (data_queue == NULL)
     568                 :          0 :                         continue;
     569                 :            : 
     570                 :          0 :                 data_queue->packets_sent_total = 0;
     571                 :          0 :                 data_queue->packets_sent_failed = 0;
     572                 :            : 
     573                 :          0 :                 data_queue->packets_received_total = 0;
     574                 :          0 :                 data_queue->packets_received_failed = 0;
     575                 :            :         }
     576                 :          0 : }
     577                 :            : 
     578                 :            : static int
     579                 :          0 : virtio_crypto_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
     580                 :            :                 const struct rte_cryptodev_qp_conf *qp_conf,
     581                 :            :                 int socket_id)
     582                 :            : {
     583                 :            :         int ret;
     584                 :            :         struct virtqueue *vq;
     585                 :            : 
     586                 :          0 :         PMD_INIT_FUNC_TRACE();
     587                 :            : 
     588                 :            :         /* if virtio dev is started, do not touch the virtqueues */
     589         [ #  # ]:          0 :         if (dev->data->dev_started)
     590                 :            :                 return 0;
     591                 :            : 
     592                 :          0 :         ret = virtio_crypto_queue_setup(dev, VTCRYPTO_DATAQ, queue_pair_id,
     593                 :          0 :                         qp_conf->nb_descriptors, socket_id, &vq);
     594         [ #  # ]:          0 :         if (ret < 0) {
     595                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR(
     596                 :            :                         "virtio crypto data queue initialization failed");
     597                 :          0 :                 return ret;
     598                 :            :         }
     599                 :            : 
     600                 :          0 :         dev->data->queue_pairs[queue_pair_id] = vq;
     601                 :            : 
     602                 :          0 :         return 0;
     603                 :            : }
     604                 :            : 
     605                 :            : static int
     606                 :          0 : virtio_crypto_qp_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
     607                 :            : {
     608                 :          0 :         struct virtqueue *vq
     609                 :          0 :                 = (struct virtqueue *)dev->data->queue_pairs[queue_pair_id];
     610                 :            : 
     611                 :          0 :         PMD_INIT_FUNC_TRACE();
     612                 :            : 
     613         [ #  # ]:          0 :         if (vq == NULL) {
     614                 :          0 :                 VIRTIO_CRYPTO_DRV_LOG_DBG("vq already freed");
     615                 :          0 :                 return 0;
     616                 :            :         }
     617                 :            : 
     618                 :          0 :         virtio_crypto_queue_release(vq);
     619                 :          0 :         dev->data->queue_pairs[queue_pair_id] = NULL;
     620                 :          0 :         return 0;
     621                 :            : }
     622                 :            : 
     623                 :            : static int
     624                 :          0 : virtio_negotiate_features(struct virtio_crypto_hw *hw, uint64_t req_features)
     625                 :            : {
     626                 :            :         uint64_t host_features;
     627                 :            : 
     628                 :          0 :         PMD_INIT_FUNC_TRACE();
     629                 :            : 
     630                 :            :         /* Prepare guest_features: feature that driver wants to support */
     631                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("guest_features before negotiate = %" PRIx64,
     632                 :            :                 req_features);
     633                 :            : 
     634                 :            :         /* Read device(host) feature bits */
     635                 :          0 :         host_features = VTPCI_OPS(hw)->get_features(hw);
     636                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("host_features before negotiate = %" PRIx64,
     637                 :            :                 host_features);
     638                 :            : 
     639                 :            :         /*
     640                 :            :          * Negotiate features: Subset of device feature bits are written back
     641                 :            :          * guest feature bits.
     642                 :            :          */
     643                 :          0 :         hw->guest_features = req_features;
     644                 :          0 :         hw->guest_features = vtpci_cryptodev_negotiate_features(hw,
     645                 :            :                                                         host_features);
     646                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("features after negotiate = %" PRIx64,
     647                 :            :                 hw->guest_features);
     648                 :            : 
     649         [ #  # ]:          0 :         if (hw->modern) {
     650         [ #  # ]:          0 :                 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
     651                 :          0 :                         VIRTIO_CRYPTO_INIT_LOG_ERR(
     652                 :            :                                 "VIRTIO_F_VERSION_1 features is not enabled.");
     653                 :          0 :                         return -1;
     654                 :            :                 }
     655                 :          0 :                 vtpci_cryptodev_set_status(hw,
     656                 :            :                         VIRTIO_CONFIG_STATUS_FEATURES_OK);
     657         [ #  # ]:          0 :                 if (!(vtpci_cryptodev_get_status(hw) &
     658                 :            :                         VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
     659                 :          0 :                         VIRTIO_CRYPTO_INIT_LOG_ERR("failed to set FEATURES_OK "
     660                 :            :                                                 "status!");
     661                 :          0 :                         return -1;
     662                 :            :                 }
     663                 :            :         }
     664                 :            : 
     665                 :          0 :         hw->req_guest_features = req_features;
     666                 :            : 
     667                 :          0 :         return 0;
     668                 :            : }
     669                 :            : 
     670                 :            : /* reset device and renegotiate features if needed */
     671                 :            : static int
     672                 :          0 : virtio_crypto_init_device(struct rte_cryptodev *cryptodev,
     673                 :            :         uint64_t req_features)
     674                 :            : {
     675                 :          0 :         struct virtio_crypto_hw *hw = cryptodev->data->dev_private;
     676                 :            :         struct virtio_crypto_config local_config;
     677                 :            :         struct virtio_crypto_config *config = &local_config;
     678                 :            : 
     679                 :          0 :         PMD_INIT_FUNC_TRACE();
     680                 :            : 
     681                 :            :         /* Reset the device although not necessary at startup */
     682                 :          0 :         vtpci_cryptodev_reset(hw);
     683                 :            : 
     684                 :            :         /* Tell the host we've noticed this device. */
     685                 :          0 :         vtpci_cryptodev_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
     686                 :            : 
     687                 :            :         /* Tell the host we've known how to drive the device. */
     688                 :          0 :         vtpci_cryptodev_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
     689         [ #  # ]:          0 :         if (virtio_negotiate_features(hw, req_features) < 0)
     690                 :            :                 return -1;
     691                 :            : 
     692                 :            :         /* Get status of the device */
     693                 :          0 :         vtpci_read_cryptodev_config(hw,
     694                 :            :                 offsetof(struct virtio_crypto_config, status),
     695                 :            :                 &config->status, sizeof(config->status));
     696         [ #  # ]:          0 :         if (config->status != VIRTIO_CRYPTO_S_HW_READY) {
     697                 :          0 :                 VIRTIO_CRYPTO_DRV_LOG_ERR("accelerator hardware is "
     698                 :            :                                 "not ready");
     699                 :          0 :                 return -1;
     700                 :            :         }
     701                 :            : 
     702                 :            :         /* Get number of data queues */
     703                 :          0 :         vtpci_read_cryptodev_config(hw,
     704                 :            :                 offsetof(struct virtio_crypto_config, max_dataqueues),
     705                 :            :                 &config->max_dataqueues,
     706                 :            :                 sizeof(config->max_dataqueues));
     707                 :          0 :         hw->max_dataqueues = config->max_dataqueues;
     708                 :            : 
     709                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("hw->max_dataqueues=%d",
     710                 :            :                 hw->max_dataqueues);
     711                 :            : 
     712                 :          0 :         return 0;
     713                 :            : }
     714                 :            : 
     715                 :            : /*
     716                 :            :  * This function is based on probe() function
     717                 :            :  * It returns 0 on success.
     718                 :            :  */
     719                 :            : static int
     720                 :          0 : crypto_virtio_create(const char *name, struct rte_pci_device *pci_dev,
     721                 :            :                 struct rte_cryptodev_pmd_init_params *init_params)
     722                 :            : {
     723                 :            :         struct rte_cryptodev *cryptodev;
     724                 :            :         struct virtio_crypto_hw *hw;
     725                 :            : 
     726                 :          0 :         PMD_INIT_FUNC_TRACE();
     727                 :            : 
     728                 :          0 :         cryptodev = rte_cryptodev_pmd_create(name, &pci_dev->device,
     729                 :            :                                         init_params);
     730         [ #  # ]:          0 :         if (cryptodev == NULL)
     731                 :            :                 return -ENODEV;
     732                 :            : 
     733                 :          0 :         cryptodev->driver_id = cryptodev_virtio_driver_id;
     734                 :          0 :         cryptodev->dev_ops = &virtio_crypto_dev_ops;
     735                 :            : 
     736                 :          0 :         cryptodev->enqueue_burst = virtio_crypto_pkt_tx_burst;
     737                 :          0 :         cryptodev->dequeue_burst = virtio_crypto_pkt_rx_burst;
     738                 :            : 
     739                 :          0 :         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
     740                 :            :                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
     741                 :            :                 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
     742                 :            : 
     743                 :          0 :         hw = cryptodev->data->dev_private;
     744                 :          0 :         hw->dev_id = cryptodev->data->dev_id;
     745                 :          0 :         hw->virtio_dev_capabilities = virtio_capabilities;
     746                 :            : 
     747                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("dev %d vendorID=0x%x deviceID=0x%x",
     748                 :            :                 cryptodev->data->dev_id, pci_dev->id.vendor_id,
     749                 :            :                 pci_dev->id.device_id);
     750                 :            : 
     751                 :            :         /* pci device init */
     752         [ #  # ]:          0 :         if (vtpci_cryptodev_init(pci_dev, hw))
     753                 :            :                 return -1;
     754                 :            : 
     755         [ #  # ]:          0 :         if (virtio_crypto_init_device(cryptodev,
     756                 :            :                         VIRTIO_CRYPTO_PMD_GUEST_FEATURES) < 0)
     757                 :            :                 return -1;
     758                 :            : 
     759                 :          0 :         rte_cryptodev_pmd_probing_finish(cryptodev);
     760                 :            : 
     761                 :          0 :         return 0;
     762                 :            : }
     763                 :            : 
     764                 :            : static int
     765                 :          0 : virtio_crypto_dev_uninit(struct rte_cryptodev *cryptodev)
     766                 :            : {
     767                 :          0 :         PMD_INIT_FUNC_TRACE();
     768                 :            : 
     769         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
     770                 :            :                 return -EPERM;
     771                 :            : 
     772         [ #  # ]:          0 :         if (cryptodev->data->dev_started) {
     773                 :          0 :                 virtio_crypto_dev_stop(cryptodev);
     774                 :            :                 virtio_crypto_dev_close(cryptodev);
     775                 :            :         }
     776                 :            : 
     777                 :          0 :         cryptodev->dev_ops = NULL;
     778                 :          0 :         cryptodev->enqueue_burst = NULL;
     779                 :          0 :         cryptodev->dequeue_burst = NULL;
     780                 :            : 
     781                 :          0 :         rte_free(cryptodev->data);
     782                 :          0 :         cryptodev->data = NULL;
     783                 :            : 
     784                 :          0 :         VIRTIO_CRYPTO_DRV_LOG_INFO("dev_uninit completed");
     785                 :            : 
     786                 :          0 :         return 0;
     787                 :            : }
     788                 :            : 
     789                 :            : static int
     790                 :          0 : virtio_crypto_dev_configure(struct rte_cryptodev *cryptodev,
     791                 :            :         struct rte_cryptodev_config *config __rte_unused)
     792                 :            : {
     793                 :          0 :         struct virtio_crypto_hw *hw = cryptodev->data->dev_private;
     794                 :            : 
     795                 :          0 :         PMD_INIT_FUNC_TRACE();
     796                 :            : 
     797         [ #  # ]:          0 :         if (virtio_crypto_init_device(cryptodev,
     798                 :            :                         VIRTIO_CRYPTO_PMD_GUEST_FEATURES) < 0)
     799                 :            :                 return -1;
     800                 :            : 
     801                 :            :         /* setup control queue
     802                 :            :          * [0, 1, ... ,(config->max_dataqueues - 1)] are data queues
     803                 :            :          * config->max_dataqueues is the control queue
     804                 :            :          */
     805         [ #  # ]:          0 :         if (virtio_crypto_ctrlq_setup(cryptodev, hw->max_dataqueues) < 0) {
     806                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_ERR("control queue setup error");
     807                 :          0 :                 return -1;
     808                 :            :         }
     809                 :          0 :         virtio_crypto_ctrlq_start(cryptodev);
     810                 :            : 
     811                 :          0 :         return 0;
     812                 :            : }
     813                 :            : 
     814                 :            : static void
     815                 :          0 : virtio_crypto_dev_stop(struct rte_cryptodev *dev)
     816                 :            : {
     817                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     818                 :            : 
     819                 :          0 :         PMD_INIT_FUNC_TRACE();
     820                 :          0 :         VIRTIO_CRYPTO_DRV_LOG_DBG("virtio_dev_stop");
     821                 :            : 
     822                 :          0 :         vtpci_cryptodev_reset(hw);
     823                 :            : 
     824                 :          0 :         virtio_crypto_dev_free_mbufs(dev);
     825                 :          0 :         virtio_crypto_free_queues(dev);
     826                 :            : 
     827                 :          0 :         dev->data->dev_started = 0;
     828                 :          0 : }
     829                 :            : 
     830                 :            : static int
     831                 :          0 : virtio_crypto_dev_start(struct rte_cryptodev *dev)
     832                 :            : {
     833                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
     834                 :            : 
     835         [ #  # ]:          0 :         if (dev->data->dev_started)
     836                 :            :                 return 0;
     837                 :            : 
     838                 :            :         /* Do final configuration before queue engine starts */
     839                 :          0 :         virtio_crypto_dataq_start(dev);
     840                 :          0 :         vtpci_cryptodev_reinit_complete(hw);
     841                 :            : 
     842                 :          0 :         dev->data->dev_started = 1;
     843                 :            : 
     844                 :          0 :         return 0;
     845                 :            : }
     846                 :            : 
     847                 :            : static void
     848                 :          0 : virtio_crypto_dev_free_mbufs(struct rte_cryptodev *dev)
     849                 :            : {
     850                 :            :         uint32_t i;
     851                 :            : 
     852         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
     853                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_DBG("Before freeing dataq[%d] used "
     854                 :            :                         "and unused buf", i);
     855                 :          0 :                 VIRTQUEUE_DUMP((struct virtqueue *)
     856                 :            :                         dev->data->queue_pairs[i]);
     857                 :            : 
     858                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_DBG("queue_pairs[%d]=%p",
     859                 :            :                                 i, dev->data->queue_pairs[i]);
     860                 :            : 
     861                 :          0 :                 virtqueue_detatch_unused(dev->data->queue_pairs[i]);
     862                 :            : 
     863                 :          0 :                 VIRTIO_CRYPTO_INIT_LOG_DBG("After freeing dataq[%d] used and "
     864                 :            :                                         "unused buf", i);
     865                 :          0 :                 VIRTQUEUE_DUMP(
     866                 :            :                         (struct virtqueue *)dev->data->queue_pairs[i]);
     867                 :            :         }
     868                 :          0 : }
     869                 :            : 
     870                 :            : static unsigned int
     871                 :          0 : virtio_crypto_sym_get_session_private_size(
     872                 :            :                 struct rte_cryptodev *dev __rte_unused)
     873                 :            : {
     874                 :          0 :         PMD_INIT_FUNC_TRACE();
     875                 :            : 
     876                 :          0 :         return RTE_ALIGN_CEIL(sizeof(struct virtio_crypto_session), 16);
     877                 :            : }
     878                 :            : 
     879                 :            : static int
     880                 :          0 : virtio_crypto_check_sym_session_paras(
     881                 :            :                 struct rte_cryptodev *dev)
     882                 :            : {
     883                 :            :         struct virtio_crypto_hw *hw;
     884                 :            : 
     885                 :          0 :         PMD_INIT_FUNC_TRACE();
     886                 :            : 
     887         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
     888                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("dev is NULL");
     889                 :          0 :                 return -1;
     890                 :            :         }
     891         [ #  # ]:          0 :         if (unlikely(dev->data == NULL)) {
     892                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("dev->data is NULL");
     893                 :          0 :                 return -1;
     894                 :            :         }
     895                 :          0 :         hw = dev->data->dev_private;
     896         [ #  # ]:          0 :         if (unlikely(hw == NULL)) {
     897                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("hw is NULL");
     898                 :          0 :                 return -1;
     899                 :            :         }
     900         [ #  # ]:          0 :         if (unlikely(hw->cvq == NULL)) {
     901                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("vq is NULL");
     902                 :          0 :                 return -1;
     903                 :            :         }
     904                 :            : 
     905                 :            :         return 0;
     906                 :            : }
     907                 :            : 
     908                 :            : static int
     909                 :          0 : virtio_crypto_check_sym_clear_session_paras(
     910                 :            :                 struct rte_cryptodev *dev,
     911                 :            :                 struct rte_cryptodev_sym_session *sess)
     912                 :            : {
     913                 :          0 :         PMD_INIT_FUNC_TRACE();
     914                 :            : 
     915         [ #  # ]:          0 :         if (sess == NULL) {
     916                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("sym_session is NULL");
     917                 :          0 :                 return -1;
     918                 :            :         }
     919                 :            : 
     920                 :          0 :         return virtio_crypto_check_sym_session_paras(dev);
     921                 :            : }
     922                 :            : 
     923                 :            : #define NUM_ENTRY_SYM_CLEAR_SESSION 2
     924                 :            : 
     925                 :            : static void
     926                 :          0 : virtio_crypto_sym_clear_session(
     927                 :            :                 struct rte_cryptodev *dev,
     928                 :            :                 struct rte_cryptodev_sym_session *sess)
     929                 :            : {
     930                 :            :         struct virtio_crypto_hw *hw;
     931                 :            :         struct virtqueue *vq;
     932                 :            :         struct virtio_crypto_session *session;
     933                 :            :         struct virtio_crypto_op_ctrl_req *ctrl;
     934                 :            :         struct vring_desc *desc;
     935                 :            :         uint8_t *status;
     936                 :            :         uint8_t needed = 1;
     937                 :            :         uint32_t head;
     938                 :            :         uint8_t *malloc_virt_addr;
     939                 :            :         uint64_t malloc_phys_addr;
     940                 :            :         uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
     941                 :            :         uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
     942                 :            :         uint32_t desc_offset = len_op_ctrl_req + len_inhdr;
     943                 :            : 
     944                 :          0 :         PMD_INIT_FUNC_TRACE();
     945                 :            : 
     946         [ #  # ]:          0 :         if (virtio_crypto_check_sym_clear_session_paras(dev, sess) < 0)
     947                 :            :                 return;
     948                 :            : 
     949                 :          0 :         hw = dev->data->dev_private;
     950                 :          0 :         vq = hw->cvq;
     951                 :            :         session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
     952                 :            : 
     953                 :          0 :         VIRTIO_CRYPTO_SESSION_LOG_INFO("vq->vq_desc_head_idx = %d, "
     954                 :            :                         "vq = %p", vq->vq_desc_head_idx, vq);
     955                 :            : 
     956         [ #  # ]:          0 :         if (vq->vq_free_cnt < needed) {
     957                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR(
     958                 :            :                                 "vq->vq_free_cnt = %d is less than %d, "
     959                 :            :                                 "not enough", vq->vq_free_cnt, needed);
     960                 :          0 :                 return;
     961                 :            :         }
     962                 :            : 
     963                 :            :         /*
     964                 :            :          * malloc memory to store information of ctrl request op,
     965                 :            :          * returned status and desc vring
     966                 :            :          */
     967                 :          0 :         malloc_virt_addr = rte_malloc(NULL, len_op_ctrl_req + len_inhdr
     968                 :            :                 + NUM_ENTRY_SYM_CLEAR_SESSION
     969                 :            :                 * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
     970         [ #  # ]:          0 :         if (malloc_virt_addr == NULL) {
     971                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap room");
     972                 :          0 :                 return;
     973                 :            :         }
     974                 :          0 :         malloc_phys_addr = rte_malloc_virt2iova(malloc_virt_addr);
     975                 :            : 
     976                 :            :         /* assign ctrl request op part */
     977                 :            :         ctrl = (struct virtio_crypto_op_ctrl_req *)malloc_virt_addr;
     978                 :          0 :         ctrl->header.opcode = VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION;
     979                 :            :         /* default data virtqueue is 0 */
     980                 :          0 :         ctrl->header.queue_id = 0;
     981                 :          0 :         ctrl->u.destroy_session.session_id = session->session_id;
     982                 :            : 
     983                 :            :         /* status part */
     984                 :            :         status = &(((struct virtio_crypto_inhdr *)
     985                 :            :                 ((uint8_t *)malloc_virt_addr + len_op_ctrl_req))->status);
     986                 :          0 :         *status = VIRTIO_CRYPTO_ERR;
     987                 :            : 
     988                 :            :         /* indirect desc vring part */
     989                 :            :         desc = (struct vring_desc *)((uint8_t *)malloc_virt_addr
     990                 :            :                 + desc_offset);
     991                 :            : 
     992                 :            :         /* ctrl request part */
     993                 :          0 :         desc[0].addr = malloc_phys_addr;
     994                 :          0 :         desc[0].len = len_op_ctrl_req;
     995                 :          0 :         desc[0].flags = VRING_DESC_F_NEXT;
     996                 :          0 :         desc[0].next = 1;
     997                 :            : 
     998                 :            :         /* status part */
     999                 :          0 :         desc[1].addr = malloc_phys_addr + len_op_ctrl_req;
    1000                 :          0 :         desc[1].len = len_inhdr;
    1001                 :          0 :         desc[1].flags = VRING_DESC_F_WRITE;
    1002                 :            : 
    1003                 :            :         /* use only a single desc entry */
    1004                 :          0 :         head = vq->vq_desc_head_idx;
    1005                 :          0 :         vq->vq_ring.desc[head].flags = VRING_DESC_F_INDIRECT;
    1006                 :          0 :         vq->vq_ring.desc[head].addr = malloc_phys_addr + desc_offset;
    1007                 :            :         vq->vq_ring.desc[head].len
    1008                 :          0 :                 = NUM_ENTRY_SYM_CLEAR_SESSION
    1009                 :            :                 * sizeof(struct vring_desc);
    1010                 :          0 :         vq->vq_free_cnt -= needed;
    1011                 :            : 
    1012         [ #  # ]:          0 :         vq->vq_desc_head_idx = vq->vq_ring.desc[head].next;
    1013                 :            : 
    1014                 :            :         vq_update_avail_ring(vq, head);
    1015                 :            :         vq_update_avail_idx(vq);
    1016                 :            : 
    1017                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_queue_index = %d",
    1018                 :            :                                         vq->vq_queue_index);
    1019                 :            : 
    1020                 :            :         virtqueue_notify(vq);
    1021                 :            : 
    1022                 :            :         rte_rmb();
    1023         [ #  # ]:          0 :         while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
    1024                 :            :                 rte_rmb();
    1025                 :          0 :                 usleep(100);
    1026                 :            :         }
    1027                 :            : 
    1028         [ #  # ]:          0 :         while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
    1029                 :            :                 uint32_t idx, desc_idx, used_idx;
    1030                 :            :                 struct vring_used_elem *uep;
    1031                 :            : 
    1032                 :          0 :                 used_idx = (uint32_t)(vq->vq_used_cons_idx
    1033                 :          0 :                                 & (vq->vq_nentries - 1));
    1034                 :            :                 uep = &vq->vq_ring.used->ring[used_idx];
    1035                 :          0 :                 idx = (uint32_t) uep->id;
    1036                 :            :                 desc_idx = idx;
    1037         [ #  # ]:          0 :                 while (vq->vq_ring.desc[desc_idx].flags
    1038                 :            :                                 & VRING_DESC_F_NEXT) {
    1039                 :          0 :                         desc_idx = vq->vq_ring.desc[desc_idx].next;
    1040                 :          0 :                         vq->vq_free_cnt++;
    1041                 :            :                 }
    1042                 :            : 
    1043                 :          0 :                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
    1044                 :          0 :                 vq->vq_desc_head_idx = idx;
    1045                 :          0 :                 vq->vq_used_cons_idx++;
    1046                 :          0 :                 vq->vq_free_cnt++;
    1047                 :            :         }
    1048                 :            : 
    1049         [ #  # ]:          0 :         if (*status != VIRTIO_CRYPTO_OK) {
    1050                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Close session failed "
    1051                 :            :                                 "status=%"PRIu32", session_id=%"PRIu64"",
    1052                 :            :                                 *status, session->session_id);
    1053                 :          0 :                 rte_free(malloc_virt_addr);
    1054                 :          0 :                 return;
    1055                 :            :         }
    1056                 :            : 
    1057                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d", vq->vq_free_cnt);
    1058                 :          0 :         VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_desc_head_idx=%d", vq->vq_desc_head_idx);
    1059                 :            : 
    1060                 :          0 :         VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
    1061                 :            :                         session->session_id);
    1062                 :            : 
    1063                 :          0 :         rte_free(malloc_virt_addr);
    1064                 :            : }
    1065                 :            : 
    1066                 :            : static struct rte_crypto_cipher_xform *
    1067                 :            : virtio_crypto_get_cipher_xform(struct rte_crypto_sym_xform *xform)
    1068                 :            : {
    1069                 :            :         do {
    1070         [ #  # ]:          0 :                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
    1071                 :          0 :                         return &xform->cipher;
    1072                 :            : 
    1073                 :          0 :                 xform = xform->next;
    1074         [ #  # ]:          0 :         } while (xform);
    1075                 :            : 
    1076                 :            :         return NULL;
    1077                 :            : }
    1078                 :            : 
    1079                 :            : static struct rte_crypto_auth_xform *
    1080                 :            : virtio_crypto_get_auth_xform(struct rte_crypto_sym_xform *xform)
    1081                 :            : {
    1082                 :            :         do {
    1083         [ #  # ]:          0 :                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
    1084                 :          0 :                         return &xform->auth;
    1085                 :            : 
    1086                 :          0 :                 xform = xform->next;
    1087         [ #  # ]:          0 :         } while (xform);
    1088                 :            : 
    1089                 :            :         return NULL;
    1090                 :            : }
    1091                 :            : 
    1092                 :            : /** Get xform chain order */
    1093                 :            : static int
    1094                 :          0 : virtio_crypto_get_chain_order(struct rte_crypto_sym_xform *xform)
    1095                 :            : {
    1096         [ #  # ]:          0 :         if (xform == NULL)
    1097                 :            :                 return -1;
    1098                 :            : 
    1099                 :            :         /* Cipher Only */
    1100         [ #  # ]:          0 :         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
    1101         [ #  # ]:          0 :                         xform->next == NULL)
    1102                 :            :                 return VIRTIO_CRYPTO_CMD_CIPHER;
    1103                 :            : 
    1104                 :            :         /* Authentication Only */
    1105         [ #  # ]:          0 :         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
    1106         [ #  # ]:          0 :                         xform->next == NULL)
    1107                 :            :                 return VIRTIO_CRYPTO_CMD_AUTH;
    1108                 :            : 
    1109                 :            :         /* Authenticate then Cipher */
    1110         [ #  # ]:          0 :         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
    1111         [ #  # ]:          0 :                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
    1112                 :            :                 return VIRTIO_CRYPTO_CMD_HASH_CIPHER;
    1113                 :            : 
    1114                 :            :         /* Cipher then Authenticate */
    1115         [ #  # ]:          0 :         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
    1116         [ #  # ]:          0 :                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
    1117                 :          0 :                 return VIRTIO_CRYPTO_CMD_CIPHER_HASH;
    1118                 :            : 
    1119                 :            :         return -1;
    1120                 :            : }
    1121                 :            : 
    1122                 :            : static int
    1123                 :          0 : virtio_crypto_sym_pad_cipher_param(
    1124                 :            :                 struct virtio_crypto_cipher_session_para *para,
    1125                 :            :                 struct rte_crypto_cipher_xform *cipher_xform)
    1126                 :            : {
    1127         [ #  # ]:          0 :         switch (cipher_xform->algo) {
    1128                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
    1129                 :          0 :                 para->algo = VIRTIO_CRYPTO_CIPHER_AES_CBC;
    1130                 :            :                 break;
    1131                 :          0 :         default:
    1132                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Crypto: Unsupported "
    1133                 :            :                                 "Cipher alg %u", cipher_xform->algo);
    1134                 :          0 :                 return -1;
    1135                 :            :         }
    1136                 :            : 
    1137                 :          0 :         para->keylen = cipher_xform->key.length;
    1138      [ #  #  # ]:          0 :         switch (cipher_xform->op) {
    1139                 :          0 :         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
    1140                 :          0 :                 para->op = VIRTIO_CRYPTO_OP_ENCRYPT;
    1141                 :          0 :                 break;
    1142                 :          0 :         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
    1143                 :          0 :                 para->op = VIRTIO_CRYPTO_OP_DECRYPT;
    1144                 :          0 :                 break;
    1145                 :          0 :         default:
    1146                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported cipher operation "
    1147                 :            :                                         "parameter");
    1148                 :          0 :                 return -1;
    1149                 :            :         }
    1150                 :            : 
    1151                 :            :         return 0;
    1152                 :            : }
    1153                 :            : 
    1154                 :            : static int
    1155                 :          0 : virtio_crypto_sym_pad_auth_param(
    1156                 :            :                 struct virtio_crypto_op_ctrl_req *ctrl,
    1157                 :            :                 struct rte_crypto_auth_xform *auth_xform)
    1158                 :            : {
    1159                 :            :         uint32_t *algo;
    1160                 :            :         struct virtio_crypto_alg_chain_session_para *para =
    1161                 :            :                 &(ctrl->u.sym_create_session.u.chain.para);
    1162                 :            : 
    1163      [ #  #  # ]:          0 :         switch (ctrl->u.sym_create_session.u.chain.para.hash_mode) {
    1164                 :          0 :         case VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN:
    1165                 :          0 :                 algo = &(para->u.hash_param.algo);
    1166                 :          0 :                 break;
    1167                 :          0 :         case VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH:
    1168                 :          0 :                 algo = &(para->u.mac_param.algo);
    1169                 :          0 :                 break;
    1170                 :          0 :         default:
    1171                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported hash mode %u "
    1172                 :            :                         "specified",
    1173                 :            :                         ctrl->u.sym_create_session.u.chain.para.hash_mode);
    1174                 :          0 :                 return -1;
    1175                 :            :         }
    1176                 :            : 
    1177         [ #  # ]:          0 :         switch (auth_xform->algo) {
    1178                 :          0 :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
    1179                 :          0 :                 *algo = VIRTIO_CRYPTO_MAC_HMAC_SHA1;
    1180                 :            :                 break;
    1181                 :          0 :         default:
    1182                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1183                 :            :                         "Crypto: Undefined Hash algo %u specified",
    1184                 :            :                         auth_xform->algo);
    1185                 :          0 :                 return -1;
    1186                 :            :         }
    1187                 :            : 
    1188                 :          0 :         return 0;
    1189                 :            : }
    1190                 :            : 
    1191                 :            : static int
    1192                 :          0 : virtio_crypto_sym_pad_op_ctrl_req(
    1193                 :            :                 struct virtio_crypto_op_ctrl_req *ctrl,
    1194                 :            :                 struct rte_crypto_sym_xform *xform, bool is_chainned,
    1195                 :            :                 uint8_t *cipher_key_data, uint8_t *auth_key_data,
    1196                 :            :                 struct virtio_crypto_session *session)
    1197                 :            : {
    1198                 :            :         int ret;
    1199                 :            :         struct rte_crypto_auth_xform *auth_xform = NULL;
    1200                 :            :         struct rte_crypto_cipher_xform *cipher_xform = NULL;
    1201                 :            : 
    1202                 :            :         /* Get cipher xform from crypto xform chain */
    1203                 :            :         cipher_xform = virtio_crypto_get_cipher_xform(xform);
    1204         [ #  # ]:          0 :         if (cipher_xform) {
    1205         [ #  # ]:          0 :                 if (cipher_xform->key.length > VIRTIO_CRYPTO_MAX_KEY_SIZE) {
    1206                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1207                 :            :                                 "cipher key size cannot be longer than %u",
    1208                 :            :                                 VIRTIO_CRYPTO_MAX_KEY_SIZE);
    1209                 :          0 :                         return -1;
    1210                 :            :                 }
    1211         [ #  # ]:          0 :                 if (cipher_xform->iv.length > VIRTIO_CRYPTO_MAX_IV_SIZE) {
    1212                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1213                 :            :                                 "cipher IV size cannot be longer than %u",
    1214                 :            :                                 VIRTIO_CRYPTO_MAX_IV_SIZE);
    1215                 :          0 :                         return -1;
    1216                 :            :                 }
    1217         [ #  # ]:          0 :                 if (is_chainned)
    1218                 :          0 :                         ret = virtio_crypto_sym_pad_cipher_param(
    1219                 :            :                                 &ctrl->u.sym_create_session.u.chain.para
    1220                 :            :                                                 .cipher_param, cipher_xform);
    1221                 :            :                 else
    1222                 :          0 :                         ret = virtio_crypto_sym_pad_cipher_param(
    1223                 :            :                                 &ctrl->u.sym_create_session.u.cipher.para,
    1224                 :            :                                 cipher_xform);
    1225                 :            : 
    1226         [ #  # ]:          0 :                 if (ret < 0) {
    1227                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1228                 :            :                                 "pad cipher parameter failed");
    1229                 :          0 :                         return -1;
    1230                 :            :                 }
    1231                 :            : 
    1232                 :          0 :                 memcpy(cipher_key_data, cipher_xform->key.data,
    1233                 :          0 :                                 cipher_xform->key.length);
    1234                 :            : 
    1235                 :          0 :                 session->iv.offset = cipher_xform->iv.offset;
    1236                 :          0 :                 session->iv.length = cipher_xform->iv.length;
    1237                 :            :         }
    1238                 :            : 
    1239                 :            :         /* Get auth xform from crypto xform chain */
    1240                 :            :         auth_xform = virtio_crypto_get_auth_xform(xform);
    1241         [ #  # ]:          0 :         if (auth_xform) {
    1242                 :            :                 /* FIXME: support VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
    1243                 :            :                 struct virtio_crypto_alg_chain_session_para *para =
    1244                 :            :                         &(ctrl->u.sym_create_session.u.chain.para);
    1245         [ #  # ]:          0 :                 if (auth_xform->key.length) {
    1246         [ #  # ]:          0 :                         if (auth_xform->key.length >
    1247                 :            :                                         VIRTIO_CRYPTO_MAX_KEY_SIZE) {
    1248                 :          0 :                                 VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1249                 :            :                                 "auth key size cannot be longer than %u",
    1250                 :            :                                         VIRTIO_CRYPTO_MAX_KEY_SIZE);
    1251                 :          0 :                                 return -1;
    1252                 :            :                         }
    1253                 :          0 :                         para->hash_mode = VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH;
    1254                 :          0 :                         para->u.mac_param.auth_key_len =
    1255                 :          0 :                                 (uint32_t)auth_xform->key.length;
    1256                 :          0 :                         para->u.mac_param.hash_result_len =
    1257                 :          0 :                                 auth_xform->digest_length;
    1258                 :          0 :                         memcpy(auth_key_data, auth_xform->key.data,
    1259                 :            :                                         auth_xform->key.length);
    1260                 :            :                 } else {
    1261                 :          0 :                         para->hash_mode      = VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN;
    1262                 :          0 :                         para->u.hash_param.hash_result_len =
    1263                 :          0 :                                 auth_xform->digest_length;
    1264                 :            :                 }
    1265                 :            : 
    1266                 :          0 :                 ret = virtio_crypto_sym_pad_auth_param(ctrl, auth_xform);
    1267         [ #  # ]:          0 :                 if (ret < 0) {
    1268                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR("pad auth parameter "
    1269                 :            :                                                 "failed");
    1270                 :          0 :                         return -1;
    1271                 :            :                 }
    1272                 :            :         }
    1273                 :            : 
    1274                 :            :         return 0;
    1275                 :            : }
    1276                 :            : 
    1277                 :            : static int
    1278                 :          0 : virtio_crypto_check_sym_configure_session_paras(
    1279                 :            :                 struct rte_cryptodev *dev,
    1280                 :            :                 struct rte_crypto_sym_xform *xform,
    1281                 :            :                 struct rte_cryptodev_sym_session *sym_sess)
    1282                 :            : {
    1283   [ #  #  #  # ]:          0 :         if (unlikely(xform == NULL) || unlikely(sym_sess == NULL)) {
    1284                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("NULL pointer");
    1285                 :          0 :                 return -1;
    1286                 :            :         }
    1287                 :            : 
    1288         [ #  # ]:          0 :         if (virtio_crypto_check_sym_session_paras(dev) < 0)
    1289                 :          0 :                 return -1;
    1290                 :            : 
    1291                 :            :         return 0;
    1292                 :            : }
    1293                 :            : 
    1294                 :            : static int
    1295                 :          0 : virtio_crypto_sym_configure_session(
    1296                 :            :                 struct rte_cryptodev *dev,
    1297                 :            :                 struct rte_crypto_sym_xform *xform,
    1298                 :            :                 struct rte_cryptodev_sym_session *sess)
    1299                 :            : {
    1300                 :            :         int ret;
    1301                 :            :         struct virtio_crypto_session *session;
    1302                 :            :         struct virtio_crypto_op_ctrl_req *ctrl_req;
    1303                 :            :         enum virtio_crypto_cmd_id cmd_id;
    1304                 :          0 :         uint8_t cipher_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
    1305                 :          0 :         uint8_t auth_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
    1306                 :            :         struct virtio_crypto_hw *hw;
    1307                 :            :         struct virtqueue *control_vq;
    1308                 :            : 
    1309                 :          0 :         PMD_INIT_FUNC_TRACE();
    1310                 :            : 
    1311                 :          0 :         ret = virtio_crypto_check_sym_configure_session_paras(dev, xform,
    1312                 :            :                         sess);
    1313         [ #  # ]:          0 :         if (ret < 0) {
    1314                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid parameters");
    1315                 :          0 :                 return ret;
    1316                 :            :         }
    1317         [ #  # ]:          0 :         session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
    1318                 :            :         memset(session, 0, sizeof(struct virtio_crypto_session));
    1319                 :          0 :         ctrl_req = &session->ctrl;
    1320                 :          0 :         ctrl_req->header.opcode = VIRTIO_CRYPTO_CIPHER_CREATE_SESSION;
    1321                 :            :         /* FIXME: support multiqueue */
    1322                 :          0 :         ctrl_req->header.queue_id = 0;
    1323                 :            : 
    1324                 :          0 :         hw = dev->data->dev_private;
    1325                 :          0 :         control_vq = hw->cvq;
    1326                 :            : 
    1327                 :          0 :         cmd_id = virtio_crypto_get_chain_order(xform);
    1328         [ #  # ]:          0 :         if (cmd_id == VIRTIO_CRYPTO_CMD_CIPHER_HASH)
    1329                 :            :                 ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
    1330                 :          0 :                         = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
    1331         [ #  # ]:          0 :         if (cmd_id == VIRTIO_CRYPTO_CMD_HASH_CIPHER)
    1332                 :            :                 ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
    1333                 :          0 :                         = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
    1334                 :            : 
    1335      [ #  #  # ]:          0 :         switch (cmd_id) {
    1336                 :          0 :         case VIRTIO_CRYPTO_CMD_CIPHER_HASH:
    1337                 :            :         case VIRTIO_CRYPTO_CMD_HASH_CIPHER:
    1338                 :            :                 ctrl_req->u.sym_create_session.op_type
    1339                 :          0 :                         = VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING;
    1340                 :            : 
    1341                 :          0 :                 ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req,
    1342                 :            :                         xform, true, cipher_key_data, auth_key_data, session);
    1343         [ #  # ]:          0 :                 if (ret < 0) {
    1344                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1345                 :            :                                 "padding sym op ctrl req failed");
    1346                 :          0 :                         goto error_out;
    1347                 :            :                 }
    1348                 :          0 :                 ret = virtio_crypto_send_command(control_vq, ctrl_req,
    1349                 :            :                         cipher_key_data, auth_key_data, session);
    1350         [ #  # ]:          0 :                 if (ret < 0) {
    1351                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1352                 :            :                                 "create session failed: %d", ret);
    1353                 :          0 :                         goto error_out;
    1354                 :            :                 }
    1355                 :            :                 break;
    1356                 :          0 :         case VIRTIO_CRYPTO_CMD_CIPHER:
    1357                 :            :                 ctrl_req->u.sym_create_session.op_type
    1358                 :          0 :                         = VIRTIO_CRYPTO_SYM_OP_CIPHER;
    1359                 :          0 :                 ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req, xform,
    1360                 :            :                         false, cipher_key_data, auth_key_data, session);
    1361         [ #  # ]:          0 :                 if (ret < 0) {
    1362                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1363                 :            :                                 "padding sym op ctrl req failed");
    1364                 :          0 :                         goto error_out;
    1365                 :            :                 }
    1366                 :          0 :                 ret = virtio_crypto_send_command(control_vq, ctrl_req,
    1367                 :            :                         cipher_key_data, NULL, session);
    1368         [ #  # ]:          0 :                 if (ret < 0) {
    1369                 :          0 :                         VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1370                 :            :                                 "create session failed: %d", ret);
    1371                 :          0 :                         goto error_out;
    1372                 :            :                 }
    1373                 :            :                 break;
    1374                 :          0 :         default:
    1375                 :          0 :                 VIRTIO_CRYPTO_SESSION_LOG_ERR(
    1376                 :            :                         "Unsupported operation chain order parameter");
    1377                 :          0 :                 goto error_out;
    1378                 :            :         }
    1379                 :            :         return 0;
    1380                 :            : 
    1381                 :            : error_out:
    1382                 :            :         return -1;
    1383                 :            : }
    1384                 :            : 
    1385                 :            : static void
    1386                 :          0 : virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
    1387                 :            :                 struct rte_cryptodev_info *info)
    1388                 :            : {
    1389                 :          0 :         struct virtio_crypto_hw *hw = dev->data->dev_private;
    1390                 :            : 
    1391                 :          0 :         PMD_INIT_FUNC_TRACE();
    1392                 :            : 
    1393         [ #  # ]:          0 :         if (info != NULL) {
    1394                 :          0 :                 info->driver_id = cryptodev_virtio_driver_id;
    1395                 :          0 :                 info->feature_flags = dev->feature_flags;
    1396                 :          0 :                 info->max_nb_queue_pairs = hw->max_dataqueues;
    1397                 :            :                 /* No limit of number of sessions */
    1398                 :          0 :                 info->sym.max_nb_sessions = 0;
    1399                 :          0 :                 info->capabilities = hw->virtio_dev_capabilities;
    1400                 :            :         }
    1401                 :          0 : }
    1402                 :            : 
    1403                 :            : static int
    1404                 :          0 : crypto_virtio_pci_probe(
    1405                 :            :         struct rte_pci_driver *pci_drv __rte_unused,
    1406                 :            :         struct rte_pci_device *pci_dev)
    1407                 :            : {
    1408                 :          0 :         struct rte_cryptodev_pmd_init_params init_params = {
    1409                 :            :                 .name = "",
    1410                 :          0 :                 .socket_id = pci_dev->device.numa_node,
    1411                 :            :                 .private_data_size = sizeof(struct virtio_crypto_hw)
    1412                 :            :         };
    1413                 :            :         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
    1414                 :            : 
    1415                 :          0 :         VIRTIO_CRYPTO_DRV_LOG_DBG("Found Crypto device at %02x:%02x.%x",
    1416                 :            :                         pci_dev->addr.bus,
    1417                 :            :                         pci_dev->addr.devid,
    1418                 :            :                         pci_dev->addr.function);
    1419                 :            : 
    1420                 :          0 :         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
    1421                 :            : 
    1422                 :          0 :         return crypto_virtio_create(name, pci_dev, &init_params);
    1423                 :            : }
    1424                 :            : 
    1425                 :            : static int
    1426                 :          0 : crypto_virtio_pci_remove(
    1427                 :            :         struct rte_pci_device *pci_dev __rte_unused)
    1428                 :            : {
    1429                 :            :         struct rte_cryptodev *cryptodev;
    1430                 :            :         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
    1431                 :            : 
    1432         [ #  # ]:          0 :         if (pci_dev == NULL)
    1433                 :            :                 return -EINVAL;
    1434                 :            : 
    1435                 :          0 :         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
    1436                 :            :                         sizeof(cryptodev_name));
    1437                 :            : 
    1438                 :          0 :         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
    1439         [ #  # ]:          0 :         if (cryptodev == NULL)
    1440                 :            :                 return -ENODEV;
    1441                 :            : 
    1442                 :          0 :         return virtio_crypto_dev_uninit(cryptodev);
    1443                 :            : }
    1444                 :            : 
    1445                 :            : static struct rte_pci_driver rte_virtio_crypto_driver = {
    1446                 :            :         .id_table = pci_id_virtio_crypto_map,
    1447                 :            :         .drv_flags = 0,
    1448                 :            :         .probe = crypto_virtio_pci_probe,
    1449                 :            :         .remove = crypto_virtio_pci_remove
    1450                 :            : };
    1451                 :            : 
    1452                 :            : static struct cryptodev_driver virtio_crypto_drv;
    1453                 :            : 
    1454                 :        252 : RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_VIRTIO_PMD, rte_virtio_crypto_driver);
    1455                 :        252 : RTE_PMD_REGISTER_CRYPTO_DRIVER(virtio_crypto_drv,
    1456                 :            :         rte_virtio_crypto_driver.driver,
    1457                 :            :         cryptodev_virtio_driver_id);
    1458         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_init, init, NOTICE);
    1459         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_session, session, NOTICE);
    1460         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_rx, rx, NOTICE);
    1461         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_tx, tx, NOTICE);
    1462         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_driver, driver, NOTICE);

Generated by: LCOV version 1.14