1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium, Inc
9 #include "cpt_common.h"
10 #include "cpt_hw_types.h"
11 #include "cpt_mcode_defines.h"
14 * This file defines functions that are interfaces to microcode spec.
18 static uint8_t zuc_d[32] = {
19 0x44, 0xD7, 0x26, 0xBC, 0x62, 0x6B, 0x13, 0x5E,
20 0x57, 0x89, 0x35, 0xE2, 0x71, 0x35, 0x09, 0xAF,
21 0x4D, 0x78, 0x2F, 0x13, 0x6B, 0xC4, 0x1A, 0xF1,
22 0x5E, 0x26, 0x3C, 0x4D, 0x78, 0x9A, 0x47, 0xAC
25 static __rte_always_inline void
26 gen_key_snow3g(const uint8_t *ck, uint32_t *keyx)
30 for (i = 0; i < 4; i++) {
32 keyx[3 - i] = (ck[base] << 24) | (ck[base + 1] << 16) |
33 (ck[base + 2] << 8) | (ck[base + 3]);
34 keyx[3 - i] = rte_cpu_to_be_32(keyx[3 - i]);
38 static __rte_always_inline int
39 cpt_mac_len_verify(struct rte_crypto_auth_xform *auth)
41 uint16_t mac_len = auth->digest_length;
45 case RTE_CRYPTO_AUTH_MD5:
46 case RTE_CRYPTO_AUTH_MD5_HMAC:
47 ret = (mac_len == 16) ? 0 : -1;
49 case RTE_CRYPTO_AUTH_SHA1:
50 case RTE_CRYPTO_AUTH_SHA1_HMAC:
51 ret = (mac_len == 20) ? 0 : -1;
53 case RTE_CRYPTO_AUTH_SHA224:
54 case RTE_CRYPTO_AUTH_SHA224_HMAC:
55 ret = (mac_len == 28) ? 0 : -1;
57 case RTE_CRYPTO_AUTH_SHA256:
58 case RTE_CRYPTO_AUTH_SHA256_HMAC:
59 ret = (mac_len == 32) ? 0 : -1;
61 case RTE_CRYPTO_AUTH_SHA384:
62 case RTE_CRYPTO_AUTH_SHA384_HMAC:
63 ret = (mac_len == 48) ? 0 : -1;
65 case RTE_CRYPTO_AUTH_SHA512:
66 case RTE_CRYPTO_AUTH_SHA512_HMAC:
67 ret = (mac_len == 64) ? 0 : -1;
69 case RTE_CRYPTO_AUTH_NULL:
79 static __rte_always_inline void
80 cpt_fc_salt_update(struct cpt_ctx *cpt_ctx,
83 mc_fc_context_t *fctx = &cpt_ctx->mc_ctx.fctx;
84 memcpy(fctx->enc.encr_iv, salt, 4);
87 static __rte_always_inline int
88 cpt_fc_ciph_validate_key_aes(uint16_t key_len)
100 static __rte_always_inline int
101 cpt_fc_ciph_set_type(cipher_type_t type, struct cpt_ctx *ctx, uint16_t key_len)
117 if (unlikely(cpt_fc_ciph_validate_key_aes(key_len) != 0))
125 key_len = key_len / 2;
126 if (unlikely(key_len == 24)) {
127 CPT_LOG_DP_ERR("Invalid AES key len for XTS");
130 if (unlikely(cpt_fc_ciph_validate_key_aes(key_len) != 0))
136 if (unlikely(key_len != 16))
138 /* No support for AEAD yet */
139 if (unlikely(ctx->hash_type))
141 fc_type = ZUC_SNOW3G;
145 if (unlikely(key_len != 16))
147 /* No support for AEAD yet */
148 if (unlikely(ctx->hash_type))
156 ctx->fc_type = fc_type;
160 static __rte_always_inline void
161 cpt_fc_ciph_set_key_passthrough(struct cpt_ctx *cpt_ctx, mc_fc_context_t *fctx)
163 cpt_ctx->enc_cipher = 0;
164 fctx->enc.enc_cipher = 0;
167 static __rte_always_inline void
168 cpt_fc_ciph_set_key_set_aes_key_type(mc_fc_context_t *fctx, uint16_t key_len)
170 mc_aes_type_t aes_key_type = 0;
173 aes_key_type = AES_128_BIT;
176 aes_key_type = AES_192_BIT;
179 aes_key_type = AES_256_BIT;
182 /* This should not happen */
183 CPT_LOG_DP_ERR("Invalid AES key len");
186 fctx->enc.aes_key = aes_key_type;
189 static __rte_always_inline void
190 cpt_fc_ciph_set_key_snow3g_uea2(struct cpt_ctx *cpt_ctx, const uint8_t *key,
193 mc_zuc_snow3g_ctx_t *zs_ctx = &cpt_ctx->mc_ctx.zs_ctx;
197 gen_key_snow3g(key, keyx);
198 memcpy(zs_ctx->ci_key, keyx, key_len);
199 cpt_ctx->zsk_flags = 0;
202 static __rte_always_inline void
203 cpt_fc_ciph_set_key_zuc_eea3(struct cpt_ctx *cpt_ctx, const uint8_t *key,
206 mc_zuc_snow3g_ctx_t *zs_ctx = &cpt_ctx->mc_ctx.zs_ctx;
209 memcpy(zs_ctx->ci_key, key, key_len);
210 memcpy(zs_ctx->zuc_const, zuc_d, 32);
211 cpt_ctx->zsk_flags = 0;
214 static __rte_always_inline void
215 cpt_fc_ciph_set_key_kasumi_f8_ecb(struct cpt_ctx *cpt_ctx, const uint8_t *key,
218 mc_kasumi_ctx_t *k_ctx = &cpt_ctx->mc_ctx.k_ctx;
221 memcpy(k_ctx->ci_key, key, key_len);
222 cpt_ctx->zsk_flags = 0;
225 static __rte_always_inline void
226 cpt_fc_ciph_set_key_kasumi_f8_cbc(struct cpt_ctx *cpt_ctx, const uint8_t *key,
229 mc_kasumi_ctx_t *k_ctx = &cpt_ctx->mc_ctx.k_ctx;
231 memcpy(k_ctx->ci_key, key, key_len);
232 cpt_ctx->zsk_flags = 0;
235 static __rte_always_inline int
236 cpt_fc_ciph_set_key(struct cpt_ctx *cpt_ctx, cipher_type_t type,
237 const uint8_t *key, uint16_t key_len, uint8_t *salt)
239 mc_fc_context_t *fctx = &cpt_ctx->mc_ctx.fctx;
242 ret = cpt_fc_ciph_set_type(type, cpt_ctx, key_len);
246 if (cpt_ctx->fc_type == FC_GEN) {
248 * We need to always say IV is from DPTR as user can
249 * sometimes iverride IV per operation.
251 fctx->enc.iv_source = CPT_FROM_DPTR;
253 if (cpt_ctx->auth_key_len > 64)
259 cpt_fc_ciph_set_key_passthrough(cpt_ctx, fctx);
262 /* CPT performs DES using 3DES with the 8B DES-key
263 * replicated 2 more times to match the 24B 3DES-key.
264 * Eg. If org. key is "0x0a 0x0b", then new key is
265 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
268 /* Skipping the first 8B as it will be copied
269 * in the regular code flow
271 memcpy(fctx->enc.encr_key+key_len, key, key_len);
272 memcpy(fctx->enc.encr_key+2*key_len, key, key_len);
276 /* For DES3_ECB IV need to be from CTX. */
277 fctx->enc.iv_source = CPT_FROM_CTX;
284 cpt_fc_ciph_set_key_set_aes_key_type(fctx, key_len);
287 /* Even though iv source is from dptr,
288 * aes_gcm salt is taken from ctx
291 memcpy(fctx->enc.encr_iv, salt, 4);
292 /* Assuming it was just salt update
298 cpt_fc_ciph_set_key_set_aes_key_type(fctx, key_len);
301 key_len = key_len / 2;
302 cpt_fc_ciph_set_key_set_aes_key_type(fctx, key_len);
304 /* Copy key2 for XTS into ipad */
305 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
306 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
309 cpt_fc_ciph_set_key_snow3g_uea2(cpt_ctx, key, key_len);
312 cpt_fc_ciph_set_key_zuc_eea3(cpt_ctx, key, key_len);
315 cpt_fc_ciph_set_key_kasumi_f8_ecb(cpt_ctx, key, key_len);
318 cpt_fc_ciph_set_key_kasumi_f8_cbc(cpt_ctx, key, key_len);
324 /* Only for FC_GEN case */
326 /* For GMAC auth, cipher must be NULL */
327 if (cpt_ctx->hash_type != GMAC_TYPE)
328 fctx->enc.enc_cipher = type;
330 memcpy(fctx->enc.encr_key, key, key_len);
333 cpt_ctx->enc_cipher = type;
338 static __rte_always_inline uint32_t
339 fill_sg_comp(sg_comp_t *list,
341 phys_addr_t dma_addr,
344 sg_comp_t *to = &list[i>>2];
346 to->u.s.len[i%4] = rte_cpu_to_be_16(size);
347 to->ptr[i%4] = rte_cpu_to_be_64(dma_addr);
352 static __rte_always_inline uint32_t
353 fill_sg_comp_from_buf(sg_comp_t *list,
357 sg_comp_t *to = &list[i>>2];
359 to->u.s.len[i%4] = rte_cpu_to_be_16(from->size);
360 to->ptr[i%4] = rte_cpu_to_be_64(from->dma_addr);
365 static __rte_always_inline uint32_t
366 fill_sg_comp_from_buf_min(sg_comp_t *list,
371 sg_comp_t *to = &list[i >> 2];
372 uint32_t size = *psize;
375 e_len = (size > from->size) ? from->size : size;
376 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
377 to->ptr[i % 4] = rte_cpu_to_be_64(from->dma_addr);
384 * This fills the MC expected SGIO list
385 * from IOV given by user.
387 static __rte_always_inline uint32_t
388 fill_sg_comp_from_iov(sg_comp_t *list,
390 iov_ptr_t *from, uint32_t from_offset,
391 uint32_t *psize, buf_ptr_t *extra_buf,
392 uint32_t extra_offset)
395 uint32_t extra_len = extra_buf ? extra_buf->size : 0;
396 uint32_t size = *psize;
400 for (j = 0; (j < from->buf_cnt) && size; j++) {
401 phys_addr_t e_dma_addr;
403 sg_comp_t *to = &list[i >> 2];
405 if (unlikely(from_offset)) {
406 if (from_offset >= bufs[j].size) {
407 from_offset -= bufs[j].size;
410 e_dma_addr = bufs[j].dma_addr + from_offset;
411 e_len = (size > (bufs[j].size - from_offset)) ?
412 (bufs[j].size - from_offset) : size;
415 e_dma_addr = bufs[j].dma_addr;
416 e_len = (size > bufs[j].size) ?
420 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
421 to->ptr[i % 4] = rte_cpu_to_be_64(e_dma_addr);
423 if (extra_len && (e_len >= extra_offset)) {
424 /* Break the data at given offset */
425 uint32_t next_len = e_len - extra_offset;
426 phys_addr_t next_dma = e_dma_addr + extra_offset;
431 e_len = extra_offset;
433 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
436 extra_len = RTE_MIN(extra_len, size);
437 /* Insert extra data ptr */
442 rte_cpu_to_be_16(extra_len);
444 rte_cpu_to_be_64(extra_buf->dma_addr);
448 next_len = RTE_MIN(next_len, size);
449 /* insert the rest of the data */
453 to->u.s.len[i % 4] = rte_cpu_to_be_16(next_len);
454 to->ptr[i % 4] = rte_cpu_to_be_64(next_dma);
463 extra_offset -= size;
471 static __rte_always_inline void
472 cpt_digest_gen_prep(uint32_t flags,
474 digest_params_t *params,
478 struct cpt_request_info *req;
480 uint16_t data_len, mac_len, key_len;
481 auth_type_t hash_type;
484 sg_comp_t *gather_comp;
485 sg_comp_t *scatter_comp;
487 uint32_t g_size_bytes, s_size_bytes;
488 uint64_t dptr_dma, rptr_dma;
489 vq_cmd_word0_t vq_cmd_w0;
490 void *c_vaddr, *m_vaddr;
491 uint64_t c_dma, m_dma;
493 ctx = params->ctx_buf.vaddr;
494 meta_p = ¶ms->meta_buf;
496 m_vaddr = meta_p->vaddr;
497 m_dma = meta_p->dma_addr;
500 * Save initial space that followed app data for completion code &
501 * alternate completion code to fall in same cache line as app data
503 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
504 m_dma += COMPLETION_CODE_SIZE;
505 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
507 c_vaddr = (uint8_t *)m_vaddr + size;
508 c_dma = m_dma + size;
509 size += sizeof(cpt_res_s_t);
511 m_vaddr = (uint8_t *)m_vaddr + size;
516 size = sizeof(struct cpt_request_info);
517 m_vaddr = (uint8_t *)m_vaddr + size;
520 hash_type = ctx->hash_type;
521 mac_len = ctx->mac_len;
522 key_len = ctx->auth_key_len;
523 data_len = AUTH_DLEN(d_lens);
526 vq_cmd_w0.s.opcode.minor = 0;
527 vq_cmd_w0.s.param2 = ((uint16_t)hash_type << 8);
529 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_HMAC | CPT_DMA_MODE;
530 vq_cmd_w0.s.param1 = key_len;
531 vq_cmd_w0.s.dlen = data_len + RTE_ALIGN_CEIL(key_len, 8);
533 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_HASH | CPT_DMA_MODE;
534 vq_cmd_w0.s.param1 = 0;
535 vq_cmd_w0.s.dlen = data_len;
538 /* Null auth only case enters the if */
539 if (unlikely(!hash_type && !ctx->enc_cipher)) {
540 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_MISC;
541 /* Minor op is passthrough */
542 vq_cmd_w0.s.opcode.minor = 0x03;
543 /* Send out completion code only */
544 vq_cmd_w0.s.param2 = 0x1;
547 /* DPTR has SG list */
551 ((uint16_t *)in_buffer)[0] = 0;
552 ((uint16_t *)in_buffer)[1] = 0;
554 /* TODO Add error check if space will be sufficient */
555 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
564 uint64_t k_dma = ctx->auth_key_iova;
566 i = fill_sg_comp(gather_comp, i, k_dma,
567 RTE_ALIGN_CEIL(key_len, 8));
573 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov,
575 if (unlikely(size)) {
576 CPT_LOG_DP_DEBUG("Insufficient dst IOV size, short"
582 * Looks like we need to support zero data
583 * gather ptr in case of hash & hmac
587 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
588 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
595 scatter_comp = (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
597 if (flags & VALID_MAC_BUF) {
598 if (unlikely(params->mac_buf.size < mac_len)) {
599 CPT_LOG_DP_ERR("Insufficient MAC size");
604 i = fill_sg_comp_from_buf_min(scatter_comp, i,
605 ¶ms->mac_buf, &size);
608 i = fill_sg_comp_from_iov(scatter_comp, i,
609 params->src_iov, data_len,
611 if (unlikely(size)) {
612 CPT_LOG_DP_ERR("Insufficient dst IOV size, short by"
618 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
619 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
621 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
623 /* This is DPTR len incase of SG mode */
624 vq_cmd_w0.s.dlen = size;
626 m_vaddr = (uint8_t *)m_vaddr + size;
629 /* cpt alternate completion address saved earlier */
630 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
631 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
632 rptr_dma = c_dma - 8;
634 req->ist.ei1 = dptr_dma;
635 req->ist.ei2 = rptr_dma;
637 /* 16 byte aligned cpt res address */
638 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
639 *req->completion_addr = COMPLETION_CODE_INIT;
640 req->comp_baddr = c_dma;
642 /* Fill microcode part of instruction */
643 req->ist.ei0 = vq_cmd_w0.u64;
651 static __rte_always_inline void
652 cpt_enc_hmac_prep(uint32_t flags,
655 fc_params_t *fc_params,
659 uint32_t iv_offset = 0;
660 int32_t inputlen, outputlen, enc_dlen, auth_dlen;
661 struct cpt_ctx *cpt_ctx;
662 uint32_t cipher_type, hash_type;
663 uint32_t mac_len, size;
665 struct cpt_request_info *req;
666 buf_ptr_t *meta_p, *aad_buf = NULL;
667 uint32_t encr_offset, auth_offset;
668 uint32_t encr_data_len, auth_data_len, aad_len = 0;
669 uint32_t passthrough_len = 0;
670 void *m_vaddr, *offset_vaddr;
671 uint64_t m_dma, offset_dma;
672 vq_cmd_word0_t vq_cmd_w0;
676 meta_p = &fc_params->meta_buf;
677 m_vaddr = meta_p->vaddr;
678 m_dma = meta_p->dma_addr;
680 encr_offset = ENCR_OFFSET(d_offs);
681 auth_offset = AUTH_OFFSET(d_offs);
682 encr_data_len = ENCR_DLEN(d_lens);
683 auth_data_len = AUTH_DLEN(d_lens);
684 if (unlikely(flags & VALID_AAD_BUF)) {
686 * We dont support both aad
687 * and auth data separately
691 aad_len = fc_params->aad_buf.size;
692 aad_buf = &fc_params->aad_buf;
694 cpt_ctx = fc_params->ctx_buf.vaddr;
695 cipher_type = cpt_ctx->enc_cipher;
696 hash_type = cpt_ctx->hash_type;
697 mac_len = cpt_ctx->mac_len;
700 * Save initial space that followed app data for completion code &
701 * alternate completion code to fall in same cache line as app data
703 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
704 m_dma += COMPLETION_CODE_SIZE;
705 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
708 c_vaddr = (uint8_t *)m_vaddr + size;
709 c_dma = m_dma + size;
710 size += sizeof(cpt_res_s_t);
712 m_vaddr = (uint8_t *)m_vaddr + size;
715 /* start cpt request info struct at 8 byte boundary */
716 size = (uint8_t *)RTE_PTR_ALIGN(m_vaddr, 8) -
719 req = (struct cpt_request_info *)((uint8_t *)m_vaddr + size);
721 size += sizeof(struct cpt_request_info);
722 m_vaddr = (uint8_t *)m_vaddr + size;
725 if (unlikely(!(flags & VALID_IV_BUF))) {
727 iv_offset = ENCR_IV_OFFSET(d_offs);
730 if (unlikely(flags & VALID_AAD_BUF)) {
732 * When AAD is given, data above encr_offset is pass through
733 * Since AAD is given as separate pointer and not as offset,
734 * this is a special case as we need to fragment input data
735 * into passthrough + encr_data and then insert AAD in between.
737 if (hash_type != GMAC_TYPE) {
738 passthrough_len = encr_offset;
739 auth_offset = passthrough_len + iv_len;
740 encr_offset = passthrough_len + aad_len + iv_len;
741 auth_data_len = aad_len + encr_data_len;
743 passthrough_len = 16 + aad_len;
744 auth_offset = passthrough_len + iv_len;
745 auth_data_len = aad_len;
748 encr_offset += iv_len;
749 auth_offset += iv_len;
753 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_FC;
754 vq_cmd_w0.s.opcode.minor = CPT_FC_MINOR_OP_ENCRYPT;
755 vq_cmd_w0.s.opcode.minor |= (cpt_ctx->auth_enc <<
756 CPT_HMAC_FIRST_BIT_POS);
758 if (hash_type == GMAC_TYPE) {
763 auth_dlen = auth_offset + auth_data_len;
764 enc_dlen = encr_data_len + encr_offset;
765 if (unlikely(encr_data_len & 0xf)) {
766 if ((cipher_type == DES3_CBC) || (cipher_type == DES3_ECB))
767 enc_dlen = RTE_ALIGN_CEIL(encr_data_len, 8) +
769 else if (likely((cipher_type == AES_CBC) ||
770 (cipher_type == AES_ECB)))
771 enc_dlen = RTE_ALIGN_CEIL(encr_data_len, 8) +
775 if (unlikely(auth_dlen > enc_dlen)) {
776 inputlen = auth_dlen;
777 outputlen = auth_dlen + mac_len;
780 outputlen = enc_dlen + mac_len;
783 if (cpt_ctx->auth_enc != 0)
784 outputlen = enc_dlen;
787 vq_cmd_w0.s.param1 = encr_data_len;
788 vq_cmd_w0.s.param2 = auth_data_len;
790 * In 83XX since we have a limitation of
791 * IV & Offset control word not part of instruction
792 * and need to be part of Data Buffer, we check if
793 * head room is there and then only do the Direct mode processing
795 if (likely((flags & SINGLE_BUF_INPLACE) &&
796 (flags & SINGLE_BUF_HEADTAILROOM))) {
797 void *dm_vaddr = fc_params->bufs[0].vaddr;
798 uint64_t dm_dma_addr = fc_params->bufs[0].dma_addr;
800 * This flag indicates that there is 24 bytes head room and
801 * 8 bytes tail room available, so that we get to do
802 * DIRECT MODE with limitation
805 offset_vaddr = (uint8_t *)dm_vaddr - OFF_CTRL_LEN - iv_len;
806 offset_dma = dm_dma_addr - OFF_CTRL_LEN - iv_len;
809 req->ist.ei1 = offset_dma;
810 /* RPTR should just exclude offset control word */
811 req->ist.ei2 = dm_dma_addr - iv_len;
812 req->alternate_caddr = (uint64_t *)((uint8_t *)dm_vaddr
813 + outputlen - iv_len);
815 vq_cmd_w0.s.dlen = inputlen + OFF_CTRL_LEN;
817 if (likely(iv_len)) {
818 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr
820 uint64_t *src = fc_params->iv_buf;
825 *(uint64_t *)offset_vaddr =
826 rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
827 ((uint64_t)iv_offset << 8) |
828 ((uint64_t)auth_offset));
831 uint32_t i, g_size_bytes, s_size_bytes;
832 uint64_t dptr_dma, rptr_dma;
833 sg_comp_t *gather_comp;
834 sg_comp_t *scatter_comp;
837 /* This falls under strict SG mode */
838 offset_vaddr = m_vaddr;
840 size = OFF_CTRL_LEN + iv_len;
842 m_vaddr = (uint8_t *)m_vaddr + size;
845 vq_cmd_w0.s.opcode.major |= CPT_DMA_MODE;
847 if (likely(iv_len)) {
848 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr
850 uint64_t *src = fc_params->iv_buf;
855 *(uint64_t *)offset_vaddr =
856 rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
857 ((uint64_t)iv_offset << 8) |
858 ((uint64_t)auth_offset));
860 /* DPTR has SG list */
864 ((uint16_t *)in_buffer)[0] = 0;
865 ((uint16_t *)in_buffer)[1] = 0;
867 /* TODO Add error check if space will be sufficient */
868 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
876 /* Offset control word that includes iv */
877 i = fill_sg_comp(gather_comp, i, offset_dma,
878 OFF_CTRL_LEN + iv_len);
881 size = inputlen - iv_len;
883 uint32_t aad_offset = aad_len ? passthrough_len : 0;
885 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
886 i = fill_sg_comp_from_buf_min(gather_comp, i,
890 i = fill_sg_comp_from_iov(gather_comp, i,
893 aad_buf, aad_offset);
896 if (unlikely(size)) {
897 CPT_LOG_DP_ERR("Insufficient buffer space,"
898 " size %d needed", size);
902 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
903 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
906 * Output Scatter list
910 (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
913 if (likely(iv_len)) {
914 i = fill_sg_comp(scatter_comp, i,
915 offset_dma + OFF_CTRL_LEN,
919 /* output data or output data + digest*/
920 if (unlikely(flags & VALID_MAC_BUF)) {
921 size = outputlen - iv_len - mac_len;
923 uint32_t aad_offset =
924 aad_len ? passthrough_len : 0;
926 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
927 i = fill_sg_comp_from_buf_min(
933 i = fill_sg_comp_from_iov(scatter_comp,
941 if (unlikely(size)) {
942 CPT_LOG_DP_ERR("Insufficient buffer"
943 " space, size %d needed",
950 i = fill_sg_comp_from_buf(scatter_comp, i,
951 &fc_params->mac_buf);
954 /* Output including mac */
955 size = outputlen - iv_len;
957 uint32_t aad_offset =
958 aad_len ? passthrough_len : 0;
960 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
961 i = fill_sg_comp_from_buf_min(
967 i = fill_sg_comp_from_iov(scatter_comp,
975 if (unlikely(size)) {
976 CPT_LOG_DP_ERR("Insufficient buffer"
977 " space, size %d needed",
983 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
984 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
986 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
988 /* This is DPTR len incase of SG mode */
989 vq_cmd_w0.s.dlen = size;
991 m_vaddr = (uint8_t *)m_vaddr + size;
994 /* cpt alternate completion address saved earlier */
995 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
996 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
997 rptr_dma = c_dma - 8;
999 req->ist.ei1 = dptr_dma;
1000 req->ist.ei2 = rptr_dma;
1003 if (unlikely((encr_offset >> 16) ||
1005 (auth_offset >> 8))) {
1006 CPT_LOG_DP_ERR("Offset not supported");
1007 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
1008 CPT_LOG_DP_ERR("iv_offset : %d", iv_offset);
1009 CPT_LOG_DP_ERR("auth_offset: %d", auth_offset);
1013 /* 16 byte aligned cpt res address */
1014 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
1015 *req->completion_addr = COMPLETION_CODE_INIT;
1016 req->comp_baddr = c_dma;
1018 /* Fill microcode part of instruction */
1019 req->ist.ei0 = vq_cmd_w0.u64;
1027 static __rte_always_inline void
1028 cpt_dec_hmac_prep(uint32_t flags,
1031 fc_params_t *fc_params,
1035 uint32_t iv_offset = 0, size;
1036 int32_t inputlen, outputlen, enc_dlen, auth_dlen;
1037 struct cpt_ctx *cpt_ctx;
1038 int32_t hash_type, mac_len;
1039 uint8_t iv_len = 16;
1040 struct cpt_request_info *req;
1041 buf_ptr_t *meta_p, *aad_buf = NULL;
1042 uint32_t encr_offset, auth_offset;
1043 uint32_t encr_data_len, auth_data_len, aad_len = 0;
1044 uint32_t passthrough_len = 0;
1045 void *m_vaddr, *offset_vaddr;
1046 uint64_t m_dma, offset_dma;
1047 vq_cmd_word0_t vq_cmd_w0;
1051 meta_p = &fc_params->meta_buf;
1052 m_vaddr = meta_p->vaddr;
1053 m_dma = meta_p->dma_addr;
1055 encr_offset = ENCR_OFFSET(d_offs);
1056 auth_offset = AUTH_OFFSET(d_offs);
1057 encr_data_len = ENCR_DLEN(d_lens);
1058 auth_data_len = AUTH_DLEN(d_lens);
1060 if (unlikely(flags & VALID_AAD_BUF)) {
1062 * We dont support both aad
1063 * and auth data separately
1067 aad_len = fc_params->aad_buf.size;
1068 aad_buf = &fc_params->aad_buf;
1071 cpt_ctx = fc_params->ctx_buf.vaddr;
1072 hash_type = cpt_ctx->hash_type;
1073 mac_len = cpt_ctx->mac_len;
1075 if (unlikely(!(flags & VALID_IV_BUF))) {
1077 iv_offset = ENCR_IV_OFFSET(d_offs);
1080 if (unlikely(flags & VALID_AAD_BUF)) {
1082 * When AAD is given, data above encr_offset is pass through
1083 * Since AAD is given as separate pointer and not as offset,
1084 * this is a special case as we need to fragment input data
1085 * into passthrough + encr_data and then insert AAD in between.
1087 if (hash_type != GMAC_TYPE) {
1088 passthrough_len = encr_offset;
1089 auth_offset = passthrough_len + iv_len;
1090 encr_offset = passthrough_len + aad_len + iv_len;
1091 auth_data_len = aad_len + encr_data_len;
1093 passthrough_len = 16 + aad_len;
1094 auth_offset = passthrough_len + iv_len;
1095 auth_data_len = aad_len;
1098 encr_offset += iv_len;
1099 auth_offset += iv_len;
1103 * Save initial space that followed app data for completion code &
1104 * alternate completion code to fall in same cache line as app data
1106 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
1107 m_dma += COMPLETION_CODE_SIZE;
1108 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
1110 c_vaddr = (uint8_t *)m_vaddr + size;
1111 c_dma = m_dma + size;
1112 size += sizeof(cpt_res_s_t);
1114 m_vaddr = (uint8_t *)m_vaddr + size;
1117 /* start cpt request info structure at 8 byte alignment */
1118 size = (uint8_t *)RTE_PTR_ALIGN(m_vaddr, 8) -
1121 req = (struct cpt_request_info *)((uint8_t *)m_vaddr + size);
1123 size += sizeof(struct cpt_request_info);
1124 m_vaddr = (uint8_t *)m_vaddr + size;
1128 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_FC;
1129 vq_cmd_w0.s.opcode.minor = CPT_FC_MINOR_OP_DECRYPT;
1130 vq_cmd_w0.s.opcode.minor |= (cpt_ctx->dec_auth <<
1131 CPT_HMAC_FIRST_BIT_POS);
1133 if (hash_type == GMAC_TYPE) {
1138 enc_dlen = encr_offset + encr_data_len;
1139 auth_dlen = auth_offset + auth_data_len;
1141 if (auth_dlen > enc_dlen) {
1142 inputlen = auth_dlen + mac_len;
1143 outputlen = auth_dlen;
1145 inputlen = enc_dlen + mac_len;
1146 outputlen = enc_dlen;
1149 if (cpt_ctx->dec_auth != 0)
1150 outputlen = inputlen = enc_dlen;
1152 vq_cmd_w0.s.param1 = encr_data_len;
1153 vq_cmd_w0.s.param2 = auth_data_len;
1156 * In 83XX since we have a limitation of
1157 * IV & Offset control word not part of instruction
1158 * and need to be part of Data Buffer, we check if
1159 * head room is there and then only do the Direct mode processing
1161 if (likely((flags & SINGLE_BUF_INPLACE) &&
1162 (flags & SINGLE_BUF_HEADTAILROOM))) {
1163 void *dm_vaddr = fc_params->bufs[0].vaddr;
1164 uint64_t dm_dma_addr = fc_params->bufs[0].dma_addr;
1166 * This flag indicates that there is 24 bytes head room and
1167 * 8 bytes tail room available, so that we get to do
1168 * DIRECT MODE with limitation
1171 offset_vaddr = (uint8_t *)dm_vaddr - OFF_CTRL_LEN - iv_len;
1172 offset_dma = dm_dma_addr - OFF_CTRL_LEN - iv_len;
1173 req->ist.ei1 = offset_dma;
1175 /* RPTR should just exclude offset control word */
1176 req->ist.ei2 = dm_dma_addr - iv_len;
1178 req->alternate_caddr = (uint64_t *)((uint8_t *)dm_vaddr +
1179 outputlen - iv_len);
1180 /* since this is decryption,
1181 * don't touch the content of
1182 * alternate ccode space as it contains
1186 vq_cmd_w0.s.dlen = inputlen + OFF_CTRL_LEN;
1188 if (likely(iv_len)) {
1189 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
1191 uint64_t *src = fc_params->iv_buf;
1196 if (unlikely((encr_offset >> 16) ||
1198 (auth_offset >> 8))) {
1199 CPT_LOG_DP_ERR("Offset not supported");
1200 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
1201 CPT_LOG_DP_ERR("iv_offset : %d", iv_offset);
1202 CPT_LOG_DP_ERR("auth_offset: %d", auth_offset);
1206 *(uint64_t *)offset_vaddr =
1207 rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
1208 ((uint64_t)iv_offset << 8) |
1209 ((uint64_t)auth_offset));
1212 uint64_t dptr_dma, rptr_dma;
1213 uint32_t g_size_bytes, s_size_bytes;
1214 sg_comp_t *gather_comp;
1215 sg_comp_t *scatter_comp;
1219 /* This falls under strict SG mode */
1220 offset_vaddr = m_vaddr;
1222 size = OFF_CTRL_LEN + iv_len;
1224 m_vaddr = (uint8_t *)m_vaddr + size;
1227 vq_cmd_w0.s.opcode.major |= CPT_DMA_MODE;
1229 if (likely(iv_len)) {
1230 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
1232 uint64_t *src = fc_params->iv_buf;
1237 if (unlikely((encr_offset >> 16) ||
1239 (auth_offset >> 8))) {
1240 CPT_LOG_DP_ERR("Offset not supported");
1241 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
1242 CPT_LOG_DP_ERR("iv_offset : %d", iv_offset);
1243 CPT_LOG_DP_ERR("auth_offset: %d", auth_offset);
1247 *(uint64_t *)offset_vaddr =
1248 rte_cpu_to_be_64(((uint64_t)encr_offset << 16) |
1249 ((uint64_t)iv_offset << 8) |
1250 ((uint64_t)auth_offset));
1252 /* DPTR has SG list */
1253 in_buffer = m_vaddr;
1256 ((uint16_t *)in_buffer)[0] = 0;
1257 ((uint16_t *)in_buffer)[1] = 0;
1259 /* TODO Add error check if space will be sufficient */
1260 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
1267 /* Offset control word that includes iv */
1268 i = fill_sg_comp(gather_comp, i, offset_dma,
1269 OFF_CTRL_LEN + iv_len);
1271 /* Add input data */
1272 if (flags & VALID_MAC_BUF) {
1273 size = inputlen - iv_len - mac_len;
1275 /* input data only */
1276 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
1277 i = fill_sg_comp_from_buf_min(
1282 uint32_t aad_offset = aad_len ?
1283 passthrough_len : 0;
1285 i = fill_sg_comp_from_iov(gather_comp,
1292 if (unlikely(size)) {
1293 CPT_LOG_DP_ERR("Insufficient buffer"
1294 " space, size %d needed",
1302 i = fill_sg_comp_from_buf(gather_comp, i,
1303 &fc_params->mac_buf);
1306 /* input data + mac */
1307 size = inputlen - iv_len;
1309 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
1310 i = fill_sg_comp_from_buf_min(
1315 uint32_t aad_offset = aad_len ?
1316 passthrough_len : 0;
1318 if (unlikely(!fc_params->src_iov)) {
1319 CPT_LOG_DP_ERR("Bad input args");
1323 i = fill_sg_comp_from_iov(
1331 if (unlikely(size)) {
1332 CPT_LOG_DP_ERR("Insufficient buffer"
1333 " space, size %d needed",
1339 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1340 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1343 * Output Scatter List
1348 (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
1352 i = fill_sg_comp(scatter_comp, i,
1353 offset_dma + OFF_CTRL_LEN,
1357 /* Add output data */
1358 size = outputlen - iv_len;
1360 if (unlikely(flags & SINGLE_BUF_INPLACE)) {
1361 /* handle single buffer here */
1362 i = fill_sg_comp_from_buf_min(scatter_comp, i,
1366 uint32_t aad_offset = aad_len ?
1367 passthrough_len : 0;
1369 if (unlikely(!fc_params->dst_iov)) {
1370 CPT_LOG_DP_ERR("Bad input args");
1374 i = fill_sg_comp_from_iov(scatter_comp, i,
1375 fc_params->dst_iov, 0,
1380 if (unlikely(size)) {
1381 CPT_LOG_DP_ERR("Insufficient buffer space,"
1382 " size %d needed", size);
1387 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1388 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1390 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
1392 /* This is DPTR len incase of SG mode */
1393 vq_cmd_w0.s.dlen = size;
1395 m_vaddr = (uint8_t *)m_vaddr + size;
1398 /* cpt alternate completion address saved earlier */
1399 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
1400 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
1401 rptr_dma = c_dma - 8;
1402 size += COMPLETION_CODE_SIZE;
1404 req->ist.ei1 = dptr_dma;
1405 req->ist.ei2 = rptr_dma;
1408 /* 16 byte aligned cpt res address */
1409 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
1410 *req->completion_addr = COMPLETION_CODE_INIT;
1411 req->comp_baddr = c_dma;
1413 /* Fill microcode part of instruction */
1414 req->ist.ei0 = vq_cmd_w0.u64;
1422 static __rte_always_inline void
1423 cpt_zuc_snow3g_enc_prep(uint32_t req_flags,
1426 fc_params_t *params,
1431 int32_t inputlen, outputlen;
1432 struct cpt_ctx *cpt_ctx;
1433 uint32_t mac_len = 0;
1435 struct cpt_request_info *req;
1437 uint32_t encr_offset = 0, auth_offset = 0;
1438 uint32_t encr_data_len = 0, auth_data_len = 0;
1439 int flags, iv_len = 16;
1440 void *m_vaddr, *c_vaddr;
1441 uint64_t m_dma, c_dma, offset_ctrl;
1442 uint64_t *offset_vaddr, offset_dma;
1443 uint32_t *iv_s, iv[4];
1444 vq_cmd_word0_t vq_cmd_w0;
1446 buf_p = ¶ms->meta_buf;
1447 m_vaddr = buf_p->vaddr;
1448 m_dma = buf_p->dma_addr;
1450 cpt_ctx = params->ctx_buf.vaddr;
1451 flags = cpt_ctx->zsk_flags;
1452 mac_len = cpt_ctx->mac_len;
1453 snow3g = cpt_ctx->snow3g;
1456 * Save initial space that followed app data for completion code &
1457 * alternate completion code to fall in same cache line as app data
1459 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
1460 m_dma += COMPLETION_CODE_SIZE;
1461 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
1464 c_vaddr = (uint8_t *)m_vaddr + size;
1465 c_dma = m_dma + size;
1466 size += sizeof(cpt_res_s_t);
1468 m_vaddr = (uint8_t *)m_vaddr + size;
1471 /* Reserve memory for cpt request info */
1474 size = sizeof(struct cpt_request_info);
1475 m_vaddr = (uint8_t *)m_vaddr + size;
1478 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_ZUC_SNOW3G;
1480 /* indicates CPTR ctx, operation type, KEY & IV mode from DPTR */
1482 vq_cmd_w0.s.opcode.minor = ((1 << 7) | (snow3g << 5) | (0 << 4) |
1483 (0 << 3) | (flags & 0x7));
1487 * Microcode expects offsets in bytes
1488 * TODO: Rounding off
1490 auth_data_len = AUTH_DLEN(d_lens);
1493 auth_offset = AUTH_OFFSET(d_offs);
1494 auth_offset = auth_offset / 8;
1496 /* consider iv len */
1497 auth_offset += iv_len;
1499 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
1500 outputlen = mac_len;
1502 offset_ctrl = rte_cpu_to_be_64((uint64_t)auth_offset);
1507 * Microcode expects offsets in bytes
1508 * TODO: Rounding off
1510 encr_data_len = ENCR_DLEN(d_lens);
1512 encr_offset = ENCR_OFFSET(d_offs);
1513 encr_offset = encr_offset / 8;
1514 /* consider iv len */
1515 encr_offset += iv_len;
1517 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
1518 outputlen = inputlen;
1520 /* iv offset is 0 */
1521 offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1524 if (unlikely((encr_offset >> 16) ||
1525 (auth_offset >> 8))) {
1526 CPT_LOG_DP_ERR("Offset not supported");
1527 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
1528 CPT_LOG_DP_ERR("auth_offset: %d", auth_offset);
1533 iv_s = (flags == 0x1) ? params->auth_iv_buf :
1538 * DPDK seems to provide it in form of IV3 IV2 IV1 IV0
1539 * and BigEndian, MC needs it as IV0 IV1 IV2 IV3
1542 for (j = 0; j < 4; j++)
1543 iv[j] = iv_s[3 - j];
1545 /* ZUC doesn't need a swap */
1546 for (j = 0; j < 4; j++)
1551 * GP op header, lengths are expected in bits.
1553 vq_cmd_w0.s.param1 = encr_data_len;
1554 vq_cmd_w0.s.param2 = auth_data_len;
1557 * In 83XX since we have a limitation of
1558 * IV & Offset control word not part of instruction
1559 * and need to be part of Data Buffer, we check if
1560 * head room is there and then only do the Direct mode processing
1562 if (likely((req_flags & SINGLE_BUF_INPLACE) &&
1563 (req_flags & SINGLE_BUF_HEADTAILROOM))) {
1564 void *dm_vaddr = params->bufs[0].vaddr;
1565 uint64_t dm_dma_addr = params->bufs[0].dma_addr;
1567 * This flag indicates that there is 24 bytes head room and
1568 * 8 bytes tail room available, so that we get to do
1569 * DIRECT MODE with limitation
1572 offset_vaddr = (uint64_t *)((uint8_t *)dm_vaddr -
1573 OFF_CTRL_LEN - iv_len);
1574 offset_dma = dm_dma_addr - OFF_CTRL_LEN - iv_len;
1577 req->ist.ei1 = offset_dma;
1578 /* RPTR should just exclude offset control word */
1579 req->ist.ei2 = dm_dma_addr - iv_len;
1580 req->alternate_caddr = (uint64_t *)((uint8_t *)dm_vaddr
1581 + outputlen - iv_len);
1583 vq_cmd_w0.s.dlen = inputlen + OFF_CTRL_LEN;
1585 if (likely(iv_len)) {
1586 uint32_t *iv_d = (uint32_t *)((uint8_t *)offset_vaddr
1588 memcpy(iv_d, iv, 16);
1591 *offset_vaddr = offset_ctrl;
1593 uint32_t i, g_size_bytes, s_size_bytes;
1594 uint64_t dptr_dma, rptr_dma;
1595 sg_comp_t *gather_comp;
1596 sg_comp_t *scatter_comp;
1600 /* save space for iv */
1601 offset_vaddr = m_vaddr;
1604 m_vaddr = (uint8_t *)m_vaddr + OFF_CTRL_LEN + iv_len;
1605 m_dma += OFF_CTRL_LEN + iv_len;
1607 vq_cmd_w0.s.opcode.major |= CPT_DMA_MODE;
1609 /* DPTR has SG list */
1610 in_buffer = m_vaddr;
1613 ((uint16_t *)in_buffer)[0] = 0;
1614 ((uint16_t *)in_buffer)[1] = 0;
1616 /* TODO Add error check if space will be sufficient */
1617 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
1624 /* Offset control word followed by iv */
1626 i = fill_sg_comp(gather_comp, i, offset_dma,
1627 OFF_CTRL_LEN + iv_len);
1629 /* iv offset is 0 */
1630 *offset_vaddr = offset_ctrl;
1632 iv_d = (uint32_t *)((uint8_t *)offset_vaddr + OFF_CTRL_LEN);
1633 memcpy(iv_d, iv, 16);
1636 size = inputlen - iv_len;
1638 i = fill_sg_comp_from_iov(gather_comp, i,
1641 if (unlikely(size)) {
1642 CPT_LOG_DP_ERR("Insufficient buffer space,"
1643 " size %d needed", size);
1647 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1648 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1651 * Output Scatter List
1656 (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
1659 /* IV in SLIST only for EEA3 & UEA2 */
1664 i = fill_sg_comp(scatter_comp, i,
1665 offset_dma + OFF_CTRL_LEN, iv_len);
1668 /* Add output data */
1669 if (req_flags & VALID_MAC_BUF) {
1670 size = outputlen - iv_len - mac_len;
1672 i = fill_sg_comp_from_iov(scatter_comp, i,
1676 if (unlikely(size)) {
1677 CPT_LOG_DP_ERR("Insufficient buffer space,"
1678 " size %d needed", size);
1685 i = fill_sg_comp_from_buf(scatter_comp, i,
1689 /* Output including mac */
1690 size = outputlen - iv_len;
1692 i = fill_sg_comp_from_iov(scatter_comp, i,
1696 if (unlikely(size)) {
1697 CPT_LOG_DP_ERR("Insufficient buffer space,"
1698 " size %d needed", size);
1703 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1704 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1706 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
1708 /* This is DPTR len incase of SG mode */
1709 vq_cmd_w0.s.dlen = size;
1711 m_vaddr = (uint8_t *)m_vaddr + size;
1714 /* cpt alternate completion address saved earlier */
1715 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
1716 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
1717 rptr_dma = c_dma - 8;
1719 req->ist.ei1 = dptr_dma;
1720 req->ist.ei2 = rptr_dma;
1723 /* 16 byte aligned cpt res address */
1724 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
1725 *req->completion_addr = COMPLETION_CODE_INIT;
1726 req->comp_baddr = c_dma;
1728 /* Fill microcode part of instruction */
1729 req->ist.ei0 = vq_cmd_w0.u64;
1737 static __rte_always_inline void
1738 cpt_zuc_snow3g_dec_prep(uint32_t req_flags,
1741 fc_params_t *params,
1746 int32_t inputlen = 0, outputlen;
1747 struct cpt_ctx *cpt_ctx;
1748 uint8_t snow3g, iv_len = 16;
1749 struct cpt_request_info *req;
1751 uint32_t encr_offset;
1752 uint32_t encr_data_len;
1754 void *m_vaddr, *c_vaddr;
1755 uint64_t m_dma, c_dma;
1756 uint64_t *offset_vaddr, offset_dma;
1757 uint32_t *iv_s, iv[4], j;
1758 vq_cmd_word0_t vq_cmd_w0;
1760 buf_p = ¶ms->meta_buf;
1761 m_vaddr = buf_p->vaddr;
1762 m_dma = buf_p->dma_addr;
1765 * Microcode expects offsets in bytes
1766 * TODO: Rounding off
1768 encr_offset = ENCR_OFFSET(d_offs) / 8;
1769 encr_data_len = ENCR_DLEN(d_lens);
1771 cpt_ctx = params->ctx_buf.vaddr;
1772 flags = cpt_ctx->zsk_flags;
1773 snow3g = cpt_ctx->snow3g;
1775 * Save initial space that followed app data for completion code &
1776 * alternate completion code to fall in same cache line as app data
1778 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
1779 m_dma += COMPLETION_CODE_SIZE;
1780 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
1783 c_vaddr = (uint8_t *)m_vaddr + size;
1784 c_dma = m_dma + size;
1785 size += sizeof(cpt_res_s_t);
1787 m_vaddr = (uint8_t *)m_vaddr + size;
1790 /* Reserve memory for cpt request info */
1793 size = sizeof(struct cpt_request_info);
1794 m_vaddr = (uint8_t *)m_vaddr + size;
1798 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_ZUC_SNOW3G;
1800 /* indicates CPTR ctx, operation type, KEY & IV mode from DPTR */
1802 vq_cmd_w0.s.opcode.minor = ((1 << 7) | (snow3g << 5) | (0 << 4) |
1803 (0 << 3) | (flags & 0x7));
1805 /* consider iv len */
1806 encr_offset += iv_len;
1808 inputlen = encr_offset +
1809 (RTE_ALIGN(encr_data_len, 8) / 8);
1810 outputlen = inputlen;
1813 iv_s = params->iv_buf;
1816 * DPDK seems to provide it in form of IV3 IV2 IV1 IV0
1817 * and BigEndian, MC needs it as IV0 IV1 IV2 IV3
1820 for (j = 0; j < 4; j++)
1821 iv[j] = iv_s[3 - j];
1823 /* ZUC doesn't need a swap */
1824 for (j = 0; j < 4; j++)
1829 * GP op header, lengths are expected in bits.
1831 vq_cmd_w0.s.param1 = encr_data_len;
1834 * In 83XX since we have a limitation of
1835 * IV & Offset control word not part of instruction
1836 * and need to be part of Data Buffer, we check if
1837 * head room is there and then only do the Direct mode processing
1839 if (likely((req_flags & SINGLE_BUF_INPLACE) &&
1840 (req_flags & SINGLE_BUF_HEADTAILROOM))) {
1841 void *dm_vaddr = params->bufs[0].vaddr;
1842 uint64_t dm_dma_addr = params->bufs[0].dma_addr;
1844 * This flag indicates that there is 24 bytes head room and
1845 * 8 bytes tail room available, so that we get to do
1846 * DIRECT MODE with limitation
1849 offset_vaddr = (uint64_t *)((uint8_t *)dm_vaddr -
1850 OFF_CTRL_LEN - iv_len);
1851 offset_dma = dm_dma_addr - OFF_CTRL_LEN - iv_len;
1854 req->ist.ei1 = offset_dma;
1855 /* RPTR should just exclude offset control word */
1856 req->ist.ei2 = dm_dma_addr - iv_len;
1857 req->alternate_caddr = (uint64_t *)((uint8_t *)dm_vaddr
1858 + outputlen - iv_len);
1860 vq_cmd_w0.s.dlen = inputlen + OFF_CTRL_LEN;
1862 if (likely(iv_len)) {
1863 uint32_t *iv_d = (uint32_t *)((uint8_t *)offset_vaddr
1865 memcpy(iv_d, iv, 16);
1868 /* iv offset is 0 */
1869 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1871 uint32_t i, g_size_bytes, s_size_bytes;
1872 uint64_t dptr_dma, rptr_dma;
1873 sg_comp_t *gather_comp;
1874 sg_comp_t *scatter_comp;
1878 /* save space for offset and iv... */
1879 offset_vaddr = m_vaddr;
1882 m_vaddr = (uint8_t *)m_vaddr + OFF_CTRL_LEN + iv_len;
1883 m_dma += OFF_CTRL_LEN + iv_len;
1885 vq_cmd_w0.s.opcode.major |= CPT_DMA_MODE;
1887 /* DPTR has SG list */
1888 in_buffer = m_vaddr;
1891 ((uint16_t *)in_buffer)[0] = 0;
1892 ((uint16_t *)in_buffer)[1] = 0;
1894 /* TODO Add error check if space will be sufficient */
1895 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
1902 /* Offset control word */
1904 /* iv offset is 0 */
1905 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1907 i = fill_sg_comp(gather_comp, i, offset_dma,
1908 OFF_CTRL_LEN + iv_len);
1910 iv_d = (uint32_t *)((uint8_t *)offset_vaddr + OFF_CTRL_LEN);
1911 memcpy(iv_d, iv, 16);
1913 /* Add input data */
1914 size = inputlen - iv_len;
1916 i = fill_sg_comp_from_iov(gather_comp, i,
1919 if (unlikely(size)) {
1920 CPT_LOG_DP_ERR("Insufficient buffer space,"
1921 " size %d needed", size);
1925 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1926 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1929 * Output Scatter List
1934 (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
1937 i = fill_sg_comp(scatter_comp, i,
1938 offset_dma + OFF_CTRL_LEN,
1941 /* Add output data */
1942 size = outputlen - iv_len;
1944 i = fill_sg_comp_from_iov(scatter_comp, i,
1948 if (unlikely(size)) {
1949 CPT_LOG_DP_ERR("Insufficient buffer space,"
1950 " size %d needed", size);
1954 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1955 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
1957 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
1959 /* This is DPTR len incase of SG mode */
1960 vq_cmd_w0.s.dlen = size;
1962 m_vaddr = (uint8_t *)m_vaddr + size;
1965 /* cpt alternate completion address saved earlier */
1966 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
1967 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
1968 rptr_dma = c_dma - 8;
1970 req->ist.ei1 = dptr_dma;
1971 req->ist.ei2 = rptr_dma;
1974 if (unlikely((encr_offset >> 16))) {
1975 CPT_LOG_DP_ERR("Offset not supported");
1976 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
1980 /* 16 byte aligned cpt res address */
1981 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
1982 *req->completion_addr = COMPLETION_CODE_INIT;
1983 req->comp_baddr = c_dma;
1985 /* Fill microcode part of instruction */
1986 req->ist.ei0 = vq_cmd_w0.u64;
1994 static __rte_always_inline void
1995 cpt_kasumi_enc_prep(uint32_t req_flags,
1998 fc_params_t *params,
2003 int32_t inputlen = 0, outputlen = 0;
2004 struct cpt_ctx *cpt_ctx;
2005 uint32_t mac_len = 0;
2007 struct cpt_request_info *req;
2009 uint32_t encr_offset, auth_offset;
2010 uint32_t encr_data_len, auth_data_len;
2012 uint8_t *iv_s, *iv_d, iv_len = 8;
2014 void *m_vaddr, *c_vaddr;
2015 uint64_t m_dma, c_dma;
2016 uint64_t *offset_vaddr, offset_dma;
2017 vq_cmd_word0_t vq_cmd_w0;
2019 uint32_t g_size_bytes, s_size_bytes;
2020 uint64_t dptr_dma, rptr_dma;
2021 sg_comp_t *gather_comp;
2022 sg_comp_t *scatter_comp;
2024 buf_p = ¶ms->meta_buf;
2025 m_vaddr = buf_p->vaddr;
2026 m_dma = buf_p->dma_addr;
2028 encr_offset = ENCR_OFFSET(d_offs) / 8;
2029 auth_offset = AUTH_OFFSET(d_offs) / 8;
2030 encr_data_len = ENCR_DLEN(d_lens);
2031 auth_data_len = AUTH_DLEN(d_lens);
2033 cpt_ctx = params->ctx_buf.vaddr;
2034 flags = cpt_ctx->zsk_flags;
2035 mac_len = cpt_ctx->mac_len;
2038 iv_s = params->iv_buf;
2040 iv_s = params->auth_iv_buf;
2042 dir = iv_s[8] & 0x1;
2045 * Save initial space that followed app data for completion code &
2046 * alternate completion code to fall in same cache line as app data
2048 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
2049 m_dma += COMPLETION_CODE_SIZE;
2050 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
2053 c_vaddr = (uint8_t *)m_vaddr + size;
2054 c_dma = m_dma + size;
2055 size += sizeof(cpt_res_s_t);
2057 m_vaddr = (uint8_t *)m_vaddr + size;
2060 /* Reserve memory for cpt request info */
2063 size = sizeof(struct cpt_request_info);
2064 m_vaddr = (uint8_t *)m_vaddr + size;
2067 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_KASUMI | CPT_DMA_MODE;
2069 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
2070 vq_cmd_w0.s.opcode.minor = ((1 << 6) | (cpt_ctx->k_ecb << 5) |
2071 (dir << 4) | (0 << 3) | (flags & 0x7));
2074 * GP op header, lengths are expected in bits.
2076 vq_cmd_w0.s.param1 = encr_data_len;
2077 vq_cmd_w0.s.param2 = auth_data_len;
2079 /* consider iv len */
2081 encr_offset += iv_len;
2082 auth_offset += iv_len;
2085 /* save space for offset ctrl and iv */
2086 offset_vaddr = m_vaddr;
2089 m_vaddr = (uint8_t *)m_vaddr + OFF_CTRL_LEN + iv_len;
2090 m_dma += OFF_CTRL_LEN + iv_len;
2092 /* DPTR has SG list */
2093 in_buffer = m_vaddr;
2096 ((uint16_t *)in_buffer)[0] = 0;
2097 ((uint16_t *)in_buffer)[1] = 0;
2099 /* TODO Add error check if space will be sufficient */
2100 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
2107 /* Offset control word followed by iv */
2110 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
2111 outputlen = inputlen;
2112 /* iv offset is 0 */
2113 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
2114 if (unlikely((encr_offset >> 16))) {
2115 CPT_LOG_DP_ERR("Offset not supported");
2116 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
2120 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
2121 outputlen = mac_len;
2122 /* iv offset is 0 */
2123 *offset_vaddr = rte_cpu_to_be_64((uint64_t)auth_offset);
2124 if (unlikely((auth_offset >> 8))) {
2125 CPT_LOG_DP_ERR("Offset not supported");
2126 CPT_LOG_DP_ERR("auth_offset: %d", auth_offset);
2131 i = fill_sg_comp(gather_comp, i, offset_dma, OFF_CTRL_LEN + iv_len);
2134 iv_d = (uint8_t *)offset_vaddr + OFF_CTRL_LEN;
2135 memcpy(iv_d, iv_s, iv_len);
2138 size = inputlen - iv_len;
2140 i = fill_sg_comp_from_iov(gather_comp, i,
2144 if (unlikely(size)) {
2145 CPT_LOG_DP_ERR("Insufficient buffer space,"
2146 " size %d needed", size);
2150 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
2151 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
2154 * Output Scatter List
2158 scatter_comp = (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
2161 /* IV in SLIST only for F8 */
2167 i = fill_sg_comp(scatter_comp, i,
2168 offset_dma + OFF_CTRL_LEN,
2172 /* Add output data */
2173 if (req_flags & VALID_MAC_BUF) {
2174 size = outputlen - iv_len - mac_len;
2176 i = fill_sg_comp_from_iov(scatter_comp, i,
2180 if (unlikely(size)) {
2181 CPT_LOG_DP_ERR("Insufficient buffer space,"
2182 " size %d needed", size);
2189 i = fill_sg_comp_from_buf(scatter_comp, i,
2193 /* Output including mac */
2194 size = outputlen - iv_len;
2196 i = fill_sg_comp_from_iov(scatter_comp, i,
2200 if (unlikely(size)) {
2201 CPT_LOG_DP_ERR("Insufficient buffer space,"
2202 " size %d needed", size);
2207 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
2208 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
2210 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
2212 /* This is DPTR len incase of SG mode */
2213 vq_cmd_w0.s.dlen = size;
2215 m_vaddr = (uint8_t *)m_vaddr + size;
2218 /* cpt alternate completion address saved earlier */
2219 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
2220 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
2221 rptr_dma = c_dma - 8;
2223 req->ist.ei1 = dptr_dma;
2224 req->ist.ei2 = rptr_dma;
2226 /* 16 byte aligned cpt res address */
2227 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
2228 *req->completion_addr = COMPLETION_CODE_INIT;
2229 req->comp_baddr = c_dma;
2231 /* Fill microcode part of instruction */
2232 req->ist.ei0 = vq_cmd_w0.u64;
2240 static __rte_always_inline void
2241 cpt_kasumi_dec_prep(uint64_t d_offs,
2243 fc_params_t *params,
2248 int32_t inputlen = 0, outputlen;
2249 struct cpt_ctx *cpt_ctx;
2250 uint8_t i = 0, iv_len = 8;
2251 struct cpt_request_info *req;
2253 uint32_t encr_offset;
2254 uint32_t encr_data_len;
2257 void *m_vaddr, *c_vaddr;
2258 uint64_t m_dma, c_dma;
2259 uint64_t *offset_vaddr, offset_dma;
2260 vq_cmd_word0_t vq_cmd_w0;
2262 uint32_t g_size_bytes, s_size_bytes;
2263 uint64_t dptr_dma, rptr_dma;
2264 sg_comp_t *gather_comp;
2265 sg_comp_t *scatter_comp;
2267 buf_p = ¶ms->meta_buf;
2268 m_vaddr = buf_p->vaddr;
2269 m_dma = buf_p->dma_addr;
2271 encr_offset = ENCR_OFFSET(d_offs) / 8;
2272 encr_data_len = ENCR_DLEN(d_lens);
2274 cpt_ctx = params->ctx_buf.vaddr;
2275 flags = cpt_ctx->zsk_flags;
2277 * Save initial space that followed app data for completion code &
2278 * alternate completion code to fall in same cache line as app data
2280 m_vaddr = (uint8_t *)m_vaddr + COMPLETION_CODE_SIZE;
2281 m_dma += COMPLETION_CODE_SIZE;
2282 size = (uint8_t *)RTE_PTR_ALIGN((uint8_t *)m_vaddr, 16) -
2285 c_vaddr = (uint8_t *)m_vaddr + size;
2286 c_dma = m_dma + size;
2287 size += sizeof(cpt_res_s_t);
2289 m_vaddr = (uint8_t *)m_vaddr + size;
2292 /* Reserve memory for cpt request info */
2295 size = sizeof(struct cpt_request_info);
2296 m_vaddr = (uint8_t *)m_vaddr + size;
2300 vq_cmd_w0.s.opcode.major = CPT_MAJOR_OP_KASUMI | CPT_DMA_MODE;
2302 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
2303 vq_cmd_w0.s.opcode.minor = ((1 << 6) | (cpt_ctx->k_ecb << 5) |
2304 (dir << 4) | (0 << 3) | (flags & 0x7));
2307 * GP op header, lengths are expected in bits.
2309 vq_cmd_w0.s.param1 = encr_data_len;
2311 /* consider iv len */
2312 encr_offset += iv_len;
2314 inputlen = iv_len + (RTE_ALIGN(encr_data_len, 8) / 8);
2315 outputlen = inputlen;
2317 /* save space for offset ctrl & iv */
2318 offset_vaddr = m_vaddr;
2321 m_vaddr = (uint8_t *)m_vaddr + OFF_CTRL_LEN + iv_len;
2322 m_dma += OFF_CTRL_LEN + iv_len;
2324 /* DPTR has SG list */
2325 in_buffer = m_vaddr;
2328 ((uint16_t *)in_buffer)[0] = 0;
2329 ((uint16_t *)in_buffer)[1] = 0;
2331 /* TODO Add error check if space will be sufficient */
2332 gather_comp = (sg_comp_t *)((uint8_t *)m_vaddr + 8);
2339 /* Offset control word followed by iv */
2340 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
2341 if (unlikely((encr_offset >> 16))) {
2342 CPT_LOG_DP_ERR("Offset not supported");
2343 CPT_LOG_DP_ERR("enc_offset: %d", encr_offset);
2347 i = fill_sg_comp(gather_comp, i, offset_dma, OFF_CTRL_LEN + iv_len);
2350 memcpy((uint8_t *)offset_vaddr + OFF_CTRL_LEN,
2351 params->iv_buf, iv_len);
2353 /* Add input data */
2354 size = inputlen - iv_len;
2356 i = fill_sg_comp_from_iov(gather_comp, i,
2359 if (unlikely(size)) {
2360 CPT_LOG_DP_ERR("Insufficient buffer space,"
2361 " size %d needed", size);
2365 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
2366 g_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
2369 * Output Scatter List
2373 scatter_comp = (sg_comp_t *)((uint8_t *)gather_comp + g_size_bytes);
2376 i = fill_sg_comp(scatter_comp, i,
2377 offset_dma + OFF_CTRL_LEN,
2380 /* Add output data */
2381 size = outputlen - iv_len;
2383 i = fill_sg_comp_from_iov(scatter_comp, i,
2386 if (unlikely(size)) {
2387 CPT_LOG_DP_ERR("Insufficient buffer space,"
2388 " size %d needed", size);
2392 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
2393 s_size_bytes = ((i + 3) / 4) * sizeof(sg_comp_t);
2395 size = g_size_bytes + s_size_bytes + SG_LIST_HDR_SIZE;
2397 /* This is DPTR len incase of SG mode */
2398 vq_cmd_w0.s.dlen = size;
2400 m_vaddr = (uint8_t *)m_vaddr + size;
2403 /* cpt alternate completion address saved earlier */
2404 req->alternate_caddr = (uint64_t *)((uint8_t *)c_vaddr - 8);
2405 *req->alternate_caddr = ~((uint64_t)COMPLETION_CODE_INIT);
2406 rptr_dma = c_dma - 8;
2408 req->ist.ei1 = dptr_dma;
2409 req->ist.ei2 = rptr_dma;
2411 /* 16 byte aligned cpt res address */
2412 req->completion_addr = (uint64_t *)((uint8_t *)c_vaddr);
2413 *req->completion_addr = COMPLETION_CODE_INIT;
2414 req->comp_baddr = c_dma;
2416 /* Fill microcode part of instruction */
2417 req->ist.ei0 = vq_cmd_w0.u64;
2425 static __rte_always_inline void *
2426 cpt_fc_dec_hmac_prep(uint32_t flags,
2429 fc_params_t *fc_params,
2432 struct cpt_ctx *ctx = fc_params->ctx_buf.vaddr;
2434 void *prep_req = NULL;
2436 fc_type = ctx->fc_type;
2438 if (likely(fc_type == FC_GEN)) {
2439 cpt_dec_hmac_prep(flags, d_offs, d_lens, fc_params, op,
2441 } else if (fc_type == ZUC_SNOW3G) {
2442 cpt_zuc_snow3g_dec_prep(flags, d_offs, d_lens, fc_params, op,
2444 } else if (fc_type == KASUMI) {
2445 cpt_kasumi_dec_prep(d_offs, d_lens, fc_params, op, &prep_req);
2449 * For AUTH_ONLY case,
2450 * MC only supports digest generation and verification
2451 * should be done in software by memcmp()
2457 static __rte_always_inline void *__rte_hot
2458 cpt_fc_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
2459 fc_params_t *fc_params, void *op)
2461 struct cpt_ctx *ctx = fc_params->ctx_buf.vaddr;
2463 void *prep_req = NULL;
2465 fc_type = ctx->fc_type;
2467 /* Common api for rest of the ops */
2468 if (likely(fc_type == FC_GEN)) {
2469 cpt_enc_hmac_prep(flags, d_offs, d_lens, fc_params, op,
2471 } else if (fc_type == ZUC_SNOW3G) {
2472 cpt_zuc_snow3g_enc_prep(flags, d_offs, d_lens, fc_params, op,
2474 } else if (fc_type == KASUMI) {
2475 cpt_kasumi_enc_prep(flags, d_offs, d_lens, fc_params, op,
2477 } else if (fc_type == HASH_HMAC) {
2478 cpt_digest_gen_prep(flags, d_lens, fc_params, op, &prep_req);
2484 static __rte_always_inline int
2485 cpt_fc_auth_set_key(struct cpt_ctx *cpt_ctx, auth_type_t type,
2486 const uint8_t *key, uint16_t key_len, uint16_t mac_len)
2488 mc_fc_context_t *fctx = &cpt_ctx->mc_ctx.fctx;
2489 mc_zuc_snow3g_ctx_t *zs_ctx = &cpt_ctx->mc_ctx.zs_ctx;
2490 mc_kasumi_ctx_t *k_ctx = &cpt_ctx->mc_ctx.k_ctx;
2492 if ((type >= ZUC_EIA3) && (type <= KASUMI_F9_ECB)) {
2497 /* No support for AEAD yet */
2498 if (cpt_ctx->enc_cipher)
2500 /* For ZUC/SNOW3G/Kasumi */
2503 cpt_ctx->snow3g = 1;
2504 gen_key_snow3g(key, keyx);
2505 memcpy(zs_ctx->ci_key, keyx, key_len);
2506 cpt_ctx->fc_type = ZUC_SNOW3G;
2507 cpt_ctx->zsk_flags = 0x1;
2510 cpt_ctx->snow3g = 0;
2511 memcpy(zs_ctx->ci_key, key, key_len);
2512 memcpy(zs_ctx->zuc_const, zuc_d, 32);
2513 cpt_ctx->fc_type = ZUC_SNOW3G;
2514 cpt_ctx->zsk_flags = 0x1;
2517 /* Kasumi ECB mode */
2519 memcpy(k_ctx->ci_key, key, key_len);
2520 cpt_ctx->fc_type = KASUMI;
2521 cpt_ctx->zsk_flags = 0x1;
2524 memcpy(k_ctx->ci_key, key, key_len);
2525 cpt_ctx->fc_type = KASUMI;
2526 cpt_ctx->zsk_flags = 0x1;
2531 cpt_ctx->mac_len = 4;
2532 cpt_ctx->hash_type = type;
2536 if (!(cpt_ctx->fc_type == FC_GEN && !type)) {
2537 if (!cpt_ctx->fc_type || !cpt_ctx->enc_cipher)
2538 cpt_ctx->fc_type = HASH_HMAC;
2541 if (cpt_ctx->fc_type == FC_GEN && key_len > 64)
2544 /* For GMAC auth, cipher must be NULL */
2545 if (type == GMAC_TYPE)
2546 fctx->enc.enc_cipher = 0;
2548 fctx->enc.hash_type = cpt_ctx->hash_type = type;
2549 fctx->enc.mac_len = cpt_ctx->mac_len = mac_len;
2554 cpt_ctx->auth_key = rte_zmalloc(NULL, key_len, 8);
2555 if (cpt_ctx->auth_key == NULL)
2558 cpt_ctx->auth_key_iova = rte_mem_virt2iova(cpt_ctx->auth_key);
2559 memcpy(cpt_ctx->auth_key, key, key_len);
2560 cpt_ctx->auth_key_len = key_len;
2561 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
2562 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
2565 memcpy(fctx->hmac.opad, key, key_len);
2566 fctx->enc.auth_input_type = 1;
2571 static __rte_always_inline int
2572 fill_sess_aead(struct rte_crypto_sym_xform *xform,
2573 struct cpt_sess_misc *sess)
2575 struct rte_crypto_aead_xform *aead_form;
2576 cipher_type_t enc_type = 0; /* NULL Cipher type */
2577 auth_type_t auth_type = 0; /* NULL Auth type */
2578 uint32_t cipher_key_len = 0;
2579 uint8_t aes_gcm = 0;
2580 aead_form = &xform->aead;
2581 void *ctx = SESS_PRIV(sess);
2583 if (aead_form->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
2584 sess->cpt_op |= CPT_OP_CIPHER_ENCRYPT;
2585 sess->cpt_op |= CPT_OP_AUTH_GENERATE;
2586 } else if (aead_form->op == RTE_CRYPTO_AEAD_OP_DECRYPT) {
2587 sess->cpt_op |= CPT_OP_CIPHER_DECRYPT;
2588 sess->cpt_op |= CPT_OP_AUTH_VERIFY;
2590 CPT_LOG_DP_ERR("Unknown aead operation\n");
2593 switch (aead_form->algo) {
2594 case RTE_CRYPTO_AEAD_AES_GCM:
2596 cipher_key_len = 16;
2599 case RTE_CRYPTO_AEAD_AES_CCM:
2600 CPT_LOG_DP_ERR("Crypto: Unsupported cipher algo %u",
2603 case RTE_CRYPTO_AEAD_CHACHA20_POLY1305:
2604 enc_type = CHACHA20;
2605 auth_type = POLY1305;
2606 cipher_key_len = 32;
2607 sess->chacha_poly = 1;
2610 CPT_LOG_DP_ERR("Crypto: Undefined cipher algo %u specified",
2614 if (aead_form->key.length < cipher_key_len) {
2615 CPT_LOG_DP_ERR("Invalid cipher params keylen %lu",
2616 (unsigned int long)aead_form->key.length);
2620 sess->aes_gcm = aes_gcm;
2621 sess->mac_len = aead_form->digest_length;
2622 sess->iv_offset = aead_form->iv.offset;
2623 sess->iv_length = aead_form->iv.length;
2624 sess->aad_length = aead_form->aad_length;
2626 if (unlikely(cpt_fc_ciph_set_key(ctx, enc_type, aead_form->key.data,
2627 aead_form->key.length, NULL)))
2630 if (unlikely(cpt_fc_auth_set_key(ctx, auth_type, NULL, 0,
2631 aead_form->digest_length)))
2637 static __rte_always_inline int
2638 fill_sess_cipher(struct rte_crypto_sym_xform *xform,
2639 struct cpt_sess_misc *sess)
2641 struct rte_crypto_cipher_xform *c_form;
2642 struct cpt_ctx *ctx = SESS_PRIV(sess);
2643 cipher_type_t enc_type = 0; /* NULL Cipher type */
2644 uint32_t cipher_key_len = 0;
2645 uint8_t zsk_flag = 0, aes_ctr = 0, is_null = 0;
2647 c_form = &xform->cipher;
2649 if (c_form->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
2650 sess->cpt_op |= CPT_OP_CIPHER_ENCRYPT;
2651 else if (c_form->op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2652 sess->cpt_op |= CPT_OP_CIPHER_DECRYPT;
2653 if (xform->next != NULL &&
2654 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2655 /* Perform decryption followed by auth verify */
2659 CPT_LOG_DP_ERR("Unknown cipher operation\n");
2663 switch (c_form->algo) {
2664 case RTE_CRYPTO_CIPHER_AES_CBC:
2666 cipher_key_len = 16;
2668 case RTE_CRYPTO_CIPHER_3DES_CBC:
2669 enc_type = DES3_CBC;
2670 cipher_key_len = 24;
2672 case RTE_CRYPTO_CIPHER_DES_CBC:
2673 /* DES is implemented using 3DES in hardware */
2674 enc_type = DES3_CBC;
2677 case RTE_CRYPTO_CIPHER_AES_CTR:
2679 cipher_key_len = 16;
2682 case RTE_CRYPTO_CIPHER_NULL:
2686 case RTE_CRYPTO_CIPHER_KASUMI_F8:
2687 enc_type = KASUMI_F8_ECB;
2688 cipher_key_len = 16;
2691 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2692 enc_type = SNOW3G_UEA2;
2693 cipher_key_len = 16;
2696 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2697 enc_type = ZUC_EEA3;
2698 cipher_key_len = 16;
2701 case RTE_CRYPTO_CIPHER_AES_XTS:
2703 cipher_key_len = 16;
2705 case RTE_CRYPTO_CIPHER_3DES_ECB:
2706 enc_type = DES3_ECB;
2707 cipher_key_len = 24;
2709 case RTE_CRYPTO_CIPHER_AES_ECB:
2711 cipher_key_len = 16;
2713 case RTE_CRYPTO_CIPHER_3DES_CTR:
2714 case RTE_CRYPTO_CIPHER_AES_F8:
2715 case RTE_CRYPTO_CIPHER_ARC4:
2716 CPT_LOG_DP_ERR("Crypto: Unsupported cipher algo %u",
2720 CPT_LOG_DP_ERR("Crypto: Undefined cipher algo %u specified",
2725 if (c_form->key.length < cipher_key_len) {
2726 CPT_LOG_DP_ERR("Invalid cipher params keylen %lu",
2727 (unsigned long) c_form->key.length);
2731 sess->zsk_flag = zsk_flag;
2733 sess->aes_ctr = aes_ctr;
2734 sess->iv_offset = c_form->iv.offset;
2735 sess->iv_length = c_form->iv.length;
2736 sess->is_null = is_null;
2738 if (unlikely(cpt_fc_ciph_set_key(SESS_PRIV(sess), enc_type,
2739 c_form->key.data, c_form->key.length, NULL)))
2745 static __rte_always_inline int
2746 fill_sess_auth(struct rte_crypto_sym_xform *xform,
2747 struct cpt_sess_misc *sess)
2749 struct cpt_ctx *ctx = SESS_PRIV(sess);
2750 struct rte_crypto_auth_xform *a_form;
2751 auth_type_t auth_type = 0; /* NULL Auth type */
2752 uint8_t zsk_flag = 0, aes_gcm = 0, is_null = 0;
2754 if (xform->next != NULL &&
2755 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2756 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2757 /* Perform auth followed by encryption */
2761 a_form = &xform->auth;
2763 if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
2764 sess->cpt_op |= CPT_OP_AUTH_VERIFY;
2765 else if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
2766 sess->cpt_op |= CPT_OP_AUTH_GENERATE;
2768 CPT_LOG_DP_ERR("Unknown auth operation");
2772 switch (a_form->algo) {
2773 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2775 case RTE_CRYPTO_AUTH_SHA1:
2776 auth_type = SHA1_TYPE;
2778 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2779 case RTE_CRYPTO_AUTH_SHA256:
2780 auth_type = SHA2_SHA256;
2782 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2783 case RTE_CRYPTO_AUTH_SHA512:
2784 auth_type = SHA2_SHA512;
2786 case RTE_CRYPTO_AUTH_AES_GMAC:
2787 auth_type = GMAC_TYPE;
2790 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2791 case RTE_CRYPTO_AUTH_SHA224:
2792 auth_type = SHA2_SHA224;
2794 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2795 case RTE_CRYPTO_AUTH_SHA384:
2796 auth_type = SHA2_SHA384;
2798 case RTE_CRYPTO_AUTH_MD5_HMAC:
2799 case RTE_CRYPTO_AUTH_MD5:
2800 auth_type = MD5_TYPE;
2802 case RTE_CRYPTO_AUTH_KASUMI_F9:
2803 auth_type = KASUMI_F9_ECB;
2805 * Indicate that direction needs to be taken out
2810 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2811 auth_type = SNOW3G_UIA2;
2814 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2815 auth_type = ZUC_EIA3;
2818 case RTE_CRYPTO_AUTH_NULL:
2822 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2823 case RTE_CRYPTO_AUTH_AES_CMAC:
2824 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2825 CPT_LOG_DP_ERR("Crypto: Unsupported hash algo %u",
2829 CPT_LOG_DP_ERR("Crypto: Undefined Hash algo %u specified",
2834 sess->zsk_flag = zsk_flag;
2835 sess->aes_gcm = aes_gcm;
2836 sess->mac_len = a_form->digest_length;
2837 sess->is_null = is_null;
2839 sess->auth_iv_offset = a_form->iv.offset;
2840 sess->auth_iv_length = a_form->iv.length;
2842 if (unlikely(cpt_fc_auth_set_key(SESS_PRIV(sess), auth_type,
2843 a_form->key.data, a_form->key.length,
2844 a_form->digest_length)))
2850 static __rte_always_inline int
2851 fill_sess_gmac(struct rte_crypto_sym_xform *xform,
2852 struct cpt_sess_misc *sess)
2854 struct rte_crypto_auth_xform *a_form;
2855 cipher_type_t enc_type = 0; /* NULL Cipher type */
2856 auth_type_t auth_type = 0; /* NULL Auth type */
2857 void *ctx = SESS_PRIV(sess);
2859 a_form = &xform->auth;
2861 if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
2862 sess->cpt_op |= CPT_OP_ENCODE;
2863 else if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
2864 sess->cpt_op |= CPT_OP_DECODE;
2866 CPT_LOG_DP_ERR("Unknown auth operation");
2870 switch (a_form->algo) {
2871 case RTE_CRYPTO_AUTH_AES_GMAC:
2873 auth_type = GMAC_TYPE;
2876 CPT_LOG_DP_ERR("Crypto: Undefined cipher algo %u specified",
2884 sess->iv_offset = a_form->iv.offset;
2885 sess->iv_length = a_form->iv.length;
2886 sess->mac_len = a_form->digest_length;
2888 if (unlikely(cpt_fc_ciph_set_key(ctx, enc_type, a_form->key.data,
2889 a_form->key.length, NULL)))
2892 if (unlikely(cpt_fc_auth_set_key(ctx, auth_type, NULL, 0,
2893 a_form->digest_length)))
2899 static __rte_always_inline void *
2900 alloc_op_meta(struct rte_mbuf *m_src,
2903 struct rte_mempool *cpt_meta_pool)
2907 #ifndef CPT_ALWAYS_USE_SEPARATE_BUF
2908 if (likely(m_src && (m_src->nb_segs == 1))) {
2912 /* Check if tailroom is sufficient to hold meta data */
2913 tailroom = rte_pktmbuf_tailroom(m_src);
2914 if (likely(tailroom > len + 8)) {
2915 mdata = (uint8_t *)m_src->buf_addr + m_src->buf_len;
2916 mphys = m_src->buf_iova + m_src->buf_len;
2920 buf->dma_addr = mphys;
2922 /* Indicate that this is a mbuf allocated mdata */
2923 mdata = (uint8_t *)((uint64_t)mdata | 1ull);
2928 RTE_SET_USED(m_src);
2931 if (unlikely(rte_mempool_get(cpt_meta_pool, (void **)&mdata) < 0))
2935 buf->dma_addr = rte_mempool_virt2iova(mdata);
2942 * cpt_free_metabuf - free metabuf to mempool.
2943 * @param instance: pointer to instance.
2944 * @param objp: pointer to the metabuf.
2946 static __rte_always_inline void
2947 free_op_meta(void *mdata, struct rte_mempool *cpt_meta_pool)
2949 bool nofree = ((uintptr_t)mdata & 1ull);
2953 rte_mempool_put(cpt_meta_pool, mdata);
2956 static __rte_always_inline uint32_t
2957 prepare_iov_from_pkt(struct rte_mbuf *pkt,
2958 iov_ptr_t *iovec, uint32_t start_offset)
2961 void *seg_data = NULL;
2962 phys_addr_t seg_phys;
2963 int32_t seg_size = 0;
2970 if (!start_offset) {
2971 seg_data = rte_pktmbuf_mtod(pkt, void *);
2972 seg_phys = rte_pktmbuf_iova(pkt);
2973 seg_size = pkt->data_len;
2975 while (start_offset >= pkt->data_len) {
2976 start_offset -= pkt->data_len;
2980 seg_data = rte_pktmbuf_mtod_offset(pkt, void *, start_offset);
2981 seg_phys = rte_pktmbuf_iova_offset(pkt, start_offset);
2982 seg_size = pkt->data_len - start_offset;
2988 iovec->bufs[index].vaddr = seg_data;
2989 iovec->bufs[index].dma_addr = seg_phys;
2990 iovec->bufs[index].size = seg_size;
2994 while (unlikely(pkt != NULL)) {
2995 seg_data = rte_pktmbuf_mtod(pkt, void *);
2996 seg_phys = rte_pktmbuf_iova(pkt);
2997 seg_size = pkt->data_len;
3001 iovec->bufs[index].vaddr = seg_data;
3002 iovec->bufs[index].dma_addr = seg_phys;
3003 iovec->bufs[index].size = seg_size;
3010 iovec->buf_cnt = index;
3014 static __rte_always_inline uint32_t
3015 prepare_iov_from_pkt_inplace(struct rte_mbuf *pkt,
3020 void *seg_data = NULL;
3021 phys_addr_t seg_phys;
3022 uint32_t seg_size = 0;
3025 seg_data = rte_pktmbuf_mtod(pkt, void *);
3026 seg_phys = rte_pktmbuf_iova(pkt);
3027 seg_size = pkt->data_len;
3030 if (likely(!pkt->next)) {
3031 uint32_t headroom, tailroom;
3033 *flags |= SINGLE_BUF_INPLACE;
3034 headroom = rte_pktmbuf_headroom(pkt);
3035 tailroom = rte_pktmbuf_tailroom(pkt);
3036 if (likely((headroom >= 24) &&
3038 /* In 83XX this is prerequivisit for Direct mode */
3039 *flags |= SINGLE_BUF_HEADTAILROOM;
3041 param->bufs[0].vaddr = seg_data;
3042 param->bufs[0].dma_addr = seg_phys;
3043 param->bufs[0].size = seg_size;
3046 iovec = param->src_iov;
3047 iovec->bufs[index].vaddr = seg_data;
3048 iovec->bufs[index].dma_addr = seg_phys;
3049 iovec->bufs[index].size = seg_size;
3053 while (unlikely(pkt != NULL)) {
3054 seg_data = rte_pktmbuf_mtod(pkt, void *);
3055 seg_phys = rte_pktmbuf_iova(pkt);
3056 seg_size = pkt->data_len;
3061 iovec->bufs[index].vaddr = seg_data;
3062 iovec->bufs[index].dma_addr = seg_phys;
3063 iovec->bufs[index].size = seg_size;
3070 iovec->buf_cnt = index;
3074 static __rte_always_inline int
3075 fill_fc_params(struct rte_crypto_op *cop,
3076 struct cpt_sess_misc *sess_misc,
3077 struct cpt_qp_meta_info *m_info,
3082 struct rte_crypto_sym_op *sym_op = cop->sym;
3083 struct cpt_ctx *ctx = SESS_PRIV(sess_misc);
3086 uint32_t mc_hash_off;
3088 uint64_t d_offs, d_lens;
3089 struct rte_mbuf *m_src, *m_dst;
3090 uint8_t cpt_op = sess_misc->cpt_op;
3091 #ifdef CPT_ALWAYS_USE_SG_MODE
3092 uint8_t inplace = 0;
3094 uint8_t inplace = 1;
3096 fc_params_t fc_params;
3097 char src[SRC_IOV_SIZE];
3098 char dst[SRC_IOV_SIZE];
3102 if (likely(sess_misc->iv_length)) {
3103 flags |= VALID_IV_BUF;
3104 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop,
3105 uint8_t *, sess_misc->iv_offset);
3106 if (sess_misc->aes_ctr &&
3107 unlikely(sess_misc->iv_length != 16)) {
3108 memcpy((uint8_t *)iv_buf,
3109 rte_crypto_op_ctod_offset(cop,
3110 uint8_t *, sess_misc->iv_offset), 12);
3111 iv_buf[3] = rte_cpu_to_be_32(0x1);
3112 fc_params.iv_buf = iv_buf;
3116 if (sess_misc->zsk_flag) {
3117 fc_params.auth_iv_buf = rte_crypto_op_ctod_offset(cop,
3119 sess_misc->auth_iv_offset);
3120 if (sess_misc->zsk_flag != ZS_EA)
3123 m_src = sym_op->m_src;
3124 m_dst = sym_op->m_dst;
3126 if (sess_misc->aes_gcm || sess_misc->chacha_poly) {
3131 d_offs = sym_op->aead.data.offset;
3132 d_lens = sym_op->aead.data.length;
3133 mc_hash_off = sym_op->aead.data.offset +
3134 sym_op->aead.data.length;
3136 aad_data = sym_op->aead.aad.data;
3137 aad_len = sess_misc->aad_length;
3138 if (likely((aad_data + aad_len) ==
3139 rte_pktmbuf_mtod_offset(m_src,
3141 sym_op->aead.data.offset))) {
3142 d_offs = (d_offs - aad_len) | (d_offs << 16);
3143 d_lens = (d_lens + aad_len) | (d_lens << 32);
3145 fc_params.aad_buf.vaddr = sym_op->aead.aad.data;
3146 fc_params.aad_buf.dma_addr = sym_op->aead.aad.phys_addr;
3147 fc_params.aad_buf.size = aad_len;
3148 flags |= VALID_AAD_BUF;
3150 d_offs = d_offs << 16;
3151 d_lens = d_lens << 32;
3154 salt = fc_params.iv_buf;
3155 if (unlikely(*(uint32_t *)salt != sess_misc->salt)) {
3156 cpt_fc_salt_update(SESS_PRIV(sess_misc), salt);
3157 sess_misc->salt = *(uint32_t *)salt;
3159 fc_params.iv_buf = salt + 4;
3160 if (likely(sess_misc->mac_len)) {
3161 struct rte_mbuf *m = (cpt_op & CPT_OP_ENCODE) ? m_dst :
3167 /* hmac immediately following data is best case */
3168 if (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
3170 (uint8_t *)sym_op->aead.digest.data)) {
3171 flags |= VALID_MAC_BUF;
3172 fc_params.mac_buf.size = sess_misc->mac_len;
3173 fc_params.mac_buf.vaddr =
3174 sym_op->aead.digest.data;
3175 fc_params.mac_buf.dma_addr =
3176 sym_op->aead.digest.phys_addr;
3181 d_offs = sym_op->cipher.data.offset;
3182 d_lens = sym_op->cipher.data.length;
3183 mc_hash_off = sym_op->cipher.data.offset +
3184 sym_op->cipher.data.length;
3185 d_offs = (d_offs << 16) | sym_op->auth.data.offset;
3186 d_lens = (d_lens << 32) | sym_op->auth.data.length;
3188 if (mc_hash_off < (sym_op->auth.data.offset +
3189 sym_op->auth.data.length)){
3190 mc_hash_off = (sym_op->auth.data.offset +
3191 sym_op->auth.data.length);
3193 /* for gmac, salt should be updated like in gcm */
3194 if (unlikely(sess_misc->is_gmac)) {
3196 salt = fc_params.iv_buf;
3197 if (unlikely(*(uint32_t *)salt != sess_misc->salt)) {
3198 cpt_fc_salt_update(SESS_PRIV(sess_misc), salt);
3199 sess_misc->salt = *(uint32_t *)salt;
3201 fc_params.iv_buf = salt + 4;
3203 if (likely(sess_misc->mac_len)) {
3206 m = (cpt_op & CPT_OP_ENCODE) ? m_dst : m_src;
3210 /* hmac immediately following data is best case */
3211 if (!ctx->dec_auth && !ctx->auth_enc &&
3212 (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
3214 (uint8_t *)sym_op->auth.digest.data))) {
3215 flags |= VALID_MAC_BUF;
3216 fc_params.mac_buf.size =
3218 fc_params.mac_buf.vaddr =
3219 sym_op->auth.digest.data;
3220 fc_params.mac_buf.dma_addr =
3221 sym_op->auth.digest.phys_addr;
3226 fc_params.ctx_buf.vaddr = SESS_PRIV(sess_misc);
3227 fc_params.ctx_buf.dma_addr = sess_misc->ctx_dma_addr;
3229 if (!ctx->dec_auth &&
3230 unlikely(sess_misc->is_null ||
3231 sess_misc->cpt_op == CPT_OP_DECODE))
3234 if (likely(!m_dst && inplace)) {
3235 /* Case of single buffer without AAD buf or
3236 * separate mac buf in place and
3239 fc_params.dst_iov = fc_params.src_iov = (void *)src;
3241 if (unlikely(prepare_iov_from_pkt_inplace(m_src,
3244 CPT_LOG_DP_ERR("Prepare inplace src iov failed");
3250 /* Out of place processing */
3251 fc_params.src_iov = (void *)src;
3252 fc_params.dst_iov = (void *)dst;
3254 /* Store SG I/O in the api for reuse */
3255 if (prepare_iov_from_pkt(m_src, fc_params.src_iov, 0)) {
3256 CPT_LOG_DP_ERR("Prepare src iov failed");
3261 if (unlikely(m_dst != NULL)) {
3264 /* Try to make room as much as src has */
3265 pkt_len = rte_pktmbuf_pkt_len(m_dst);
3267 if (unlikely(pkt_len < rte_pktmbuf_pkt_len(m_src))) {
3268 pkt_len = rte_pktmbuf_pkt_len(m_src) - pkt_len;
3269 if (!rte_pktmbuf_append(m_dst, pkt_len)) {
3270 CPT_LOG_DP_ERR("Not enough space in "
3279 if (prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0)) {
3280 CPT_LOG_DP_ERR("Prepare dst iov failed for "
3286 fc_params.dst_iov = (void *)src;
3290 if (likely(flags & SINGLE_BUF_HEADTAILROOM))
3291 mdata = alloc_op_meta(m_src, &fc_params.meta_buf,
3292 m_info->lb_mlen, m_info->pool);
3294 mdata = alloc_op_meta(NULL, &fc_params.meta_buf,
3295 m_info->sg_mlen, m_info->pool);
3297 if (unlikely(mdata == NULL)) {
3298 CPT_LOG_DP_ERR("Error allocating meta buffer for request");
3303 op = (uintptr_t *)((uintptr_t)mdata & (uintptr_t)~1ull);
3304 op[0] = (uintptr_t)mdata;
3305 op[1] = (uintptr_t)cop;
3306 op[2] = op[3] = 0; /* Used to indicate auth verify */
3307 space += 4 * sizeof(uint64_t);
3309 fc_params.meta_buf.vaddr = (uint8_t *)op + space;
3310 fc_params.meta_buf.dma_addr += space;
3311 fc_params.meta_buf.size -= space;
3313 /* Finally prepare the instruction */
3314 if (cpt_op & CPT_OP_ENCODE)
3315 *prep_req = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens,
3318 *prep_req = cpt_fc_dec_hmac_prep(flags, d_offs, d_lens,
3321 if (unlikely(*prep_req == NULL)) {
3322 CPT_LOG_DP_ERR("Preparing request failed due to bad input arg");
3324 goto free_mdata_and_exit;
3331 free_mdata_and_exit:
3332 free_op_meta(mdata, m_info->pool);
3337 static __rte_always_inline void
3338 compl_auth_verify(struct rte_crypto_op *op,
3343 struct rte_crypto_sym_op *sym_op = op->sym;
3345 if (sym_op->auth.digest.data)
3346 mac = sym_op->auth.digest.data;
3348 mac = rte_pktmbuf_mtod_offset(sym_op->m_src,
3350 sym_op->auth.data.length +
3351 sym_op->auth.data.offset);
3353 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3357 if (memcmp(mac, gen_mac, mac_len))
3358 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
3360 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3363 static __rte_always_inline void
3364 find_kasumif9_direction_and_length(uint8_t *src,
3365 uint32_t counter_num_bytes,
3366 uint32_t *addr_length_in_bits,
3367 uint8_t *addr_direction)
3372 while (!found && counter_num_bytes > 0) {
3373 counter_num_bytes--;
3374 if (src[counter_num_bytes] == 0x00)
3376 pos = rte_bsf32(src[counter_num_bytes]);
3378 if (likely(counter_num_bytes > 0)) {
3379 last_byte = src[counter_num_bytes - 1];
3380 *addr_direction = last_byte & 0x1;
3381 *addr_length_in_bits = counter_num_bytes * 8
3385 last_byte = src[counter_num_bytes];
3386 *addr_direction = (last_byte >> (pos + 1)) & 0x1;
3387 *addr_length_in_bits = counter_num_bytes * 8
3395 * This handles all auth only except AES_GMAC
3397 static __rte_always_inline int
3398 fill_digest_params(struct rte_crypto_op *cop,
3399 struct cpt_sess_misc *sess,
3400 struct cpt_qp_meta_info *m_info,
3405 struct rte_crypto_sym_op *sym_op = cop->sym;
3409 uint32_t auth_range_off;
3411 uint64_t d_offs = 0, d_lens;
3412 struct rte_mbuf *m_src, *m_dst;
3413 uint16_t auth_op = sess->cpt_op & CPT_OP_AUTH_MASK;
3414 uint16_t mac_len = sess->mac_len;
3416 char src[SRC_IOV_SIZE];
3420 memset(¶ms, 0, sizeof(fc_params_t));
3422 m_src = sym_op->m_src;
3424 /* For just digest lets force mempool alloc */
3425 mdata = alloc_op_meta(NULL, ¶ms.meta_buf, m_info->sg_mlen,
3427 if (mdata == NULL) {
3432 mphys = params.meta_buf.dma_addr;
3435 op[0] = (uintptr_t)mdata;
3436 op[1] = (uintptr_t)cop;
3437 op[2] = op[3] = 0; /* Used to indicate auth verify */
3438 space += 4 * sizeof(uint64_t);
3440 auth_range_off = sym_op->auth.data.offset;
3442 flags = VALID_MAC_BUF;
3443 params.src_iov = (void *)src;
3444 if (unlikely(sess->zsk_flag)) {
3446 * Since for Zuc, Kasumi, Snow3g offsets are in bits
3447 * we will send pass through even for auth only case,
3450 d_offs = auth_range_off;
3452 params.auth_iv_buf = rte_crypto_op_ctod_offset(cop,
3453 uint8_t *, sess->auth_iv_offset);
3454 if (sess->zsk_flag == K_F9) {
3455 uint32_t length_in_bits, num_bytes;
3456 uint8_t *src, direction = 0;
3458 memcpy(iv_buf, rte_pktmbuf_mtod(cop->sym->m_src,
3461 * This is kasumi f9, take direction from
3464 length_in_bits = cop->sym->auth.data.length;
3465 num_bytes = (length_in_bits >> 3);
3466 src = rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *);
3467 find_kasumif9_direction_and_length(src,
3471 length_in_bits -= 64;
3472 cop->sym->auth.data.offset += 64;
3473 d_offs = cop->sym->auth.data.offset;
3474 auth_range_off = d_offs / 8;
3475 cop->sym->auth.data.length = length_in_bits;
3477 /* Store it at end of auth iv */
3478 iv_buf[8] = direction;
3479 params.auth_iv_buf = iv_buf;
3483 d_lens = sym_op->auth.data.length;
3485 params.ctx_buf.vaddr = SESS_PRIV(sess);
3486 params.ctx_buf.dma_addr = sess->ctx_dma_addr;
3488 if (auth_op == CPT_OP_AUTH_GENERATE) {
3489 if (sym_op->auth.digest.data) {
3491 * Digest to be generated
3492 * in separate buffer
3494 params.mac_buf.size =
3496 params.mac_buf.vaddr =
3497 sym_op->auth.digest.data;
3498 params.mac_buf.dma_addr =
3499 sym_op->auth.digest.phys_addr;
3501 uint32_t off = sym_op->auth.data.offset +
3502 sym_op->auth.data.length;
3503 int32_t dlen, space;
3505 m_dst = sym_op->m_dst ?
3506 sym_op->m_dst : sym_op->m_src;
3507 dlen = rte_pktmbuf_pkt_len(m_dst);
3509 space = off + mac_len - dlen;
3511 if (!rte_pktmbuf_append(m_dst, space)) {
3512 CPT_LOG_DP_ERR("Failed to extend "
3513 "mbuf by %uB", space);
3515 goto free_mdata_and_exit;
3518 params.mac_buf.vaddr =
3519 rte_pktmbuf_mtod_offset(m_dst, void *, off);
3520 params.mac_buf.dma_addr =
3521 rte_pktmbuf_iova_offset(m_dst, off);
3522 params.mac_buf.size = mac_len;
3525 /* Need space for storing generated mac */
3526 params.mac_buf.vaddr = (uint8_t *)mdata + space;
3527 params.mac_buf.dma_addr = mphys + space;
3528 params.mac_buf.size = mac_len;
3529 space += RTE_ALIGN_CEIL(mac_len, 8);
3530 op[2] = (uintptr_t)params.mac_buf.vaddr;
3534 params.meta_buf.vaddr = (uint8_t *)mdata + space;
3535 params.meta_buf.dma_addr = mphys + space;
3536 params.meta_buf.size -= space;
3538 /* Out of place processing */
3539 params.src_iov = (void *)src;
3541 /*Store SG I/O in the api for reuse */
3542 if (prepare_iov_from_pkt(m_src, params.src_iov, auth_range_off)) {
3543 CPT_LOG_DP_ERR("Prepare src iov failed");
3545 goto free_mdata_and_exit;
3548 *prep_req = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, ¶ms, op);
3549 if (unlikely(*prep_req == NULL)) {
3551 goto free_mdata_and_exit;
3558 free_mdata_and_exit:
3559 free_op_meta(mdata, m_info->pool);
3564 #endif /*_CPT_UCODE_H_ */