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

Generated by: LCOV version 1.14