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

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

Generated by: LCOV version 1.14