LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cnxk_se.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 1305 0.0 %
Date: 2024-12-01 18:57:19 Functions: 0 9 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 3817 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2021 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _CNXK_SE_H_
       6                 :            : #define _CNXK_SE_H_
       7                 :            : #include <stdbool.h>
       8                 :            : 
       9                 :            : #include <rte_cryptodev.h>
      10                 :            : 
      11                 :            : #include "cnxk_cryptodev.h"
      12                 :            : #include "cnxk_cryptodev_ops.h"
      13                 :            : #include "cnxk_sg.h"
      14                 :            : 
      15                 :            : #define SRC_IOV_SIZE                                                                               \
      16                 :            :         (sizeof(struct roc_se_iov_ptr) + (sizeof(struct roc_se_buf_ptr) * ROC_MAX_SG_CNT))
      17                 :            : #define DST_IOV_SIZE                                                                               \
      18                 :            :         (sizeof(struct roc_se_iov_ptr) + (sizeof(struct roc_se_buf_ptr) * ROC_MAX_SG_CNT))
      19                 :            : 
      20                 :            : enum cpt_dp_thread_type {
      21                 :            :         CPT_DP_THREAD_TYPE_FC_CHAIN = 0x1,
      22                 :            :         CPT_DP_THREAD_TYPE_FC_AEAD,
      23                 :            :         CPT_DP_THREAD_TYPE_PDCP,
      24                 :            :         CPT_DP_THREAD_TYPE_PDCP_CHAIN,
      25                 :            :         CPT_DP_THREAD_TYPE_KASUMI,
      26                 :            :         CPT_DP_THREAD_TYPE_SM,
      27                 :            :         CPT_DP_THREAD_AUTH_ONLY,
      28                 :            :         CPT_DP_THREAD_GENERIC,
      29                 :            :         CPT_DP_THREAD_TYPE_PT,
      30                 :            : };
      31                 :            : 
      32                 :            : #define SYM_SESS_SIZE sizeof(struct rte_cryptodev_sym_session)
      33                 :            : 
      34                 :            : struct __rte_aligned(ROC_ALIGN) cnxk_se_sess {
      35                 :            :         uint8_t rte_sess[SYM_SESS_SIZE];
      36                 :            : 
      37                 :            :         uint8_t aes_gcm : 1;
      38                 :            :         uint8_t aes_ccm : 1;
      39                 :            :         uint8_t aes_ctr : 1;
      40                 :            :         uint8_t chacha_poly : 1;
      41                 :            :         uint8_t is_null : 1;
      42                 :            :         uint8_t is_gmac : 1;
      43                 :            :         uint8_t chained_op : 1;
      44                 :            :         uint8_t auth_first : 1;
      45                 :            :         uint8_t aes_ctr_eea2 : 1;
      46                 :            :         uint8_t is_sha3 : 1;
      47                 :            :         uint8_t short_iv : 1;
      48                 :            :         uint8_t is_sm3 : 1;
      49                 :            :         uint8_t passthrough : 1;
      50                 :            :         uint8_t is_sm4 : 1;
      51                 :            :         uint8_t cipher_only : 1;
      52                 :            :         uint8_t rsvd : 1;
      53                 :            :         uint8_t cpt_op : 4;
      54                 :            :         uint8_t zsk_flag : 4;
      55                 :            :         uint8_t zs_cipher : 4;
      56                 :            :         uint8_t zs_auth : 4;
      57                 :            :         uint8_t dp_thr_type;
      58                 :            :         uint8_t mac_len;
      59                 :            :         uint8_t iv_length;
      60                 :            :         uint8_t auth_iv_length;
      61                 :            :         uint16_t aad_length;
      62                 :            :         uint16_t iv_offset;
      63                 :            :         uint16_t auth_iv_offset;
      64                 :            :         uint32_t salt;
      65                 :            :         uint64_t cpt_inst_w7;
      66                 :            :         uint64_t cpt_inst_w2;
      67                 :            :         struct cnxk_cpt_qp *qp;
      68                 :            :         struct roc_se_ctx roc_se_ctx;
      69                 :            :         struct roc_cpt_lf *lf;
      70                 :            : };
      71                 :            : 
      72                 :            : struct cnxk_sym_dp_ctx {
      73                 :            :         struct cnxk_se_sess *sess;
      74                 :            : };
      75                 :            : 
      76                 :            : struct cnxk_iov {
      77                 :            :         char src[SRC_IOV_SIZE];
      78                 :            :         char dst[SRC_IOV_SIZE];
      79                 :            :         void *iv_buf;
      80                 :            :         void *aad_buf;
      81                 :            :         void *mac_buf;
      82                 :            :         uint16_t c_head;
      83                 :            :         uint16_t c_tail;
      84                 :            :         uint16_t a_head;
      85                 :            :         uint16_t a_tail;
      86                 :            :         int data_len;
      87                 :            : };
      88                 :            : 
      89                 :            : static __rte_always_inline int fill_sess_gmac(struct rte_crypto_sym_xform *xform,
      90                 :            :                                               struct cnxk_se_sess *sess);
      91                 :            : 
      92                 :            : static inline void
      93                 :          0 : cpt_pack_iv(uint8_t *iv_src, uint8_t *iv_dst)
      94                 :            : {
      95                 :            :         /* pack the first 8 bytes of IV to 6 bytes.
      96                 :            :          * discard the 2 MSB bits of each byte
      97                 :            :          */
      98                 :          0 :         iv_dst[0] = (((iv_src[0] & 0x3f) << 2) | ((iv_src[1] >> 4) & 0x3));
      99                 :          0 :         iv_dst[1] = (((iv_src[1] & 0xf) << 4) | ((iv_src[2] >> 2) & 0xf));
     100                 :          0 :         iv_dst[2] = (((iv_src[2] & 0x3) << 6) | (iv_src[3] & 0x3f));
     101                 :            : 
     102                 :          0 :         iv_dst[3] = (((iv_src[4] & 0x3f) << 2) | ((iv_src[5] >> 4) & 0x3));
     103                 :          0 :         iv_dst[4] = (((iv_src[5] & 0xf) << 4) | ((iv_src[6] >> 2) & 0xf));
     104                 :          0 :         iv_dst[5] = (((iv_src[6] & 0x3) << 6) | (iv_src[7] & 0x3f));
     105                 :          0 : }
     106                 :            : 
     107                 :            : static inline void
     108                 :          0 : pdcp_iv_copy(uint8_t *iv_d, const uint8_t *iv_s, const uint8_t pdcp_alg_type, const bool pack_iv)
     109                 :            : {
     110                 :            :         const uint32_t *iv_s_temp;
     111                 :            :         uint32_t iv_temp[4];
     112                 :            :         int j;
     113                 :            : 
     114         [ #  # ]:          0 :         if (unlikely(iv_s == NULL)) {
     115                 :            :                 memset(iv_d, 0, 16);
     116                 :          0 :                 return;
     117                 :            :         }
     118                 :            : 
     119         [ #  # ]:          0 :         if (pdcp_alg_type == ROC_SE_PDCP_ALG_TYPE_SNOW3G) {
     120                 :            :                 /*
     121                 :            :                  * DPDK seems to provide it in form of IV3 IV2 IV1 IV0
     122                 :            :                  * and BigEndian, MC needs it as IV0 IV1 IV2 IV3
     123                 :            :                  */
     124                 :            : 
     125                 :            :                 iv_s_temp = (const uint32_t *)iv_s;
     126                 :            : 
     127         [ #  # ]:          0 :                 for (j = 0; j < 4; j++)
     128                 :          0 :                         iv_temp[j] = iv_s_temp[3 - j];
     129                 :            :                 memcpy(iv_d, iv_temp, 16);
     130                 :          0 :         } else if ((pdcp_alg_type == ROC_SE_PDCP_ALG_TYPE_ZUC) ||
     131         [ #  # ]:          0 :                    pdcp_alg_type == ROC_SE_PDCP_ALG_TYPE_AES_CTR) {
     132                 :            :                 memcpy(iv_d, iv_s, 16);
     133         [ #  # ]:          0 :                 if (pack_iv) {
     134                 :            :                         uint8_t iv_d23, iv_d24;
     135                 :            : 
     136                 :            :                         /* Save last two bytes as only 23B IV space is available */
     137                 :          0 :                         iv_d23 = iv_d[23];
     138                 :          0 :                         iv_d24 = iv_d[24];
     139                 :            : 
     140                 :            :                         /* Copy remaining part of IV */
     141                 :          0 :                         memcpy(iv_d + 16, iv_s + 16, 25 - 16);
     142                 :            : 
     143                 :            :                         /* Swap IV */
     144                 :            :                         roc_se_zuc_bytes_swap(iv_d, 25);
     145                 :            : 
     146                 :            :                         /* Pack IV */
     147                 :          0 :                         cpt_pack_iv(iv_d, iv_d);
     148                 :            : 
     149                 :            :                         /* Move IV */
     150         [ #  # ]:          0 :                         for (j = 6; j < 23; j++)
     151                 :          0 :                                 iv_d[j] = iv_d[j + 2];
     152                 :            : 
     153                 :          0 :                         iv_d[23] = iv_d23;
     154                 :          0 :                         iv_d[24] = iv_d24;
     155                 :            :                 }
     156                 :            :         }
     157                 :            : }
     158                 :            : 
     159                 :            : /*
     160                 :            :  * Digest immediately at the end of the data is the best case. Switch to SG if
     161                 :            :  * that cannot be ensured.
     162                 :            :  */
     163                 :            : static inline void
     164                 :          0 : cpt_digest_buf_lb_check(const struct cnxk_se_sess *sess, struct rte_mbuf *m,
     165                 :            :                         struct roc_se_fc_params *fc_params, uint32_t *flags,
     166                 :            :                         struct rte_crypto_sym_op *sym_op, bool *inplace, uint32_t a_data_off,
     167                 :            :                         uint32_t a_data_len, uint32_t c_data_off, uint32_t c_data_len,
     168                 :            :                         const bool is_pdcp_chain)
     169                 :            : {
     170                 :          0 :         const uint32_t auth_end = a_data_off + a_data_len;
     171                 :            :         uint32_t mc_hash_off;
     172                 :            : 
     173                 :            :         /* PDCP_CHAIN only supports auth_first */
     174                 :            : 
     175   [ #  #  #  # ]:          0 :         if (is_pdcp_chain || sess->auth_first)
     176                 :            :                 mc_hash_off = auth_end;
     177                 :            :         else
     178                 :          0 :                 mc_hash_off = RTE_MAX(c_data_off + c_data_len, auth_end);
     179                 :            : 
     180                 :            :         /* Digest immediately following data is best case */
     181                 :            : 
     182         [ #  # ]:          0 :         if (unlikely(rte_pktmbuf_mtod_offset(m, uint8_t *, mc_hash_off) !=
     183                 :            :                      sym_op->auth.digest.data)) {
     184                 :          0 :                 *flags |= ROC_SE_VALID_MAC_BUF;
     185                 :          0 :                 fc_params->mac_buf.size = sess->mac_len;
     186                 :          0 :                 fc_params->mac_buf.vaddr = sym_op->auth.digest.data;
     187                 :          0 :                 *inplace = false;
     188                 :            :         }
     189                 :          0 : }
     190                 :            : 
     191                 :            : static inline struct rte_mbuf *
     192                 :          0 : cpt_m_dst_get(uint8_t cpt_op, struct rte_mbuf *m_src, struct rte_mbuf *m_dst)
     193                 :            : {
     194   [ #  #  #  # ]:          0 :         if (m_dst != NULL && (cpt_op & ROC_SE_OP_ENCODE))
     195                 :            :                 return m_dst;
     196                 :            :         else
     197                 :          0 :                 return m_src;
     198                 :            : }
     199                 :            : 
     200                 :            : static __rte_always_inline int
     201                 :            : cpt_mac_len_verify(struct rte_crypto_auth_xform *auth)
     202                 :            : {
     203                 :          0 :         uint16_t mac_len = auth->digest_length;
     204                 :            :         int ret;
     205                 :            : 
     206   [ #  #  #  # ]:          0 :         if ((auth->algo != RTE_CRYPTO_AUTH_NULL) && (mac_len == 0))
     207                 :            :                 return -1;
     208                 :            : 
     209   [ #  #  #  #  :          0 :         switch (auth->algo) {
          #  #  #  #  #  
                      # ]
     210                 :          0 :         case RTE_CRYPTO_AUTH_MD5:
     211                 :            :         case RTE_CRYPTO_AUTH_MD5_HMAC:
     212         [ #  # ]:          0 :                 ret = (mac_len <= 16) ? 0 : -1;
     213                 :            :                 break;
     214                 :          0 :         case RTE_CRYPTO_AUTH_SHA1:
     215                 :            :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
     216         [ #  # ]:          0 :                 ret = (mac_len <= 20) ? 0 : -1;
     217                 :            :                 break;
     218                 :          0 :         case RTE_CRYPTO_AUTH_SHA224:
     219                 :            :         case RTE_CRYPTO_AUTH_SHA224_HMAC:
     220                 :            :         case RTE_CRYPTO_AUTH_SHA3_224:
     221                 :            :         case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
     222         [ #  # ]:          0 :                 ret = (mac_len <= 28) ? 0 : -1;
     223                 :            :                 break;
     224                 :          0 :         case RTE_CRYPTO_AUTH_SHA256:
     225                 :            :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
     226                 :            :         case RTE_CRYPTO_AUTH_SHA3_256:
     227                 :            :         case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
     228         [ #  # ]:          0 :                 ret = (mac_len <= 32) ? 0 : -1;
     229                 :            :                 break;
     230                 :          0 :         case RTE_CRYPTO_AUTH_SHA384:
     231                 :            :         case RTE_CRYPTO_AUTH_SHA384_HMAC:
     232                 :            :         case RTE_CRYPTO_AUTH_SHA3_384:
     233                 :            :         case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
     234         [ #  # ]:          0 :                 ret = (mac_len <= 48) ? 0 : -1;
     235                 :            :                 break;
     236                 :          0 :         case RTE_CRYPTO_AUTH_SHA512:
     237                 :            :         case RTE_CRYPTO_AUTH_SHA512_HMAC:
     238                 :            :         case RTE_CRYPTO_AUTH_SHA3_512:
     239                 :            :         case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
     240         [ #  # ]:          0 :                 ret = (mac_len <= 64) ? 0 : -1;
     241                 :            :                 break;
     242                 :            :         /* SHAKE itself doesn't have limitation of digest length,
     243                 :            :          * but in microcode size of length field is limited to 8 bits
     244                 :            :          */
     245                 :          0 :         case RTE_CRYPTO_AUTH_SHAKE_128:
     246                 :            :         case RTE_CRYPTO_AUTH_SHAKE_256:
     247         [ #  # ]:          0 :                 ret = (mac_len <= UINT8_MAX) ? 0 : -1;
     248                 :            :                 break;
     249                 :          0 :         case RTE_CRYPTO_AUTH_SM3:
     250         [ #  # ]:          0 :                 ret = (mac_len <= 32) ? 0 : -1;
     251                 :            :                 break;
     252                 :            :         case RTE_CRYPTO_AUTH_NULL:
     253                 :            :                 ret = 0;
     254                 :            :                 break;
     255                 :            :         default:
     256                 :            :                 ret = -1;
     257                 :            :         }
     258                 :            : 
     259                 :            :         return ret;
     260                 :            : }
     261                 :            : 
     262                 :            : static __rte_always_inline int
     263                 :            : sg_inst_prep(struct roc_se_fc_params *params, struct cpt_inst_s *inst, uint64_t offset_ctrl,
     264                 :            :              const uint8_t *iv_s, int iv_len, const bool pack_iv, uint8_t pdcp_alg_type,
     265                 :            :              int32_t inputlen, int32_t outputlen, uint32_t passthrough_len, uint32_t req_flags,
     266                 :            :              int pdcp_flag, int decrypt)
     267                 :            : {
     268                 :            :         struct roc_sglist_comp *gather_comp, *scatter_comp;
     269                 :          0 :         void *m_vaddr = params->meta_buf.vaddr;
     270                 :            :         struct roc_se_buf_ptr *aad_buf = NULL;
     271                 :            :         uint32_t mac_len = 0, aad_len = 0;
     272                 :            :         struct roc_se_ctx *se_ctx;
     273                 :            :         uint32_t i, g_size_bytes;
     274                 :            :         int zsk_flags, ret = 0;
     275                 :            :         uint64_t *offset_vaddr;
     276                 :            :         uint32_t s_size_bytes;
     277                 :            :         uint8_t *in_buffer;
     278                 :            :         uint32_t size;
     279                 :            :         uint8_t *iv_d;
     280                 :            : 
     281                 :            :         se_ctx = params->ctx;
     282                 :          0 :         zsk_flags = se_ctx->zsk_flags;
     283                 :          0 :         mac_len = se_ctx->mac_len;
     284                 :            : 
     285                 :          0 :         if (unlikely(req_flags & ROC_SE_VALID_AAD_BUF)) {
     286                 :            :                 /* We don't support both AAD and auth data separately */
     287                 :            :                 aad_len = params->aad_buf.size;
     288                 :            :                 aad_buf = &params->aad_buf;
     289                 :            :         }
     290                 :            : 
     291                 :            :         /* save space for iv */
     292                 :            :         offset_vaddr = m_vaddr;
     293                 :            : 
     294                 :          0 :         m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + RTE_ALIGN_CEIL(iv_len, 8);
     295                 :            : 
     296                 :          0 :         inst->w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
     297                 :            : 
     298                 :            :         /* iv offset is 0 */
     299                 :          0 :         *offset_vaddr = offset_ctrl;
     300                 :            : 
     301   [ #  #  #  #  :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
          #  #  #  #  #  
                #  #  # ]
     302                 :            : 
     303                 :            :         if (pdcp_flag) {
     304                 :          0 :                 if (likely(iv_len)) {
     305   [ #  #  #  #  :          0 :                         if (zsk_flags == 0x1)
             #  #  #  # ]
     306                 :          0 :                                 pdcp_iv_copy(iv_d + params->pdcp_iv_offset, iv_s, pdcp_alg_type,
     307                 :            :                                              pack_iv);
     308                 :            :                         else
     309                 :          0 :                                 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type, pack_iv);
     310                 :            :                 }
     311                 :            :         } else {
     312   [ #  #  #  #  :          0 :                 if (likely(iv_len))
          #  #  #  #  #  
                #  #  # ]
     313                 :          0 :                         memcpy(iv_d, iv_s, iv_len);
     314                 :            :         }
     315                 :            : 
     316                 :            :         /* DPTR has SG list */
     317                 :            : 
     318                 :            :         /* TODO Add error check if space will be sufficient */
     319                 :          0 :         gather_comp = (struct roc_sglist_comp *)((uint8_t *)m_vaddr + 8);
     320                 :            : 
     321                 :            :         /*
     322                 :            :          * Input Gather List
     323                 :            :          */
     324                 :            :         i = 0;
     325                 :            : 
     326                 :            :         /* Offset control word followed by iv */
     327                 :            : 
     328   [ #  #  #  #  :          0 :         i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr, ROC_SE_OFF_CTRL_LEN + iv_len);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     329                 :            : 
     330                 :            :         /* Add input data */
     331   [ #  #  #  #  :          0 :         if (decrypt && (req_flags & ROC_SE_VALID_MAC_BUF)) {
          #  #  #  #  #  
                #  #  # ]
     332                 :          0 :                 size = inputlen - iv_len - mac_len;
     333   [ #  #  #  #  :          0 :                 if (likely(size)) {
          #  #  #  #  #  
                #  #  # ]
     334   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
                   #  # ]
     335                 :            :                         /* input data only */
     336   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
             #  #  #  # ]
     337                 :            :                                 i = fill_sg_comp_from_buf_min(gather_comp, i, params->bufs, &size);
     338                 :            :                         } else {
     339   [ #  #  #  # ]:          0 :                                 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0, &size,
     340                 :            :                                                           aad_buf, aad_offset);
     341                 :            :                         }
     342   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
                #  #  # ]
     343                 :          0 :                                 plt_dp_err("Insufficient buffer"
     344                 :            :                                            " space, size %d needed",
     345                 :            :                                            size);
     346                 :          0 :                                 return -1;
     347                 :            :                         }
     348                 :            :                 }
     349                 :            : 
     350   [ #  #  #  #  :          0 :                 if (mac_len)
          #  #  #  #  #  
                #  #  # ]
     351                 :            :                         i = fill_sg_comp_from_buf(gather_comp, i, &params->mac_buf);
     352                 :            :         } else {
     353                 :            :                 /* input data */
     354                 :          0 :                 size = inputlen - iv_len;
     355   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     356   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
          #  #  #  #  #  
                #  #  # ]
     357   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     358                 :            :                                 i = fill_sg_comp_from_buf_min(gather_comp, i, params->bufs, &size);
     359                 :            :                         } else {
     360   [ #  #  #  #  :          0 :                                 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0, &size,
             #  #  #  # ]
     361                 :            :                                                           aad_buf, aad_offset);
     362                 :            :                         }
     363   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     364                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     365                 :            :                                            " size %d needed",
     366                 :            :                                            size);
     367                 :          0 :                                 return -1;
     368                 :            :                         }
     369                 :            :                 }
     370                 :            :         }
     371                 :            : 
     372                 :            :         in_buffer = m_vaddr;
     373                 :            : 
     374                 :          0 :         ((uint16_t *)in_buffer)[0] = 0;
     375                 :          0 :         ((uint16_t *)in_buffer)[1] = 0;
     376   [ #  #  #  #  :          0 :         ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     377                 :            : 
     378                 :          0 :         g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     379                 :            :         /*
     380                 :            :          * Output Scatter List
     381                 :            :          */
     382                 :            : 
     383                 :            :         i = 0;
     384                 :          0 :         scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
     385                 :            : 
     386   [ #  #  #  #  :          0 :         if ((zsk_flags == 0x1) && (se_ctx->fc_type == ROC_SE_KASUMI)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     387                 :            :                 /* IV in SLIST only for EEA3 & UEA2 or for F8 */
     388                 :            :                 iv_len = 0;
     389                 :            :         }
     390                 :            : 
     391   [ #  #  #  #  :          0 :         if (iv_len) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     392   [ #  #  #  #  :          0 :                 i = fill_sg_comp(scatter_comp, i, (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     393                 :            :                                  iv_len);
     394                 :            :         }
     395                 :            : 
     396                 :            :         /* Add output data */
     397   [ #  #  #  #  :          0 :         if ((!decrypt) && (req_flags & ROC_SE_VALID_MAC_BUF)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     398                 :          0 :                 size = outputlen - iv_len - mac_len;
     399   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     400                 :            : 
     401   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
                   #  # ]
     402                 :            : 
     403   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
             #  #  #  # ]
     404                 :            :                                 i = fill_sg_comp_from_buf_min(scatter_comp, i, params->bufs, &size);
     405                 :            :                         } else {
     406   [ #  #  #  # ]:          0 :                                 i = fill_sg_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
     407                 :            :                                                           &size, aad_buf, aad_offset);
     408                 :            :                         }
     409   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     410                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     411                 :            :                                            " size %d needed",
     412                 :            :                                            size);
     413                 :          0 :                                 return -1;
     414                 :            :                         }
     415                 :            :                 }
     416                 :            : 
     417                 :            :                 /* mac data */
     418   [ #  #  #  #  :          0 :                 if (mac_len)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     419                 :            :                         i = fill_sg_comp_from_buf(scatter_comp, i, &params->mac_buf);
     420                 :            :         } else {
     421                 :            :                 /* Output including mac */
     422                 :          0 :                 size = outputlen - iv_len;
     423   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     424   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
          #  #  #  #  #  
                #  #  # ]
     425                 :            : 
     426   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     427                 :            :                                 i = fill_sg_comp_from_buf_min(scatter_comp, i, params->bufs, &size);
     428                 :            :                         } else {
     429   [ #  #  #  #  :          0 :                                 i = fill_sg_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
             #  #  #  # ]
     430                 :            :                                                           &size, aad_buf, aad_offset);
     431                 :            :                         }
     432                 :            : 
     433   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     434                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     435                 :            :                                            " size %d needed",
     436                 :            :                                            size);
     437                 :          0 :                                 return -1;
     438                 :            :                         }
     439                 :            :                 }
     440                 :            :         }
     441   [ #  #  #  #  :          0 :         ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     442                 :          0 :         s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     443                 :            : 
     444                 :          0 :         size = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
     445                 :            : 
     446                 :            :         /* This is DPTR len in case of SG mode */
     447                 :          0 :         inst->w4.s.dlen = size;
     448                 :            : 
     449   [ #  #  #  #  :          0 :         if (unlikely(size > ROC_SG_MAX_DLEN_SIZE)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     450                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
     451                 :            :                 ret = -1;
     452                 :            :         }
     453                 :            : 
     454                 :          0 :         inst->dptr = (uint64_t)in_buffer;
     455                 :          0 :         return ret;
     456                 :            : }
     457                 :            : 
     458                 :            : static __rte_always_inline int
     459                 :            : sg2_inst_prep(struct roc_se_fc_params *params, struct cpt_inst_s *inst, uint64_t offset_ctrl,
     460                 :            :               const uint8_t *iv_s, int iv_len, const bool pack_iv, uint8_t pdcp_alg_type,
     461                 :            :               int32_t inputlen, int32_t outputlen, uint32_t passthrough_len, uint32_t req_flags,
     462                 :            :               int pdcp_flag, int decrypt)
     463                 :            : {
     464                 :            :         struct roc_sg2list_comp *gather_comp, *scatter_comp;
     465                 :          0 :         void *m_vaddr = params->meta_buf.vaddr;
     466                 :            :         struct roc_se_buf_ptr *aad_buf = NULL;
     467                 :            :         uint32_t mac_len = 0, aad_len = 0;
     468                 :            :         uint16_t scatter_sz, gather_sz;
     469                 :            :         union cpt_inst_w5 cpt_inst_w5;
     470                 :            :         union cpt_inst_w6 cpt_inst_w6;
     471                 :            :         struct roc_se_ctx *se_ctx;
     472                 :            :         uint32_t i, g_size_bytes;
     473                 :            :         uint64_t *offset_vaddr;
     474                 :            :         int zsk_flags, ret = 0;
     475                 :            :         uint32_t size;
     476                 :            :         uint8_t *iv_d;
     477                 :            : 
     478                 :            :         se_ctx = params->ctx;
     479                 :          0 :         zsk_flags = se_ctx->zsk_flags;
     480                 :          0 :         mac_len = se_ctx->mac_len;
     481                 :            : 
     482                 :          0 :         if (unlikely(req_flags & ROC_SE_VALID_AAD_BUF)) {
     483                 :            :                 /* We don't support both AAD and auth data separately */
     484                 :            :                 aad_len = params->aad_buf.size;
     485                 :            :                 aad_buf = &params->aad_buf;
     486                 :            :         }
     487                 :            : 
     488                 :            :         /* save space for iv */
     489                 :            :         offset_vaddr = m_vaddr;
     490                 :            : 
     491                 :          0 :         m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + RTE_ALIGN_CEIL(iv_len, 8);
     492                 :            : 
     493                 :          0 :         inst->w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
     494                 :            : 
     495                 :            :         /* This is DPTR len in case of SG mode */
     496                 :          0 :         inst->w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
     497                 :            : 
     498                 :            :         /* iv offset is 0 */
     499                 :          0 :         *offset_vaddr = offset_ctrl;
     500                 :            : 
     501   [ #  #  #  #  :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
          #  #  #  #  #  
                #  #  # ]
     502                 :            :         if (pdcp_flag) {
     503                 :          0 :                 if (likely(iv_len)) {
     504   [ #  #  #  #  :          0 :                         if (zsk_flags == 0x1)
             #  #  #  # ]
     505                 :          0 :                                 pdcp_iv_copy(iv_d + params->pdcp_iv_offset, iv_s, pdcp_alg_type,
     506                 :            :                                              pack_iv);
     507                 :            :                         else
     508                 :          0 :                                 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type, pack_iv);
     509                 :            :                 }
     510                 :            :         } else {
     511   [ #  #  #  #  :          0 :                 if (likely(iv_len))
          #  #  #  #  #  
                #  #  # ]
     512                 :          0 :                         memcpy(iv_d, iv_s, iv_len);
     513                 :            :         }
     514                 :            : 
     515                 :            :         /* DPTR has SG list */
     516                 :            : 
     517                 :            :         /* TODO Add error check if space will be sufficient */
     518                 :            :         gather_comp = (struct roc_sg2list_comp *)((uint8_t *)m_vaddr);
     519                 :            : 
     520                 :            :         /*
     521                 :            :          * Input Gather List
     522                 :            :          */
     523                 :            :         i = 0;
     524                 :            : 
     525                 :            :         /* Offset control word followed by iv */
     526                 :            : 
     527   [ #  #  #  #  :          0 :         i = fill_sg2_comp(gather_comp, i, (uint64_t)offset_vaddr, ROC_SE_OFF_CTRL_LEN + iv_len);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     528                 :            : 
     529                 :            :         /* Add input data */
     530   [ #  #  #  #  :          0 :         if (decrypt && (req_flags & ROC_SE_VALID_MAC_BUF)) {
          #  #  #  #  #  
                #  #  # ]
     531                 :          0 :                 size = inputlen - iv_len - mac_len;
     532   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
                #  #  # ]
     533                 :            :                         /* input data only */
     534   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
             #  #  #  # ]
     535                 :            :                                 i = fill_sg2_comp_from_buf_min(gather_comp, i, params->bufs, &size);
     536                 :            :                         } else {
     537   [ #  #  #  #  :          0 :                                 uint32_t aad_offset = aad_len ? passthrough_len : 0;
                   #  # ]
     538                 :            : 
     539   [ #  #  #  # ]:          0 :                                 i = fill_sg2_comp_from_iov(gather_comp, i, params->src_iov, 0,
     540                 :            :                                                            &size, aad_buf, aad_offset);
     541                 :            :                         }
     542   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
                #  #  # ]
     543                 :          0 :                                 plt_dp_err("Insufficient buffer"
     544                 :            :                                            " space, size %d needed",
     545                 :            :                                            size);
     546                 :          0 :                                 return -1;
     547                 :            :                         }
     548                 :            :                 }
     549                 :            : 
     550                 :            :                 /* mac data */
     551   [ #  #  #  #  :          0 :                 if (mac_len)
          #  #  #  #  #  
                #  #  # ]
     552                 :            :                         i = fill_sg2_comp_from_buf(gather_comp, i, &params->mac_buf);
     553                 :            :         } else {
     554                 :            :                 /* input data */
     555                 :          0 :                 size = inputlen - iv_len;
     556   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     557   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
          #  #  #  #  #  
                #  #  # ]
     558   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     559                 :            :                                 i = fill_sg2_comp_from_buf_min(gather_comp, i, params->bufs, &size);
     560                 :            :                         } else {
     561   [ #  #  #  #  :          0 :                                 i = fill_sg2_comp_from_iov(gather_comp, i, params->src_iov, 0,
             #  #  #  # ]
     562                 :            :                                                            &size, aad_buf, aad_offset);
     563                 :            :                         }
     564   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     565                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     566                 :            :                                            " size %d needed",
     567                 :            :                                            size);
     568                 :          0 :                                 return -1;
     569                 :            :                         }
     570                 :            :                 }
     571                 :            :         }
     572                 :            : 
     573                 :          0 :         gather_sz = (i + 2) / 3;
     574                 :          0 :         g_size_bytes = gather_sz * sizeof(struct roc_sg2list_comp);
     575                 :            : 
     576                 :            :         /*
     577                 :            :          * Output Scatter List
     578                 :            :          */
     579                 :            : 
     580                 :            :         i = 0;
     581                 :          0 :         scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
     582                 :            : 
     583   [ #  #  #  #  :          0 :         if ((zsk_flags == 0x1) && (se_ctx->fc_type == ROC_SE_KASUMI)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     584                 :            :                 /* IV in SLIST only for EEA3 & UEA2 or for F8 */
     585                 :            :                 iv_len = 0;
     586                 :            :         }
     587                 :            : 
     588   [ #  #  #  #  :          0 :         if (iv_len) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     589                 :          0 :                 i = fill_sg2_comp(scatter_comp, i, (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN,
     590                 :            :                                   iv_len);
     591                 :            :         }
     592                 :            : 
     593                 :            :         /* Add output data */
     594   [ #  #  #  #  :          0 :         if ((!decrypt) && (req_flags & ROC_SE_VALID_MAC_BUF)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     595                 :          0 :                 size = outputlen - iv_len - mac_len;
     596   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     597                 :            : 
     598   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
                   #  # ]
     599                 :            : 
     600   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
             #  #  #  # ]
     601                 :            :                                 i = fill_sg2_comp_from_buf_min(scatter_comp, i, params->bufs,
     602                 :            :                                                                &size);
     603                 :            :                         } else {
     604   [ #  #  #  # ]:          0 :                                 i = fill_sg2_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
     605                 :            :                                                            &size, aad_buf, aad_offset);
     606                 :            :                         }
     607   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     608                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     609                 :            :                                            " size %d needed",
     610                 :            :                                            size);
     611                 :          0 :                                 return -1;
     612                 :            :                         }
     613                 :            :                 }
     614                 :            : 
     615                 :            :                 /* mac data */
     616   [ #  #  #  #  :          0 :                 if (mac_len)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     617                 :            :                         i = fill_sg2_comp_from_buf(scatter_comp, i, &params->mac_buf);
     618                 :            :         } else {
     619                 :            :                 /* Output including mac */
     620                 :          0 :                 size = outputlen - iv_len;
     621   [ #  #  #  #  :          0 :                 if (size) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     622   [ #  #  #  #  :          0 :                         uint32_t aad_offset = aad_len ? passthrough_len : 0;
          #  #  #  #  #  
                #  #  # ]
     623                 :            : 
     624   [ #  #  #  #  :          0 :                         if (unlikely(req_flags & ROC_SE_SINGLE_BUF_INPLACE)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     625                 :            :                                 i = fill_sg2_comp_from_buf_min(scatter_comp, i, params->bufs,
     626                 :            :                                                                &size);
     627                 :            :                         } else {
     628   [ #  #  #  #  :          0 :                                 i = fill_sg2_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
             #  #  #  # ]
     629                 :            :                                                            &size, aad_buf, aad_offset);
     630                 :            :                         }
     631                 :            : 
     632   [ #  #  #  #  :          0 :                         if (unlikely(size)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     633                 :          0 :                                 plt_dp_err("Insufficient buffer space,"
     634                 :            :                                            " size %d needed",
     635                 :            :                                            size);
     636                 :          0 :                                 return -1;
     637                 :            :                         }
     638                 :            :                 }
     639                 :            :         }
     640                 :            : 
     641                 :          0 :         scatter_sz = (i + 2) / 3;
     642                 :            : 
     643                 :          0 :         cpt_inst_w5.s.gather_sz = gather_sz;
     644                 :          0 :         cpt_inst_w6.s.scatter_sz = scatter_sz;
     645                 :            : 
     646                 :          0 :         cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
     647                 :          0 :         cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
     648                 :            : 
     649                 :          0 :         inst->w5.u64 = cpt_inst_w5.u64;
     650                 :          0 :         inst->w6.u64 = cpt_inst_w6.u64;
     651                 :            : 
     652   [ #  #  #  #  :          0 :         if (unlikely((scatter_sz >> 4) || (gather_sz >> 4))) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     653                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
     654                 :            :                 ret = -1;
     655                 :            :         }
     656                 :            : 
     657                 :            :         return ret;
     658                 :            : }
     659                 :            : 
     660                 :            : static __rte_always_inline int
     661                 :            : cpt_digest_gen_sg_ver1_prep(uint32_t flags, uint64_t d_lens, struct roc_se_fc_params *params,
     662                 :            :                             struct cpt_inst_s *inst)
     663                 :            : {
     664                 :            :         struct roc_sglist_comp *gather_comp, *scatter_comp;
     665                 :            :         void *m_vaddr = params->meta_buf.vaddr;
     666                 :            :         uint32_t g_size_bytes, s_size_bytes;
     667                 :            :         uint16_t data_len, mac_len, key_len;
     668                 :            :         union cpt_inst_w4 cpt_inst_w4;
     669                 :            :         roc_se_auth_type hash_type;
     670                 :            :         struct roc_se_ctx *ctx;
     671                 :            :         uint8_t *in_buffer;
     672                 :            :         uint32_t size, i;
     673                 :            :         int ret = 0;
     674                 :            : 
     675                 :            :         ctx = params->ctx;
     676                 :            : 
     677                 :          0 :         hash_type = ctx->hash_type;
     678                 :          0 :         mac_len = ctx->mac_len;
     679                 :          0 :         key_len = ctx->auth_key_len;
     680                 :          0 :         data_len = ROC_SE_AUTH_DLEN(d_lens);
     681                 :            : 
     682                 :          0 :         cpt_inst_w4.u64 = ctx->template_w4.u64;
     683                 :          0 :         cpt_inst_w4.s.param2 = ((uint16_t)hash_type << 8) | mac_len;
     684   [ #  #  #  #  :          0 :         if (ctx->hmac) {
                   #  # ]
     685                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_HMAC | ROC_DMA_MODE_SG;
     686                 :          0 :                 cpt_inst_w4.s.param1 = key_len;
     687                 :          0 :                 cpt_inst_w4.s.dlen = data_len + RTE_ALIGN_CEIL(key_len, 8);
     688                 :            :         } else {
     689                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_HASH | ROC_DMA_MODE_SG;
     690                 :          0 :                 cpt_inst_w4.s.param1 = 0;
     691                 :          0 :                 cpt_inst_w4.s.dlen = data_len;
     692                 :            :         }
     693                 :            : 
     694                 :            :         /* DPTR has SG list */
     695                 :            :         in_buffer = m_vaddr;
     696                 :            : 
     697                 :          0 :         ((uint16_t *)in_buffer)[0] = 0;
     698                 :          0 :         ((uint16_t *)in_buffer)[1] = 0;
     699                 :            : 
     700                 :            :         /* TODO Add error check if space will be sufficient */
     701                 :          0 :         gather_comp = (struct roc_sglist_comp *)((uint8_t *)m_vaddr + 8);
     702                 :            : 
     703                 :            :         /*
     704                 :            :          * Input gather list
     705                 :            :          */
     706                 :            : 
     707                 :            :         i = 0;
     708                 :            : 
     709   [ #  #  #  #  :          0 :         if (ctx->hmac) {
                   #  # ]
     710                 :          0 :                 uint64_t k_vaddr = (uint64_t)ctx->auth_key;
     711                 :            :                 /* Key */
     712                 :          0 :                 i = fill_sg_comp(gather_comp, i, k_vaddr,
     713   [ #  #  #  #  :          0 :                                  RTE_ALIGN_CEIL(key_len, 8));
                   #  # ]
     714                 :            :         }
     715                 :            : 
     716                 :            :         /* input data */
     717                 :          0 :         size = data_len;
     718                 :            :         i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0, &size, NULL, 0);
     719   [ #  #  #  #  :          0 :         if (unlikely(size)) {
                   #  # ]
     720                 :          0 :                 plt_dp_err("Insufficient dst IOV size, short by %dB", size);
     721                 :            :                 return -1;
     722                 :            :         }
     723   [ #  #  #  #  :          0 :         ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
                   #  # ]
     724                 :          0 :         g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     725                 :            : 
     726                 :            :         /*
     727                 :            :          * Output Gather list
     728                 :            :          */
     729                 :            : 
     730                 :            :         i = 0;
     731                 :          0 :         scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
     732                 :            : 
     733                 :            :         if (flags & ROC_SE_VALID_MAC_BUF) {
     734   [ #  #  #  #  :          0 :                 if (unlikely(params->mac_buf.size < mac_len)) {
                   #  # ]
     735                 :          0 :                         plt_dp_err("Insufficient MAC size");
     736                 :            :                         return -1;
     737                 :            :                 }
     738                 :            : 
     739                 :            :                 size = mac_len;
     740                 :            :                 i = fill_sg_comp_from_buf_min(scatter_comp, i, &params->mac_buf,
     741                 :            :                                               &size);
     742                 :            :         } else {
     743                 :            :                 size = mac_len;
     744                 :            :                 i = fill_sg_comp_from_iov(scatter_comp, i, params->src_iov,
     745                 :            :                                           data_len, &size, NULL, 0);
     746                 :            :                 if (unlikely(size)) {
     747                 :            :                         plt_dp_err("Insufficient dst IOV size, short by %dB",
     748                 :            :                                    size);
     749                 :            :                         return -1;
     750                 :            :                 }
     751                 :            :         }
     752                 :            : 
     753                 :          0 :         ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
     754                 :            :         s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     755                 :            : 
     756                 :          0 :         size = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
     757                 :            : 
     758   [ #  #  #  #  :          0 :         if (unlikely(size > ROC_SG_MAX_DLEN_SIZE)) {
                   #  # ]
     759                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
     760                 :            :                 ret = -1;
     761                 :            :         }
     762                 :            : 
     763                 :            :         /* This is DPTR len in case of SG mode */
     764                 :          0 :         cpt_inst_w4.s.dlen = size;
     765                 :            : 
     766                 :          0 :         inst->dptr = (uint64_t)in_buffer;
     767                 :          0 :         inst->w4.u64 = cpt_inst_w4.u64;
     768                 :            : 
     769                 :            :         return ret;
     770                 :            : }
     771                 :            : 
     772                 :            : static __rte_always_inline int
     773                 :            : cpt_digest_gen_sg_ver2_prep(uint32_t flags, uint64_t d_lens, struct roc_se_fc_params *params,
     774                 :            :                             struct cpt_inst_s *inst)
     775                 :            : {
     776                 :            :         uint16_t data_len, mac_len, key_len, scatter_sz, gather_sz;
     777                 :            :         struct roc_sg2list_comp *gather_comp, *scatter_comp;
     778                 :            :         void *m_vaddr = params->meta_buf.vaddr;
     779                 :            :         union cpt_inst_w4 cpt_inst_w4;
     780                 :            :         union cpt_inst_w5 cpt_inst_w5;
     781                 :            :         union cpt_inst_w6 cpt_inst_w6;
     782                 :            :         roc_se_auth_type hash_type;
     783                 :            :         struct roc_se_ctx *ctx;
     784                 :            :         uint32_t g_size_bytes;
     785                 :            :         uint32_t size, i;
     786                 :            :         int ret = 0;
     787                 :            : 
     788                 :            :         ctx = params->ctx;
     789                 :            : 
     790                 :          0 :         hash_type = ctx->hash_type;
     791                 :          0 :         mac_len = ctx->mac_len;
     792                 :          0 :         key_len = ctx->auth_key_len;
     793                 :          0 :         data_len = ROC_SE_AUTH_DLEN(d_lens);
     794                 :            : 
     795                 :          0 :         cpt_inst_w4.u64 = ctx->template_w4.u64;
     796                 :          0 :         cpt_inst_w4.s.param2 = ((uint16_t)hash_type << 8) | mac_len;
     797   [ #  #  #  #  :          0 :         if (ctx->hmac) {
                   #  # ]
     798                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_HMAC;
     799                 :          0 :                 cpt_inst_w4.s.param1 = key_len;
     800                 :          0 :                 cpt_inst_w4.s.dlen = data_len + RTE_ALIGN_CEIL(key_len, 8);
     801                 :            :         } else {
     802                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_HASH;
     803                 :          0 :                 cpt_inst_w4.s.param1 = 0;
     804                 :          0 :                 cpt_inst_w4.s.dlen = data_len;
     805                 :            :         }
     806                 :            : 
     807                 :            :         /* DPTR has SG list */
     808                 :            : 
     809                 :            :         /* TODO Add error check if space will be sufficient */
     810                 :            :         gather_comp = (struct roc_sg2list_comp *)((uint8_t *)m_vaddr + 0);
     811                 :            : 
     812                 :            :         /*
     813                 :            :          * Input gather list
     814                 :            :          */
     815                 :            : 
     816                 :            :         i = 0;
     817                 :            : 
     818   [ #  #  #  #  :          0 :         if (ctx->hmac) {
                   #  # ]
     819                 :          0 :                 uint64_t k_vaddr = (uint64_t)ctx->auth_key;
     820                 :            :                 /* Key */
     821                 :          0 :                 i = fill_sg2_comp(gather_comp, i, k_vaddr, RTE_ALIGN_CEIL(key_len, 8));
     822                 :            :         }
     823                 :            : 
     824                 :            :         /* input data */
     825                 :          0 :         size = data_len;
     826                 :            :         i = fill_sg2_comp_from_iov(gather_comp, i, params->src_iov, 0, &size, NULL, 0);
     827   [ #  #  #  #  :          0 :         if (unlikely(size)) {
                   #  # ]
     828                 :          0 :                 plt_dp_err("Insufficient dst IOV size, short by %dB", size);
     829                 :            :                 return -1;
     830                 :            :         }
     831                 :            : 
     832                 :          0 :         gather_sz = (i + 2) / 3;
     833                 :          0 :         g_size_bytes = gather_sz * sizeof(struct roc_sg2list_comp);
     834                 :            : 
     835                 :            :         /*
     836                 :            :          * Output Gather list
     837                 :            :          */
     838                 :            : 
     839                 :            :         i = 0;
     840                 :          0 :         scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
     841                 :            : 
     842                 :            :         if (flags & ROC_SE_VALID_MAC_BUF) {
     843   [ #  #  #  #  :          0 :                 if (unlikely(params->mac_buf.size < mac_len)) {
                   #  # ]
     844                 :          0 :                         plt_dp_err("Insufficient MAC size");
     845                 :            :                         return -1;
     846                 :            :                 }
     847                 :            : 
     848                 :            :                 size = mac_len;
     849                 :            :                 i = fill_sg2_comp_from_buf_min(scatter_comp, i, &params->mac_buf, &size);
     850                 :            :         } else {
     851                 :            :                 size = mac_len;
     852                 :            :                 i = fill_sg2_comp_from_iov(scatter_comp, i, params->src_iov, data_len, &size, NULL,
     853                 :            :                                            0);
     854                 :            :                 if (unlikely(size)) {
     855                 :            :                         plt_dp_err("Insufficient dst IOV size, short by %dB", size);
     856                 :            :                         return -1;
     857                 :            :                 }
     858                 :            :         }
     859                 :            : 
     860                 :            :         scatter_sz = (i + 2) / 3;
     861                 :            : 
     862                 :          0 :         cpt_inst_w5.s.gather_sz = gather_sz;
     863                 :          0 :         cpt_inst_w6.s.scatter_sz = scatter_sz;
     864                 :            : 
     865                 :          0 :         cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
     866                 :          0 :         cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
     867                 :            : 
     868                 :          0 :         inst->w5.u64 = cpt_inst_w5.u64;
     869                 :          0 :         inst->w6.u64 = cpt_inst_w6.u64;
     870                 :            : 
     871                 :          0 :         inst->w4.u64 = cpt_inst_w4.u64;
     872                 :            : 
     873   [ #  #  #  #  :          0 :         if (unlikely((scatter_sz >> 4) || (gather_sz >> 4))) {
                   #  # ]
     874                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
     875                 :            :                 ret = -1;
     876                 :            :         }
     877                 :            : 
     878                 :            :         return ret;
     879                 :            : }
     880                 :            : 
     881                 :            : static inline int
     882                 :          0 : pdcp_chain_sg1_prep(struct roc_se_fc_params *params, struct roc_se_ctx *cpt_ctx,
     883                 :            :                     struct cpt_inst_s *inst, union cpt_inst_w4 w4, int32_t inputlen,
     884                 :            :                     uint8_t hdr_len, uint64_t offset_ctrl, uint32_t req_flags,
     885                 :            :                     const uint8_t *cipher_iv, const uint8_t *auth_iv, const bool pack_iv,
     886                 :            :                     const uint8_t pdcp_ci_alg, const uint8_t pdcp_auth_alg)
     887                 :            : {
     888                 :            :         struct roc_sglist_comp *scatter_comp, *gather_comp;
     889                 :          0 :         void *m_vaddr = params->meta_buf.vaddr;
     890                 :            :         uint32_t i, g_size_bytes, s_size_bytes;
     891                 :            :         const uint32_t mac_len = 4;
     892                 :            :         uint8_t *iv_d, *in_buffer;
     893                 :            :         uint64_t *offset_vaddr;
     894                 :            :         uint32_t size;
     895                 :            :         int ret = 0;
     896                 :            : 
     897                 :            :         /* save space for IV */
     898                 :            :         offset_vaddr = m_vaddr;
     899                 :            : 
     900                 :          0 :         m_vaddr = PLT_PTR_ADD(m_vaddr, ROC_SE_OFF_CTRL_LEN + PLT_ALIGN_CEIL(hdr_len, 8));
     901                 :            : 
     902                 :          0 :         w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
     903                 :            : 
     904                 :            :         /* DPTR has SG list */
     905                 :            :         in_buffer = m_vaddr;
     906                 :            : 
     907                 :          0 :         ((uint16_t *)in_buffer)[0] = 0;
     908                 :          0 :         ((uint16_t *)in_buffer)[1] = 0;
     909                 :            : 
     910                 :          0 :         gather_comp = PLT_PTR_ADD(m_vaddr, 8);
     911                 :            : 
     912                 :            :         /* Input Gather List */
     913                 :            :         i = 0;
     914                 :            : 
     915                 :            :         /* Offset control word followed by IV */
     916                 :            : 
     917         [ #  # ]:          0 :         i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr, ROC_SE_OFF_CTRL_LEN + hdr_len);
     918                 :            : 
     919                 :          0 :         *(uint64_t *)offset_vaddr = offset_ctrl;
     920                 :            : 
     921                 :            :         /* Cipher IV */
     922                 :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
     923                 :          0 :         pdcp_iv_copy(iv_d, cipher_iv, pdcp_ci_alg, pack_iv);
     924                 :            : 
     925                 :            :         /* Auth IV */
     926                 :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN + params->pdcp_iv_offset);
     927                 :          0 :         pdcp_iv_copy(iv_d, auth_iv, pdcp_auth_alg, pack_iv);
     928                 :            : 
     929                 :            :         /* input data */
     930                 :          0 :         size = inputlen - hdr_len;
     931         [ #  # ]:          0 :         if (size) {
     932                 :          0 :                 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0, &size, NULL, 0);
     933         [ #  # ]:          0 :                 if (unlikely(size)) {
     934                 :          0 :                         plt_dp_err("Insufficient buffer space, size %d needed", size);
     935                 :          0 :                         return -1;
     936                 :            :                 }
     937                 :            :         }
     938         [ #  # ]:          0 :         ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
     939                 :          0 :         g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     940                 :            : 
     941                 :            :         /*
     942                 :            :          * Output Scatter List
     943                 :            :          */
     944                 :            : 
     945                 :            :         i = 0;
     946                 :          0 :         scatter_comp = PLT_PTR_ADD(gather_comp, g_size_bytes);
     947                 :            : 
     948         [ #  # ]:          0 :         if ((hdr_len)) {
     949         [ #  # ]:          0 :                 i = fill_sg_comp(scatter_comp, i, (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN,
     950                 :            :                                  hdr_len);
     951                 :            :         }
     952                 :            : 
     953                 :            :         /* Add output data */
     954   [ #  #  #  # ]:          0 :         if (cpt_ctx->ciph_then_auth && (req_flags & ROC_SE_VALID_MAC_BUF))
     955                 :          0 :                 size = inputlen;
     956                 :            :         else
     957                 :            :                 /* Output including mac */
     958                 :          0 :                 size = inputlen + mac_len;
     959                 :            : 
     960                 :          0 :         size -= hdr_len;
     961                 :            : 
     962         [ #  # ]:          0 :         if (size) {
     963                 :          0 :                 i = fill_sg_comp_from_iov(scatter_comp, i, params->dst_iov, 0, &size, NULL, 0);
     964                 :            : 
     965         [ #  # ]:          0 :                 if (unlikely(size)) {
     966                 :          0 :                         plt_dp_err("Insufficient buffer space, size %d needed", size);
     967                 :          0 :                         return -1;
     968                 :            :                 }
     969                 :            :         }
     970                 :            : 
     971         [ #  # ]:          0 :         ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
     972                 :          0 :         s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
     973                 :            : 
     974                 :          0 :         size = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
     975                 :            : 
     976         [ #  # ]:          0 :         if (unlikely(size > ROC_SG_MAX_DLEN_SIZE)) {
     977                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
     978                 :            :                 ret = -1;
     979                 :            :         }
     980                 :            : 
     981                 :            :         /* This is DPTR len in case of SG mode */
     982                 :          0 :         w4.s.dlen = size;
     983                 :          0 :         inst->w4.u64 = w4.u64;
     984                 :            : 
     985                 :          0 :         inst->dptr = (uint64_t)in_buffer;
     986                 :            : 
     987                 :          0 :         return ret;
     988                 :            : }
     989                 :            : 
     990                 :            : static inline int
     991                 :          0 : pdcp_chain_sg2_prep(struct roc_se_fc_params *params, struct roc_se_ctx *cpt_ctx,
     992                 :            :                     struct cpt_inst_s *inst, union cpt_inst_w4 w4, int32_t inputlen,
     993                 :            :                     uint8_t hdr_len, uint64_t offset_ctrl, uint32_t req_flags,
     994                 :            :                     const uint8_t *cipher_iv, const uint8_t *auth_iv, const bool pack_iv,
     995                 :            :                     const uint8_t pdcp_ci_alg, const uint8_t pdcp_auth_alg)
     996                 :            : {
     997                 :            :         struct roc_sg2list_comp *gather_comp, *scatter_comp;
     998                 :          0 :         void *m_vaddr = params->meta_buf.vaddr;
     999                 :            :         uint16_t scatter_sz, gather_sz;
    1000                 :            :         const uint32_t mac_len = 4;
    1001                 :            :         uint32_t i, g_size_bytes;
    1002                 :            :         uint64_t *offset_vaddr;
    1003                 :            :         union cpt_inst_w5 w5;
    1004                 :            :         union cpt_inst_w6 w6;
    1005                 :            :         uint8_t *iv_d;
    1006                 :            :         uint32_t size;
    1007                 :            :         int ret = 0;
    1008                 :            : 
    1009                 :            :         /* save space for IV */
    1010                 :            :         offset_vaddr = m_vaddr;
    1011                 :            : 
    1012                 :          0 :         m_vaddr = PLT_PTR_ADD(m_vaddr, ROC_SE_OFF_CTRL_LEN + RTE_ALIGN_CEIL(hdr_len, 8));
    1013                 :            : 
    1014                 :          0 :         w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
    1015                 :          0 :         w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1016                 :            : 
    1017                 :            :         gather_comp = m_vaddr;
    1018                 :            : 
    1019                 :            :         /* Input Gather List */
    1020                 :            :         i = 0;
    1021                 :            : 
    1022                 :            :         /* Offset control word followed by IV */
    1023                 :          0 :         *(uint64_t *)offset_vaddr = offset_ctrl;
    1024                 :            : 
    1025                 :          0 :         i = fill_sg2_comp(gather_comp, i, (uint64_t)offset_vaddr, ROC_SE_OFF_CTRL_LEN + hdr_len);
    1026                 :            : 
    1027                 :            :         /* Cipher IV */
    1028                 :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
    1029                 :          0 :         pdcp_iv_copy(iv_d, cipher_iv, pdcp_ci_alg, pack_iv);
    1030                 :            : 
    1031                 :            :         /* Auth IV */
    1032                 :          0 :         iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN + params->pdcp_iv_offset);
    1033                 :          0 :         pdcp_iv_copy(iv_d, auth_iv, pdcp_auth_alg, pack_iv);
    1034                 :            : 
    1035                 :            :         /* input data */
    1036                 :          0 :         size = inputlen - hdr_len;
    1037         [ #  # ]:          0 :         if (size) {
    1038                 :          0 :                 i = fill_sg2_comp_from_iov(gather_comp, i, params->src_iov, 0, &size, NULL, 0);
    1039         [ #  # ]:          0 :                 if (unlikely(size)) {
    1040                 :          0 :                         plt_dp_err("Insufficient buffer space, size %d needed", size);
    1041                 :          0 :                         return -1;
    1042                 :            :                 }
    1043                 :            :         }
    1044                 :            : 
    1045                 :          0 :         gather_sz = (i + 2) / 3;
    1046                 :          0 :         g_size_bytes = gather_sz * sizeof(struct roc_sg2list_comp);
    1047                 :            : 
    1048                 :            :         /*
    1049                 :            :          * Output Scatter List
    1050                 :            :          */
    1051                 :            : 
    1052                 :            :         i = 0;
    1053                 :          0 :         scatter_comp = PLT_PTR_ADD(gather_comp, g_size_bytes);
    1054                 :            : 
    1055         [ #  # ]:          0 :         if ((hdr_len))
    1056                 :          0 :                 i = fill_sg2_comp(scatter_comp, i, (uint64_t)(offset_vaddr) + ROC_SE_OFF_CTRL_LEN,
    1057                 :            :                                   hdr_len);
    1058                 :            : 
    1059                 :            :         /* Add output data */
    1060   [ #  #  #  # ]:          0 :         if (cpt_ctx->ciph_then_auth && (req_flags & ROC_SE_VALID_MAC_BUF))
    1061                 :          0 :                 size = inputlen;
    1062                 :            :         else
    1063                 :            :                 /* Output including mac */
    1064                 :          0 :                 size = inputlen + mac_len;
    1065                 :            : 
    1066                 :          0 :         size -= hdr_len;
    1067                 :            : 
    1068         [ #  # ]:          0 :         if (size) {
    1069                 :          0 :                 i = fill_sg2_comp_from_iov(scatter_comp, i, params->dst_iov, 0, &size, NULL, 0);
    1070                 :            : 
    1071         [ #  # ]:          0 :                 if (unlikely(size)) {
    1072                 :          0 :                         plt_dp_err("Insufficient buffer space, size %d needed", size);
    1073                 :          0 :                         return -1;
    1074                 :            :                 }
    1075                 :            :         }
    1076                 :            : 
    1077                 :          0 :         scatter_sz = (i + 2) / 3;
    1078                 :            : 
    1079                 :          0 :         w5.s.gather_sz = gather_sz;
    1080                 :          0 :         w6.s.scatter_sz = scatter_sz;
    1081                 :            : 
    1082                 :          0 :         w5.s.dptr = (uint64_t)gather_comp;
    1083                 :          0 :         w6.s.rptr = (uint64_t)scatter_comp;
    1084                 :            : 
    1085                 :          0 :         inst->w4.u64 = w4.u64;
    1086                 :          0 :         inst->w5.u64 = w5.u64;
    1087                 :          0 :         inst->w6.u64 = w6.u64;
    1088                 :            : 
    1089   [ #  #  #  # ]:          0 :         if (unlikely((scatter_sz >> 4) || (gather_sz >> 4))) {
    1090                 :          0 :                 plt_dp_err("Exceeds max supported components. Reduce segments");
    1091                 :            :                 ret = -1;
    1092                 :            :         }
    1093                 :            : 
    1094                 :            :         return ret;
    1095                 :            : }
    1096                 :            : 
    1097                 :            : static __rte_always_inline int
    1098                 :            : cpt_sm_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens, struct roc_se_fc_params *fc_params,
    1099                 :            :             struct cpt_inst_s *inst, const bool is_sg_ver2, int decrypt)
    1100                 :            : {
    1101                 :            :         int32_t inputlen, outputlen, enc_dlen;
    1102                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1103                 :            :         uint32_t passthr_len, pad_len;
    1104                 :            :         uint32_t passthrough_len = 0;
    1105                 :            :         const uint8_t *src = NULL;
    1106                 :            :         struct roc_se_ctx *se_ctx;
    1107                 :            :         uint32_t encr_data_len;
    1108                 :            :         uint32_t encr_offset;
    1109                 :            :         uint64_t offset_ctrl;
    1110                 :            :         uint8_t iv_len = 16;
    1111                 :            :         void *offset_vaddr;
    1112                 :            :         int ret;
    1113                 :            : 
    1114                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
    1115                 :            :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1116                 :            : 
    1117                 :            :         se_ctx = fc_params->ctx;
    1118                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1119                 :            : 
    1120   [ #  #  #  # ]:          0 :         if (unlikely(!(flags & ROC_SE_VALID_IV_BUF)))
    1121                 :            :                 iv_len = 0;
    1122                 :            : 
    1123                 :          0 :         passthr_len = encr_offset + iv_len;
    1124                 :          0 :         passthr_len = RTE_ALIGN_CEIL(passthr_len, 8);
    1125                 :          0 :         pad_len = passthr_len - encr_offset - iv_len;
    1126                 :          0 :         enc_dlen = RTE_ALIGN_CEIL(encr_data_len, 8) + passthr_len;
    1127                 :            : 
    1128                 :            :         inputlen = enc_dlen;
    1129                 :            :         outputlen = enc_dlen;
    1130                 :            : 
    1131                 :          0 :         cpt_inst_w4.s.param1 = encr_data_len;
    1132                 :            : 
    1133                 :          0 :         offset_ctrl = passthr_len & 0xff;
    1134   [ #  #  #  # ]:          0 :         offset_ctrl = rte_cpu_to_be_64(offset_ctrl);
    1135                 :            : 
    1136                 :            :         /*
    1137                 :            :          * In cn9k, cn10k since we have a limitation of
    1138                 :            :          * IV & Offset control word not part of instruction
    1139                 :            :          * and need to be part of Data Buffer, we check if
    1140                 :            :          * head room is there and then only do the Direct mode processing
    1141                 :            :          */
    1142   [ #  #  #  # ]:          0 :         if (likely((flags & ROC_SE_SINGLE_BUF_INPLACE) && (flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
    1143                 :            :                 void *dm_vaddr = fc_params->bufs[0].vaddr;
    1144                 :            : 
    1145                 :            :                 /* Use Direct mode */
    1146                 :            : 
    1147                 :          0 :                 offset_vaddr = PLT_PTR_SUB(dm_vaddr, ROC_SE_OFF_CTRL_LEN + pad_len + iv_len);
    1148                 :          0 :                 *(uint64_t *)offset_vaddr = offset_ctrl;
    1149                 :            : 
    1150                 :            :                 /* DPTR */
    1151                 :          0 :                 inst->dptr = (uint64_t)offset_vaddr;
    1152                 :            : 
    1153                 :            :                 /* RPTR should just exclude offset control word */
    1154                 :          0 :                 inst->rptr = (uint64_t)dm_vaddr - iv_len - pad_len;
    1155                 :            : 
    1156                 :          0 :                 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1157                 :            : 
    1158   [ #  #  #  # ]:          0 :                 if (likely(iv_len)) {
    1159   [ #  #  #  # ]:          0 :                         void *dst = PLT_PTR_ADD(offset_vaddr, ROC_SE_OFF_CTRL_LEN);
    1160                 :            :                         const uint64_t *src = fc_params->iv_buf;
    1161                 :            : 
    1162                 :            :                         rte_memcpy(dst, src, 16);
    1163                 :            :                 }
    1164                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1165                 :            :         } else {
    1166   [ #  #  #  # ]:          0 :                 if (likely(iv_len))
    1167                 :            :                         src = fc_params->iv_buf;
    1168                 :            : 
    1169                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1170                 :            : 
    1171   [ #  #  #  # ]:          0 :                 if (is_sg_ver2)
    1172   [ #  #  #  # ]:          0 :                         ret = sg2_inst_prep(fc_params, inst, offset_ctrl, src, iv_len + pad_len, 0,
    1173                 :            :                                             0, inputlen, outputlen, passthrough_len, flags, 0,
    1174                 :            :                                             decrypt);
    1175                 :            :                 else
    1176   [ #  #  #  # ]:          0 :                         ret = sg_inst_prep(fc_params, inst, offset_ctrl, src, iv_len + pad_len, 0,
    1177                 :            :                                            0, inputlen, outputlen, passthrough_len, flags, 0,
    1178                 :            :                                            decrypt);
    1179                 :            : 
    1180   [ #  #  #  # ]:          0 :                 if (unlikely(ret)) {
    1181                 :          0 :                         plt_dp_err("sg prep failed");
    1182                 :          0 :                         return -1;
    1183                 :            :                 }
    1184                 :            :         }
    1185                 :            : 
    1186                 :            :         return 0;
    1187                 :            : }
    1188                 :            : 
    1189                 :            : static __rte_always_inline int
    1190                 :            : cpt_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
    1191                 :            :                   struct roc_se_fc_params *fc_params, struct cpt_inst_s *inst,
    1192                 :            :                   const bool is_sg_ver2)
    1193                 :            : {
    1194                 :            :         uint32_t encr_data_len, auth_data_len, aad_len = 0;
    1195                 :            :         uint32_t encr_offset, auth_offset, iv_offset = 0;
    1196                 :            :         int32_t inputlen, outputlen, enc_dlen, auth_dlen;
    1197                 :            :         uint32_t cipher_type, hash_type;
    1198                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1199                 :            :         uint32_t passthrough_len = 0;
    1200                 :            :         const uint8_t *src = NULL;
    1201                 :            :         struct roc_se_ctx *se_ctx;
    1202                 :            :         uint64_t offset_ctrl;
    1203                 :            :         uint8_t iv_len = 16;
    1204                 :            :         void *offset_vaddr;
    1205                 :            :         uint8_t op_minor;
    1206                 :            :         uint32_t mac_len;
    1207                 :            :         int ret;
    1208                 :            : 
    1209                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
    1210                 :          0 :         auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
    1211                 :          0 :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1212                 :          0 :         auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
    1213   [ #  #  #  #  :          0 :         if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
                   #  # ]
    1214                 :            :                 /* We don't support both AAD and auth data separately */
    1215                 :            :                 auth_data_len = 0;
    1216                 :            :                 auth_offset = 0;
    1217                 :          0 :                 aad_len = fc_params->aad_buf.size;
    1218                 :            :         }
    1219                 :            : 
    1220                 :            :         se_ctx = fc_params->ctx;
    1221                 :          0 :         cipher_type = se_ctx->enc_cipher;
    1222                 :          0 :         hash_type = se_ctx->hash_type;
    1223                 :          0 :         mac_len = se_ctx->mac_len;
    1224                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1225                 :            :         op_minor = cpt_inst_w4.s.opcode_minor;
    1226                 :            : 
    1227   [ #  #  #  #  :          0 :         if (unlikely(!(flags & ROC_SE_VALID_IV_BUF))) {
          #  #  #  #  #  
                #  #  # ]
    1228                 :            :                 iv_len = 0;
    1229                 :          0 :                 iv_offset = ROC_SE_ENCR_IV_OFFSET(d_offs);
    1230                 :            :         }
    1231                 :            : 
    1232   [ #  #  #  #  :          0 :         if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
                   #  # ]
    1233                 :            :                 /*
    1234                 :            :                  * When AAD is given, data above encr_offset is pass through
    1235                 :            :                  * Since AAD is given as separate pointer and not as offset,
    1236                 :            :                  * this is a special case as we need to fragment input data
    1237                 :            :                  * into passthrough + encr_data and then insert AAD in between.
    1238                 :            :                  */
    1239   [ #  #  #  #  :          0 :                 if (hash_type != ROC_SE_GMAC_TYPE) {
                   #  # ]
    1240                 :            :                         passthrough_len = encr_offset;
    1241                 :          0 :                         auth_offset = passthrough_len + iv_len;
    1242                 :          0 :                         encr_offset = passthrough_len + aad_len + iv_len;
    1243                 :          0 :                         auth_data_len = aad_len + encr_data_len;
    1244                 :            :                 } else {
    1245                 :          0 :                         passthrough_len = 16 + aad_len;
    1246                 :          0 :                         auth_offset = passthrough_len + iv_len;
    1247                 :            :                         auth_data_len = aad_len;
    1248                 :            :                 }
    1249                 :            :         } else {
    1250                 :          0 :                 encr_offset += iv_len;
    1251                 :          0 :                 auth_offset += iv_len;
    1252                 :            :         }
    1253                 :            : 
    1254                 :            :         /* Encryption */
    1255                 :          0 :         cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_FC;
    1256                 :            :         cpt_inst_w4.s.opcode_minor |= ROC_SE_FC_MINOR_OP_ENCRYPT;
    1257                 :            : 
    1258   [ #  #  #  #  :          0 :         if (hash_type == ROC_SE_GMAC_TYPE) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1259                 :            :                 encr_offset = 0;
    1260                 :            :                 encr_data_len = 0;
    1261                 :            :         }
    1262                 :            : 
    1263                 :          0 :         auth_dlen = auth_offset + auth_data_len;
    1264                 :          0 :         enc_dlen = encr_data_len + encr_offset;
    1265   [ #  #  #  #  :          0 :         if (unlikely(encr_data_len & 0xf)) {
          #  #  #  #  #  
                #  #  # ]
    1266   [ #  #  #  #  :          0 :                 if ((cipher_type == ROC_SE_DES3_CBC) ||
          #  #  #  #  #  
                #  #  # ]
    1267                 :            :                     (cipher_type == ROC_SE_DES3_ECB))
    1268                 :          0 :                         enc_dlen =
    1269                 :          0 :                                 RTE_ALIGN_CEIL(encr_data_len, 8) + encr_offset;
    1270   [ #  #  #  #  :          0 :                 else if (likely((cipher_type == ROC_SE_AES_CBC) ||
          #  #  #  #  #  
                #  #  # ]
    1271                 :            :                                 (cipher_type == ROC_SE_AES_ECB)))
    1272                 :          0 :                         enc_dlen =
    1273                 :          0 :                                 RTE_ALIGN_CEIL(encr_data_len, 8) + encr_offset;
    1274                 :            :         }
    1275                 :            : 
    1276   [ #  #  #  #  :          0 :         if (unlikely(auth_dlen > enc_dlen)) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1277                 :            :                 inputlen = auth_dlen;
    1278                 :          0 :                 outputlen = auth_dlen + mac_len;
    1279                 :            :         } else {
    1280                 :            :                 inputlen = enc_dlen;
    1281                 :          0 :                 outputlen = enc_dlen + mac_len;
    1282                 :            :         }
    1283                 :            : 
    1284   [ #  #  #  #  :          0 :         if (op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1285                 :            :                 outputlen = enc_dlen;
    1286                 :            : 
    1287                 :          0 :         cpt_inst_w4.s.param1 = encr_data_len;
    1288                 :          0 :         cpt_inst_w4.s.param2 = auth_data_len;
    1289                 :            : 
    1290   [ #  #  #  #  :          0 :         if (unlikely((encr_offset >> 16) || (iv_offset >> 8) || (auth_offset >> 8))) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1291                 :          0 :                 plt_dp_err("Offset not supported");
    1292                 :          0 :                 plt_dp_err("enc_offset: %d", encr_offset);
    1293                 :          0 :                 plt_dp_err("iv_offset : %d", iv_offset);
    1294                 :          0 :                 plt_dp_err("auth_offset: %d", auth_offset);
    1295                 :          0 :                 return -1;
    1296                 :            :         }
    1297                 :            : 
    1298   [ #  #  #  #  :          0 :         offset_ctrl = rte_cpu_to_be_64(((uint64_t)encr_offset << 16) | ((uint64_t)iv_offset << 8) |
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1299                 :            :                                        ((uint64_t)auth_offset));
    1300                 :            : 
    1301                 :            :         /*
    1302                 :            :          * In cn9k, cn10k since we have a limitation of
    1303                 :            :          * IV & Offset control word not part of instruction
    1304                 :            :          * and need to be part of Data Buffer, we check if
    1305                 :            :          * head room is there and then only do the Direct mode processing
    1306                 :            :          */
    1307   [ #  #  #  #  :          0 :         if (likely((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
             #  #  #  # ]
    1308                 :            :                    (flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
    1309                 :          0 :                 void *dm_vaddr = fc_params->bufs[0].vaddr;
    1310                 :            : 
    1311                 :            :                 /* Use Direct mode */
    1312                 :            : 
    1313                 :          0 :                 offset_vaddr = (uint8_t *)dm_vaddr - ROC_SE_OFF_CTRL_LEN - iv_len;
    1314                 :            : 
    1315                 :          0 :                 *(uint64_t *)offset_vaddr =
    1316   [ #  #  #  #  :          0 :                         rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
             #  #  #  # ]
    1317                 :            :                                          ((uint64_t)iv_offset << 8) | ((uint64_t)auth_offset));
    1318                 :            : 
    1319                 :            :                 /* DPTR */
    1320                 :          0 :                 inst->dptr = (uint64_t)offset_vaddr;
    1321                 :            : 
    1322                 :            :                 /* RPTR should just exclude offset control word */
    1323                 :          0 :                 inst->rptr = (uint64_t)dm_vaddr - iv_len;
    1324                 :            : 
    1325                 :          0 :                 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1326                 :            : 
    1327   [ #  #  #  #  :          0 :                 if (likely(iv_len)) {
             #  #  #  # ]
    1328                 :            :                         uint64_t *dest =
    1329                 :            :                                 (uint64_t *)((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
    1330                 :          0 :                         const uint64_t *src = fc_params->iv_buf;
    1331                 :          0 :                         dest[0] = src[0];
    1332                 :          0 :                         dest[1] = src[1];
    1333                 :            :                 }
    1334                 :            : 
    1335                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1336                 :            :         } else {
    1337   [ #  #  #  #  :          0 :                 if (likely(iv_len))
          #  #  #  #  #  
                #  #  # ]
    1338                 :          0 :                         src = fc_params->iv_buf;
    1339                 :            : 
    1340   [ #  #  #  # ]:          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1341                 :            : 
    1342   [ #  #  #  #  :          0 :                 if (is_sg_ver2)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1343   [ #  #  #  #  :          0 :                         ret = sg2_inst_prep(fc_params, inst, offset_ctrl, src, iv_len, 0, 0,
          #  #  #  #  #  
                #  #  # ]
    1344                 :            :                                             inputlen, outputlen, passthrough_len, flags, 0, 0);
    1345                 :            :                 else
    1346   [ #  #  #  #  :          0 :                         ret = sg_inst_prep(fc_params, inst, offset_ctrl, src, iv_len, 0, 0,
          #  #  #  #  #  
                #  #  # ]
    1347                 :            :                                            inputlen, outputlen, passthrough_len, flags, 0, 0);
    1348                 :            : 
    1349   [ #  #  #  #  :          0 :                 if (unlikely(ret)) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1350                 :          0 :                         plt_dp_err("sg prep failed");
    1351                 :          0 :                         return -1;
    1352                 :            :                 }
    1353                 :            :         }
    1354                 :            : 
    1355                 :            :         return 0;
    1356                 :            : }
    1357                 :            : 
    1358                 :            : static __rte_always_inline int
    1359                 :            : cpt_dec_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
    1360                 :            :                   struct roc_se_fc_params *fc_params, struct cpt_inst_s *inst,
    1361                 :            :                   const bool is_sg_ver2)
    1362                 :            : {
    1363                 :            :         uint32_t encr_data_len, auth_data_len, aad_len = 0;
    1364                 :            :         uint32_t encr_offset, auth_offset, iv_offset = 0;
    1365                 :            :         int32_t inputlen, outputlen, enc_dlen, auth_dlen;
    1366                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1367                 :            :         uint32_t passthrough_len = 0;
    1368                 :            :         int32_t hash_type, mac_len;
    1369                 :            :         const uint8_t *src = NULL;
    1370                 :            :         struct roc_se_ctx *se_ctx;
    1371                 :            :         uint64_t offset_ctrl;
    1372                 :            :         uint8_t iv_len = 16;
    1373                 :            :         void *offset_vaddr;
    1374                 :            :         uint8_t op_minor;
    1375                 :            :         int ret;
    1376                 :            : 
    1377                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
    1378                 :          0 :         auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
    1379                 :          0 :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1380                 :          0 :         auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
    1381                 :            : 
    1382   [ #  #  #  #  :          0 :         if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
                   #  # ]
    1383                 :            :                 /* We don't support both AAD and auth data separately */
    1384                 :            :                 auth_data_len = 0;
    1385                 :            :                 auth_offset = 0;
    1386                 :          0 :                 aad_len = fc_params->aad_buf.size;
    1387                 :            :         }
    1388                 :            : 
    1389                 :            :         se_ctx = fc_params->ctx;
    1390                 :          0 :         hash_type = se_ctx->hash_type;
    1391                 :          0 :         mac_len = se_ctx->mac_len;
    1392                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1393                 :            :         op_minor = cpt_inst_w4.s.opcode_minor;
    1394                 :            : 
    1395   [ #  #  #  #  :          0 :         if (unlikely(!(flags & ROC_SE_VALID_IV_BUF))) {
          #  #  #  #  #  
                #  #  # ]
    1396                 :            :                 iv_len = 0;
    1397                 :          0 :                 iv_offset = ROC_SE_ENCR_IV_OFFSET(d_offs);
    1398                 :            :         }
    1399                 :            : 
    1400   [ #  #  #  #  :          0 :         if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
                   #  # ]
    1401                 :            :                 /*
    1402                 :            :                  * When AAD is given, data above encr_offset is pass through
    1403                 :            :                  * Since AAD is given as separate pointer and not as offset,
    1404                 :            :                  * this is a special case as we need to fragment input data
    1405                 :            :                  * into passthrough + encr_data and then insert AAD in between.
    1406                 :            :                  */
    1407   [ #  #  #  #  :          0 :                 if (hash_type != ROC_SE_GMAC_TYPE) {
                   #  # ]
    1408                 :            :                         passthrough_len = encr_offset;
    1409                 :          0 :                         auth_offset = passthrough_len + iv_len;
    1410                 :          0 :                         encr_offset = passthrough_len + aad_len + iv_len;
    1411                 :          0 :                         auth_data_len = aad_len + encr_data_len;
    1412                 :            :                 } else {
    1413                 :          0 :                         passthrough_len = 16 + aad_len;
    1414                 :          0 :                         auth_offset = passthrough_len + iv_len;
    1415                 :            :                         auth_data_len = aad_len;
    1416                 :            :                 }
    1417                 :            :         } else {
    1418                 :          0 :                 encr_offset += iv_len;
    1419                 :          0 :                 auth_offset += iv_len;
    1420                 :            :         }
    1421                 :            : 
    1422                 :            :         /* Decryption */
    1423                 :          0 :         cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_FC;
    1424                 :            :         cpt_inst_w4.s.opcode_minor = ROC_SE_FC_MINOR_OP_DECRYPT;
    1425                 :          0 :         cpt_inst_w4.s.opcode_minor |= (uint64_t)op_minor;
    1426                 :            : 
    1427   [ #  #  #  #  :          0 :         if (hash_type == ROC_SE_GMAC_TYPE) {
          #  #  #  #  #  
                #  #  # ]
    1428                 :            :                 encr_offset = 0;
    1429                 :            :                 encr_data_len = 0;
    1430                 :            :         }
    1431                 :            : 
    1432                 :          0 :         enc_dlen = encr_offset + encr_data_len;
    1433                 :          0 :         auth_dlen = auth_offset + auth_data_len;
    1434                 :            : 
    1435   [ #  #  #  #  :          0 :         if (auth_dlen > enc_dlen) {
          #  #  #  #  #  
                #  #  # ]
    1436                 :          0 :                 inputlen = auth_dlen + mac_len;
    1437                 :            :                 outputlen = auth_dlen;
    1438                 :            :         } else {
    1439                 :          0 :                 inputlen = enc_dlen + mac_len;
    1440                 :            :                 outputlen = enc_dlen;
    1441                 :            :         }
    1442                 :            : 
    1443   [ #  #  #  #  :          0 :         if (op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST)
          #  #  #  #  #  
                #  #  # ]
    1444                 :            :                 outputlen = inputlen = enc_dlen;
    1445                 :            : 
    1446                 :          0 :         cpt_inst_w4.s.param1 = encr_data_len;
    1447                 :          0 :         cpt_inst_w4.s.param2 = auth_data_len;
    1448                 :            : 
    1449   [ #  #  #  #  :          0 :         if (unlikely((encr_offset >> 16) || (iv_offset >> 8) || (auth_offset >> 8))) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1450                 :          0 :                 plt_dp_err("Offset not supported");
    1451                 :          0 :                 plt_dp_err("enc_offset: %d", encr_offset);
    1452                 :          0 :                 plt_dp_err("iv_offset : %d", iv_offset);
    1453                 :          0 :                 plt_dp_err("auth_offset: %d", auth_offset);
    1454                 :          0 :                 return -1;
    1455                 :            :         }
    1456                 :            : 
    1457   [ #  #  #  #  :          0 :         offset_ctrl = rte_cpu_to_be_64(((uint64_t)encr_offset << 16) | ((uint64_t)iv_offset << 8) |
          #  #  #  #  #  
                #  #  # ]
    1458                 :            :                                        ((uint64_t)auth_offset));
    1459                 :            : 
    1460                 :            :         /*
    1461                 :            :          * In cn9k, cn10k since we have a limitation of
    1462                 :            :          * IV & Offset control word not part of instruction
    1463                 :            :          * and need to be part of Data Buffer, we check if
    1464                 :            :          * head room is there and then only do the Direct mode processing
    1465                 :            :          */
    1466   [ #  #  #  #  :          0 :         if (likely((flags & ROC_SE_SINGLE_BUF_INPLACE) && (flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
             #  #  #  # ]
    1467                 :          0 :                 void *dm_vaddr = fc_params->bufs[0].vaddr;
    1468                 :            : 
    1469                 :            :                 /* Use Direct mode */
    1470                 :            : 
    1471                 :          0 :                 offset_vaddr = (uint8_t *)dm_vaddr - ROC_SE_OFF_CTRL_LEN - iv_len;
    1472                 :            : 
    1473                 :          0 :                 *(uint64_t *)offset_vaddr =
    1474   [ #  #  #  #  :          0 :                         rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
             #  #  #  # ]
    1475                 :            :                                          ((uint64_t)iv_offset << 8) | ((uint64_t)auth_offset));
    1476                 :            : 
    1477                 :          0 :                 inst->dptr = (uint64_t)offset_vaddr;
    1478                 :            : 
    1479                 :            :                 /* RPTR should just exclude offset control word */
    1480                 :          0 :                 inst->rptr = (uint64_t)dm_vaddr - iv_len;
    1481                 :            : 
    1482                 :          0 :                 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1483                 :            : 
    1484   [ #  #  #  #  :          0 :                 if (likely(iv_len)) {
             #  #  #  # ]
    1485                 :            :                         uint64_t *dest =
    1486                 :            :                                 (uint64_t *)((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
    1487                 :          0 :                         const uint64_t *src = fc_params->iv_buf;
    1488                 :          0 :                         dest[0] = src[0];
    1489                 :          0 :                         dest[1] = src[1];
    1490                 :            :                 }
    1491                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1492                 :            : 
    1493                 :            :         } else {
    1494   [ #  #  #  #  :          0 :                 if (likely(iv_len)) {
          #  #  #  #  #  
                #  #  # ]
    1495                 :          0 :                         src = fc_params->iv_buf;
    1496                 :            :                 }
    1497                 :            : 
    1498                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1499                 :            : 
    1500   [ #  #  #  #  :          0 :                 if (is_sg_ver2)
          #  #  #  #  #  
                #  #  # ]
    1501   [ #  #  #  #  :          0 :                         ret = sg2_inst_prep(fc_params, inst, offset_ctrl, src, iv_len, 0, 0,
          #  #  #  #  #  
                #  #  # ]
    1502                 :            :                                             inputlen, outputlen, passthrough_len, flags, 0, 1);
    1503                 :            :                 else
    1504   [ #  #  #  #  :          0 :                         ret = sg_inst_prep(fc_params, inst, offset_ctrl, src, iv_len, 0, 0,
          #  #  #  #  #  
                #  #  # ]
    1505                 :            :                                            inputlen, outputlen, passthrough_len, flags, 0, 1);
    1506   [ #  #  #  #  :          0 :                 if (unlikely(ret)) {
          #  #  #  #  #  
                #  #  # ]
    1507                 :          0 :                         plt_dp_err("sg prep failed");
    1508                 :          0 :                         return -1;
    1509                 :            :                 }
    1510                 :            :         }
    1511                 :            : 
    1512                 :            :         return 0;
    1513                 :            : }
    1514                 :            : 
    1515                 :            : static __rte_always_inline int
    1516                 :            : cpt_pdcp_chain_alg_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
    1517                 :            :                         struct roc_se_fc_params *params, struct cpt_inst_s *inst,
    1518                 :            :                         const bool is_sg_ver2)
    1519                 :            : {
    1520                 :            :         uint32_t encr_data_len, auth_data_len, aad_len, passthr_len, pad_len, hdr_len;
    1521                 :            :         uint32_t encr_offset, auth_offset, iv_offset = 0;
    1522                 :            :         const uint8_t *auth_iv = NULL, *cipher_iv = NULL;
    1523                 :          0 :         uint8_t pdcp_iv_off = params->pdcp_iv_offset;
    1524                 :          0 :         const int iv_len = pdcp_iv_off * 2;
    1525                 :            :         uint8_t pdcp_ci_alg, pdcp_auth_alg;
    1526                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1527                 :            :         struct roc_se_ctx *se_ctx;
    1528                 :            :         uint64_t *offset_vaddr;
    1529                 :            :         uint64_t offset_ctrl;
    1530                 :            :         int32_t inputlen;
    1531                 :            :         void *dm_vaddr;
    1532                 :            :         uint8_t *iv_d;
    1533                 :            : 
    1534                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
    1535                 :          0 :         auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
    1536                 :            : 
    1537                 :          0 :         aad_len = encr_offset - auth_offset;
    1538                 :            : 
    1539                 :            :         if (unlikely(encr_offset >> 16)) {
    1540                 :            :                 plt_dp_err("Offset not supported");
    1541                 :            :                 plt_dp_err("enc_offset: %d", encr_offset);
    1542                 :            :                 return -1;
    1543                 :            :         }
    1544                 :            : 
    1545                 :          0 :         se_ctx = params->ctx;
    1546                 :          0 :         pdcp_ci_alg = se_ctx->pdcp_ci_alg;
    1547                 :          0 :         pdcp_auth_alg = se_ctx->pdcp_auth_alg;
    1548                 :            : 
    1549                 :          0 :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1550                 :          0 :         auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
    1551                 :          0 :         auth_data_len -= aad_len;
    1552                 :            : 
    1553                 :          0 :         encr_offset += iv_len;
    1554                 :          0 :         auth_offset = encr_offset - aad_len;
    1555                 :          0 :         passthr_len = RTE_ALIGN_CEIL(auth_offset, 8);
    1556                 :            : 
    1557   [ #  #  #  # ]:          0 :         if (unlikely((aad_len >> 16) || (passthr_len >> 8))) {
    1558                 :          0 :                 plt_dp_err("Length not supported");
    1559                 :          0 :                 plt_dp_err("AAD_len: %d", aad_len);
    1560                 :          0 :                 plt_dp_err("Passthrough_len: %d", passthr_len);
    1561                 :          0 :                 return -1;
    1562                 :            :         }
    1563                 :            : 
    1564                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1565                 :          0 :         cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_PDCP_CHAIN;
    1566                 :            : 
    1567                 :          0 :         cpt_inst_w4.s.param1 = auth_data_len;
    1568                 :          0 :         cpt_inst_w4.s.param2 = 0;
    1569                 :            : 
    1570   [ #  #  #  # ]:          0 :         if (likely(params->auth_iv_len))
    1571                 :          0 :                 auth_iv = params->auth_iv_buf;
    1572                 :            : 
    1573   [ #  #  #  # ]:          0 :         if (likely(params->cipher_iv_len))
    1574                 :          0 :                 cipher_iv = params->iv_buf;
    1575                 :            : 
    1576                 :          0 :         pad_len = passthr_len - auth_offset;
    1577                 :          0 :         hdr_len = iv_len + pad_len;
    1578                 :            : 
    1579   [ #  #  #  # ]:          0 :         if (se_ctx->auth_then_ciph)
    1580                 :          0 :                 inputlen = auth_data_len;
    1581                 :            :         else
    1582                 :          0 :                 inputlen = encr_data_len;
    1583                 :            : 
    1584                 :          0 :         inputlen += (encr_offset + pad_len);
    1585                 :            : 
    1586   [ #  #  #  # ]:          0 :         offset_ctrl = rte_cpu_to_be_64(((uint64_t)(aad_len) << 16) | ((uint64_t)(iv_offset) << 8) |
    1587                 :            :                                        ((uint64_t)(passthr_len)));
    1588                 :            : 
    1589   [ #  #  #  # ]:          0 :         if (likely(((req_flags & ROC_SE_SINGLE_BUF_INPLACE)) &&
    1590                 :            :                    ((req_flags & ROC_SE_SINGLE_BUF_HEADROOM)))) {
    1591                 :            : 
    1592                 :          0 :                 dm_vaddr = params->bufs[0].vaddr;
    1593                 :            : 
    1594                 :            :                 /* Use Direct mode */
    1595                 :            : 
    1596                 :          0 :                 offset_vaddr = PLT_PTR_SUB(dm_vaddr, ROC_SE_OFF_CTRL_LEN + hdr_len);
    1597                 :          0 :                 *offset_vaddr = offset_ctrl;
    1598                 :            : 
    1599                 :            :                 /* DPTR */
    1600                 :          0 :                 inst->dptr = (uint64_t)offset_vaddr;
    1601                 :            :                 /* RPTR should just exclude offset control word */
    1602                 :          0 :                 inst->rptr = (uint64_t)PLT_PTR_SUB(dm_vaddr, hdr_len);
    1603                 :            : 
    1604                 :          0 :                 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1605                 :            : 
    1606                 :          0 :                 iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
    1607                 :          0 :                 pdcp_iv_copy(iv_d, cipher_iv, pdcp_ci_alg, false);
    1608                 :            : 
    1609                 :          0 :                 iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN + pdcp_iv_off);
    1610                 :          0 :                 pdcp_iv_copy(iv_d, auth_iv, pdcp_auth_alg, false);
    1611                 :            : 
    1612                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1613                 :          0 :                 return 0;
    1614                 :            : 
    1615                 :            :         } else {
    1616   [ #  #  #  # ]:          0 :                 if (is_sg_ver2)
    1617                 :          0 :                         return pdcp_chain_sg2_prep(params, se_ctx, inst, cpt_inst_w4, inputlen,
    1618                 :            :                                                    hdr_len, offset_ctrl, req_flags, cipher_iv,
    1619                 :            :                                                    auth_iv, false, pdcp_ci_alg, pdcp_auth_alg);
    1620                 :            :                 else
    1621                 :          0 :                         return pdcp_chain_sg1_prep(params, se_ctx, inst, cpt_inst_w4, inputlen,
    1622                 :            :                                                    hdr_len, offset_ctrl, req_flags, cipher_iv,
    1623                 :            :                                                    auth_iv, false, pdcp_ci_alg, pdcp_auth_alg);
    1624                 :            :         }
    1625                 :            : }
    1626                 :            : 
    1627                 :            : static __rte_always_inline int
    1628                 :            : cpt_pdcp_alg_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
    1629                 :            :                   struct roc_se_fc_params *params, struct cpt_inst_s *inst, const bool is_sg_ver2)
    1630                 :            : {
    1631                 :            :         /*
    1632                 :            :          * pdcp_iv_offset is auth_iv_offset wrt cipher_iv_offset which is
    1633                 :            :          * 16 with old microcode without ZUC 256 support
    1634                 :            :          * whereas it is 24 with new microcode which has ZUC 256.
    1635                 :            :          * So iv_len reserved is 32B for cipher and auth IVs with old microcode
    1636                 :            :          * and 48B with new microcode.
    1637                 :            :          */
    1638                 :          0 :         const int iv_len = params->pdcp_iv_offset * 2;
    1639                 :            :         struct roc_se_ctx *se_ctx = params->ctx;
    1640                 :            :         uint32_t encr_data_len, auth_data_len;
    1641                 :          0 :         const int flags = se_ctx->zsk_flags;
    1642                 :            :         uint32_t encr_offset, auth_offset;
    1643                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1644                 :            :         int32_t inputlen, outputlen;
    1645                 :            :         uint64_t *offset_vaddr;
    1646                 :            :         uint8_t pdcp_alg_type;
    1647                 :            :         uint32_t mac_len = 0;
    1648                 :            :         uint64_t offset_ctrl;
    1649                 :            :         bool pack_iv = false;
    1650                 :            :         const uint8_t *iv_s;
    1651                 :            :         int ret;
    1652                 :            : 
    1653                 :          0 :         mac_len = se_ctx->mac_len;
    1654                 :            : 
    1655                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1656                 :            : 
    1657   [ #  #  #  #  :          0 :         if (flags == 0x1) {
          #  #  #  #  #  
                      # ]
    1658                 :          0 :                 cpt_inst_w4.s.opcode_minor = 1;
    1659                 :          0 :                 iv_s = params->auth_iv_buf;
    1660                 :            : 
    1661                 :            :                 /*
    1662                 :            :                  * Microcode expects offsets in bytes
    1663                 :            :                  * TODO: Rounding off
    1664                 :            :                  */
    1665                 :            :                 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
    1666                 :          0 :                 auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
    1667                 :          0 :                 pdcp_alg_type = se_ctx->pdcp_auth_alg;
    1668                 :            : 
    1669   [ #  #  #  #  :          0 :                 if (pdcp_alg_type != ROC_SE_PDCP_ALG_TYPE_AES_CMAC) {
                   #  # ]
    1670                 :            : 
    1671   [ #  #  #  # ]:          0 :                         if (params->auth_iv_len == 25)
    1672                 :            :                                 pack_iv = true;
    1673                 :            : 
    1674                 :          0 :                         auth_offset = auth_offset / 8;
    1675                 :          0 :                         auth_data_len = RTE_ALIGN(auth_data_len, 8) / 8;
    1676                 :            :                 }
    1677                 :            : 
    1678                 :            :                 /* consider iv len */
    1679                 :          0 :                 auth_offset += iv_len;
    1680                 :            : 
    1681                 :          0 :                 inputlen = auth_offset + auth_data_len;
    1682                 :          0 :                 outputlen = iv_len + mac_len;
    1683                 :            : 
    1684   [ #  #  #  #  :          0 :                 offset_ctrl = rte_cpu_to_be_64((uint64_t)auth_offset);
          #  #  #  #  #  
                      # ]
    1685                 :          0 :                 cpt_inst_w4.s.param1 = auth_data_len;
    1686                 :            : 
    1687                 :            :                 encr_data_len = 0;
    1688                 :            :                 encr_offset = 0;
    1689                 :            :         } else {
    1690                 :          0 :                 cpt_inst_w4.s.opcode_minor = 0;
    1691                 :            :                 iv_s = params->iv_buf;
    1692                 :          0 :                 pdcp_alg_type = se_ctx->pdcp_ci_alg;
    1693                 :            : 
    1694   [ #  #  #  # ]:          0 :                 if (params->cipher_iv_len == 25)
    1695                 :            :                         pack_iv = true;
    1696                 :            : 
    1697                 :            :                 /*
    1698                 :            :                  * Microcode expects offsets in bytes
    1699                 :            :                  * TODO: Rounding off
    1700                 :            :                  */
    1701                 :            :                 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1702                 :            : 
    1703                 :          0 :                 encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
    1704                 :          0 :                 encr_offset = encr_offset / 8;
    1705                 :            : 
    1706                 :            :                 /* consider iv len */
    1707                 :          0 :                 encr_offset += iv_len;
    1708                 :            : 
    1709                 :          0 :                 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
    1710                 :            :                 outputlen = inputlen;
    1711                 :            : 
    1712                 :            :                 /* iv offset is 0 */
    1713   [ #  #  #  #  :          0 :                 offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset);
             #  #  #  # ]
    1714                 :            : 
    1715                 :            :                 auth_data_len = 0;
    1716                 :            :                 auth_offset = 0;
    1717                 :          0 :                 cpt_inst_w4.s.param1 = (RTE_ALIGN(encr_data_len, 8) / 8);
    1718                 :            :         }
    1719                 :            : 
    1720   [ #  #  #  #  :          0 :         if (unlikely((encr_offset >> 16) || (auth_offset >> 8))) {
          #  #  #  #  #  
                      # ]
    1721                 :          0 :                 plt_dp_err("Offset not supported");
    1722                 :          0 :                 plt_dp_err("enc_offset: %d", encr_offset);
    1723                 :          0 :                 plt_dp_err("auth_offset: %d", auth_offset);
    1724                 :          0 :                 return -1;
    1725                 :            :         }
    1726                 :            : 
    1727                 :            :         /*
    1728                 :            :          * In cn9k, cn10k since we have a limitation of
    1729                 :            :          * IV & Offset control word not part of instruction
    1730                 :            :          * and need to be part of Data Buffer, we check if
    1731                 :            :          * head room is there and then only do the Direct mode processing
    1732                 :            :          */
    1733   [ #  #  #  # ]:          0 :         if (likely((req_flags & ROC_SE_SINGLE_BUF_INPLACE) &&
    1734                 :            :                    (req_flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
    1735                 :            :                 void *dm_vaddr = params->bufs[0].vaddr;
    1736                 :            : 
    1737                 :            :                 /* Use Direct mode */
    1738                 :            : 
    1739                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_PDCP_CHAIN;
    1740                 :          0 :                 offset_vaddr = (uint64_t *)((uint8_t *)dm_vaddr - ROC_SE_OFF_CTRL_LEN - iv_len);
    1741                 :            : 
    1742                 :            :                 /* DPTR */
    1743                 :          0 :                 inst->dptr = (uint64_t)offset_vaddr;
    1744                 :            :                 /* RPTR should just exclude offset control word */
    1745                 :          0 :                 inst->rptr = (uint64_t)dm_vaddr - iv_len;
    1746                 :            : 
    1747                 :          0 :                 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
    1748                 :            : 
    1749                 :          0 :                 uint8_t *iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
    1750                 :          0 :                 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type, pack_iv);
    1751                 :            : 
    1752                 :          0 :                 *offset_vaddr = offset_ctrl;
    1753                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1754                 :            :         } else {
    1755                 :          0 :                 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_PDCP_CHAIN | ROC_DMA_MODE_SG;
    1756                 :          0 :                 inst->w4.u64 = cpt_inst_w4.u64;
    1757   [ #  #  #  #  :          0 :                 if (is_sg_ver2)
          #  #  #  #  #  
                      # ]
    1758   [ #  #  #  #  :          0 :                         ret = sg2_inst_prep(params, inst, offset_ctrl, iv_s, iv_len, pack_iv,
             #  #  #  # ]
    1759                 :            :                                             pdcp_alg_type, inputlen, outputlen, 0, req_flags, 1, 0);
    1760                 :            :                 else
    1761   [ #  #  #  #  :          0 :                         ret = sg_inst_prep(params, inst, offset_ctrl, iv_s, iv_len, pack_iv,
             #  #  #  # ]
    1762                 :            :                                            pdcp_alg_type, inputlen, outputlen, 0, req_flags, 1, 0);
    1763   [ #  #  #  #  :          0 :                 if (unlikely(ret)) {
          #  #  #  #  #  
                      # ]
    1764                 :          0 :                         plt_dp_err("sg prep failed");
    1765                 :          0 :                         return -1;
    1766                 :            :                 }
    1767                 :            :         }
    1768                 :            : 
    1769                 :            :         return 0;
    1770                 :            : }
    1771                 :            : 
    1772                 :            : static __rte_always_inline int
    1773                 :            : cpt_kasumi_enc_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
    1774                 :            :                     struct roc_se_fc_params *params, struct cpt_inst_s *inst, const bool is_sg_ver2)
    1775                 :            : {
    1776                 :            :         uint32_t encr_data_len, auth_data_len;
    1777                 :            :         int32_t inputlen = 0, outputlen = 0;
    1778                 :            :         uint32_t encr_offset, auth_offset;
    1779                 :            :         const uint8_t *iv_s, iv_len = 8;
    1780                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1781                 :            :         struct roc_se_ctx *se_ctx;
    1782                 :            :         uint64_t offset_ctrl;
    1783                 :            :         uint32_t mac_len = 0;
    1784                 :            :         uint8_t dir = 0;
    1785                 :            :         int flags;
    1786                 :            : 
    1787                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
    1788                 :          0 :         auth_offset = ROC_SE_AUTH_OFFSET(d_offs) / 8;
    1789                 :          0 :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1790                 :          0 :         auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
    1791                 :            : 
    1792                 :            :         se_ctx = params->ctx;
    1793                 :          0 :         flags = se_ctx->zsk_flags;
    1794                 :          0 :         mac_len = se_ctx->mac_len;
    1795                 :            : 
    1796                 :          0 :         cpt_inst_w4.u64 = se_ctx->template_w4.u64;
    1797                 :            : 
    1798   [ #  #  #  #  :          0 :         if (flags == 0x0) {
          #  #  #  #  #  
                      # ]
    1799                 :            :                 iv_s = params->iv_buf;
    1800                 :            :                 /* Consider IV len */
    1801                 :          0 :                 encr_offset += iv_len;
    1802                 :            : 
    1803                 :          0 :                 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
    1804                 :            :                 outputlen = inputlen;
    1805                 :            :                 /* iv offset is 0 */
    1806   [ #  #  #  #  :          0 :                 offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
             #  #  #  # ]
    1807                 :            :                 if (unlikely((encr_offset >> 16))) {
    1808                 :            :                         plt_dp_err("Offset not supported");
    1809                 :            :                         plt_dp_err("enc_offset: %d", encr_offset);
    1810                 :            :                         return -1;
    1811                 :            :                 }
    1812                 :            :         } else {
    1813                 :          0 :                 iv_s = params->auth_iv_buf;
    1814                 :          0 :                 dir = iv_s[8] & 0x1;
    1815                 :            : 
    1816                 :          0 :                 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
    1817                 :          0 :                 outputlen = mac_len;
    1818                 :            :                 /* iv offset is 0 */
    1819   [ #  #  #  #  :          0 :                 offset_ctrl = rte_cpu_to_be_64((uint64_t)auth_offset);
          #  #  #  #  #  
                      # ]
    1820   [ #  #  #  #  :          0 :                 if (unlikely((auth_offset >> 8))) {
          #  #  #  #  #  
                      # ]
    1821                 :          0 :                         plt_dp_err("Offset not supported");
    1822                 :          0 :                         plt_dp_err("auth_offset: %d", auth_offset);
    1823                 :          0 :                         return -1;
    1824                 :            :                 }
    1825                 :            :         }
    1826                 :            : 
    1827                 :          0 :         cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_DMA_MODE_SG;
    1828                 :            : 
    1829                 :            :         /* Indicate ECB/CBC, direction, CTX from CPTR, IV from DPTR */
    1830                 :          0 :         cpt_inst_w4.s.opcode_minor =
    1831                 :          0 :                 ((1 << 6) | (se_ctx->k_ecb << 5) | (dir << 4) | (0 << 3) | (flags & 0x7));
    1832                 :            : 
    1833                 :          0 :         cpt_inst_w4.s.param1 = encr_data_len;
    1834                 :          0 :         cpt_inst_w4.s.param2 = auth_data_len;
    1835                 :            : 
    1836                 :          0 :         inst->w4.u64 = cpt_inst_w4.u64;
    1837                 :            : 
    1838   [ #  #  #  #  :          0 :         if (unlikely(iv_s == NULL))
             #  #  #  # ]
    1839                 :            :                 return -1;
    1840                 :            : 
    1841   [ #  #  #  #  :          0 :         if (is_sg_ver2)
             #  #  #  # ]
    1842                 :            :                 sg2_inst_prep(params, inst, offset_ctrl, iv_s, iv_len, 0, 0, inputlen, outputlen, 0,
    1843                 :            :                               req_flags, 0, 0);
    1844                 :            :         else
    1845                 :            :                 sg_inst_prep(params, inst, offset_ctrl, iv_s, iv_len, 0, 0, inputlen, outputlen, 0,
    1846                 :            :                              req_flags, 0, 0);
    1847                 :            : 
    1848                 :            :         return 0;
    1849                 :            : }
    1850                 :            : 
    1851                 :            : static __rte_always_inline int
    1852                 :            : cpt_kasumi_dec_prep(uint64_t d_offs, uint64_t d_lens, struct roc_se_fc_params *params,
    1853                 :            :                     struct cpt_inst_s *inst, const bool is_sg_ver2)
    1854                 :            : {
    1855                 :            :         int32_t inputlen = 0, outputlen;
    1856                 :            :         struct roc_se_ctx *se_ctx;
    1857                 :            :         uint8_t iv_len = 8;
    1858                 :            :         uint32_t encr_offset;
    1859                 :            :         uint32_t encr_data_len;
    1860                 :            :         int flags;
    1861                 :            :         uint8_t dir = 0;
    1862                 :            :         union cpt_inst_w4 cpt_inst_w4;
    1863                 :            :         uint64_t offset_ctrl;
    1864                 :            : 
    1865                 :          0 :         encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
    1866                 :          0 :         encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
    1867                 :            : 
    1868                 :            :         se_ctx = params->ctx;
    1869                 :          0 :         flags = se_ctx->zsk_flags;
    1870                 :            : 
    1871                 :          0 :         cpt_inst_w4.u64 = 0;
    1872                 :          0 :         cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_DMA_MODE_SG;
    1873                 :            : 
    1874                 :            :         /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
    1875                 :          0 :         cpt_inst_w4.s.opcode_minor =
    1876                 :          0 :                 ((1 << 6) | (se_ctx->k_ecb << 5) | (dir << 4) | (0 << 3) | (flags & 0x7));
    1877                 :            : 
    1878                 :            :         /*
    1879                 :            :          * Lengths are expected in bits.
    1880                 :            :          */
    1881                 :          0 :         cpt_inst_w4.s.param1 = encr_data_len;
    1882                 :            : 
    1883                 :            :         /* consider iv len */
    1884                 :          0 :         encr_offset += iv_len;
    1885                 :            : 
    1886                 :          0 :         inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
    1887                 :            :         outputlen = inputlen;
    1888                 :            : 
    1889   [ #  #  #  # ]:          0 :         offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
    1890                 :            :         if (unlikely((encr_offset >> 16))) {
    1891                 :            :                 plt_dp_err("Offset not supported");
    1892                 :            :                 plt_dp_err("enc_offset: %d", encr_offset);
    1893                 :            :                 return -1;
    1894                 :            :         }
    1895                 :            : 
    1896   [ #  #  #  # ]:          0 :         inst->w4.u64 = cpt_inst_w4.u64;
    1897                 :            : 
    1898                 :            :         if (unlikely(params->iv_buf == NULL))
    1899                 :            :                 return -1;
    1900                 :            : 
    1901   [ #  #  #  # ]:          0 :         if (is_sg_ver2)
    1902                 :            :                 sg2_inst_prep(params, inst, offset_ctrl, params->iv_buf, iv_len, 0, 0, inputlen,
    1903                 :            :                               outputlen, 0, 0, 0, 1);
    1904                 :            :         else
    1905                 :            :                 sg_inst_prep(params, inst, offset_ctrl, params->iv_buf, iv_len, 0, 0, inputlen,
    1906                 :            :                              outputlen, 0, 0, 0, 1);
    1907                 :            : 
    1908                 :            :         return 0;
    1909                 :            : }
    1910                 :            : 
    1911                 :            : static __rte_always_inline int
    1912                 :            : cpt_fc_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
    1913                 :            :                      struct roc_se_fc_params *fc_params, struct cpt_inst_s *inst,
    1914                 :            :                      const bool is_sg_ver2)
    1915                 :            : {
    1916                 :            :         struct roc_se_ctx *ctx = fc_params->ctx;
    1917                 :            :         uint8_t fc_type;
    1918                 :            :         int ret = -1;
    1919                 :            : 
    1920                 :          0 :         fc_type = ctx->fc_type;
    1921                 :            : 
    1922   [ #  #  #  # ]:          0 :         if (likely(fc_type == ROC_SE_FC_GEN)) {
    1923                 :            :                 ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, fc_params, inst, is_sg_ver2);
    1924   [ #  #  #  #  :          0 :         } else if (fc_type == ROC_SE_PDCP) {
                   #  # ]
    1925                 :            :                 ret = cpt_pdcp_alg_prep(flags, d_offs, d_lens, fc_params, inst, is_sg_ver2);
    1926   [ #  #  #  #  :          0 :         } else if (fc_type == ROC_SE_KASUMI) {
                   #  # ]
    1927                 :            :                 ret = cpt_kasumi_enc_prep(flags, d_offs, d_lens, fc_params, inst, is_sg_ver2);
    1928   [ #  #  #  #  :          0 :         } else if (fc_type == ROC_SE_HASH_HMAC) {
                   #  # ]
    1929   [ #  #  #  #  :          0 :                 if (is_sg_ver2)
                   #  # ]
    1930                 :            :                         ret = cpt_digest_gen_sg_ver2_prep(flags, d_lens, fc_params, inst);
    1931                 :            :                 else
    1932                 :            :                         ret = cpt_digest_gen_sg_ver1_prep(flags, d_lens, fc_params, inst);
    1933                 :            :         }
    1934                 :            : 
    1935                 :            :         return ret;
    1936                 :            : }
    1937                 :            : 
    1938                 :            : static __rte_always_inline int
    1939                 :            : fill_sess_aead(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
    1940                 :            : {
    1941                 :            :         struct rte_crypto_aead_xform *aead_form;
    1942                 :            :         uint8_t aes_gcm = 0, aes_ccm = 0;
    1943                 :            :         roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
    1944                 :            :         roc_se_auth_type auth_type = 0;  /* NULL Auth type */
    1945                 :            :         uint32_t cipher_key_len = 0;
    1946                 :            :         aead_form = &xform->aead;
    1947                 :            : 
    1948         [ #  # ]:          0 :         if (aead_form->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
    1949                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
    1950                 :          0 :                 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
    1951         [ #  # ]:          0 :         } else if (aead_form->op == RTE_CRYPTO_AEAD_OP_DECRYPT) {
    1952                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
    1953                 :          0 :                 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
    1954                 :            :         } else {
    1955                 :          0 :                 plt_dp_err("Unknown aead operation");
    1956                 :            :                 return -1;
    1957                 :            :         }
    1958   [ #  #  #  # ]:          0 :         switch (aead_form->algo) {
    1959                 :            :         case RTE_CRYPTO_AEAD_AES_GCM:
    1960                 :            :                 enc_type = ROC_SE_AES_GCM;
    1961                 :            :                 cipher_key_len = 16;
    1962                 :            :                 aes_gcm = 1;
    1963                 :            :                 break;
    1964                 :          0 :         case RTE_CRYPTO_AEAD_AES_CCM:
    1965                 :            :                 enc_type = ROC_SE_AES_CCM;
    1966                 :            :                 cipher_key_len = 16;
    1967                 :            :                 aes_ccm = 1;
    1968                 :          0 :                 break;
    1969                 :          0 :         case RTE_CRYPTO_AEAD_CHACHA20_POLY1305:
    1970                 :            :                 enc_type = ROC_SE_CHACHA20;
    1971                 :            :                 auth_type = ROC_SE_POLY1305;
    1972                 :            :                 cipher_key_len = 32;
    1973                 :          0 :                 sess->chacha_poly = 1;
    1974                 :          0 :                 break;
    1975                 :          0 :         default:
    1976                 :          0 :                 plt_dp_err("Crypto: Undefined cipher algo %u specified",
    1977                 :            :                            aead_form->algo);
    1978                 :            :                 return -1;
    1979                 :            :         }
    1980         [ #  # ]:          0 :         if (aead_form->key.length < cipher_key_len) {
    1981                 :          0 :                 plt_dp_err("Invalid cipher params keylen %u",
    1982                 :            :                            aead_form->key.length);
    1983                 :            :                 return -1;
    1984                 :            :         }
    1985                 :          0 :         sess->zsk_flag = 0;
    1986                 :          0 :         sess->aes_gcm = aes_gcm;
    1987                 :          0 :         sess->aes_ccm = aes_ccm;
    1988                 :          0 :         sess->mac_len = aead_form->digest_length;
    1989                 :          0 :         sess->iv_offset = aead_form->iv.offset;
    1990                 :          0 :         sess->iv_length = aead_form->iv.length;
    1991                 :          0 :         sess->aad_length = aead_form->aad_length;
    1992                 :            : 
    1993         [ #  # ]:          0 :         if (aes_ccm) {
    1994         [ #  # ]:          0 :                 if ((sess->iv_length < 11) || (sess->iv_length > 13)) {
    1995                 :          0 :                         plt_dp_err("Crypto: Unsupported IV length %u", sess->iv_length);
    1996                 :            :                         return -1;
    1997                 :            :                 }
    1998                 :            :         } else {
    1999      [ #  #  # ]:          0 :                 switch (sess->iv_length) {
    2000                 :          0 :                 case 12:
    2001                 :          0 :                         sess->short_iv = 1;
    2002                 :            :                 case 16:
    2003                 :            :                         break;
    2004                 :          0 :                 default:
    2005                 :          0 :                         plt_dp_err("Crypto: Unsupported IV length %u", sess->iv_length);
    2006                 :            :                         return -1;
    2007                 :            :                 }
    2008                 :            :         }
    2009                 :            : 
    2010         [ #  # ]:          0 :         if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type, aead_form->key.data,
    2011                 :            :                                          aead_form->key.length)))
    2012                 :            :                 return -1;
    2013                 :            : 
    2014         [ #  # ]:          0 :         if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
    2015                 :            :                                          aead_form->digest_length)))
    2016                 :            :                 return -1;
    2017                 :            : 
    2018         [ #  # ]:          0 :         if (enc_type == ROC_SE_CHACHA20)
    2019                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor |= BIT(5);
    2020                 :            :         return 0;
    2021                 :            : }
    2022                 :            : 
    2023                 :            : static __rte_always_inline int
    2024                 :            : fill_sm_sess_cipher(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
    2025                 :            : {
    2026                 :            :         struct roc_se_sm_context *sm_ctx = &sess->roc_se_ctx.se_ctx.sm_ctx;
    2027                 :            :         struct rte_crypto_cipher_xform *c_form;
    2028                 :            :         roc_sm_cipher_type enc_type = 0;
    2029                 :            : 
    2030                 :            :         c_form = &xform->cipher;
    2031                 :            : 
    2032   [ #  #  #  #  :          0 :         if (c_form->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
                   #  # ]
    2033                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
    2034                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor = ROC_SE_FC_MINOR_OP_ENCRYPT;
    2035   [ #  #  #  #  :          0 :         } else if (c_form->op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
                   #  # ]
    2036                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
    2037                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor = ROC_SE_FC_MINOR_OP_DECRYPT;
    2038                 :            :         } else {
    2039                 :          0 :                 plt_dp_err("Unknown cipher operation");
    2040                 :            :                 return -1;
    2041                 :            :         }
    2042                 :            : 
    2043                 :            :         switch (c_form->algo) {
    2044                 :            :         case RTE_CRYPTO_CIPHER_SM4_CBC:
    2045                 :            :                 enc_type = ROC_SM4_CBC;
    2046                 :            :                 break;
    2047                 :            :         case RTE_CRYPTO_CIPHER_SM4_ECB:
    2048                 :            :                 enc_type = ROC_SM4_ECB;
    2049                 :            :                 break;
    2050                 :            :         case RTE_CRYPTO_CIPHER_SM4_CTR:
    2051                 :            :                 enc_type = ROC_SM4_CTR;
    2052                 :            :                 break;
    2053                 :            :         case RTE_CRYPTO_CIPHER_SM4_CFB:
    2054                 :            :                 enc_type = ROC_SM4_CFB;
    2055                 :            :                 break;
    2056                 :            :         case RTE_CRYPTO_CIPHER_SM4_OFB:
    2057                 :            :                 enc_type = ROC_SM4_OFB;
    2058                 :            :                 break;
    2059                 :          0 :         default:
    2060                 :          0 :                 plt_dp_err("Crypto: Undefined cipher algo %u specified", c_form->algo);
    2061                 :            :                 return -1;
    2062                 :            :         }
    2063                 :            : 
    2064                 :          0 :         sess->iv_offset = c_form->iv.offset;
    2065                 :          0 :         sess->iv_length = c_form->iv.length;
    2066                 :            : 
    2067   [ #  #  #  #  :          0 :         if (c_form->key.length != ROC_SE_SM4_KEY_LEN) {
                   #  # ]
    2068                 :          0 :                 plt_dp_err("Invalid cipher params keylen %u", c_form->key.length);
    2069                 :            :                 return -1;
    2070                 :            :         }
    2071                 :            : 
    2072                 :          0 :         sess->zsk_flag = 0;
    2073                 :          0 :         sess->zs_cipher = 0;
    2074                 :          0 :         sess->aes_gcm = 0;
    2075                 :          0 :         sess->aes_ctr = 0;
    2076                 :          0 :         sess->is_null = 0;
    2077                 :          0 :         sess->is_sm4 = 1;
    2078                 :          0 :         sess->roc_se_ctx.fc_type = ROC_SE_SM;
    2079                 :            : 
    2080                 :          0 :         sess->roc_se_ctx.template_w4.s.opcode_major = ROC_SE_MAJOR_OP_SM;
    2081                 :            : 
    2082                 :          0 :         memcpy(sm_ctx->encr_key, c_form->key.data, ROC_SE_SM4_KEY_LEN);
    2083                 :          0 :         sm_ctx->enc_cipher = enc_type;
    2084                 :            : 
    2085                 :            :         return 0;
    2086                 :            : }
    2087                 :            : 
    2088                 :            : static __rte_always_inline int
    2089                 :            : fill_sess_cipher(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
    2090                 :            : {
    2091                 :            :         uint8_t zsk_flag = 0, zs_cipher = 0, aes_ctr = 0, is_null = 0;
    2092                 :            :         struct rte_crypto_cipher_xform *c_form;
    2093                 :            :         roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
    2094                 :            :         uint32_t cipher_key_len = 0;
    2095                 :            : 
    2096                 :            :         c_form = &xform->cipher;
    2097                 :            : 
    2098                 :          0 :         if ((c_form->algo == RTE_CRYPTO_CIPHER_SM4_CBC) ||
    2099                 :            :             (c_form->algo == RTE_CRYPTO_CIPHER_SM4_ECB) ||
    2100   [ #  #  #  #  :          0 :             (c_form->algo == RTE_CRYPTO_CIPHER_SM4_CTR) ||
                   #  # ]
    2101   [ #  #  #  #  :          0 :             (c_form->algo == RTE_CRYPTO_CIPHER_SM4_CFB) ||
                   #  # ]
    2102                 :            :             (c_form->algo == RTE_CRYPTO_CIPHER_SM4_OFB))
    2103                 :            :                 return fill_sm_sess_cipher(xform, sess);
    2104                 :            : 
    2105   [ #  #  #  #  :          0 :         if (c_form->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
                   #  # ]
    2106                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
    2107   [ #  #  #  #  :          0 :         else if (c_form->op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
                   #  # ]
    2108                 :          0 :                 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
    2109   [ #  #  #  #  :          0 :                 if (xform->next != NULL &&
                   #  # ]
    2110   [ #  #  #  #  :          0 :                     xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
                   #  # ]
    2111                 :            :                         /* Perform decryption followed by auth verify */
    2112                 :          0 :                         sess->roc_se_ctx.template_w4.s.opcode_minor =
    2113                 :            :                                 ROC_SE_FC_MINOR_OP_HMAC_FIRST;
    2114                 :            :                 }
    2115                 :            :         } else {
    2116                 :          0 :                 plt_dp_err("Unknown cipher operation");
    2117                 :            :                 return -1;
    2118                 :            :         }
    2119                 :            : 
    2120   [ #  #  #  #  :          0 :         switch (c_form->algo) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2121                 :            :         case RTE_CRYPTO_CIPHER_AES_CBC:
    2122                 :            :                 enc_type = ROC_SE_AES_CBC;
    2123                 :            :                 cipher_key_len = 16;
    2124                 :            :                 break;
    2125                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_CBC:
    2126                 :            :                 enc_type = ROC_SE_DES3_CBC;
    2127                 :            :                 cipher_key_len = 24;
    2128                 :          0 :                 break;
    2129                 :          0 :         case RTE_CRYPTO_CIPHER_DES_CBC:
    2130                 :            :                 /* DES is implemented using 3DES in hardware */
    2131                 :            :                 enc_type = ROC_SE_DES3_CBC;
    2132                 :            :                 cipher_key_len = 8;
    2133                 :          0 :                 break;
    2134                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CTR:
    2135   [ #  #  #  #  :          0 :                 if (sess->aes_ctr_eea2) {
                   #  # ]
    2136                 :            :                         enc_type = ROC_SE_AES_CTR_EEA2;
    2137                 :            :                 } else {
    2138                 :            :                         enc_type = ROC_SE_AES_CTR;
    2139                 :            :                         aes_ctr = 1;
    2140                 :            :                 }
    2141                 :            :                 cipher_key_len = 16;
    2142                 :            :                 break;
    2143                 :          0 :         case RTE_CRYPTO_CIPHER_NULL:
    2144                 :            :                 enc_type = 0;
    2145                 :            :                 is_null = 1;
    2146                 :          0 :                 break;
    2147                 :          0 :         case RTE_CRYPTO_CIPHER_KASUMI_F8:
    2148   [ #  #  #  # ]:          0 :                 if (sess->chained_op)
    2149                 :            :                         return -ENOTSUP;
    2150   [ #  #  #  # ]:          0 :                 if (c_form->iv.length != 8)
    2151                 :            :                         return -EINVAL;
    2152                 :            :                 enc_type = ROC_SE_KASUMI_F8_ECB;
    2153                 :            :                 cipher_key_len = 16;
    2154                 :            :                 zsk_flag = ROC_SE_K_F8;
    2155                 :            :                 zs_cipher = ROC_SE_K_F8;
    2156                 :            :                 break;
    2157                 :          0 :         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
    2158                 :            :                 enc_type = ROC_SE_SNOW3G_UEA2;
    2159                 :            :                 cipher_key_len = 16;
    2160                 :            :                 zsk_flag = ROC_SE_ZS_EA;
    2161                 :            :                 zs_cipher = ROC_SE_ZS_EA;
    2162                 :          0 :                 break;
    2163                 :          0 :         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
    2164                 :            :                 enc_type = ROC_SE_ZUC_EEA3;
    2165                 :          0 :                 cipher_key_len = c_form->key.length;
    2166                 :            :                 zsk_flag = ROC_SE_ZS_EA;
    2167                 :            :                 zs_cipher = ROC_SE_ZS_EA;
    2168                 :          0 :                 break;
    2169                 :          0 :         case RTE_CRYPTO_CIPHER_AES_XTS:
    2170                 :            :                 enc_type = ROC_SE_AES_XTS;
    2171                 :            :                 cipher_key_len = 16;
    2172                 :          0 :                 break;
    2173                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_ECB:
    2174                 :            :                 enc_type = ROC_SE_DES3_ECB;
    2175                 :            :                 cipher_key_len = 24;
    2176                 :          0 :                 break;
    2177                 :          0 :         case RTE_CRYPTO_CIPHER_AES_ECB:
    2178                 :            :                 enc_type = ROC_SE_AES_ECB;
    2179                 :            :                 cipher_key_len = 16;
    2180                 :          0 :                 break;
    2181                 :          0 :         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
    2182                 :            :                 /* Set DOCSIS flag */
    2183                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor |= ROC_SE_FC_MINOR_OP_DOCSIS;
    2184                 :            :                 enc_type = ROC_SE_AES_DOCSISBPI;
    2185                 :            :                 cipher_key_len = 16;
    2186                 :          0 :                 break;
    2187                 :          0 :         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
    2188                 :            :                 /* Set DOCSIS flag */
    2189                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor |= ROC_SE_FC_MINOR_OP_DOCSIS;
    2190                 :            :                 enc_type = ROC_SE_DES_DOCSISBPI;
    2191                 :            :                 cipher_key_len = 8;
    2192                 :          0 :                 break;
    2193                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_CTR:
    2194                 :            :         case RTE_CRYPTO_CIPHER_AES_F8:
    2195                 :            :         case RTE_CRYPTO_CIPHER_ARC4:
    2196                 :          0 :                 plt_dp_err("Crypto: Unsupported cipher algo %u", c_form->algo);
    2197                 :            :                 return -1;
    2198                 :          0 :         default:
    2199                 :          0 :                 plt_dp_err("Crypto: Undefined cipher algo %u specified",
    2200                 :            :                            c_form->algo);
    2201                 :            :                 return -1;
    2202                 :            :         }
    2203                 :            : 
    2204   [ #  #  #  #  :          0 :         if (c_form->key.length < cipher_key_len) {
                   #  # ]
    2205                 :          0 :                 plt_dp_err("Invalid cipher params keylen %u",
    2206                 :            :                            c_form->key.length);
    2207                 :            :                 return -1;
    2208                 :            :         }
    2209                 :            : 
    2210   [ #  #  #  #  :          0 :         if (zsk_flag && sess->roc_se_ctx.ciph_then_auth) {
          #  #  #  #  #  
                      # ]
    2211                 :            :                 struct rte_crypto_auth_xform *a_form;
    2212                 :          0 :                 a_form = &xform->next->auth;
    2213   [ #  #  #  #  :          0 :                 if (c_form->op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
                   #  # ]
    2214   [ #  #  #  #  :          0 :                     a_form->op != RTE_CRYPTO_AUTH_OP_VERIFY) {
                   #  # ]
    2215                 :          0 :                         plt_dp_err("Crypto: PDCP cipher then auth must use"
    2216                 :            :                                    " options: decrypt and verify");
    2217                 :            :                         return -EINVAL;
    2218                 :            :                 }
    2219                 :            :         }
    2220                 :            : 
    2221                 :          0 :         sess->cipher_only = 1;
    2222                 :          0 :         sess->zsk_flag = zsk_flag;
    2223                 :          0 :         sess->zs_cipher = zs_cipher;
    2224                 :          0 :         sess->aes_gcm = 0;
    2225                 :          0 :         sess->aes_ccm = 0;
    2226                 :          0 :         sess->aes_ctr = aes_ctr;
    2227                 :          0 :         sess->iv_offset = c_form->iv.offset;
    2228                 :          0 :         sess->iv_length = c_form->iv.length;
    2229                 :          0 :         sess->is_null = is_null;
    2230                 :            : 
    2231   [ #  #  #  #  :          0 :         if (aes_ctr)
                   #  # ]
    2232   [ #  #  #  #  :          0 :                 switch (sess->iv_length) {
             #  #  #  #  
                      # ]
    2233                 :          0 :                 case 12:
    2234                 :          0 :                         sess->short_iv = 1;
    2235                 :            :                 case 16:
    2236                 :            :                         break;
    2237                 :          0 :                 default:
    2238                 :          0 :                         plt_dp_err("Crypto: Unsupported IV length %u", sess->iv_length);
    2239                 :            :                         return -1;
    2240                 :            :                 }
    2241                 :            : 
    2242   [ #  #  #  #  :          0 :         if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type, c_form->key.data,
                   #  # ]
    2243                 :            :                                          c_form->key.length)))
    2244                 :            :                 return -1;
    2245                 :            : 
    2246                 :            :         return 0;
    2247                 :            : }
    2248                 :            : 
    2249                 :            : static __rte_always_inline int
    2250                 :            : fill_sess_auth(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
    2251                 :            : {
    2252                 :            :         uint8_t zsk_flag = 0, zs_auth = 0, aes_gcm = 0, is_null = 0, is_sha3 = 0;
    2253                 :            :         struct rte_crypto_auth_xform *a_form;
    2254                 :            :         roc_se_auth_type auth_type = 0; /* NULL Auth type */
    2255                 :            :         uint8_t is_sm3 = 0;
    2256                 :            : 
    2257   [ #  #  #  #  :          0 :         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
                   #  # ]
    2258                 :            :                 return fill_sess_gmac(xform, sess);
    2259                 :            : 
    2260   [ #  #  #  #  :          0 :         if (xform->next != NULL &&
                   #  # ]
    2261   [ #  #  #  #  :          0 :             xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
                   #  # ]
    2262   [ #  #  #  #  :          0 :             xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
                   #  # ]
    2263                 :            :                 /* Perform auth followed by encryption */
    2264                 :          0 :                 sess->roc_se_ctx.template_w4.s.opcode_minor =
    2265                 :            :                         ROC_SE_FC_MINOR_OP_HMAC_FIRST;
    2266                 :            :         }
    2267                 :            : 
    2268                 :            :         a_form = &xform->auth;
    2269                 :            : 
    2270   [ #  #  #  #  :          0 :         if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
                   #  # ]
    2271                 :          0 :                 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
    2272   [ #  #  #  #  :          0 :         else if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
                   #  # ]
    2273                 :          0 :                 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
    2274                 :            :         else {
    2275                 :          0 :                 plt_dp_err("Unknown auth operation");
    2276                 :            :                 return -1;
    2277                 :            :         }
    2278                 :            : 
    2279   [ #  #  #  #  :          0 :         switch (a_form->algo) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    2280                 :            :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
    2281                 :            :                 /* Fall through */
    2282                 :            :         case RTE_CRYPTO_AUTH_SHA1:
    2283                 :            :                 auth_type = ROC_SE_SHA1_TYPE;
    2284                 :            :                 break;
    2285                 :          0 :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
    2286                 :            :         case RTE_CRYPTO_AUTH_SHA256:
    2287                 :            :                 auth_type = ROC_SE_SHA2_SHA256;
    2288                 :          0 :                 break;
    2289                 :          0 :         case RTE_CRYPTO_AUTH_SHA512_HMAC:
    2290                 :            :         case RTE_CRYPTO_AUTH_SHA512:
    2291                 :            :                 auth_type = ROC_SE_SHA2_SHA512;
    2292                 :          0 :                 break;
    2293                 :            :         case RTE_CRYPTO_AUTH_AES_GMAC:
    2294                 :            :                 auth_type = ROC_SE_GMAC_TYPE;
    2295                 :            :                 aes_gcm = 1;
    2296                 :            :                 break;
    2297                 :          0 :         case RTE_CRYPTO_AUTH_SHA224_HMAC:
    2298                 :            :         case RTE_CRYPTO_AUTH_SHA224:
    2299                 :            :                 auth_type = ROC_SE_SHA2_SHA224;
    2300                 :          0 :                 break;
    2301                 :          0 :         case RTE_CRYPTO_AUTH_SHA384_HMAC:
    2302                 :            :         case RTE_CRYPTO_AUTH_SHA384:
    2303                 :            :                 auth_type = ROC_SE_SHA2_SHA384;
    2304                 :          0 :                 break;
    2305                 :          0 :         case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
    2306                 :            :         case RTE_CRYPTO_AUTH_SHA3_224:
    2307                 :            :                 is_sha3 = 1;
    2308                 :            :                 auth_type = ROC_SE_SHA3_SHA224;
    2309                 :          0 :                 break;
    2310                 :          0 :         case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
    2311                 :            :         case RTE_CRYPTO_AUTH_SHA3_256:
    2312                 :            :                 is_sha3 = 1;
    2313                 :            :                 auth_type = ROC_SE_SHA3_SHA256;
    2314                 :          0 :                 break;
    2315                 :          0 :         case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
    2316                 :            :         case RTE_CRYPTO_AUTH_SHA3_384:
    2317                 :            :                 is_sha3 = 1;
    2318                 :            :                 auth_type = ROC_SE_SHA3_SHA384;
    2319                 :          0 :                 break;
    2320                 :          0 :         case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
    2321                 :            :         case RTE_CRYPTO_AUTH_SHA3_512:
    2322                 :            :                 is_sha3 = 1;
    2323                 :            :                 auth_type = ROC_SE_SHA3_SHA512;
    2324                 :          0 :                 break;
    2325                 :          0 :         case RTE_CRYPTO_AUTH_SHAKE_128:
    2326                 :            :                 is_sha3 = 1;
    2327                 :            :                 auth_type = ROC_SE_SHA3_SHAKE128;
    2328                 :          0 :                 break;
    2329                 :          0 :         case RTE_CRYPTO_AUTH_SHAKE_256:
    2330                 :            :                 is_sha3 = 1;
    2331                 :            :                 auth_type = ROC_SE_SHA3_SHAKE256;
    2332                 :          0 :                 break;
    2333                 :          0 :         case RTE_CRYPTO_AUTH_MD5_HMAC:
    2334                 :            :         case RTE_CRYPTO_AUTH_MD5:
    2335                 :            :                 auth_type = ROC_SE_MD5_TYPE;
    2336                 :          0 :                 break;
    2337                 :          0 :         case RTE_CRYPTO_AUTH_KASUMI_F9:
    2338   [ #  #  #  # ]:          0 :                 if (sess->chained_op)
    2339                 :            :                         return -ENOTSUP;
    2340                 :            :                 auth_type = ROC_SE_KASUMI_F9_ECB;
    2341                 :            :                 /*
    2342                 :            :                  * Indicate that direction needs to be taken out
    2343                 :            :                  * from end of src
    2344                 :            :                  */
    2345                 :            :                 zsk_flag = ROC_SE_K_F9;
    2346                 :            :                 zs_auth = ROC_SE_K_F9;
    2347                 :            :                 break;
    2348                 :          0 :         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
    2349                 :            :                 auth_type = ROC_SE_SNOW3G_UIA2;
    2350                 :            :                 zsk_flag = ROC_SE_ZS_IA;
    2351                 :            :                 zs_auth = ROC_SE_ZS_IA;
    2352                 :          0 :                 break;
    2353                 :          0 :         case RTE_CRYPTO_AUTH_ZUC_EIA3:
    2354                 :            :                 auth_type = ROC_SE_ZUC_EIA3;
    2355                 :            :                 zsk_flag = ROC_SE_ZS_IA;
    2356                 :            :                 zs_auth = ROC_SE_ZS_IA;
    2357                 :          0 :                 break;
    2358                 :          0 :         case RTE_CRYPTO_AUTH_NULL:
    2359                 :            :                 auth_type = 0;
    2360                 :            :                 is_null = 1;
    2361                 :          0 :                 break;
    2362                 :          0 :         case RTE_CRYPTO_AUTH_AES_CMAC:
    2363                 :            :                 auth_type = ROC_SE_AES_CMAC_EIA2;
    2364                 :            :                 zsk_flag = ROC_SE_ZS_IA;
    2365                 :          0 :                 break;
    2366                 :          0 :         case RTE_CRYPTO_AUTH_SM3:
    2367                 :            :                 auth_type = ROC_SE_SM3;
    2368                 :            :                 is_sm3 = 1;
    2369                 :          0 :                 break;
    2370                 :          0 :         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
    2371                 :            :         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
    2372                 :          0 :                 plt_dp_err("Crypto: Unsupported hash algo %u", a_form->algo);
    2373                 :            :                 return -1;
    2374                 :          0 :         default:
    2375                 :          0 :                 plt_dp_err("Crypto: Undefined Hash algo %u specified",
    2376                 :            :                            a_form->algo);
    2377                 :            :                 return -1;
    2378                 :            :         }
    2379                 :            : 
    2380   [ #  #  #  #  :          0 :         if (zsk_flag && sess->roc_se_ctx.auth_then_ciph) {
          #  #  #  #  #  
                      # ]
    2381                 :            :                 struct rte_crypto_cipher_xform *c_form;
    2382   [ #  #  #  #  :          0 :                 if (xform->next != NULL) {
                   #  # ]
    2383                 :            :                         c_form = &xform->next->cipher;
    2384   [ #  #  #  #  :          0 :                         if ((c_form != NULL) && (c_form->op != RTE_CRYPTO_CIPHER_OP_ENCRYPT) &&
          #  #  #  #  #  
                #  #  # ]
    2385                 :            :                             a_form->op != RTE_CRYPTO_AUTH_OP_GENERATE) {
    2386                 :          0 :                                 plt_dp_err("Crypto: PDCP auth then cipher must use"
    2387                 :            :                                            " options: encrypt and generate");
    2388                 :            :                                 return -EINVAL;
    2389                 :            :                         }
    2390                 :            :                 }
    2391                 :            :         }
    2392                 :            : 
    2393                 :          0 :         sess->zsk_flag = zsk_flag;
    2394                 :          0 :         sess->zs_auth = zs_auth;
    2395                 :          0 :         sess->aes_gcm = aes_gcm;
    2396                 :          0 :         sess->mac_len = a_form->digest_length;
    2397                 :          0 :         sess->is_null = is_null;
    2398                 :          0 :         sess->is_sha3 = is_sha3;
    2399                 :          0 :         sess->is_sm3 = is_sm3;
    2400   [ #  #  #  #  :          0 :         if (zsk_flag) {
                   #  # ]
    2401                 :          0 :                 sess->auth_iv_offset = a_form->iv.offset;
    2402                 :          0 :                 sess->auth_iv_length = a_form->iv.length;
    2403                 :            :         }
    2404   [ #  #  #  #  :          0 :         if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, a_form->key.data,
                   #  # ]
    2405                 :            :                                          a_form->key.length, a_form->digest_length)))
    2406                 :            :                 return -1;
    2407                 :            : 
    2408                 :            :         return 0;
    2409                 :            : }
    2410                 :            : 
    2411                 :            : static __rte_always_inline int
    2412                 :            : fill_sess_gmac(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
    2413                 :            : {
    2414                 :            :         struct rte_crypto_auth_xform *a_form;
    2415                 :            :         roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
    2416                 :            :         roc_se_auth_type auth_type = 0;  /* NULL Auth type */
    2417                 :            : 
    2418                 :            :         a_form = &xform->auth;
    2419                 :            : 
    2420   [ #  #  #  #  :          0 :         if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
                   #  # ]
    2421                 :          0 :                 sess->cpt_op |= ROC_SE_OP_ENCODE;
    2422   [ #  #  #  #  :          0 :         else if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
                   #  # ]
    2423                 :          0 :                 sess->cpt_op |= ROC_SE_OP_DECODE;
    2424                 :            :         else {
    2425                 :          0 :                 plt_dp_err("Unknown auth operation");
    2426                 :            :                 return -1;
    2427                 :            :         }
    2428                 :            : 
    2429                 :            :         switch (a_form->algo) {
    2430                 :            :         case RTE_CRYPTO_AUTH_AES_GMAC:
    2431                 :            :                 enc_type = ROC_SE_AES_GCM;
    2432                 :            :                 auth_type = ROC_SE_GMAC_TYPE;
    2433                 :            :                 break;
    2434                 :            :         default:
    2435                 :            :                 plt_dp_err("Crypto: Undefined cipher algo %u specified",
    2436                 :            :                            a_form->algo);
    2437                 :            :                 return -1;
    2438                 :            :         }
    2439                 :            : 
    2440                 :          0 :         sess->zsk_flag = 0;
    2441                 :          0 :         sess->aes_gcm = 0;
    2442                 :          0 :         sess->is_gmac = 1;
    2443                 :          0 :         sess->iv_offset = a_form->iv.offset;
    2444                 :          0 :         sess->iv_length = a_form->iv.length;
    2445                 :          0 :         sess->mac_len = a_form->digest_length;
    2446                 :            : 
    2447   [ #  #  #  #  :          0 :         switch (sess->iv_length) {
             #  #  #  #  
                      # ]
    2448                 :          0 :         case 12:
    2449                 :          0 :                 sess->short_iv = 1;
    2450                 :            :         case 16:
    2451                 :            :                 break;
    2452                 :          0 :         default:
    2453                 :          0 :                 plt_dp_err("Crypto: Unsupported IV length %u", sess->iv_length);
    2454                 :            :                 return -1;
    2455                 :            :         }
    2456                 :            : 
    2457   [ #  #  #  #  :          0 :         if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type, a_form->key.data,
                   #  # ]
    2458                 :            :                                          a_form->key.length)))
    2459                 :            :                 return -1;
    2460                 :            : 
    2461   [ #  #  #  #  :          0 :         if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
                   #  # ]
    2462                 :            :                                          a_form->digest_length)))
    2463                 :            :                 return -1;
    2464                 :            : 
    2465                 :            :         return 0;
    2466                 :            : }
    2467                 :            : 
    2468                 :            : static __rte_always_inline uint32_t
    2469                 :            : prepare_iov_from_pkt(struct rte_mbuf *pkt, struct roc_se_iov_ptr *iovec, uint32_t start_offset,
    2470                 :            :                      const bool is_aead, const bool is_sg_ver2)
    2471                 :            : {
    2472                 :            :         uint16_t index = 0;
    2473                 :            :         void *seg_data = NULL;
    2474                 :            :         int32_t seg_size = 0;
    2475                 :            : 
    2476   [ #  #  #  #  :          0 :         if (!pkt || (is_sg_ver2 && (pkt->data_len == 0) && !is_aead)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2477                 :          0 :                 iovec->buf_cnt = 0;
    2478                 :          0 :                 return 0;
    2479                 :            :         }
    2480                 :            : 
    2481   [ #  #  #  # ]:          0 :         if (!start_offset) {
    2482                 :          0 :                 seg_data = rte_pktmbuf_mtod(pkt, void *);
    2483                 :          0 :                 seg_size = pkt->data_len;
    2484                 :            :         } else {
    2485   [ #  #  #  # ]:          0 :                 while (start_offset >= pkt->data_len) {
    2486                 :          0 :                         start_offset -= pkt->data_len;
    2487                 :          0 :                         pkt = pkt->next;
    2488                 :            :                 }
    2489                 :            : 
    2490                 :          0 :                 seg_data = rte_pktmbuf_mtod_offset(pkt, void *, start_offset);
    2491                 :          0 :                 seg_size = pkt->data_len - start_offset;
    2492   [ #  #  #  # ]:          0 :                 if (!seg_size)
    2493                 :            :                         return 1;
    2494                 :            :         }
    2495                 :            : 
    2496                 :            :         /* first seg */
    2497                 :          0 :         iovec->bufs[index].vaddr = seg_data;
    2498                 :          0 :         iovec->bufs[index].size = seg_size;
    2499                 :            :         index++;
    2500                 :          0 :         pkt = pkt->next;
    2501                 :            : 
    2502   [ #  #  #  #  :          0 :         while (unlikely(pkt != NULL)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2503                 :          0 :                 seg_data = rte_pktmbuf_mtod(pkt, void *);
    2504                 :          0 :                 seg_size = pkt->data_len;
    2505   [ #  #  #  #  :          0 :                 if (!seg_size)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2506                 :            :                         break;
    2507                 :            : 
    2508                 :          0 :                 iovec->bufs[index].vaddr = seg_data;
    2509                 :          0 :                 iovec->bufs[index].size = seg_size;
    2510                 :            : 
    2511                 :          0 :                 index++;
    2512                 :            : 
    2513                 :          0 :                 pkt = pkt->next;
    2514                 :            :         }
    2515                 :            : 
    2516                 :          0 :         iovec->buf_cnt = index;
    2517                 :          0 :         return 0;
    2518                 :            : }
    2519                 :            : 
    2520                 :            : static __rte_always_inline void
    2521                 :            : prepare_iov_from_pkt_inplace(struct rte_mbuf *pkt,
    2522                 :            :                              struct roc_se_fc_params *param, uint32_t *flags)
    2523                 :            : {
    2524                 :            :         uint16_t index = 0;
    2525                 :            :         void *seg_data = NULL;
    2526                 :            :         uint32_t seg_size = 0;
    2527                 :            :         struct roc_se_iov_ptr *iovec;
    2528                 :            : 
    2529                 :          0 :         seg_data = rte_pktmbuf_mtod(pkt, void *);
    2530                 :          0 :         seg_size = pkt->data_len;
    2531                 :            : 
    2532                 :            :         /* first seg */
    2533                 :          0 :         if (likely(!pkt->next)) {
    2534                 :            :                 uint32_t headroom;
    2535                 :            : 
    2536                 :          0 :                 *flags |= ROC_SE_SINGLE_BUF_INPLACE;
    2537                 :          0 :                 headroom = rte_pktmbuf_headroom(pkt);
    2538   [ #  #  #  #  :          0 :                 if (likely(headroom >= CNXK_CPT_MIN_HEADROOM_REQ))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    2539                 :          0 :                         *flags |= ROC_SE_SINGLE_BUF_HEADROOM;
    2540                 :            : 
    2541                 :          0 :                 param->bufs[0].vaddr = seg_data;
    2542                 :          0 :                 param->bufs[0].size = seg_size;
    2543                 :          0 :                 return;
    2544                 :            :         }
    2545                 :            :         iovec = param->src_iov;
    2546                 :          0 :         iovec->bufs[index].vaddr = seg_data;
    2547                 :          0 :         iovec->bufs[index].size = seg_size;
    2548                 :            :         index++;
    2549                 :            :         pkt = pkt->next;
    2550                 :            : 
    2551   [ #  #  #  #  :          0 :         while (unlikely(pkt != NULL)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    2552                 :          0 :                 seg_data = rte_pktmbuf_mtod(pkt, void *);
    2553                 :          0 :                 seg_size = pkt->data_len;
    2554                 :            : 
    2555   [ #  #  #  #  :          0 :                 if (!seg_size)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    2556                 :            :                         break;
    2557                 :            : 
    2558                 :          0 :                 iovec->bufs[index].vaddr = seg_data;
    2559                 :          0 :                 iovec->bufs[index].size = seg_size;
    2560                 :            : 
    2561                 :          0 :                 index++;
    2562                 :            : 
    2563                 :          0 :                 pkt = pkt->next;
    2564                 :            :         }
    2565                 :            : 
    2566                 :          0 :         iovec->buf_cnt = index;
    2567                 :          0 :         return;
    2568                 :            : }
    2569                 :            : 
    2570                 :            : static __rte_always_inline int
    2571                 :            : fill_sm_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
    2572                 :            :                struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    2573                 :            :                struct cpt_inst_s *inst, const bool is_sg_ver2)
    2574                 :            : {
    2575                 :            :         struct rte_crypto_sym_op *sym_op = cop->sym;
    2576                 :            :         struct roc_se_fc_params fc_params;
    2577                 :            :         struct rte_mbuf *m_src, *m_dst;
    2578                 :            :         uint8_t cpt_op = sess->cpt_op;
    2579                 :            :         uint64_t d_offs, d_lens;
    2580                 :            :         char src[SRC_IOV_SIZE];
    2581                 :            :         char dst[SRC_IOV_SIZE];
    2582                 :            :         void *mdata = NULL;
    2583                 :            :         uint32_t flags = 0;
    2584                 :            :         int ret;
    2585                 :            : 
    2586                 :          0 :         uint32_t ci_data_length = sym_op->cipher.data.length;
    2587                 :          0 :         uint32_t ci_data_offset = sym_op->cipher.data.offset;
    2588                 :            : 
    2589                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    2590                 :            :         fc_params.auth_iv_len = 0;
    2591                 :            :         fc_params.auth_iv_buf = NULL;
    2592                 :            :         fc_params.iv_buf = NULL;
    2593                 :            :         fc_params.mac_buf.size = 0;
    2594                 :            :         fc_params.mac_buf.vaddr = 0;
    2595                 :            : 
    2596                 :          0 :         if (likely(sess->iv_length)) {
    2597                 :            :                 flags |= ROC_SE_VALID_IV_BUF;
    2598                 :          0 :                 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset);
    2599                 :            :         }
    2600                 :            : 
    2601                 :          0 :         m_src = sym_op->m_src;
    2602                 :          0 :         m_dst = sym_op->m_dst;
    2603                 :            : 
    2604                 :            :         d_offs = ci_data_offset;
    2605                 :            :         d_offs = (d_offs << 16);
    2606                 :            : 
    2607                 :            :         d_lens = ci_data_length;
    2608                 :            :         d_lens = (d_lens << 32);
    2609                 :            : 
    2610                 :            :         fc_params.ctx = &sess->roc_se_ctx;
    2611                 :            : 
    2612   [ #  #  #  # ]:          0 :         if (m_dst == NULL) {
    2613   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = fc_params.src_iov = (void *)src;
    2614                 :            :                 prepare_iov_from_pkt_inplace(m_src, &fc_params, &flags);
    2615                 :            :         } else {
    2616                 :            :                 /* Out of place processing */
    2617                 :            :                 fc_params.src_iov = (void *)src;
    2618   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = (void *)dst;
    2619                 :            : 
    2620                 :            :                 /* Store SG I/O in the api for reuse */
    2621                 :            :                 if (prepare_iov_from_pkt(m_src, fc_params.src_iov, 0, false, is_sg_ver2)) {
    2622                 :            :                         plt_dp_err("Prepare src iov failed");
    2623                 :            :                         ret = -EINVAL;
    2624                 :            :                         goto err_exit;
    2625                 :            :                 }
    2626                 :            : 
    2627                 :            :                 if (prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0, false, is_sg_ver2)) {
    2628                 :            :                         plt_dp_err("Prepare dst iov failed for m_dst %p", m_dst);
    2629                 :            :                         ret = -EINVAL;
    2630                 :            :                         goto err_exit;
    2631                 :            :                 }
    2632                 :            :         }
    2633                 :            : 
    2634                 :            :         fc_params.meta_buf.vaddr = NULL;
    2635                 :            : 
    2636   [ #  #  #  # ]:          0 :         if (unlikely(!((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
    2637                 :            :                        (flags & ROC_SE_SINGLE_BUF_HEADROOM)))) {
    2638   [ #  #  #  # ]:          0 :                 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
    2639   [ #  #  #  # ]:          0 :                 if (mdata == NULL) {
    2640                 :          0 :                         plt_dp_err("Error allocating meta buffer for request");
    2641                 :          0 :                         return -ENOMEM;
    2642                 :            :                 }
    2643                 :            :         }
    2644                 :            : 
    2645                 :            :         /* Finally prepare the instruction */
    2646                 :            :         ret = cpt_sm_prep(flags, d_offs, d_lens, &fc_params, inst, is_sg_ver2,
    2647                 :            :                           !(cpt_op & ROC_SE_OP_ENCODE));
    2648                 :            : 
    2649   [ #  #  #  # ]:          0 :         if (unlikely(ret)) {
    2650                 :          0 :                 plt_dp_err("Preparing request failed due to bad input arg");
    2651                 :          0 :                 goto free_mdata_and_exit;
    2652                 :            :         }
    2653                 :            : 
    2654                 :            :         return 0;
    2655                 :            : 
    2656                 :            : free_mdata_and_exit:
    2657   [ #  #  #  # ]:          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
    2658   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
    2659                 :          0 : err_exit:
    2660                 :            :         return ret;
    2661                 :            : }
    2662                 :            : 
    2663                 :            : static __rte_always_inline int
    2664                 :            : fill_fc_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
    2665                 :            :                struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    2666                 :            :                struct cpt_inst_s *inst, const bool is_kasumi, const bool is_aead,
    2667                 :            :                const bool is_sg_ver2)
    2668                 :            : {
    2669                 :            :         struct rte_crypto_sym_op *sym_op = cop->sym;
    2670                 :            :         void *mdata = NULL;
    2671                 :            :         uint32_t mc_hash_off;
    2672                 :            :         uint32_t flags = 0;
    2673                 :            :         uint64_t d_offs, d_lens;
    2674                 :            :         struct rte_mbuf *m_src, *m_dst;
    2675                 :          0 :         uint8_t cpt_op = sess->cpt_op;
    2676                 :            : #ifdef CPT_ALWAYS_USE_SG_MODE
    2677                 :            :         uint8_t inplace = 0;
    2678                 :            : #else
    2679                 :            :         uint8_t inplace = 1;
    2680                 :            : #endif
    2681                 :            :         struct roc_se_fc_params fc_params;
    2682                 :            :         char src[SRC_IOV_SIZE];
    2683                 :            :         char dst[SRC_IOV_SIZE];
    2684                 :            :         uint8_t ccm_iv_buf[16];
    2685                 :            :         uint32_t iv_buf[4];
    2686                 :            :         int ret;
    2687                 :            : 
    2688                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    2689                 :          0 :         fc_params.auth_iv_len = 0;
    2690                 :          0 :         fc_params.auth_iv_buf = NULL;
    2691                 :          0 :         fc_params.iv_buf = NULL;
    2692                 :          0 :         fc_params.mac_buf.size = 0;
    2693                 :          0 :         fc_params.mac_buf.vaddr = 0;
    2694                 :            : 
    2695                 :          0 :         if (likely(is_kasumi || sess->iv_length)) {
    2696                 :            :                 flags |= ROC_SE_VALID_IV_BUF;
    2697                 :          0 :                 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset);
    2698   [ #  #  #  #  :          0 :                 if (sess->short_iv) {
             #  #  #  # ]
    2699                 :            :                         memcpy((uint8_t *)iv_buf,
    2700                 :            :                                rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset), 12);
    2701                 :          0 :                         iv_buf[3] = rte_cpu_to_be_32(0x1);
    2702                 :          0 :                         fc_params.iv_buf = iv_buf;
    2703                 :            :                 }
    2704   [ #  #  #  #  :          0 :                 if (sess->aes_ccm) {
          #  #  #  #  #  
                #  #  # ]
    2705                 :          0 :                         memcpy((uint8_t *)ccm_iv_buf,
    2706                 :            :                                rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset),
    2707                 :          0 :                                sess->iv_length + 1);
    2708                 :          0 :                         ccm_iv_buf[0] = 14 - sess->iv_length;
    2709                 :          0 :                         fc_params.iv_buf = ccm_iv_buf;
    2710                 :            :                 }
    2711                 :            :         }
    2712                 :            : 
    2713                 :            :         /* Kasumi would need SG mode */
    2714                 :            :         if (is_kasumi)
    2715                 :            :                 inplace = 0;
    2716                 :            : 
    2717                 :          0 :         m_src = sym_op->m_src;
    2718                 :          0 :         m_dst = sym_op->m_dst;
    2719                 :            : 
    2720                 :            :         if (is_aead) {
    2721                 :            :                 struct rte_mbuf *m;
    2722                 :            :                 uint8_t *aad_data;
    2723                 :            :                 uint16_t aad_len;
    2724                 :            : 
    2725                 :          0 :                 d_offs = sym_op->aead.data.offset;
    2726                 :          0 :                 d_lens = sym_op->aead.data.length;
    2727                 :          0 :                 mc_hash_off =
    2728                 :            :                         sym_op->aead.data.offset + sym_op->aead.data.length;
    2729                 :            : 
    2730                 :          0 :                 aad_data = sym_op->aead.aad.data;
    2731                 :          0 :                 aad_len = sess->aad_length;
    2732   [ #  #  #  #  :          0 :                 if (likely((aad_len == 0) ||
             #  #  #  # ]
    2733                 :            :                            ((aad_data + aad_len) ==
    2734                 :            :                             rte_pktmbuf_mtod_offset(m_src, uint8_t *, sym_op->aead.data.offset)))) {
    2735                 :          0 :                         d_offs = (d_offs - aad_len) | (d_offs << 16);
    2736                 :          0 :                         d_lens = (d_lens + aad_len) | (d_lens << 32);
    2737                 :            :                 } else {
    2738                 :            :                         /* For AES CCM, AAD is written 18B after aad.data as per API */
    2739   [ #  #  #  # ]:          0 :                         if (sess->aes_ccm)
    2740                 :          0 :                                 fc_params.aad_buf.vaddr = PLT_PTR_ADD(sym_op->aead.aad.data, 18);
    2741                 :            :                         else
    2742                 :          0 :                                 fc_params.aad_buf.vaddr = sym_op->aead.aad.data;
    2743                 :          0 :                         fc_params.aad_buf.size = aad_len;
    2744                 :          0 :                         flags |= ROC_SE_VALID_AAD_BUF;
    2745                 :            :                         inplace = 0;
    2746                 :          0 :                         d_offs = d_offs << 16;
    2747                 :          0 :                         d_lens = d_lens << 32;
    2748                 :            :                 }
    2749                 :            : 
    2750                 :          0 :                 m = cpt_m_dst_get(cpt_op, m_src, m_dst);
    2751                 :            : 
    2752                 :            :                 /* Digest immediately following data is best case */
    2753   [ #  #  #  # ]:          0 :                 if (unlikely(rte_pktmbuf_mtod_offset(m, uint8_t *, mc_hash_off) !=
    2754                 :            :                              (uint8_t *)sym_op->aead.digest.data)) {
    2755                 :          0 :                         flags |= ROC_SE_VALID_MAC_BUF;
    2756                 :          0 :                         fc_params.mac_buf.size = sess->mac_len;
    2757                 :          0 :                         fc_params.mac_buf.vaddr = sym_op->aead.digest.data;
    2758                 :            :                         inplace = 0;
    2759                 :            :                 }
    2760                 :            :         } else {
    2761                 :          0 :                 uint32_t ci_data_length = sym_op->cipher.data.length;
    2762                 :          0 :                 uint32_t ci_data_offset = sym_op->cipher.data.offset;
    2763                 :          0 :                 uint32_t a_data_length = sym_op->auth.data.length;
    2764                 :          0 :                 uint32_t a_data_offset = sym_op->auth.data.offset;
    2765                 :            :                 struct roc_se_ctx *ctx = &sess->roc_se_ctx;
    2766                 :            : 
    2767                 :          0 :                 const uint8_t op_minor = ctx->template_w4.s.opcode_minor;
    2768                 :            : 
    2769                 :          0 :                 d_offs = ci_data_offset;
    2770                 :          0 :                 d_offs = (d_offs << 16) | a_data_offset;
    2771                 :            : 
    2772                 :          0 :                 d_lens = ci_data_length;
    2773                 :          0 :                 d_lens = (d_lens << 32) | a_data_length;
    2774                 :            : 
    2775   [ #  #  #  #  :          0 :                 if (likely(sess->mac_len)) {
             #  #  #  # ]
    2776                 :          0 :                         struct rte_mbuf *m = cpt_m_dst_get(cpt_op, m_src, m_dst);
    2777                 :            : 
    2778   [ #  #  #  #  :          0 :                         if (sess->auth_first)
             #  #  #  # ]
    2779                 :          0 :                                 mc_hash_off = a_data_offset + a_data_length;
    2780                 :            :                         else
    2781                 :          0 :                                 mc_hash_off = ci_data_offset + ci_data_length;
    2782                 :            : 
    2783                 :          0 :                         if (mc_hash_off < (a_data_offset + a_data_length))
    2784                 :            :                                 mc_hash_off = (a_data_offset + a_data_length);
    2785                 :            : 
    2786                 :            :                         /* hmac immediately following data is best case */
    2787   [ #  #  #  #  :          0 :                         if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST) &&
             #  #  #  # ]
    2788   [ #  #  #  #  :          0 :                             (unlikely(rte_pktmbuf_mtod_offset(m, uint8_t *, mc_hash_off) !=
             #  #  #  # ]
    2789                 :            :                                       (uint8_t *)sym_op->auth.digest.data))) {
    2790                 :          0 :                                 flags |= ROC_SE_VALID_MAC_BUF;
    2791                 :          0 :                                 fc_params.mac_buf.size = sess->mac_len;
    2792                 :            :                                 fc_params.mac_buf.vaddr =
    2793                 :            :                                         sym_op->auth.digest.data;
    2794                 :            :                                 inplace = 0;
    2795                 :            :                         }
    2796                 :            :                 }
    2797                 :            :         }
    2798                 :          0 :         fc_params.ctx = &sess->roc_se_ctx;
    2799                 :            : 
    2800   [ #  #  #  #  :          0 :         if (!(sess->auth_first) && unlikely(sess->is_null || sess->cpt_op == ROC_SE_OP_DECODE))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2801                 :            :                 inplace = 0;
    2802                 :            : 
    2803   [ #  #  #  #  :          0 :         if (likely(!m_dst && inplace)) {
             #  #  #  # ]
    2804                 :            :                 /* Case of single buffer without AAD buf or
    2805                 :            :                  * separate mac buf in place and
    2806                 :            :                  * not air crypto
    2807                 :            :                  */
    2808   [ #  #  #  #  :          0 :                 fc_params.dst_iov = fc_params.src_iov = (void *)src;
             #  #  #  # ]
    2809                 :            : 
    2810                 :            :                 prepare_iov_from_pkt_inplace(m_src, &fc_params, &flags);
    2811                 :            : 
    2812                 :            :         } else {
    2813                 :            :                 /* Out of place processing */
    2814                 :          0 :                 fc_params.src_iov = (void *)src;
    2815   [ #  #  #  #  :          0 :                 fc_params.dst_iov = (void *)dst;
             #  #  #  # ]
    2816                 :            : 
    2817                 :            :                 /* Store SG I/O in the api for reuse */
    2818                 :            :                 if (prepare_iov_from_pkt(m_src, fc_params.src_iov, 0, is_aead, is_sg_ver2)) {
    2819                 :            :                         plt_dp_err("Prepare src iov failed");
    2820                 :            :                         ret = -EINVAL;
    2821                 :            :                         goto err_exit;
    2822                 :            :                 }
    2823                 :            : 
    2824   [ #  #  #  #  :          0 :                 if (unlikely(m_dst != NULL)) {
          #  #  #  #  #  
                #  #  # ]
    2825                 :            :                         if (prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0, is_aead,
    2826                 :            :                                                  is_sg_ver2)) {
    2827                 :            :                                 plt_dp_err("Prepare dst iov failed for "
    2828                 :            :                                            "m_dst %p",
    2829                 :            :                                            m_dst);
    2830                 :            :                                 ret = -EINVAL;
    2831                 :            :                                 goto err_exit;
    2832                 :            :                         }
    2833                 :            :                 } else {
    2834                 :          0 :                         fc_params.dst_iov = (void *)src;
    2835                 :            :                 }
    2836                 :            :         }
    2837                 :            : 
    2838                 :          0 :         fc_params.meta_buf.vaddr = NULL;
    2839   [ #  #  #  #  :          0 :         if (unlikely(is_kasumi || !((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
             #  #  #  # ]
    2840                 :            :                                     (flags & ROC_SE_SINGLE_BUF_HEADROOM)))) {
    2841   [ #  #  #  #  :          0 :                 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
          #  #  #  #  #  
                #  #  # ]
    2842   [ #  #  #  #  :          0 :                 if (mdata == NULL) {
          #  #  #  #  #  
                #  #  # ]
    2843                 :          0 :                         plt_dp_err("Error allocating meta buffer for request");
    2844                 :          0 :                         return -ENOMEM;
    2845                 :            :                 }
    2846                 :            :         }
    2847                 :            : 
    2848                 :            :         /* Finally prepare the instruction */
    2849                 :            : 
    2850                 :            :         if (is_kasumi) {
    2851   [ #  #  #  # ]:          0 :                 if (cpt_op & ROC_SE_OP_ENCODE)
    2852                 :            :                         ret = cpt_kasumi_enc_prep(flags, d_offs, d_lens, &fc_params, inst,
    2853                 :            :                                                   is_sg_ver2);
    2854                 :            :                 else
    2855                 :            :                         ret = cpt_kasumi_dec_prep(d_offs, d_lens, &fc_params, inst, is_sg_ver2);
    2856                 :            :         } else {
    2857   [ #  #  #  #  :          0 :                 if (cpt_op & ROC_SE_OP_ENCODE)
             #  #  #  # ]
    2858                 :            :                         ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    2859                 :            :                                                 is_sg_ver2);
    2860                 :            :                 else
    2861                 :            :                         ret = cpt_dec_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    2862                 :            :                                                 is_sg_ver2);
    2863                 :            :         }
    2864                 :            : 
    2865   [ #  #  #  #  :          0 :         if (unlikely(ret)) {
          #  #  #  #  #  
                #  #  # ]
    2866                 :          0 :                 plt_dp_err("Preparing request failed due to bad input arg");
    2867                 :          0 :                 goto free_mdata_and_exit;
    2868                 :            :         }
    2869                 :            : 
    2870                 :            :         return 0;
    2871                 :            : 
    2872                 :            : free_mdata_and_exit:
    2873   [ #  #  #  #  :          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
          #  #  #  #  #  
                #  #  # ]
    2874   [ #  #  #  #  :          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
          #  #  #  #  #  
                #  #  # ]
    2875                 :          0 : err_exit:
    2876                 :            :         return ret;
    2877                 :            : }
    2878                 :            : 
    2879                 :            : static inline int
    2880                 :          0 : fill_passthrough_params(struct rte_crypto_op *cop, struct cpt_inst_s *inst)
    2881                 :            : {
    2882                 :            :         struct rte_crypto_sym_op *sym_op = cop->sym;
    2883                 :            :         struct rte_mbuf *m_src, *m_dst;
    2884                 :            : 
    2885                 :            :         const union cpt_inst_w4 w4 = {
    2886                 :            :                 .s.opcode_major = ROC_SE_MAJOR_OP_MISC,
    2887                 :            :                 .s.opcode_minor = ROC_SE_MISC_MINOR_OP_PASSTHROUGH,
    2888                 :            :                 .s.param1 = 1,
    2889                 :            :                 .s.param2 = 1,
    2890                 :            :                 .s.dlen = 0,
    2891                 :            :         };
    2892                 :            : 
    2893                 :          0 :         m_src = sym_op->m_src;
    2894                 :          0 :         m_dst = sym_op->m_dst;
    2895                 :            : 
    2896         [ #  # ]:          0 :         if (unlikely(m_dst != NULL && m_dst != m_src)) {
    2897                 :          0 :                 void *src = rte_pktmbuf_mtod_offset(m_src, void *, cop->sym->cipher.data.offset);
    2898                 :          0 :                 void *dst = rte_pktmbuf_mtod(m_dst, void *);
    2899                 :          0 :                 int data_len = cop->sym->cipher.data.length;
    2900                 :            : 
    2901         [ #  # ]:          0 :                 rte_memcpy(dst, src, data_len);
    2902                 :            :         }
    2903                 :            : 
    2904                 :          0 :         inst->w0.u64 = 0;
    2905                 :          0 :         inst->w5.u64 = 0;
    2906                 :          0 :         inst->w6.u64 = 0;
    2907                 :          0 :         inst->w4.u64 = w4.u64;
    2908                 :            : 
    2909                 :          0 :         return 0;
    2910                 :            : }
    2911                 :            : 
    2912                 :            : static __rte_always_inline int
    2913                 :            : fill_pdcp_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
    2914                 :            :                  struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    2915                 :            :                  struct cpt_inst_s *inst, const bool is_sg_ver2)
    2916                 :            : {
    2917                 :            :         struct rte_crypto_sym_op *sym_op = cop->sym;
    2918                 :            :         struct roc_se_fc_params fc_params;
    2919                 :            :         uint32_t c_data_len, c_data_off;
    2920                 :            :         struct rte_mbuf *m_src, *m_dst;
    2921                 :            :         uint64_t d_offs, d_lens;
    2922                 :            :         char src[SRC_IOV_SIZE];
    2923                 :            :         char dst[SRC_IOV_SIZE];
    2924                 :            :         void *mdata = NULL;
    2925                 :            :         uint32_t flags = 0;
    2926                 :            :         int ret;
    2927                 :            : 
    2928                 :            :         /* Cipher only */
    2929                 :            : 
    2930                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    2931                 :            :         fc_params.auth_iv_len = 0;
    2932                 :            :         fc_params.iv_buf = NULL;
    2933                 :            :         fc_params.auth_iv_buf = NULL;
    2934                 :          0 :         fc_params.pdcp_iv_offset = sess->roc_se_ctx.pdcp_iv_offset;
    2935                 :            : 
    2936                 :          0 :         if (likely(sess->iv_length))
    2937                 :          0 :                 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset);
    2938                 :            : 
    2939                 :          0 :         m_src = sym_op->m_src;
    2940                 :          0 :         m_dst = sym_op->m_dst;
    2941                 :            : 
    2942                 :          0 :         c_data_len = sym_op->cipher.data.length;
    2943                 :          0 :         c_data_off = sym_op->cipher.data.offset;
    2944                 :            : 
    2945                 :            :         d_offs = (uint64_t)c_data_off << 16;
    2946                 :            :         d_lens = (uint64_t)c_data_len << 32;
    2947                 :            : 
    2948                 :            :         fc_params.ctx = &sess->roc_se_ctx;
    2949                 :            : 
    2950   [ #  #  #  # ]:          0 :         if (likely(m_dst == NULL || m_src == m_dst)) {
    2951   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = fc_params.src_iov = (void *)src;
    2952                 :            :                 prepare_iov_from_pkt_inplace(m_src, &fc_params, &flags);
    2953                 :            :         } else {
    2954                 :            :                 /* Out of place processing */
    2955                 :            : 
    2956                 :            :                 fc_params.src_iov = (void *)src;
    2957   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = (void *)dst;
    2958                 :            : 
    2959                 :            :                 /* Store SG I/O in the api for reuse */
    2960                 :            :                 if (unlikely(
    2961                 :            :                             prepare_iov_from_pkt(m_src, fc_params.src_iov, 0, false, is_sg_ver2))) {
    2962                 :            :                         plt_dp_err("Prepare src iov failed");
    2963                 :            :                         ret = -EINVAL;
    2964                 :            :                         goto err_exit;
    2965                 :            :                 }
    2966                 :            : 
    2967                 :            :                 if (unlikely(
    2968                 :            :                             prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0, false, is_sg_ver2))) {
    2969                 :            :                         plt_dp_err("Prepare dst iov failed for m_dst %p", m_dst);
    2970                 :            :                         ret = -EINVAL;
    2971                 :            :                         goto err_exit;
    2972                 :            :                 }
    2973                 :            :         }
    2974                 :            : 
    2975                 :            :         fc_params.meta_buf.vaddr = NULL;
    2976   [ #  #  #  # ]:          0 :         if (unlikely(!((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
    2977                 :            :                        (flags & ROC_SE_SINGLE_BUF_HEADROOM)))) {
    2978   [ #  #  #  # ]:          0 :                 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
    2979   [ #  #  #  # ]:          0 :                 if (mdata == NULL) {
    2980                 :          0 :                         plt_dp_err("Could not allocate meta buffer");
    2981                 :            :                         ret = -ENOMEM;
    2982                 :          0 :                         goto err_exit;
    2983                 :            :                 }
    2984                 :            :         }
    2985                 :            : 
    2986                 :            :         ret = cpt_pdcp_alg_prep(flags, d_offs, d_lens, &fc_params, inst, is_sg_ver2);
    2987   [ #  #  #  # ]:          0 :         if (unlikely(ret)) {
    2988                 :          0 :                 plt_dp_err("Could not prepare instruction");
    2989                 :          0 :                 goto free_mdata_and_exit;
    2990                 :            :         }
    2991                 :            : 
    2992                 :            :         return 0;
    2993                 :            : 
    2994                 :            : free_mdata_and_exit:
    2995   [ #  #  #  # ]:          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
    2996   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
    2997                 :          0 : err_exit:
    2998                 :            :         return ret;
    2999                 :            : }
    3000                 :            : 
    3001                 :            : static __rte_always_inline int
    3002                 :            : fill_pdcp_chain_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
    3003                 :            :                        struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    3004                 :            :                        struct cpt_inst_s *inst, const bool is_sg_ver2)
    3005                 :            : {
    3006                 :            :         uint32_t ci_data_length, ci_data_offset, a_data_length, a_data_offset;
    3007                 :          0 :         struct rte_crypto_sym_op *sym_op = cop->sym;
    3008                 :            :         struct roc_se_fc_params fc_params;
    3009                 :            :         struct rte_mbuf *m_src, *m_dst;
    3010                 :          0 :         uint8_t cpt_op = sess->cpt_op;
    3011                 :            :         uint64_t d_offs, d_lens;
    3012                 :            :         char src[SRC_IOV_SIZE];
    3013                 :            :         char dst[SRC_IOV_SIZE];
    3014                 :          0 :         bool inplace = true;
    3015                 :          0 :         uint32_t flags = 0;
    3016                 :            :         void *mdata;
    3017                 :            :         int ret;
    3018                 :            : 
    3019                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    3020                 :          0 :         fc_params.auth_iv_len = sess->auth_iv_length;
    3021                 :          0 :         fc_params.iv_buf = NULL;
    3022                 :          0 :         fc_params.auth_iv_buf = NULL;
    3023                 :          0 :         fc_params.pdcp_iv_offset = sess->roc_se_ctx.pdcp_iv_offset;
    3024                 :            : 
    3025                 :          0 :         m_src = sym_op->m_src;
    3026                 :          0 :         m_dst = sym_op->m_dst;
    3027                 :            : 
    3028                 :          0 :         if (likely(sess->iv_length))
    3029                 :          0 :                 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset);
    3030                 :            : 
    3031                 :          0 :         ci_data_length = sym_op->cipher.data.length;
    3032                 :          0 :         ci_data_offset = sym_op->cipher.data.offset;
    3033                 :          0 :         a_data_length = sym_op->auth.data.length;
    3034                 :          0 :         a_data_offset = sym_op->auth.data.offset;
    3035                 :            : 
    3036                 :            :         /*
    3037                 :            :          * For ZUC & SNOW, length & offset is provided in bits. Convert to
    3038                 :            :          * bytes.
    3039                 :            :          */
    3040                 :            : 
    3041   [ #  #  #  # ]:          0 :         if (sess->zs_cipher) {
    3042                 :          0 :                 ci_data_length /= 8;
    3043                 :          0 :                 ci_data_offset /= 8;
    3044                 :            :         }
    3045                 :            : 
    3046   [ #  #  #  # ]:          0 :         if (sess->zs_auth) {
    3047                 :          0 :                 a_data_length /= 8;
    3048                 :          0 :                 a_data_offset /= 8;
    3049                 :            :                 /*
    3050                 :            :                  * ZUC & SNOW would have valid iv_buf. AES-CMAC doesn't require
    3051                 :            :                  * IV from application.
    3052                 :            :                  */
    3053                 :          0 :                 fc_params.auth_iv_buf =
    3054                 :          0 :                         rte_crypto_op_ctod_offset(cop, uint8_t *, sess->auth_iv_offset);
    3055                 :            : #ifdef CNXK_CRYPTODEV_DEBUG
    3056                 :            :                 if (sess->auth_iv_length == 0)
    3057                 :            :                         plt_err("Invalid auth IV length");
    3058                 :            : #endif
    3059                 :            :         }
    3060                 :            : 
    3061                 :          0 :         d_offs = ci_data_offset;
    3062                 :          0 :         d_offs = (d_offs << 16) | a_data_offset;
    3063                 :          0 :         d_lens = ci_data_length;
    3064                 :          0 :         d_lens = (d_lens << 32) | a_data_length;
    3065                 :            : 
    3066   [ #  #  #  # ]:          0 :         if (likely(sess->mac_len)) {
    3067                 :          0 :                 struct rte_mbuf *m = cpt_m_dst_get(cpt_op, m_src, m_dst);
    3068                 :            : 
    3069                 :          0 :                 cpt_digest_buf_lb_check(sess, m, &fc_params, &flags, sym_op, &inplace,
    3070                 :            :                                         a_data_offset, a_data_length, ci_data_offset,
    3071                 :            :                                         ci_data_length, true);
    3072                 :            :         }
    3073                 :            : 
    3074                 :          0 :         fc_params.ctx = &sess->roc_se_ctx;
    3075                 :            : 
    3076   [ #  #  #  #  :          0 :         if (likely((m_dst == NULL || m_dst == m_src)) && inplace) {
             #  #  #  # ]
    3077   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = fc_params.src_iov = (void *)src;
    3078                 :            :                 prepare_iov_from_pkt_inplace(m_src, &fc_params, &flags);
    3079                 :            :         } else {
    3080                 :            :                 /* Out of place processing */
    3081                 :          0 :                 fc_params.src_iov = (void *)src;
    3082   [ #  #  #  # ]:          0 :                 fc_params.dst_iov = (void *)dst;
    3083                 :            : 
    3084                 :            :                 /* Store SG I/O in the api for reuse */
    3085                 :            :                 if (unlikely(
    3086                 :            :                             prepare_iov_from_pkt(m_src, fc_params.src_iov, 0, false, is_sg_ver2))) {
    3087                 :            :                         plt_dp_err("Could not prepare src iov");
    3088                 :            :                         ret = -EINVAL;
    3089                 :            :                         goto err_exit;
    3090                 :            :                 }
    3091                 :            : 
    3092   [ #  #  #  # ]:          0 :                 if (unlikely(m_dst != NULL)) {
    3093                 :            :                         if (unlikely(prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0, false,
    3094                 :            :                                                           is_sg_ver2))) {
    3095                 :            :                                 plt_dp_err("Could not prepare m_dst iov %p", m_dst);
    3096                 :            :                                 ret = -EINVAL;
    3097                 :            :                                 goto err_exit;
    3098                 :            :                         }
    3099                 :            :                 } else {
    3100                 :          0 :                         fc_params.dst_iov = (void *)src;
    3101                 :            :                 }
    3102                 :            :         }
    3103                 :            : 
    3104   [ #  #  #  # ]:          0 :         if (unlikely(!((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
    3105                 :            :                        (flags & ROC_SE_SINGLE_BUF_HEADROOM)))) {
    3106   [ #  #  #  # ]:          0 :                 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
    3107   [ #  #  #  # ]:          0 :                 if (unlikely(mdata == NULL)) {
    3108                 :          0 :                         plt_dp_err("Could not allocate meta buffer for request");
    3109                 :          0 :                         return -ENOMEM;
    3110                 :            :                 }
    3111                 :            :         }
    3112                 :            : 
    3113                 :            :         /* Finally prepare the instruction */
    3114   [ #  #  #  # ]:          0 :         ret = cpt_pdcp_chain_alg_prep(flags, d_offs, d_lens, &fc_params, inst, is_sg_ver2);
    3115   [ #  #  #  # ]:          0 :         if (unlikely(ret)) {
    3116                 :          0 :                 plt_dp_err("Could not prepare instruction");
    3117                 :          0 :                 goto free_mdata_and_exit;
    3118                 :            :         }
    3119                 :            : 
    3120                 :            :         return 0;
    3121                 :            : 
    3122                 :            : free_mdata_and_exit:
    3123   [ #  #  #  # ]:          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
    3124   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
    3125                 :          0 : err_exit:
    3126                 :            :         return ret;
    3127                 :            : }
    3128                 :            : 
    3129                 :            : static __rte_always_inline void
    3130                 :            : compl_auth_verify(struct rte_crypto_op *op, uint8_t *gen_mac, uint64_t mac_len)
    3131                 :            : {
    3132                 :            :         uint8_t *mac;
    3133                 :            :         struct rte_crypto_sym_op *sym_op = op->sym;
    3134                 :            : 
    3135         [ #  # ]:          0 :         if (sym_op->auth.digest.data)
    3136                 :            :                 mac = sym_op->auth.digest.data;
    3137                 :            :         else
    3138                 :          0 :                 mac = rte_pktmbuf_mtod_offset(sym_op->m_src, uint8_t *,
    3139                 :            :                                               sym_op->auth.data.length +
    3140                 :            :                                                       sym_op->auth.data.offset);
    3141         [ #  # ]:          0 :         if (!mac) {
    3142                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3143                 :          0 :                 return;
    3144                 :            :         }
    3145                 :            : 
    3146         [ #  # ]:          0 :         if (memcmp(mac, gen_mac, mac_len))
    3147                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
    3148                 :            :         else
    3149                 :            :                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3150                 :            : }
    3151                 :            : 
    3152                 :            : static __rte_always_inline void
    3153                 :            : find_kasumif9_direction_and_length(uint8_t *src, uint32_t counter_num_bytes,
    3154                 :            :                                    uint32_t *addr_length_in_bits,
    3155                 :            :                                    uint8_t *addr_direction)
    3156                 :            : {
    3157                 :            :         uint8_t found = 0;
    3158                 :            :         uint32_t pos;
    3159                 :            :         uint8_t last_byte;
    3160   [ #  #  #  # ]:          0 :         while (!found && counter_num_bytes > 0) {
    3161                 :          0 :                 counter_num_bytes--;
    3162   [ #  #  #  # ]:          0 :                 if (src[counter_num_bytes] == 0x00)
    3163                 :          0 :                         continue;
    3164                 :          0 :                 pos = rte_bsf32(src[counter_num_bytes]);
    3165   [ #  #  #  # ]:          0 :                 if (pos == 7) {
    3166   [ #  #  #  # ]:          0 :                         if (likely(counter_num_bytes > 0)) {
    3167                 :          0 :                                 last_byte = src[counter_num_bytes - 1];
    3168                 :          0 :                                 *addr_direction = last_byte & 0x1;
    3169                 :            :                                 *addr_length_in_bits =
    3170                 :          0 :                                         counter_num_bytes * 8 - 1;
    3171                 :            :                         }
    3172                 :            :                 } else {
    3173                 :          0 :                         last_byte = src[counter_num_bytes];
    3174                 :          0 :                         *addr_direction = (last_byte >> (pos + 1)) & 0x1;
    3175                 :            :                         *addr_length_in_bits =
    3176                 :          0 :                                 counter_num_bytes * 8 + (8 - (pos + 2));
    3177                 :            :                 }
    3178                 :            :                 found = 1;
    3179                 :            :         }
    3180                 :            : }
    3181                 :            : 
    3182                 :            : /*
    3183                 :            :  * This handles all auth only except AES_GMAC
    3184                 :            :  */
    3185                 :            : static __rte_always_inline int
    3186                 :            : fill_digest_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
    3187                 :            :                    struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    3188                 :            :                    struct cpt_inst_s *inst, const bool is_sg_ver2)
    3189                 :            : {
    3190                 :            :         uint32_t space = 0;
    3191                 :            :         struct rte_crypto_sym_op *sym_op = cop->sym;
    3192                 :            :         void *mdata;
    3193                 :            :         uint32_t auth_range_off;
    3194                 :            :         uint32_t flags = 0;
    3195                 :            :         uint64_t d_offs = 0, d_lens;
    3196                 :            :         struct rte_mbuf *m_src, *m_dst;
    3197                 :          0 :         uint16_t auth_op = sess->cpt_op & ROC_SE_OP_AUTH_MASK;
    3198                 :          0 :         uint16_t mac_len = sess->mac_len;
    3199                 :            :         struct roc_se_fc_params params;
    3200                 :            :         char src[SRC_IOV_SIZE];
    3201                 :            :         uint8_t iv_buf[16];
    3202                 :            :         int ret;
    3203                 :            : 
    3204                 :            :         memset(&params, 0, sizeof(struct roc_se_fc_params));
    3205                 :            : 
    3206                 :          0 :         m_src = sym_op->m_src;
    3207                 :            : 
    3208   [ #  #  #  # ]:          0 :         mdata = alloc_op_meta(&params.meta_buf, m_info->mlen, m_info->pool,
    3209                 :            :                               infl_req);
    3210   [ #  #  #  # ]:          0 :         if (mdata == NULL) {
    3211                 :            :                 ret = -ENOMEM;
    3212                 :          0 :                 goto err_exit;
    3213                 :            :         }
    3214                 :            : 
    3215                 :          0 :         auth_range_off = sym_op->auth.data.offset;
    3216                 :            : 
    3217                 :            :         flags = ROC_SE_VALID_MAC_BUF;
    3218                 :          0 :         params.src_iov = (void *)src;
    3219   [ #  #  #  # ]:          0 :         if (unlikely(sess->zsk_flag)) {
    3220                 :            :                 /*
    3221                 :            :                  * Since for Zuc, Kasumi, Snow3g offsets are in bits
    3222                 :            :                  * we will send pass through even for auth only case,
    3223                 :            :                  * let MC handle it
    3224                 :            :                  */
    3225                 :          0 :                 d_offs = auth_range_off;
    3226                 :            :                 auth_range_off = 0;
    3227                 :          0 :                 params.auth_iv_len = sess->auth_iv_length;
    3228                 :          0 :                 params.auth_iv_buf =
    3229                 :          0 :                         rte_crypto_op_ctod_offset(cop, uint8_t *, sess->auth_iv_offset);
    3230                 :          0 :                 params.pdcp_iv_offset = sess->roc_se_ctx.pdcp_iv_offset;
    3231   [ #  #  #  # ]:          0 :                 if (sess->zsk_flag == ROC_SE_K_F9) {
    3232                 :            :                         uint32_t length_in_bits, num_bytes;
    3233                 :            :                         uint8_t *src, direction = 0;
    3234                 :            : 
    3235                 :            :                         memcpy(iv_buf,
    3236                 :          0 :                                rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *), 8);
    3237                 :            :                         /*
    3238                 :            :                          * This is kasumi f9, take direction from
    3239                 :            :                          * source buffer
    3240                 :            :                          */
    3241                 :          0 :                         length_in_bits = cop->sym->auth.data.length;
    3242                 :          0 :                         num_bytes = (length_in_bits >> 3);
    3243                 :          0 :                         src = rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *);
    3244                 :            :                         find_kasumif9_direction_and_length(
    3245                 :            :                                 src, num_bytes, &length_in_bits, &direction);
    3246                 :          0 :                         length_in_bits -= 64;
    3247                 :          0 :                         cop->sym->auth.data.offset += 64;
    3248                 :          0 :                         d_offs = cop->sym->auth.data.offset;
    3249                 :          0 :                         auth_range_off = d_offs / 8;
    3250                 :          0 :                         cop->sym->auth.data.length = length_in_bits;
    3251                 :            : 
    3252                 :            :                         /* Store it at end of auth iv */
    3253                 :          0 :                         iv_buf[8] = direction;
    3254                 :          0 :                         params.auth_iv_buf = iv_buf;
    3255                 :            :                 }
    3256                 :            :         }
    3257                 :            : 
    3258                 :          0 :         d_lens = sym_op->auth.data.length;
    3259                 :            : 
    3260                 :          0 :         params.ctx = &sess->roc_se_ctx;
    3261                 :            : 
    3262   [ #  #  #  # ]:          0 :         if (auth_op == ROC_SE_OP_AUTH_GENERATE) {
    3263   [ #  #  #  # ]:          0 :                 if (sym_op->auth.digest.data) {
    3264                 :            :                         /*
    3265                 :            :                          * Digest to be generated
    3266                 :            :                          * in separate buffer
    3267                 :            :                          */
    3268                 :          0 :                         params.mac_buf.size = sess->mac_len;
    3269                 :          0 :                         params.mac_buf.vaddr = sym_op->auth.digest.data;
    3270                 :            :                 } else {
    3271                 :          0 :                         uint32_t off = sym_op->auth.data.offset +
    3272                 :            :                                        sym_op->auth.data.length;
    3273                 :            :                         int32_t dlen, space;
    3274                 :            : 
    3275   [ #  #  #  # ]:          0 :                         m_dst = sym_op->m_dst ? sym_op->m_dst : sym_op->m_src;
    3276                 :          0 :                         dlen = rte_pktmbuf_pkt_len(m_dst);
    3277                 :            : 
    3278                 :          0 :                         space = off + mac_len - dlen;
    3279   [ #  #  #  # ]:          0 :                         if (space > 0)
    3280   [ #  #  #  # ]:          0 :                                 if (!rte_pktmbuf_append(m_dst, space)) {
    3281                 :          0 :                                         plt_dp_err("Failed to extend "
    3282                 :            :                                                    "mbuf by %uB",
    3283                 :            :                                                    space);
    3284                 :            :                                         ret = -EINVAL;
    3285                 :          0 :                                         goto free_mdata_and_exit;
    3286                 :            :                                 }
    3287                 :            : 
    3288                 :          0 :                         params.mac_buf.vaddr =
    3289                 :          0 :                                 rte_pktmbuf_mtod_offset(m_dst, void *, off);
    3290                 :          0 :                         params.mac_buf.size = mac_len;
    3291                 :            :                 }
    3292                 :            :         } else {
    3293                 :            :                 uint64_t *op = mdata;
    3294                 :            : 
    3295                 :            :                 /* Need space for storing generated mac */
    3296                 :            :                 space += 2 * sizeof(uint64_t);
    3297                 :            : 
    3298                 :          0 :                 params.mac_buf.vaddr = (uint8_t *)mdata + space;
    3299                 :          0 :                 params.mac_buf.size = mac_len;
    3300                 :          0 :                 space += RTE_ALIGN_CEIL(mac_len, 8);
    3301                 :          0 :                 op[0] = (uintptr_t)params.mac_buf.vaddr;
    3302                 :          0 :                 op[1] = mac_len;
    3303                 :          0 :                 infl_req->op_flags |= CPT_OP_FLAGS_AUTH_VERIFY;
    3304                 :            :         }
    3305                 :            : 
    3306                 :          0 :         params.meta_buf.vaddr = (uint8_t *)mdata + space;
    3307   [ #  #  #  # ]:          0 :         params.meta_buf.size -= space;
    3308                 :            : 
    3309                 :            :         /* Out of place processing */
    3310                 :            :         params.src_iov = (void *)src;
    3311                 :            : 
    3312                 :            :         /*Store SG I/O in the api for reuse */
    3313                 :            :         if (prepare_iov_from_pkt(m_src, params.src_iov, auth_range_off, false, is_sg_ver2)) {
    3314                 :          0 :                 plt_dp_err("Prepare src iov failed");
    3315                 :            :                 ret = -EINVAL;
    3316                 :          0 :                 goto free_mdata_and_exit;
    3317                 :            :         }
    3318                 :            : 
    3319                 :            :         ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, &params, inst, is_sg_ver2);
    3320   [ #  #  #  # ]:          0 :         if (ret)
    3321                 :          0 :                 goto free_mdata_and_exit;
    3322                 :            : 
    3323                 :            :         return 0;
    3324                 :            : 
    3325                 :          0 : free_mdata_and_exit:
    3326   [ #  #  #  # ]:          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
    3327   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
    3328                 :          0 : err_exit:
    3329                 :            :         return ret;
    3330                 :            : }
    3331                 :            : 
    3332                 :            : static __rte_always_inline int __rte_hot
    3333                 :            : cpt_sym_inst_fill(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op, struct cnxk_se_sess *sess,
    3334                 :            :                   struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst, const bool is_sg_ver2)
    3335                 :            : {
    3336                 :            :         enum cpt_dp_thread_type dp_thr_type;
    3337                 :            :         int ret;
    3338                 :            : 
    3339                 :          0 :         dp_thr_type = sess->dp_thr_type;
    3340                 :            : 
    3341                 :            :         /*
    3342                 :            :          * With cipher only, microcode expects that cipher length is non-zero. To accept such
    3343                 :            :          * instructions, send to CPT as passthrough.
    3344                 :            :          */
    3345   [ #  #  #  #  :          0 :         if (unlikely(sess->cipher_only && op->sym->cipher.data.length == 0))
             #  #  #  # ]
    3346                 :            :                 dp_thr_type = CPT_DP_THREAD_TYPE_PT;
    3347                 :            : 
    3348   [ #  #  #  #  :          0 :         switch (dp_thr_type) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    3349                 :          0 :         case CPT_DP_THREAD_TYPE_PT:
    3350                 :          0 :                 ret = fill_passthrough_params(op, inst);
    3351                 :          0 :                 break;
    3352   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_PDCP:
    3353                 :            :                 ret = fill_pdcp_params(op, sess, &qp->meta_info, infl_req, inst, is_sg_ver2);
    3354                 :          0 :                 break;
    3355   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_FC_CHAIN:
    3356                 :            :                 ret = fill_fc_params(op, sess, &qp->meta_info, infl_req, inst, false, false,
    3357                 :            :                                      is_sg_ver2);
    3358                 :          0 :                 break;
    3359   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_FC_AEAD:
    3360                 :            :                 ret = fill_fc_params(op, sess, &qp->meta_info, infl_req, inst, false, true,
    3361                 :            :                                      is_sg_ver2);
    3362                 :          0 :                 break;
    3363   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_PDCP_CHAIN:
    3364                 :            :                 ret = fill_pdcp_chain_params(op, sess, &qp->meta_info, infl_req, inst, is_sg_ver2);
    3365                 :          0 :                 break;
    3366   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_KASUMI:
    3367                 :            :                 ret = fill_fc_params(op, sess, &qp->meta_info, infl_req, inst, true, false,
    3368                 :            :                                      is_sg_ver2);
    3369                 :          0 :                 break;
    3370   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_TYPE_SM:
    3371                 :            :                 ret = fill_sm_params(op, sess, &qp->meta_info, infl_req, inst, is_sg_ver2);
    3372                 :          0 :                 break;
    3373                 :            : 
    3374   [ #  #  #  # ]:          0 :         case CPT_DP_THREAD_AUTH_ONLY:
    3375                 :            :                 ret = fill_digest_params(op, sess, &qp->meta_info, infl_req, inst, is_sg_ver2);
    3376                 :          0 :                 break;
    3377                 :            :         default:
    3378                 :            :                 ret = -EINVAL;
    3379                 :            :         }
    3380                 :            : 
    3381                 :            :         return ret;
    3382                 :            : }
    3383                 :            : 
    3384                 :            : static __rte_always_inline uint32_t
    3385                 :            : prepare_iov_from_raw_vec(struct rte_crypto_vec *vec, struct roc_se_iov_ptr *iovec, uint32_t num)
    3386                 :            : {
    3387                 :            :         uint32_t i, total_len = 0;
    3388                 :            : 
    3389   [ #  #  #  #  :          0 :         for (i = 0; i < num; i++) {
          #  #  #  #  #  
                      # ]
    3390                 :          0 :                 iovec->bufs[i].vaddr = vec[i].base;
    3391                 :          0 :                 iovec->bufs[i].size = vec[i].len;
    3392                 :            : 
    3393                 :          0 :                 total_len += vec[i].len;
    3394                 :            :         }
    3395                 :            : 
    3396                 :          0 :         iovec->buf_cnt = i;
    3397                 :          0 :         return total_len;
    3398                 :            : }
    3399                 :            : 
    3400                 :            : static __rte_always_inline void
    3401                 :            : cnxk_raw_burst_to_iov(struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs *ofs, int index,
    3402                 :            :                       struct cnxk_iov *iov)
    3403                 :            : {
    3404                 :          0 :         iov->iv_buf = vec->iv[index].va;
    3405                 :          0 :         iov->aad_buf = vec->aad[index].va;
    3406                 :          0 :         iov->mac_buf = vec->digest[index].va;
    3407                 :            : 
    3408                 :          0 :         iov->data_len =
    3409                 :          0 :                 prepare_iov_from_raw_vec(vec->src_sgl[index].vec, (struct roc_se_iov_ptr *)iov->src,
    3410                 :          0 :                                          vec->src_sgl[index].num);
    3411                 :            : 
    3412         [ #  # ]:          0 :         if (vec->dest_sgl == NULL)
    3413                 :            :                 prepare_iov_from_raw_vec(vec->src_sgl[index].vec, (struct roc_se_iov_ptr *)iov->dst,
    3414                 :            :                                          vec->src_sgl[index].num);
    3415                 :            :         else
    3416                 :          0 :                 prepare_iov_from_raw_vec(vec->dest_sgl[index].vec,
    3417                 :            :                                          (struct roc_se_iov_ptr *)iov->dst,
    3418                 :          0 :                                          vec->dest_sgl[index].num);
    3419                 :            : 
    3420                 :          0 :         iov->c_head = ofs->ofs.cipher.head;
    3421                 :          0 :         iov->c_tail = ofs->ofs.cipher.tail;
    3422                 :            : 
    3423                 :          0 :         iov->a_head = ofs->ofs.auth.head;
    3424                 :          0 :         iov->a_tail = ofs->ofs.auth.tail;
    3425                 :            : }
    3426                 :            : 
    3427                 :            : static __rte_always_inline void
    3428                 :            : cnxk_raw_to_iov(struct rte_crypto_vec *data_vec, uint16_t n_vecs, union rte_crypto_sym_ofs *ofs,
    3429                 :            :                 struct rte_crypto_va_iova_ptr *iv, struct rte_crypto_va_iova_ptr *digest,
    3430                 :            :                 struct rte_crypto_va_iova_ptr *aad, struct cnxk_iov *iov)
    3431                 :            : {
    3432                 :          0 :         iov->iv_buf = iv->va;
    3433                 :          0 :         iov->aad_buf = aad->va;
    3434                 :          0 :         iov->mac_buf = digest->va;
    3435                 :            : 
    3436                 :          0 :         iov->data_len =
    3437                 :          0 :                 prepare_iov_from_raw_vec(data_vec, (struct roc_se_iov_ptr *)iov->src, n_vecs);
    3438                 :            :         prepare_iov_from_raw_vec(data_vec, (struct roc_se_iov_ptr *)iov->dst, n_vecs);
    3439                 :            : 
    3440                 :          0 :         iov->c_head = ofs->ofs.cipher.head;
    3441                 :          0 :         iov->c_tail = ofs->ofs.cipher.tail;
    3442                 :            : 
    3443                 :          0 :         iov->a_head = ofs->ofs.auth.head;
    3444         [ #  # ]:          0 :         iov->a_tail = ofs->ofs.auth.tail;
    3445                 :            : }
    3446                 :            : 
    3447                 :            : static inline void
    3448                 :          0 : raw_memcpy(struct cnxk_iov *iov)
    3449                 :            : {
    3450                 :            :         struct roc_se_iov_ptr *src = (struct roc_se_iov_ptr *)iov->src;
    3451                 :            :         struct roc_se_iov_ptr *dst = (struct roc_se_iov_ptr *)iov->dst;
    3452                 :          0 :         int num = src->buf_cnt;
    3453                 :            :         int i;
    3454                 :            : 
    3455                 :            :         /* skip copy in case of inplace */
    3456         [ #  # ]:          0 :         if (dst->bufs[0].vaddr == src->bufs[0].vaddr)
    3457                 :            :                 return;
    3458                 :            : 
    3459         [ #  # ]:          0 :         for (i = 0; i < num; i++) {
    3460         [ #  # ]:          0 :                 rte_memcpy(dst->bufs[i].vaddr, src->bufs[i].vaddr, src->bufs[i].size);
    3461                 :          0 :                 dst->bufs[i].size = src->bufs[i].size;
    3462                 :            :         }
    3463                 :            : }
    3464                 :            : 
    3465                 :            : static inline int
    3466                 :          0 : fill_raw_passthrough_params(struct cnxk_iov *iov, struct cpt_inst_s *inst)
    3467                 :            : {
    3468                 :            :         const union cpt_inst_w4 w4 = {
    3469                 :            :                 .s.opcode_major = ROC_SE_MAJOR_OP_MISC,
    3470                 :            :                 .s.opcode_minor = ROC_SE_MISC_MINOR_OP_PASSTHROUGH,
    3471                 :            :                 .s.param1 = 1,
    3472                 :            :                 .s.param2 = 1,
    3473                 :            :                 .s.dlen = 0,
    3474                 :            :         };
    3475                 :            : 
    3476                 :          0 :         inst->w0.u64 = 0;
    3477                 :          0 :         inst->w5.u64 = 0;
    3478                 :          0 :         inst->w4.u64 = w4.u64;
    3479                 :            : 
    3480                 :          0 :         raw_memcpy(iov);
    3481                 :            : 
    3482                 :          0 :         return 0;
    3483                 :            : }
    3484                 :            : 
    3485                 :            : static __rte_always_inline int
    3486                 :            : fill_raw_fc_params(struct cnxk_iov *iov, struct cnxk_se_sess *sess, struct cpt_qp_meta_info *m_info,
    3487                 :            :                    struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst, const bool is_kasumi,
    3488                 :            :                    const bool is_aead, const bool is_sg_ver2)
    3489                 :            : {
    3490                 :            :         uint32_t cipher_len, auth_len = 0;
    3491                 :            :         struct roc_se_fc_params fc_params;
    3492                 :          0 :         uint8_t cpt_op = sess->cpt_op;
    3493                 :            :         uint64_t d_offs, d_lens;
    3494                 :            :         uint8_t ccm_iv_buf[16];
    3495                 :            :         uint32_t flags = 0;
    3496                 :            :         void *mdata = NULL;
    3497                 :            :         uint32_t iv_buf[4];
    3498                 :            :         int ret;
    3499                 :            : 
    3500                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    3501                 :          0 :         fc_params.ctx = &sess->roc_se_ctx;
    3502                 :          0 :         fc_params.auth_iv_buf = NULL;
    3503                 :          0 :         fc_params.auth_iv_len = 0;
    3504                 :          0 :         fc_params.mac_buf.size = 0;
    3505                 :          0 :         fc_params.mac_buf.vaddr = 0;
    3506                 :          0 :         fc_params.iv_buf = NULL;
    3507                 :            : 
    3508   [ #  #  #  # ]:          0 :         if (likely(sess->iv_length)) {
    3509                 :            :                 flags |= ROC_SE_VALID_IV_BUF;
    3510                 :            : 
    3511   [ #  #  #  # ]:          0 :                 if (sess->is_gmac) {
    3512                 :          0 :                         fc_params.iv_buf = iov->aad_buf;
    3513   [ #  #  #  # ]:          0 :                         if (sess->short_iv) {
    3514                 :            :                                 memcpy((void *)iv_buf, iov->aad_buf, 12);
    3515                 :          0 :                                 iv_buf[3] = rte_cpu_to_be_32(0x1);
    3516                 :          0 :                                 fc_params.iv_buf = iv_buf;
    3517                 :            :                         }
    3518                 :            :                 } else {
    3519                 :          0 :                         fc_params.iv_buf = iov->iv_buf;
    3520   [ #  #  #  # ]:          0 :                         if (sess->short_iv) {
    3521                 :            :                                 memcpy((void *)iv_buf, iov->iv_buf, 12);
    3522                 :          0 :                                 iv_buf[3] = rte_cpu_to_be_32(0x1);
    3523                 :          0 :                                 fc_params.iv_buf = iv_buf;
    3524                 :            :                         }
    3525                 :            :                 }
    3526                 :            : 
    3527   [ #  #  #  # ]:          0 :                 if (sess->aes_ccm) {
    3528                 :          0 :                         memcpy((uint8_t *)ccm_iv_buf, iov->iv_buf, sess->iv_length + 1);
    3529                 :          0 :                         ccm_iv_buf[0] = 14 - sess->iv_length;
    3530                 :          0 :                         fc_params.iv_buf = ccm_iv_buf;
    3531                 :            :                 }
    3532                 :            :         }
    3533                 :            : 
    3534                 :          0 :         fc_params.src_iov = (void *)iov->src;
    3535                 :          0 :         fc_params.dst_iov = (void *)iov->dst;
    3536                 :            : 
    3537                 :          0 :         cipher_len = iov->data_len - iov->c_head - iov->c_tail;
    3538                 :          0 :         auth_len = iov->data_len - iov->a_head - iov->a_tail;
    3539                 :            : 
    3540                 :          0 :         d_offs = (iov->c_head << 16) | iov->a_head;
    3541                 :          0 :         d_lens = ((uint64_t)cipher_len << 32) | auth_len;
    3542                 :            : 
    3543                 :            :         if (is_aead) {
    3544                 :          0 :                 uint16_t aad_len = sess->aad_length;
    3545                 :            : 
    3546         [ #  # ]:          0 :                 if (likely(aad_len == 0)) {
    3547                 :          0 :                         d_offs = (iov->c_head << 16) | iov->c_head;
    3548                 :          0 :                         d_lens = ((uint64_t)cipher_len << 32) | cipher_len;
    3549                 :            :                 } else {
    3550                 :          0 :                         flags |= ROC_SE_VALID_AAD_BUF;
    3551                 :          0 :                         fc_params.aad_buf.size = sess->aad_length;
    3552                 :            :                         /* For AES CCM, AAD is written 18B after aad.data as per API */
    3553         [ #  # ]:          0 :                         if (sess->aes_ccm)
    3554                 :          0 :                                 fc_params.aad_buf.vaddr = PLT_PTR_ADD((uint8_t *)iov->aad_buf, 18);
    3555                 :            :                         else
    3556                 :          0 :                                 fc_params.aad_buf.vaddr = iov->aad_buf;
    3557                 :            : 
    3558                 :          0 :                         d_offs = (iov->c_head << 16);
    3559                 :            :                         d_lens = ((uint64_t)cipher_len << 32);
    3560                 :            :                 }
    3561                 :            :         }
    3562                 :            : 
    3563   [ #  #  #  # ]:          0 :         if (likely(sess->mac_len)) {
    3564                 :          0 :                 flags |= ROC_SE_VALID_MAC_BUF;
    3565                 :          0 :                 fc_params.mac_buf.size = sess->mac_len;
    3566                 :          0 :                 fc_params.mac_buf.vaddr = iov->mac_buf;
    3567                 :            :         }
    3568                 :            : 
    3569                 :          0 :         fc_params.meta_buf.vaddr = NULL;
    3570   [ #  #  #  # ]:          0 :         mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
    3571   [ #  #  #  # ]:          0 :         if (mdata == NULL) {
    3572                 :          0 :                 plt_dp_err("Error allocating meta buffer for request");
    3573                 :          0 :                 return -ENOMEM;
    3574                 :            :         }
    3575                 :            : 
    3576                 :            :         if (is_kasumi) {
    3577                 :            :                 if (cpt_op & ROC_SE_OP_ENCODE)
    3578                 :            :                         ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    3579                 :            :                                                 is_sg_ver2);
    3580                 :            :                 else
    3581                 :            :                         ret = cpt_dec_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    3582                 :            :                                                 is_sg_ver2);
    3583                 :            :         } else {
    3584   [ #  #  #  # ]:          0 :                 if (cpt_op & ROC_SE_OP_ENCODE)
    3585                 :            :                         ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    3586                 :            :                                                 is_sg_ver2);
    3587                 :            :                 else
    3588                 :            :                         ret = cpt_dec_hmac_prep(flags, d_offs, d_lens, &fc_params, inst,
    3589                 :            :                                                 is_sg_ver2);
    3590                 :            :         }
    3591                 :            : 
    3592   [ #  #  #  # ]:          0 :         if (unlikely(ret)) {
    3593                 :          0 :                 plt_dp_err("Preparing request failed due to bad input arg");
    3594                 :          0 :                 goto free_mdata_and_exit;
    3595                 :            :         }
    3596                 :            : 
    3597                 :            :         return 0;
    3598                 :            : 
    3599                 :            : free_mdata_and_exit:
    3600   [ #  #  #  # ]:          0 :         rte_mempool_put(m_info->pool, infl_req->mdata);
    3601                 :          0 :         return ret;
    3602                 :            : }
    3603                 :            : 
    3604                 :            : static __rte_always_inline int
    3605                 :            : fill_raw_digest_params(struct cnxk_iov *iov, struct cnxk_se_sess *sess,
    3606                 :            :                        struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
    3607                 :            :                        struct cpt_inst_s *inst, const bool is_sg_ver2)
    3608                 :            : {
    3609                 :          0 :         uint16_t auth_op = sess->cpt_op & ROC_SE_OP_AUTH_MASK;
    3610                 :            :         struct roc_se_fc_params fc_params;
    3611         [ #  # ]:          0 :         uint16_t mac_len = sess->mac_len;
    3612                 :            :         uint64_t d_offs, d_lens;
    3613                 :            :         uint32_t auth_len = 0;
    3614                 :            :         uint32_t flags = 0;
    3615                 :            :         void *mdata = NULL;
    3616                 :            :         uint32_t space = 0;
    3617                 :            :         int ret;
    3618                 :            : 
    3619                 :            :         memset(&fc_params, 0, sizeof(struct roc_se_fc_params));
    3620                 :          0 :         fc_params.cipher_iv_len = sess->iv_length;
    3621                 :          0 :         fc_params.ctx = &sess->roc_se_ctx;
    3622                 :            : 
    3623         [ #  # ]:          0 :         mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen, m_info->pool, infl_req);
    3624         [ #  # ]:          0 :         if (mdata == NULL) {
    3625                 :          0 :                 plt_dp_err("Error allocating meta buffer for request");
    3626                 :            :                 ret = -ENOMEM;
    3627                 :          0 :                 goto err_exit;
    3628                 :            :         }
    3629                 :            : 
    3630                 :            :         flags |= ROC_SE_VALID_MAC_BUF;
    3631                 :          0 :         fc_params.src_iov = (void *)iov->src;
    3632                 :          0 :         auth_len = iov->data_len - iov->a_head - iov->a_tail;
    3633                 :            :         d_lens = auth_len;
    3634                 :          0 :         d_offs = iov->a_head;
    3635                 :            : 
    3636         [ #  # ]:          0 :         if (auth_op == ROC_SE_OP_AUTH_GENERATE) {
    3637                 :          0 :                 fc_params.mac_buf.size = sess->mac_len;
    3638                 :          0 :                 fc_params.mac_buf.vaddr = iov->mac_buf;
    3639                 :            :         } else {
    3640                 :            :                 uint64_t *op = mdata;
    3641                 :            : 
    3642                 :            :                 /* Need space for storing generated mac */
    3643                 :            :                 space += 2 * sizeof(uint64_t);
    3644                 :            : 
    3645                 :          0 :                 fc_params.mac_buf.vaddr = (uint8_t *)mdata + space;
    3646                 :          0 :                 fc_params.mac_buf.size = mac_len;
    3647                 :          0 :                 space += RTE_ALIGN_CEIL(mac_len, 8);
    3648                 :          0 :                 op[0] = (uintptr_t)iov->mac_buf;
    3649                 :          0 :                 op[1] = mac_len;
    3650                 :          0 :                 infl_req->op_flags |= CPT_OP_FLAGS_AUTH_VERIFY;
    3651                 :            :         }
    3652                 :            : 
    3653                 :          0 :         fc_params.meta_buf.vaddr = (uint8_t *)mdata + space;
    3654         [ #  # ]:          0 :         fc_params.meta_buf.size -= space;
    3655                 :            : 
    3656                 :            :         ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, &fc_params, inst, is_sg_ver2);
    3657         [ #  # ]:          0 :         if (ret)
    3658                 :          0 :                 goto free_mdata_and_exit;
    3659                 :            : 
    3660                 :            :         return 0;
    3661                 :            : 
    3662                 :            : free_mdata_and_exit:
    3663         [ #  # ]:          0 :         if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
    3664         [ #  # ]:          0 :                 rte_mempool_put(m_info->pool, infl_req->mdata);
    3665                 :          0 : err_exit:
    3666                 :            :         return ret;
    3667                 :            : }
    3668                 :            : 
    3669                 :            : #endif /*_CNXK_SE_H_ */

Generated by: LCOV version 1.14