1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
9 #include "cnxk_cryptodev.h"
10 #include "cnxk_cryptodev_ops.h"
12 #define SRC_IOV_SIZE \
13 (sizeof(struct roc_se_iov_ptr) + \
14 (sizeof(struct roc_se_buf_ptr) * ROC_SE_MAX_SG_CNT))
15 #define DST_IOV_SIZE \
16 (sizeof(struct roc_se_iov_ptr) + \
17 (sizeof(struct roc_se_buf_ptr) * ROC_SE_MAX_SG_CNT))
21 uint16_t zsk_flag : 4;
24 uint16_t chacha_poly : 1;
31 uint8_t auth_iv_length;
33 uint16_t auth_iv_offset;
36 struct roc_se_ctx roc_se_ctx;
37 } __rte_cache_aligned;
40 pdcp_iv_copy(uint8_t *iv_d, uint8_t *iv_s, const uint8_t pdcp_alg_type)
42 uint32_t *iv_s_temp, iv_temp[4];
45 if (pdcp_alg_type == ROC_SE_PDCP_ALG_TYPE_SNOW3G) {
47 * DPDK seems to provide it in form of IV3 IV2 IV1 IV0
48 * and BigEndian, MC needs it as IV0 IV1 IV2 IV3
51 iv_s_temp = (uint32_t *)iv_s;
53 for (j = 0; j < 4; j++)
54 iv_temp[j] = iv_s_temp[3 - j];
55 memcpy(iv_d, iv_temp, 16);
57 /* ZUC doesn't need a swap */
58 memcpy(iv_d, iv_s, 16);
62 static __rte_always_inline int
63 cpt_mac_len_verify(struct rte_crypto_auth_xform *auth)
65 uint16_t mac_len = auth->digest_length;
69 case RTE_CRYPTO_AUTH_MD5:
70 case RTE_CRYPTO_AUTH_MD5_HMAC:
71 ret = (mac_len == 16) ? 0 : -1;
73 case RTE_CRYPTO_AUTH_SHA1:
74 case RTE_CRYPTO_AUTH_SHA1_HMAC:
75 ret = (mac_len == 20) ? 0 : -1;
77 case RTE_CRYPTO_AUTH_SHA224:
78 case RTE_CRYPTO_AUTH_SHA224_HMAC:
79 ret = (mac_len == 28) ? 0 : -1;
81 case RTE_CRYPTO_AUTH_SHA256:
82 case RTE_CRYPTO_AUTH_SHA256_HMAC:
83 ret = (mac_len == 32) ? 0 : -1;
85 case RTE_CRYPTO_AUTH_SHA384:
86 case RTE_CRYPTO_AUTH_SHA384_HMAC:
87 ret = (mac_len == 48) ? 0 : -1;
89 case RTE_CRYPTO_AUTH_SHA512:
90 case RTE_CRYPTO_AUTH_SHA512_HMAC:
91 ret = (mac_len == 64) ? 0 : -1;
93 case RTE_CRYPTO_AUTH_NULL:
103 static __rte_always_inline void
104 cpt_fc_salt_update(struct roc_se_ctx *se_ctx, uint8_t *salt)
106 struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
107 memcpy(fctx->enc.encr_iv, salt, 4);
110 static __rte_always_inline uint32_t
111 fill_sg_comp(struct roc_se_sglist_comp *list, uint32_t i, phys_addr_t dma_addr,
114 struct roc_se_sglist_comp *to = &list[i >> 2];
116 to->u.s.len[i % 4] = rte_cpu_to_be_16(size);
117 to->ptr[i % 4] = rte_cpu_to_be_64(dma_addr);
122 static __rte_always_inline uint32_t
123 fill_sg_comp_from_buf(struct roc_se_sglist_comp *list, uint32_t i,
124 struct roc_se_buf_ptr *from)
126 struct roc_se_sglist_comp *to = &list[i >> 2];
128 to->u.s.len[i % 4] = rte_cpu_to_be_16(from->size);
129 to->ptr[i % 4] = rte_cpu_to_be_64((uint64_t)from->vaddr);
134 static __rte_always_inline uint32_t
135 fill_sg_comp_from_buf_min(struct roc_se_sglist_comp *list, uint32_t i,
136 struct roc_se_buf_ptr *from, uint32_t *psize)
138 struct roc_se_sglist_comp *to = &list[i >> 2];
139 uint32_t size = *psize;
142 e_len = (size > from->size) ? from->size : size;
143 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
144 to->ptr[i % 4] = rte_cpu_to_be_64((uint64_t)from->vaddr);
151 * This fills the MC expected SGIO list
152 * from IOV given by user.
154 static __rte_always_inline uint32_t
155 fill_sg_comp_from_iov(struct roc_se_sglist_comp *list, uint32_t i,
156 struct roc_se_iov_ptr *from, uint32_t from_offset,
157 uint32_t *psize, struct roc_se_buf_ptr *extra_buf,
158 uint32_t extra_offset)
161 uint32_t extra_len = extra_buf ? extra_buf->size : 0;
162 uint32_t size = *psize;
163 struct roc_se_buf_ptr *bufs;
166 for (j = 0; (j < from->buf_cnt) && size; j++) {
169 struct roc_se_sglist_comp *to = &list[i >> 2];
171 if (unlikely(from_offset)) {
172 if (from_offset >= bufs[j].size) {
173 from_offset -= bufs[j].size;
176 e_vaddr = (uint64_t)bufs[j].vaddr + from_offset;
177 e_len = (size > (bufs[j].size - from_offset)) ?
178 (bufs[j].size - from_offset) :
182 e_vaddr = (uint64_t)bufs[j].vaddr;
183 e_len = (size > bufs[j].size) ? bufs[j].size : size;
186 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
187 to->ptr[i % 4] = rte_cpu_to_be_64(e_vaddr);
189 if (extra_len && (e_len >= extra_offset)) {
190 /* Break the data at given offset */
191 uint32_t next_len = e_len - extra_offset;
192 uint64_t next_vaddr = e_vaddr + extra_offset;
197 e_len = extra_offset;
199 to->u.s.len[i % 4] = rte_cpu_to_be_16(e_len);
202 extra_len = RTE_MIN(extra_len, size);
203 /* Insert extra data ptr */
208 rte_cpu_to_be_16(extra_len);
209 to->ptr[i % 4] = rte_cpu_to_be_64(
210 (uint64_t)extra_buf->vaddr);
214 next_len = RTE_MIN(next_len, size);
215 /* insert the rest of the data */
219 to->u.s.len[i % 4] = rte_cpu_to_be_16(next_len);
220 to->ptr[i % 4] = rte_cpu_to_be_64(next_vaddr);
229 extra_offset -= size;
237 static __rte_always_inline int
238 cpt_digest_gen_prep(uint32_t flags, uint64_t d_lens,
239 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
241 void *m_vaddr = params->meta_buf.vaddr;
243 uint16_t data_len, mac_len, key_len;
244 roc_se_auth_type hash_type;
245 struct roc_se_ctx *ctx;
246 struct roc_se_sglist_comp *gather_comp;
247 struct roc_se_sglist_comp *scatter_comp;
249 uint32_t g_size_bytes, s_size_bytes;
250 union cpt_inst_w4 cpt_inst_w4;
252 ctx = params->ctx_buf.vaddr;
254 hash_type = ctx->hash_type;
255 mac_len = ctx->mac_len;
256 key_len = ctx->auth_key_len;
257 data_len = ROC_SE_AUTH_DLEN(d_lens);
260 cpt_inst_w4.s.opcode_minor = 0;
261 cpt_inst_w4.s.param2 = ((uint16_t)hash_type << 8);
263 cpt_inst_w4.s.opcode_major =
264 ROC_SE_MAJOR_OP_HMAC | ROC_SE_DMA_MODE;
265 cpt_inst_w4.s.param1 = key_len;
266 cpt_inst_w4.s.dlen = data_len + RTE_ALIGN_CEIL(key_len, 8);
268 cpt_inst_w4.s.opcode_major =
269 ROC_SE_MAJOR_OP_HASH | ROC_SE_DMA_MODE;
270 cpt_inst_w4.s.param1 = 0;
271 cpt_inst_w4.s.dlen = data_len;
274 /* Null auth only case enters the if */
275 if (unlikely(!hash_type && !ctx->enc_cipher)) {
276 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_MISC;
277 /* Minor op is passthrough */
278 cpt_inst_w4.s.opcode_minor = 0x03;
279 /* Send out completion code only */
280 cpt_inst_w4.s.param2 = 0x1;
283 /* DPTR has SG list */
286 ((uint16_t *)in_buffer)[0] = 0;
287 ((uint16_t *)in_buffer)[1] = 0;
289 /* TODO Add error check if space will be sufficient */
290 gather_comp = (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
299 uint64_t k_vaddr = (uint64_t)ctx->auth_key;
301 i = fill_sg_comp(gather_comp, i, k_vaddr,
302 RTE_ALIGN_CEIL(key_len, 8));
308 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0,
310 if (unlikely(size)) {
311 plt_dp_err("Insufficient dst IOV size, short by %dB",
317 * Looks like we need to support zero data
318 * gather ptr in case of hash & hmac
322 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
323 g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
330 scatter_comp = (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
333 if (flags & ROC_SE_VALID_MAC_BUF) {
334 if (unlikely(params->mac_buf.size < mac_len)) {
335 plt_dp_err("Insufficient MAC size");
340 i = fill_sg_comp_from_buf_min(scatter_comp, i, ¶ms->mac_buf,
344 i = fill_sg_comp_from_iov(scatter_comp, i, params->src_iov,
345 data_len, &size, NULL, 0);
346 if (unlikely(size)) {
347 plt_dp_err("Insufficient dst IOV size, short by %dB",
353 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
354 s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
356 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
358 /* This is DPTR len in case of SG mode */
359 cpt_inst_w4.s.dlen = size;
361 inst->dptr = (uint64_t)in_buffer;
362 inst->w4.u64 = cpt_inst_w4.u64;
367 static __rte_always_inline int
368 cpt_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
369 struct roc_se_fc_params *fc_params, struct cpt_inst_s *inst)
371 uint32_t iv_offset = 0;
372 int32_t inputlen, outputlen, enc_dlen, auth_dlen;
373 struct roc_se_ctx *se_ctx;
374 uint32_t cipher_type, hash_type;
375 uint32_t mac_len, size;
377 struct roc_se_buf_ptr *aad_buf = NULL;
378 uint32_t encr_offset, auth_offset;
379 uint32_t encr_data_len, auth_data_len, aad_len = 0;
380 uint32_t passthrough_len = 0;
381 union cpt_inst_w4 cpt_inst_w4;
385 encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
386 auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
387 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
388 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
389 if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
390 /* We don't support both AAD and auth data separately */
393 aad_len = fc_params->aad_buf.size;
394 aad_buf = &fc_params->aad_buf;
396 se_ctx = fc_params->ctx_buf.vaddr;
397 cipher_type = se_ctx->enc_cipher;
398 hash_type = se_ctx->hash_type;
399 mac_len = se_ctx->mac_len;
400 op_minor = se_ctx->template_w4.s.opcode_minor;
402 if (unlikely(!(flags & ROC_SE_VALID_IV_BUF))) {
404 iv_offset = ROC_SE_ENCR_IV_OFFSET(d_offs);
407 if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
409 * When AAD is given, data above encr_offset is pass through
410 * Since AAD is given as separate pointer and not as offset,
411 * this is a special case as we need to fragment input data
412 * into passthrough + encr_data and then insert AAD in between.
414 if (hash_type != ROC_SE_GMAC_TYPE) {
415 passthrough_len = encr_offset;
416 auth_offset = passthrough_len + iv_len;
417 encr_offset = passthrough_len + aad_len + iv_len;
418 auth_data_len = aad_len + encr_data_len;
420 passthrough_len = 16 + aad_len;
421 auth_offset = passthrough_len + iv_len;
422 auth_data_len = aad_len;
425 encr_offset += iv_len;
426 auth_offset += iv_len;
430 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_FC;
431 cpt_inst_w4.s.opcode_minor = ROC_SE_FC_MINOR_OP_ENCRYPT;
432 cpt_inst_w4.s.opcode_minor |= (uint64_t)op_minor;
434 if (hash_type == ROC_SE_GMAC_TYPE) {
439 auth_dlen = auth_offset + auth_data_len;
440 enc_dlen = encr_data_len + encr_offset;
441 if (unlikely(encr_data_len & 0xf)) {
442 if ((cipher_type == ROC_SE_DES3_CBC) ||
443 (cipher_type == ROC_SE_DES3_ECB))
445 RTE_ALIGN_CEIL(encr_data_len, 8) + encr_offset;
446 else if (likely((cipher_type == ROC_SE_AES_CBC) ||
447 (cipher_type == ROC_SE_AES_ECB)))
449 RTE_ALIGN_CEIL(encr_data_len, 8) + encr_offset;
452 if (unlikely(auth_dlen > enc_dlen)) {
453 inputlen = auth_dlen;
454 outputlen = auth_dlen + mac_len;
457 outputlen = enc_dlen + mac_len;
460 if (op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST)
461 outputlen = enc_dlen;
464 cpt_inst_w4.s.param1 = encr_data_len;
465 cpt_inst_w4.s.param2 = auth_data_len;
468 * In cn9k, cn10k since we have a limitation of
469 * IV & Offset control word not part of instruction
470 * and need to be part of Data Buffer, we check if
471 * head room is there and then only do the Direct mode processing
473 if (likely((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
474 (flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
475 void *dm_vaddr = fc_params->bufs[0].vaddr;
477 /* Use Direct mode */
480 (uint8_t *)dm_vaddr - ROC_SE_OFF_CTRL_LEN - iv_len;
483 inst->dptr = (uint64_t)offset_vaddr;
485 /* RPTR should just exclude offset control word */
486 inst->rptr = (uint64_t)dm_vaddr - iv_len;
488 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
490 if (likely(iv_len)) {
491 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
492 ROC_SE_OFF_CTRL_LEN);
493 uint64_t *src = fc_params->iv_buf;
499 void *m_vaddr = fc_params->meta_buf.vaddr;
500 uint32_t i, g_size_bytes, s_size_bytes;
501 struct roc_se_sglist_comp *gather_comp;
502 struct roc_se_sglist_comp *scatter_comp;
505 /* This falls under strict SG mode */
506 offset_vaddr = m_vaddr;
507 size = ROC_SE_OFF_CTRL_LEN + iv_len;
509 m_vaddr = (uint8_t *)m_vaddr + size;
511 cpt_inst_w4.s.opcode_major |= (uint64_t)ROC_SE_DMA_MODE;
513 if (likely(iv_len)) {
514 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
515 ROC_SE_OFF_CTRL_LEN);
516 uint64_t *src = fc_params->iv_buf;
521 /* DPTR has SG list */
524 ((uint16_t *)in_buffer)[0] = 0;
525 ((uint16_t *)in_buffer)[1] = 0;
527 /* TODO Add error check if space will be sufficient */
529 (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
537 /* Offset control word that includes iv */
538 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
539 ROC_SE_OFF_CTRL_LEN + iv_len);
542 size = inputlen - iv_len;
544 uint32_t aad_offset = aad_len ? passthrough_len : 0;
546 if (unlikely(flags & ROC_SE_SINGLE_BUF_INPLACE)) {
547 i = fill_sg_comp_from_buf_min(
548 gather_comp, i, fc_params->bufs, &size);
550 i = fill_sg_comp_from_iov(
551 gather_comp, i, fc_params->src_iov, 0,
552 &size, aad_buf, aad_offset);
555 if (unlikely(size)) {
556 plt_dp_err("Insufficient buffer space,"
562 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
564 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
567 * Output Scatter list
571 (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
575 if (likely(iv_len)) {
576 i = fill_sg_comp(scatter_comp, i,
577 (uint64_t)offset_vaddr +
582 /* output data or output data + digest*/
583 if (unlikely(flags & ROC_SE_VALID_MAC_BUF)) {
584 size = outputlen - iv_len - mac_len;
586 uint32_t aad_offset =
587 aad_len ? passthrough_len : 0;
590 ROC_SE_SINGLE_BUF_INPLACE)) {
591 i = fill_sg_comp_from_buf_min(
593 fc_params->bufs, &size);
595 i = fill_sg_comp_from_iov(
597 fc_params->dst_iov, 0, &size,
598 aad_buf, aad_offset);
600 if (unlikely(size)) {
601 plt_dp_err("Insufficient buffer"
602 " space, size %d needed",
609 i = fill_sg_comp_from_buf(scatter_comp, i,
610 &fc_params->mac_buf);
613 /* Output including mac */
614 size = outputlen - iv_len;
616 uint32_t aad_offset =
617 aad_len ? passthrough_len : 0;
620 ROC_SE_SINGLE_BUF_INPLACE)) {
621 i = fill_sg_comp_from_buf_min(
623 fc_params->bufs, &size);
625 i = fill_sg_comp_from_iov(
627 fc_params->dst_iov, 0, &size,
628 aad_buf, aad_offset);
630 if (unlikely(size)) {
631 plt_dp_err("Insufficient buffer"
632 " space, size %d needed",
638 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
640 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
642 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
644 /* This is DPTR len in case of SG mode */
645 cpt_inst_w4.s.dlen = size;
647 inst->dptr = (uint64_t)in_buffer;
650 if (unlikely((encr_offset >> 16) || (iv_offset >> 8) ||
651 (auth_offset >> 8))) {
652 plt_dp_err("Offset not supported");
653 plt_dp_err("enc_offset: %d", encr_offset);
654 plt_dp_err("iv_offset : %d", iv_offset);
655 plt_dp_err("auth_offset: %d", auth_offset);
659 *(uint64_t *)offset_vaddr = rte_cpu_to_be_64(
660 ((uint64_t)encr_offset << 16) | ((uint64_t)iv_offset << 8) |
661 ((uint64_t)auth_offset));
663 inst->w4.u64 = cpt_inst_w4.u64;
667 static __rte_always_inline int
668 cpt_dec_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
669 struct roc_se_fc_params *fc_params, struct cpt_inst_s *inst)
671 uint32_t iv_offset = 0, size;
672 int32_t inputlen, outputlen, enc_dlen, auth_dlen;
673 struct roc_se_ctx *se_ctx;
674 int32_t hash_type, mac_len;
676 struct roc_se_buf_ptr *aad_buf = NULL;
677 uint32_t encr_offset, auth_offset;
678 uint32_t encr_data_len, auth_data_len, aad_len = 0;
679 uint32_t passthrough_len = 0;
680 union cpt_inst_w4 cpt_inst_w4;
684 encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
685 auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
686 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
687 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
689 if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
690 /* We don't support both AAD and auth data separately */
693 aad_len = fc_params->aad_buf.size;
694 aad_buf = &fc_params->aad_buf;
697 se_ctx = fc_params->ctx_buf.vaddr;
698 hash_type = se_ctx->hash_type;
699 mac_len = se_ctx->mac_len;
700 op_minor = se_ctx->template_w4.s.opcode_minor;
702 if (unlikely(!(flags & ROC_SE_VALID_IV_BUF))) {
704 iv_offset = ROC_SE_ENCR_IV_OFFSET(d_offs);
707 if (unlikely(flags & ROC_SE_VALID_AAD_BUF)) {
709 * When AAD is given, data above encr_offset is pass through
710 * Since AAD is given as separate pointer and not as offset,
711 * this is a special case as we need to fragment input data
712 * into passthrough + encr_data and then insert AAD in between.
714 if (hash_type != ROC_SE_GMAC_TYPE) {
715 passthrough_len = encr_offset;
716 auth_offset = passthrough_len + iv_len;
717 encr_offset = passthrough_len + aad_len + iv_len;
718 auth_data_len = aad_len + encr_data_len;
720 passthrough_len = 16 + aad_len;
721 auth_offset = passthrough_len + iv_len;
722 auth_data_len = aad_len;
725 encr_offset += iv_len;
726 auth_offset += iv_len;
730 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_FC;
731 cpt_inst_w4.s.opcode_minor = ROC_SE_FC_MINOR_OP_DECRYPT;
732 cpt_inst_w4.s.opcode_minor |= (uint64_t)op_minor;
734 if (hash_type == ROC_SE_GMAC_TYPE) {
739 enc_dlen = encr_offset + encr_data_len;
740 auth_dlen = auth_offset + auth_data_len;
742 if (auth_dlen > enc_dlen) {
743 inputlen = auth_dlen + mac_len;
744 outputlen = auth_dlen;
746 inputlen = enc_dlen + mac_len;
747 outputlen = enc_dlen;
750 if (op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST)
751 outputlen = inputlen = enc_dlen;
753 cpt_inst_w4.s.param1 = encr_data_len;
754 cpt_inst_w4.s.param2 = auth_data_len;
757 * In cn9k, cn10k since we have a limitation of
758 * IV & Offset control word not part of instruction
759 * and need to be part of Data Buffer, we check if
760 * head room is there and then only do the Direct mode processing
762 if (likely((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
763 (flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
764 void *dm_vaddr = fc_params->bufs[0].vaddr;
766 /* Use Direct mode */
769 (uint8_t *)dm_vaddr - ROC_SE_OFF_CTRL_LEN - iv_len;
770 inst->dptr = (uint64_t)offset_vaddr;
772 /* RPTR should just exclude offset control word */
773 inst->rptr = (uint64_t)dm_vaddr - iv_len;
775 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
777 if (likely(iv_len)) {
778 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
779 ROC_SE_OFF_CTRL_LEN);
780 uint64_t *src = fc_params->iv_buf;
786 void *m_vaddr = fc_params->meta_buf.vaddr;
787 uint32_t g_size_bytes, s_size_bytes;
788 struct roc_se_sglist_comp *gather_comp;
789 struct roc_se_sglist_comp *scatter_comp;
793 /* This falls under strict SG mode */
794 offset_vaddr = m_vaddr;
795 size = ROC_SE_OFF_CTRL_LEN + iv_len;
797 m_vaddr = (uint8_t *)m_vaddr + size;
799 cpt_inst_w4.s.opcode_major |= (uint64_t)ROC_SE_DMA_MODE;
801 if (likely(iv_len)) {
802 uint64_t *dest = (uint64_t *)((uint8_t *)offset_vaddr +
803 ROC_SE_OFF_CTRL_LEN);
804 uint64_t *src = fc_params->iv_buf;
809 /* DPTR has SG list */
812 ((uint16_t *)in_buffer)[0] = 0;
813 ((uint16_t *)in_buffer)[1] = 0;
815 /* TODO Add error check if space will be sufficient */
817 (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
824 /* Offset control word that includes iv */
825 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
826 ROC_SE_OFF_CTRL_LEN + iv_len);
829 if (flags & ROC_SE_VALID_MAC_BUF) {
830 size = inputlen - iv_len - mac_len;
832 /* input data only */
834 ROC_SE_SINGLE_BUF_INPLACE)) {
835 i = fill_sg_comp_from_buf_min(
836 gather_comp, i, fc_params->bufs,
839 uint32_t aad_offset =
840 aad_len ? passthrough_len : 0;
842 i = fill_sg_comp_from_iov(
844 fc_params->src_iov, 0, &size,
845 aad_buf, aad_offset);
847 if (unlikely(size)) {
848 plt_dp_err("Insufficient buffer"
849 " space, size %d needed",
857 i = fill_sg_comp_from_buf(gather_comp, i,
858 &fc_params->mac_buf);
861 /* input data + mac */
862 size = inputlen - iv_len;
865 ROC_SE_SINGLE_BUF_INPLACE)) {
866 i = fill_sg_comp_from_buf_min(
867 gather_comp, i, fc_params->bufs,
870 uint32_t aad_offset =
871 aad_len ? passthrough_len : 0;
873 if (unlikely(!fc_params->src_iov)) {
874 plt_dp_err("Bad input args");
878 i = fill_sg_comp_from_iov(
880 fc_params->src_iov, 0, &size,
881 aad_buf, aad_offset);
884 if (unlikely(size)) {
885 plt_dp_err("Insufficient buffer"
886 " space, size %d needed",
892 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
894 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
897 * Output Scatter List
902 (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
907 i = fill_sg_comp(scatter_comp, i,
908 (uint64_t)offset_vaddr +
913 /* Add output data */
914 size = outputlen - iv_len;
916 if (unlikely(flags & ROC_SE_SINGLE_BUF_INPLACE)) {
917 /* handle single buffer here */
918 i = fill_sg_comp_from_buf_min(scatter_comp, i,
922 uint32_t aad_offset =
923 aad_len ? passthrough_len : 0;
925 if (unlikely(!fc_params->dst_iov)) {
926 plt_dp_err("Bad input args");
930 i = fill_sg_comp_from_iov(
931 scatter_comp, i, fc_params->dst_iov, 0,
932 &size, aad_buf, aad_offset);
935 if (unlikely(size)) {
936 plt_dp_err("Insufficient buffer space,"
943 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
945 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
947 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
949 /* This is DPTR len in case of SG mode */
950 cpt_inst_w4.s.dlen = size;
952 inst->dptr = (uint64_t)in_buffer;
955 if (unlikely((encr_offset >> 16) || (iv_offset >> 8) ||
956 (auth_offset >> 8))) {
957 plt_dp_err("Offset not supported");
958 plt_dp_err("enc_offset: %d", encr_offset);
959 plt_dp_err("iv_offset : %d", iv_offset);
960 plt_dp_err("auth_offset: %d", auth_offset);
964 *(uint64_t *)offset_vaddr = rte_cpu_to_be_64(
965 ((uint64_t)encr_offset << 16) | ((uint64_t)iv_offset << 8) |
966 ((uint64_t)auth_offset));
968 inst->w4.u64 = cpt_inst_w4.u64;
972 static __rte_always_inline int
973 cpt_zuc_snow3g_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
974 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
977 int32_t inputlen, outputlen;
978 struct roc_se_ctx *se_ctx;
979 uint32_t mac_len = 0;
980 uint8_t pdcp_alg_type;
981 uint32_t encr_offset, auth_offset;
982 uint32_t encr_data_len, auth_data_len;
983 int flags, iv_len = 16;
984 uint64_t offset_ctrl;
985 uint64_t *offset_vaddr;
987 union cpt_inst_w4 cpt_inst_w4;
989 se_ctx = params->ctx_buf.vaddr;
990 flags = se_ctx->zsk_flags;
991 mac_len = se_ctx->mac_len;
992 pdcp_alg_type = se_ctx->pdcp_alg_type;
994 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_ZUC_SNOW3G;
996 /* indicates CPTR ctx, operation type, KEY & IV mode from DPTR */
998 cpt_inst_w4.s.opcode_minor = ((1 << 7) | (pdcp_alg_type << 5) |
999 (0 << 4) | (0 << 3) | (flags & 0x7));
1003 * Microcode expects offsets in bytes
1004 * TODO: Rounding off
1006 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
1009 auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
1010 auth_offset = auth_offset / 8;
1012 /* consider iv len */
1013 auth_offset += iv_len;
1015 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
1016 outputlen = mac_len;
1018 offset_ctrl = rte_cpu_to_be_64((uint64_t)auth_offset);
1023 iv_s = params->auth_iv_buf;
1027 * Microcode expects offsets in bytes
1028 * TODO: Rounding off
1030 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1032 encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
1033 encr_offset = encr_offset / 8;
1034 /* consider iv len */
1035 encr_offset += iv_len;
1037 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
1038 outputlen = inputlen;
1040 /* iv offset is 0 */
1041 offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1046 iv_s = params->iv_buf;
1049 if (unlikely((encr_offset >> 16) || (auth_offset >> 8))) {
1050 plt_dp_err("Offset not supported");
1051 plt_dp_err("enc_offset: %d", encr_offset);
1052 plt_dp_err("auth_offset: %d", auth_offset);
1057 * GP op header, lengths are expected in bits.
1059 cpt_inst_w4.s.param1 = encr_data_len;
1060 cpt_inst_w4.s.param2 = auth_data_len;
1063 * In cn9k, cn10k since we have a limitation of
1064 * IV & Offset control word not part of instruction
1065 * and need to be part of Data Buffer, we check if
1066 * head room is there and then only do the Direct mode processing
1068 if (likely((req_flags & ROC_SE_SINGLE_BUF_INPLACE) &&
1069 (req_flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
1070 void *dm_vaddr = params->bufs[0].vaddr;
1072 /* Use Direct mode */
1074 offset_vaddr = (uint64_t *)((uint8_t *)dm_vaddr -
1075 ROC_SE_OFF_CTRL_LEN - iv_len);
1078 inst->dptr = (uint64_t)offset_vaddr;
1079 /* RPTR should just exclude offset control word */
1080 inst->rptr = (uint64_t)dm_vaddr - iv_len;
1082 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
1084 uint8_t *iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
1085 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type);
1087 *offset_vaddr = offset_ctrl;
1089 void *m_vaddr = params->meta_buf.vaddr;
1090 uint32_t i, g_size_bytes, s_size_bytes;
1091 struct roc_se_sglist_comp *gather_comp;
1092 struct roc_se_sglist_comp *scatter_comp;
1096 /* save space for iv */
1097 offset_vaddr = m_vaddr;
1099 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1101 cpt_inst_w4.s.opcode_major |= (uint64_t)ROC_SE_DMA_MODE;
1103 /* DPTR has SG list */
1104 in_buffer = m_vaddr;
1106 ((uint16_t *)in_buffer)[0] = 0;
1107 ((uint16_t *)in_buffer)[1] = 0;
1109 /* TODO Add error check if space will be sufficient */
1111 (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1118 /* Offset control word followed by iv */
1120 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1121 ROC_SE_OFF_CTRL_LEN + iv_len);
1123 /* iv offset is 0 */
1124 *offset_vaddr = offset_ctrl;
1126 iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
1127 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type);
1130 size = inputlen - iv_len;
1132 i = fill_sg_comp_from_iov(gather_comp, i,
1133 params->src_iov, 0, &size,
1135 if (unlikely(size)) {
1136 plt_dp_err("Insufficient buffer space,"
1142 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1144 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1147 * Output Scatter List
1152 (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1156 /* IV in SLIST only for EEA3 & UEA2 */
1161 i = fill_sg_comp(scatter_comp, i,
1162 (uint64_t)offset_vaddr +
1163 ROC_SE_OFF_CTRL_LEN,
1167 /* Add output data */
1168 if (req_flags & ROC_SE_VALID_MAC_BUF) {
1169 size = outputlen - iv_len - mac_len;
1171 i = fill_sg_comp_from_iov(scatter_comp, i,
1175 if (unlikely(size)) {
1176 plt_dp_err("Insufficient buffer space,"
1185 i = fill_sg_comp_from_buf(scatter_comp, i,
1189 /* Output including mac */
1190 size = outputlen - iv_len;
1192 i = fill_sg_comp_from_iov(scatter_comp, i,
1196 if (unlikely(size)) {
1197 plt_dp_err("Insufficient buffer space,"
1204 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1206 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1208 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1210 /* This is DPTR len in case of SG mode */
1211 cpt_inst_w4.s.dlen = size;
1213 inst->dptr = (uint64_t)in_buffer;
1216 inst->w4.u64 = cpt_inst_w4.u64;
1221 static __rte_always_inline int
1222 cpt_kasumi_enc_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
1223 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
1225 void *m_vaddr = params->meta_buf.vaddr;
1227 int32_t inputlen = 0, outputlen = 0;
1228 struct roc_se_ctx *se_ctx;
1229 uint32_t mac_len = 0;
1231 uint32_t encr_offset, auth_offset;
1232 uint32_t encr_data_len, auth_data_len;
1234 uint8_t *iv_s, *iv_d, iv_len = 8;
1236 uint64_t *offset_vaddr;
1237 union cpt_inst_w4 cpt_inst_w4;
1239 uint32_t g_size_bytes, s_size_bytes;
1240 struct roc_se_sglist_comp *gather_comp;
1241 struct roc_se_sglist_comp *scatter_comp;
1243 encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
1244 auth_offset = ROC_SE_AUTH_OFFSET(d_offs) / 8;
1245 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1246 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
1248 se_ctx = params->ctx_buf.vaddr;
1249 flags = se_ctx->zsk_flags;
1250 mac_len = se_ctx->mac_len;
1253 iv_s = params->iv_buf;
1255 iv_s = params->auth_iv_buf;
1257 dir = iv_s[8] & 0x1;
1259 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_SE_DMA_MODE;
1261 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
1262 cpt_inst_w4.s.opcode_minor = ((1 << 6) | (se_ctx->k_ecb << 5) |
1263 (dir << 4) | (0 << 3) | (flags & 0x7));
1266 * GP op header, lengths are expected in bits.
1268 cpt_inst_w4.s.param1 = encr_data_len;
1269 cpt_inst_w4.s.param2 = auth_data_len;
1271 /* consider iv len */
1273 encr_offset += iv_len;
1274 auth_offset += iv_len;
1277 /* save space for offset ctrl and iv */
1278 offset_vaddr = m_vaddr;
1280 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1282 /* DPTR has SG list */
1283 in_buffer = m_vaddr;
1285 ((uint16_t *)in_buffer)[0] = 0;
1286 ((uint16_t *)in_buffer)[1] = 0;
1288 /* TODO Add error check if space will be sufficient */
1289 gather_comp = (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1296 /* Offset control word followed by iv */
1299 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
1300 outputlen = inputlen;
1301 /* iv offset is 0 */
1302 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1303 if (unlikely((encr_offset >> 16))) {
1304 plt_dp_err("Offset not supported");
1305 plt_dp_err("enc_offset: %d", encr_offset);
1309 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
1310 outputlen = mac_len;
1311 /* iv offset is 0 */
1312 *offset_vaddr = rte_cpu_to_be_64((uint64_t)auth_offset);
1313 if (unlikely((auth_offset >> 8))) {
1314 plt_dp_err("Offset not supported");
1315 plt_dp_err("auth_offset: %d", auth_offset);
1320 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1321 ROC_SE_OFF_CTRL_LEN + iv_len);
1324 iv_d = (uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN;
1325 memcpy(iv_d, iv_s, iv_len);
1328 size = inputlen - iv_len;
1330 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0,
1333 if (unlikely(size)) {
1334 plt_dp_err("Insufficient buffer space,"
1340 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1341 g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1344 * Output Scatter List
1348 scatter_comp = (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1352 /* IV in SLIST only for F8 */
1358 i = fill_sg_comp(scatter_comp, i,
1359 (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN,
1363 /* Add output data */
1364 if (req_flags & ROC_SE_VALID_MAC_BUF) {
1365 size = outputlen - iv_len - mac_len;
1367 i = fill_sg_comp_from_iov(scatter_comp, i,
1368 params->dst_iov, 0, &size,
1371 if (unlikely(size)) {
1372 plt_dp_err("Insufficient buffer space,"
1381 i = fill_sg_comp_from_buf(scatter_comp, i,
1385 /* Output including mac */
1386 size = outputlen - iv_len;
1388 i = fill_sg_comp_from_iov(scatter_comp, i,
1389 params->dst_iov, 0, &size,
1392 if (unlikely(size)) {
1393 plt_dp_err("Insufficient buffer space,"
1400 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1401 s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1403 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1405 /* This is DPTR len in case of SG mode */
1406 cpt_inst_w4.s.dlen = size;
1408 inst->dptr = (uint64_t)in_buffer;
1409 inst->w4.u64 = cpt_inst_w4.u64;
1414 static __rte_always_inline int
1415 cpt_kasumi_dec_prep(uint64_t d_offs, uint64_t d_lens,
1416 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
1418 void *m_vaddr = params->meta_buf.vaddr;
1420 int32_t inputlen = 0, outputlen;
1421 struct roc_se_ctx *se_ctx;
1422 uint8_t i = 0, iv_len = 8;
1423 uint32_t encr_offset;
1424 uint32_t encr_data_len;
1427 uint64_t *offset_vaddr;
1428 union cpt_inst_w4 cpt_inst_w4;
1430 uint32_t g_size_bytes, s_size_bytes;
1431 struct roc_se_sglist_comp *gather_comp;
1432 struct roc_se_sglist_comp *scatter_comp;
1434 encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
1435 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1437 se_ctx = params->ctx_buf.vaddr;
1438 flags = se_ctx->zsk_flags;
1440 cpt_inst_w4.u64 = 0;
1441 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_SE_DMA_MODE;
1443 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
1444 cpt_inst_w4.s.opcode_minor = ((1 << 6) | (se_ctx->k_ecb << 5) |
1445 (dir << 4) | (0 << 3) | (flags & 0x7));
1448 * GP op header, lengths are expected in bits.
1450 cpt_inst_w4.s.param1 = encr_data_len;
1452 /* consider iv len */
1453 encr_offset += iv_len;
1455 inputlen = iv_len + (RTE_ALIGN(encr_data_len, 8) / 8);
1456 outputlen = inputlen;
1458 /* save space for offset ctrl & iv */
1459 offset_vaddr = m_vaddr;
1461 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1463 /* DPTR has SG list */
1464 in_buffer = m_vaddr;
1466 ((uint16_t *)in_buffer)[0] = 0;
1467 ((uint16_t *)in_buffer)[1] = 0;
1469 /* TODO Add error check if space will be sufficient */
1470 gather_comp = (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1477 /* Offset control word followed by iv */
1478 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1479 if (unlikely((encr_offset >> 16))) {
1480 plt_dp_err("Offset not supported");
1481 plt_dp_err("enc_offset: %d", encr_offset);
1485 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1486 ROC_SE_OFF_CTRL_LEN + iv_len);
1489 memcpy((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN, params->iv_buf,
1492 /* Add input data */
1493 size = inputlen - iv_len;
1495 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0,
1497 if (unlikely(size)) {
1498 plt_dp_err("Insufficient buffer space,"
1504 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1505 g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1508 * Output Scatter List
1512 scatter_comp = (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1516 i = fill_sg_comp(scatter_comp, i,
1517 (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN, iv_len);
1519 /* Add output data */
1520 size = outputlen - iv_len;
1522 i = fill_sg_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
1524 if (unlikely(size)) {
1525 plt_dp_err("Insufficient buffer space,"
1531 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1532 s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1534 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1536 /* This is DPTR len in case of SG mode */
1537 cpt_inst_w4.s.dlen = size;
1539 inst->dptr = (uint64_t)in_buffer;
1540 inst->w4.u64 = cpt_inst_w4.u64;
1545 static __rte_always_inline int
1546 cpt_fc_dec_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
1547 struct roc_se_fc_params *fc_params,
1548 struct cpt_inst_s *inst)
1550 struct roc_se_ctx *ctx = fc_params->ctx_buf.vaddr;
1554 fc_type = ctx->fc_type;
1556 if (likely(fc_type == ROC_SE_FC_GEN)) {
1557 ret = cpt_dec_hmac_prep(flags, d_offs, d_lens, fc_params, inst);
1558 } else if (fc_type == ROC_SE_PDCP) {
1559 ret = cpt_zuc_snow3g_prep(flags, d_offs, d_lens, fc_params,
1561 } else if (fc_type == ROC_SE_KASUMI) {
1562 ret = cpt_kasumi_dec_prep(d_offs, d_lens, fc_params, inst);
1566 * For AUTH_ONLY case,
1567 * MC only supports digest generation and verification
1568 * should be done in software by memcmp()
1574 static __rte_always_inline int
1575 cpt_fc_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
1576 struct roc_se_fc_params *fc_params,
1577 struct cpt_inst_s *inst)
1579 struct roc_se_ctx *ctx = fc_params->ctx_buf.vaddr;
1583 fc_type = ctx->fc_type;
1585 if (likely(fc_type == ROC_SE_FC_GEN)) {
1586 ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, fc_params, inst);
1587 } else if (fc_type == ROC_SE_PDCP) {
1588 ret = cpt_zuc_snow3g_prep(flags, d_offs, d_lens, fc_params,
1590 } else if (fc_type == ROC_SE_KASUMI) {
1591 ret = cpt_kasumi_enc_prep(flags, d_offs, d_lens, fc_params,
1593 } else if (fc_type == ROC_SE_HASH_HMAC) {
1594 ret = cpt_digest_gen_prep(flags, d_lens, fc_params, inst);
1600 static __rte_always_inline int
1601 fill_sess_aead(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1603 struct rte_crypto_aead_xform *aead_form;
1604 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1605 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1606 uint32_t cipher_key_len = 0;
1607 uint8_t aes_gcm = 0;
1608 aead_form = &xform->aead;
1610 if (aead_form->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
1611 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
1612 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
1613 } else if (aead_form->op == RTE_CRYPTO_AEAD_OP_DECRYPT) {
1614 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
1615 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
1617 plt_dp_err("Unknown aead operation\n");
1620 switch (aead_form->algo) {
1621 case RTE_CRYPTO_AEAD_AES_GCM:
1622 enc_type = ROC_SE_AES_GCM;
1623 cipher_key_len = 16;
1626 case RTE_CRYPTO_AEAD_AES_CCM:
1627 plt_dp_err("Crypto: Unsupported cipher algo %u",
1630 case RTE_CRYPTO_AEAD_CHACHA20_POLY1305:
1631 enc_type = ROC_SE_CHACHA20;
1632 auth_type = ROC_SE_POLY1305;
1633 cipher_key_len = 32;
1634 sess->chacha_poly = 1;
1637 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1641 if (aead_form->key.length < cipher_key_len) {
1642 plt_dp_err("Invalid cipher params keylen %u",
1643 aead_form->key.length);
1647 sess->aes_gcm = aes_gcm;
1648 sess->mac_len = aead_form->digest_length;
1649 sess->iv_offset = aead_form->iv.offset;
1650 sess->iv_length = aead_form->iv.length;
1651 sess->aad_length = aead_form->aad_length;
1653 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1654 aead_form->key.data,
1655 aead_form->key.length, NULL)))
1658 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
1659 aead_form->digest_length)))
1665 static __rte_always_inline int
1666 fill_sess_cipher(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1668 struct rte_crypto_cipher_xform *c_form;
1669 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1670 uint32_t cipher_key_len = 0;
1671 uint8_t zsk_flag = 0, aes_ctr = 0, is_null = 0;
1673 c_form = &xform->cipher;
1675 if (c_form->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1676 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
1677 else if (c_form->op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1678 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
1679 if (xform->next != NULL &&
1680 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1681 /* Perform decryption followed by auth verify */
1682 sess->roc_se_ctx.template_w4.s.opcode_minor =
1683 ROC_SE_FC_MINOR_OP_HMAC_FIRST;
1686 plt_dp_err("Unknown cipher operation\n");
1690 switch (c_form->algo) {
1691 case RTE_CRYPTO_CIPHER_AES_CBC:
1692 enc_type = ROC_SE_AES_CBC;
1693 cipher_key_len = 16;
1695 case RTE_CRYPTO_CIPHER_3DES_CBC:
1696 enc_type = ROC_SE_DES3_CBC;
1697 cipher_key_len = 24;
1699 case RTE_CRYPTO_CIPHER_DES_CBC:
1700 /* DES is implemented using 3DES in hardware */
1701 enc_type = ROC_SE_DES3_CBC;
1704 case RTE_CRYPTO_CIPHER_AES_CTR:
1705 enc_type = ROC_SE_AES_CTR;
1706 cipher_key_len = 16;
1709 case RTE_CRYPTO_CIPHER_NULL:
1713 case RTE_CRYPTO_CIPHER_KASUMI_F8:
1714 enc_type = ROC_SE_KASUMI_F8_ECB;
1715 cipher_key_len = 16;
1716 zsk_flag = ROC_SE_K_F8;
1718 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1719 enc_type = ROC_SE_SNOW3G_UEA2;
1720 cipher_key_len = 16;
1721 zsk_flag = ROC_SE_ZS_EA;
1723 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
1724 enc_type = ROC_SE_ZUC_EEA3;
1725 cipher_key_len = 16;
1726 zsk_flag = ROC_SE_ZS_EA;
1728 case RTE_CRYPTO_CIPHER_AES_XTS:
1729 enc_type = ROC_SE_AES_XTS;
1730 cipher_key_len = 16;
1732 case RTE_CRYPTO_CIPHER_3DES_ECB:
1733 enc_type = ROC_SE_DES3_ECB;
1734 cipher_key_len = 24;
1736 case RTE_CRYPTO_CIPHER_AES_ECB:
1737 enc_type = ROC_SE_AES_ECB;
1738 cipher_key_len = 16;
1740 case RTE_CRYPTO_CIPHER_3DES_CTR:
1741 case RTE_CRYPTO_CIPHER_AES_F8:
1742 case RTE_CRYPTO_CIPHER_ARC4:
1743 plt_dp_err("Crypto: Unsupported cipher algo %u", c_form->algo);
1746 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1751 if (c_form->key.length < cipher_key_len) {
1752 plt_dp_err("Invalid cipher params keylen %u",
1753 c_form->key.length);
1757 sess->zsk_flag = zsk_flag;
1759 sess->aes_ctr = aes_ctr;
1760 sess->iv_offset = c_form->iv.offset;
1761 sess->iv_length = c_form->iv.length;
1762 sess->is_null = is_null;
1764 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1765 c_form->key.data, c_form->key.length,
1772 static __rte_always_inline int
1773 fill_sess_auth(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1775 struct rte_crypto_auth_xform *a_form;
1776 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1777 uint8_t zsk_flag = 0, aes_gcm = 0, is_null = 0;
1779 if (xform->next != NULL &&
1780 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1781 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1782 /* Perform auth followed by encryption */
1783 sess->roc_se_ctx.template_w4.s.opcode_minor =
1784 ROC_SE_FC_MINOR_OP_HMAC_FIRST;
1787 a_form = &xform->auth;
1789 if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
1790 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
1791 else if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
1792 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
1794 plt_dp_err("Unknown auth operation");
1798 switch (a_form->algo) {
1799 case RTE_CRYPTO_AUTH_SHA1_HMAC:
1801 case RTE_CRYPTO_AUTH_SHA1:
1802 auth_type = ROC_SE_SHA1_TYPE;
1804 case RTE_CRYPTO_AUTH_SHA256_HMAC:
1805 case RTE_CRYPTO_AUTH_SHA256:
1806 auth_type = ROC_SE_SHA2_SHA256;
1808 case RTE_CRYPTO_AUTH_SHA512_HMAC:
1809 case RTE_CRYPTO_AUTH_SHA512:
1810 auth_type = ROC_SE_SHA2_SHA512;
1812 case RTE_CRYPTO_AUTH_AES_GMAC:
1813 auth_type = ROC_SE_GMAC_TYPE;
1816 case RTE_CRYPTO_AUTH_SHA224_HMAC:
1817 case RTE_CRYPTO_AUTH_SHA224:
1818 auth_type = ROC_SE_SHA2_SHA224;
1820 case RTE_CRYPTO_AUTH_SHA384_HMAC:
1821 case RTE_CRYPTO_AUTH_SHA384:
1822 auth_type = ROC_SE_SHA2_SHA384;
1824 case RTE_CRYPTO_AUTH_MD5_HMAC:
1825 case RTE_CRYPTO_AUTH_MD5:
1826 auth_type = ROC_SE_MD5_TYPE;
1828 case RTE_CRYPTO_AUTH_KASUMI_F9:
1829 auth_type = ROC_SE_KASUMI_F9_ECB;
1831 * Indicate that direction needs to be taken out
1834 zsk_flag = ROC_SE_K_F9;
1836 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
1837 auth_type = ROC_SE_SNOW3G_UIA2;
1838 zsk_flag = ROC_SE_ZS_IA;
1840 case RTE_CRYPTO_AUTH_ZUC_EIA3:
1841 auth_type = ROC_SE_ZUC_EIA3;
1842 zsk_flag = ROC_SE_ZS_IA;
1844 case RTE_CRYPTO_AUTH_NULL:
1848 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
1849 case RTE_CRYPTO_AUTH_AES_CMAC:
1850 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
1851 plt_dp_err("Crypto: Unsupported hash algo %u", a_form->algo);
1854 plt_dp_err("Crypto: Undefined Hash algo %u specified",
1859 sess->zsk_flag = zsk_flag;
1860 sess->aes_gcm = aes_gcm;
1861 sess->mac_len = a_form->digest_length;
1862 sess->is_null = is_null;
1864 sess->auth_iv_offset = a_form->iv.offset;
1865 sess->auth_iv_length = a_form->iv.length;
1867 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type,
1868 a_form->key.data, a_form->key.length,
1869 a_form->digest_length)))
1875 static __rte_always_inline int
1876 fill_sess_gmac(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1878 struct rte_crypto_auth_xform *a_form;
1879 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1880 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1882 a_form = &xform->auth;
1884 if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
1885 sess->cpt_op |= ROC_SE_OP_ENCODE;
1886 else if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
1887 sess->cpt_op |= ROC_SE_OP_DECODE;
1889 plt_dp_err("Unknown auth operation");
1893 switch (a_form->algo) {
1894 case RTE_CRYPTO_AUTH_AES_GMAC:
1895 enc_type = ROC_SE_AES_GCM;
1896 auth_type = ROC_SE_GMAC_TYPE;
1899 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1907 sess->iv_offset = a_form->iv.offset;
1908 sess->iv_length = a_form->iv.length;
1909 sess->mac_len = a_form->digest_length;
1911 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1912 a_form->key.data, a_form->key.length,
1916 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
1917 a_form->digest_length)))
1923 static __rte_always_inline void *
1924 alloc_op_meta(struct roc_se_buf_ptr *buf, int32_t len,
1925 struct rte_mempool *cpt_meta_pool,
1926 struct cpt_inflight_req *infl_req)
1930 if (unlikely(rte_mempool_get(cpt_meta_pool, (void **)&mdata) < 0))
1936 infl_req->mdata = mdata;
1937 infl_req->op_flags |= CPT_OP_FLAGS_METABUF;
1942 static __rte_always_inline uint32_t
1943 prepare_iov_from_pkt(struct rte_mbuf *pkt, struct roc_se_iov_ptr *iovec,
1944 uint32_t start_offset)
1947 void *seg_data = NULL;
1948 int32_t seg_size = 0;
1955 if (!start_offset) {
1956 seg_data = rte_pktmbuf_mtod(pkt, void *);
1957 seg_size = pkt->data_len;
1959 while (start_offset >= pkt->data_len) {
1960 start_offset -= pkt->data_len;
1964 seg_data = rte_pktmbuf_mtod_offset(pkt, void *, start_offset);
1965 seg_size = pkt->data_len - start_offset;
1971 iovec->bufs[index].vaddr = seg_data;
1972 iovec->bufs[index].size = seg_size;
1976 while (unlikely(pkt != NULL)) {
1977 seg_data = rte_pktmbuf_mtod(pkt, void *);
1978 seg_size = pkt->data_len;
1982 iovec->bufs[index].vaddr = seg_data;
1983 iovec->bufs[index].size = seg_size;
1990 iovec->buf_cnt = index;
1994 static __rte_always_inline uint32_t
1995 prepare_iov_from_pkt_inplace(struct rte_mbuf *pkt,
1996 struct roc_se_fc_params *param, uint32_t *flags)
1999 void *seg_data = NULL;
2000 uint32_t seg_size = 0;
2001 struct roc_se_iov_ptr *iovec;
2003 seg_data = rte_pktmbuf_mtod(pkt, void *);
2004 seg_size = pkt->data_len;
2007 if (likely(!pkt->next)) {
2010 *flags |= ROC_SE_SINGLE_BUF_INPLACE;
2011 headroom = rte_pktmbuf_headroom(pkt);
2012 if (likely(headroom >= 24))
2013 *flags |= ROC_SE_SINGLE_BUF_HEADROOM;
2015 param->bufs[0].vaddr = seg_data;
2016 param->bufs[0].size = seg_size;
2019 iovec = param->src_iov;
2020 iovec->bufs[index].vaddr = seg_data;
2021 iovec->bufs[index].size = seg_size;
2025 while (unlikely(pkt != NULL)) {
2026 seg_data = rte_pktmbuf_mtod(pkt, void *);
2027 seg_size = pkt->data_len;
2032 iovec->bufs[index].vaddr = seg_data;
2033 iovec->bufs[index].size = seg_size;
2040 iovec->buf_cnt = index;
2044 static __rte_always_inline int
2045 fill_fc_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
2046 struct cpt_qp_meta_info *m_info,
2047 struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst)
2049 struct roc_se_ctx *ctx = &sess->roc_se_ctx;
2050 uint8_t op_minor = ctx->template_w4.s.opcode_minor;
2051 struct rte_crypto_sym_op *sym_op = cop->sym;
2053 uint32_t mc_hash_off;
2055 uint64_t d_offs, d_lens;
2056 struct rte_mbuf *m_src, *m_dst;
2057 uint8_t cpt_op = sess->cpt_op;
2058 #ifdef CPT_ALWAYS_USE_SG_MODE
2059 uint8_t inplace = 0;
2061 uint8_t inplace = 1;
2063 struct roc_se_fc_params fc_params;
2064 char src[SRC_IOV_SIZE];
2065 char dst[SRC_IOV_SIZE];
2069 if (likely(sess->iv_length)) {
2070 flags |= ROC_SE_VALID_IV_BUF;
2071 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *,
2073 if (sess->aes_ctr && unlikely(sess->iv_length != 16)) {
2074 memcpy((uint8_t *)iv_buf,
2075 rte_crypto_op_ctod_offset(cop, uint8_t *,
2078 iv_buf[3] = rte_cpu_to_be_32(0x1);
2079 fc_params.iv_buf = iv_buf;
2083 if (sess->zsk_flag) {
2084 fc_params.auth_iv_buf = rte_crypto_op_ctod_offset(
2085 cop, uint8_t *, sess->auth_iv_offset);
2086 if (sess->zsk_flag != ROC_SE_ZS_EA)
2089 m_src = sym_op->m_src;
2090 m_dst = sym_op->m_dst;
2092 if (sess->aes_gcm || sess->chacha_poly) {
2097 d_offs = sym_op->aead.data.offset;
2098 d_lens = sym_op->aead.data.length;
2100 sym_op->aead.data.offset + sym_op->aead.data.length;
2102 aad_data = sym_op->aead.aad.data;
2103 aad_len = sess->aad_length;
2104 if (likely((aad_data + aad_len) ==
2105 rte_pktmbuf_mtod_offset(m_src, uint8_t *,
2106 sym_op->aead.data.offset))) {
2107 d_offs = (d_offs - aad_len) | (d_offs << 16);
2108 d_lens = (d_lens + aad_len) | (d_lens << 32);
2110 fc_params.aad_buf.vaddr = sym_op->aead.aad.data;
2111 fc_params.aad_buf.size = aad_len;
2112 flags |= ROC_SE_VALID_AAD_BUF;
2114 d_offs = d_offs << 16;
2115 d_lens = d_lens << 32;
2118 salt = fc_params.iv_buf;
2119 if (unlikely(*(uint32_t *)salt != sess->salt)) {
2120 cpt_fc_salt_update(&sess->roc_se_ctx, salt);
2121 sess->salt = *(uint32_t *)salt;
2123 fc_params.iv_buf = salt + 4;
2124 if (likely(sess->mac_len)) {
2125 struct rte_mbuf *m =
2126 (cpt_op & ROC_SE_OP_ENCODE) ? m_dst : m_src;
2131 /* hmac immediately following data is best case */
2132 if (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
2134 (uint8_t *)sym_op->aead.digest.data)) {
2135 flags |= ROC_SE_VALID_MAC_BUF;
2136 fc_params.mac_buf.size = sess->mac_len;
2137 fc_params.mac_buf.vaddr =
2138 sym_op->aead.digest.data;
2143 d_offs = sym_op->cipher.data.offset;
2144 d_lens = sym_op->cipher.data.length;
2146 sym_op->cipher.data.offset + sym_op->cipher.data.length;
2147 d_offs = (d_offs << 16) | sym_op->auth.data.offset;
2148 d_lens = (d_lens << 32) | sym_op->auth.data.length;
2151 (sym_op->auth.data.offset + sym_op->auth.data.length)) {
2152 mc_hash_off = (sym_op->auth.data.offset +
2153 sym_op->auth.data.length);
2155 /* for gmac, salt should be updated like in gcm */
2156 if (unlikely(sess->is_gmac)) {
2158 salt = fc_params.iv_buf;
2159 if (unlikely(*(uint32_t *)salt != sess->salt)) {
2160 cpt_fc_salt_update(&sess->roc_se_ctx, salt);
2161 sess->salt = *(uint32_t *)salt;
2163 fc_params.iv_buf = salt + 4;
2165 if (likely(sess->mac_len)) {
2168 m = (cpt_op & ROC_SE_OP_ENCODE) ? m_dst : m_src;
2172 /* hmac immediately following data is best case */
2173 if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST) &&
2174 (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
2176 (uint8_t *)sym_op->auth.digest.data))) {
2177 flags |= ROC_SE_VALID_MAC_BUF;
2178 fc_params.mac_buf.size = sess->mac_len;
2179 fc_params.mac_buf.vaddr =
2180 sym_op->auth.digest.data;
2185 fc_params.ctx_buf.vaddr = &sess->roc_se_ctx;
2187 if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST) &&
2188 unlikely(sess->is_null || sess->cpt_op == ROC_SE_OP_DECODE))
2191 if (likely(!m_dst && inplace)) {
2192 /* Case of single buffer without AAD buf or
2193 * separate mac buf in place and
2196 fc_params.dst_iov = fc_params.src_iov = (void *)src;
2198 if (unlikely(prepare_iov_from_pkt_inplace(m_src, &fc_params,
2200 plt_dp_err("Prepare inplace src iov failed");
2206 /* Out of place processing */
2207 fc_params.src_iov = (void *)src;
2208 fc_params.dst_iov = (void *)dst;
2210 /* Store SG I/O in the api for reuse */
2211 if (prepare_iov_from_pkt(m_src, fc_params.src_iov, 0)) {
2212 plt_dp_err("Prepare src iov failed");
2217 if (unlikely(m_dst != NULL)) {
2220 /* Try to make room as much as src has */
2221 pkt_len = rte_pktmbuf_pkt_len(m_dst);
2223 if (unlikely(pkt_len < rte_pktmbuf_pkt_len(m_src))) {
2224 pkt_len = rte_pktmbuf_pkt_len(m_src) - pkt_len;
2225 if (!rte_pktmbuf_append(m_dst, pkt_len)) {
2226 plt_dp_err("Not enough space in "
2235 if (prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0)) {
2236 plt_dp_err("Prepare dst iov failed for "
2243 fc_params.dst_iov = (void *)src;
2247 if (unlikely(!((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
2248 (flags & ROC_SE_SINGLE_BUF_HEADROOM) &&
2249 ((ctx->fc_type == ROC_SE_FC_GEN) ||
2250 (ctx->fc_type == ROC_SE_PDCP))))) {
2251 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen,
2252 m_info->pool, infl_req);
2253 if (mdata == NULL) {
2254 plt_dp_err("Error allocating meta buffer for request");
2259 /* Finally prepare the instruction */
2260 if (cpt_op & ROC_SE_OP_ENCODE)
2261 ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, &fc_params,
2264 ret = cpt_fc_dec_hmac_prep(flags, d_offs, d_lens, &fc_params,
2267 if (unlikely(ret)) {
2268 plt_dp_err("Preparing request failed due to bad input arg");
2269 goto free_mdata_and_exit;
2274 free_mdata_and_exit:
2275 if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
2276 rte_mempool_put(m_info->pool, infl_req->mdata);
2281 static __rte_always_inline void
2282 compl_auth_verify(struct rte_crypto_op *op, uint8_t *gen_mac, uint64_t mac_len)
2285 struct rte_crypto_sym_op *sym_op = op->sym;
2287 if (sym_op->auth.digest.data)
2288 mac = sym_op->auth.digest.data;
2290 mac = rte_pktmbuf_mtod_offset(sym_op->m_src, uint8_t *,
2291 sym_op->auth.data.length +
2292 sym_op->auth.data.offset);
2294 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2298 if (memcmp(mac, gen_mac, mac_len))
2299 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
2301 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2304 static __rte_always_inline void
2305 find_kasumif9_direction_and_length(uint8_t *src, uint32_t counter_num_bytes,
2306 uint32_t *addr_length_in_bits,
2307 uint8_t *addr_direction)
2312 while (!found && counter_num_bytes > 0) {
2313 counter_num_bytes--;
2314 if (src[counter_num_bytes] == 0x00)
2316 pos = rte_bsf32(src[counter_num_bytes]);
2318 if (likely(counter_num_bytes > 0)) {
2319 last_byte = src[counter_num_bytes - 1];
2320 *addr_direction = last_byte & 0x1;
2321 *addr_length_in_bits =
2322 counter_num_bytes * 8 - 1;
2325 last_byte = src[counter_num_bytes];
2326 *addr_direction = (last_byte >> (pos + 1)) & 0x1;
2327 *addr_length_in_bits =
2328 counter_num_bytes * 8 + (8 - (pos + 2));
2335 * This handles all auth only except AES_GMAC
2337 static __rte_always_inline int
2338 fill_digest_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
2339 struct cpt_qp_meta_info *m_info,
2340 struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst)
2343 struct rte_crypto_sym_op *sym_op = cop->sym;
2345 uint32_t auth_range_off;
2347 uint64_t d_offs = 0, d_lens;
2348 struct rte_mbuf *m_src, *m_dst;
2349 uint16_t auth_op = sess->cpt_op & ROC_SE_OP_AUTH_MASK;
2350 uint16_t mac_len = sess->mac_len;
2351 struct roc_se_fc_params params;
2352 char src[SRC_IOV_SIZE];
2356 memset(¶ms, 0, sizeof(struct roc_se_fc_params));
2358 m_src = sym_op->m_src;
2360 mdata = alloc_op_meta(¶ms.meta_buf, m_info->mlen, m_info->pool,
2362 if (mdata == NULL) {
2367 auth_range_off = sym_op->auth.data.offset;
2369 flags = ROC_SE_VALID_MAC_BUF;
2370 params.src_iov = (void *)src;
2371 if (unlikely(sess->zsk_flag)) {
2373 * Since for Zuc, Kasumi, Snow3g offsets are in bits
2374 * we will send pass through even for auth only case,
2377 d_offs = auth_range_off;
2379 params.auth_iv_buf = rte_crypto_op_ctod_offset(
2380 cop, uint8_t *, sess->auth_iv_offset);
2381 if (sess->zsk_flag == ROC_SE_K_F9) {
2382 uint32_t length_in_bits, num_bytes;
2383 uint8_t *src, direction = 0;
2386 rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *), 8);
2388 * This is kasumi f9, take direction from
2391 length_in_bits = cop->sym->auth.data.length;
2392 num_bytes = (length_in_bits >> 3);
2393 src = rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *);
2394 find_kasumif9_direction_and_length(
2395 src, num_bytes, &length_in_bits, &direction);
2396 length_in_bits -= 64;
2397 cop->sym->auth.data.offset += 64;
2398 d_offs = cop->sym->auth.data.offset;
2399 auth_range_off = d_offs / 8;
2400 cop->sym->auth.data.length = length_in_bits;
2402 /* Store it at end of auth iv */
2403 iv_buf[8] = direction;
2404 params.auth_iv_buf = iv_buf;
2408 d_lens = sym_op->auth.data.length;
2410 params.ctx_buf.vaddr = &sess->roc_se_ctx;
2412 if (auth_op == ROC_SE_OP_AUTH_GENERATE) {
2413 if (sym_op->auth.digest.data) {
2415 * Digest to be generated
2416 * in separate buffer
2418 params.mac_buf.size = sess->mac_len;
2419 params.mac_buf.vaddr = sym_op->auth.digest.data;
2421 uint32_t off = sym_op->auth.data.offset +
2422 sym_op->auth.data.length;
2423 int32_t dlen, space;
2425 m_dst = sym_op->m_dst ? sym_op->m_dst : sym_op->m_src;
2426 dlen = rte_pktmbuf_pkt_len(m_dst);
2428 space = off + mac_len - dlen;
2430 if (!rte_pktmbuf_append(m_dst, space)) {
2431 plt_dp_err("Failed to extend "
2435 goto free_mdata_and_exit;
2438 params.mac_buf.vaddr =
2439 rte_pktmbuf_mtod_offset(m_dst, void *, off);
2440 params.mac_buf.size = mac_len;
2443 uint64_t *op = mdata;
2445 /* Need space for storing generated mac */
2446 space += 2 * sizeof(uint64_t);
2448 params.mac_buf.vaddr = (uint8_t *)mdata + space;
2449 params.mac_buf.size = mac_len;
2450 space += RTE_ALIGN_CEIL(mac_len, 8);
2451 op[0] = (uintptr_t)params.mac_buf.vaddr;
2453 infl_req->op_flags |= CPT_OP_FLAGS_AUTH_VERIFY;
2456 params.meta_buf.vaddr = (uint8_t *)mdata + space;
2457 params.meta_buf.size -= space;
2459 /* Out of place processing */
2460 params.src_iov = (void *)src;
2462 /*Store SG I/O in the api for reuse */
2463 if (prepare_iov_from_pkt(m_src, params.src_iov, auth_range_off)) {
2464 plt_dp_err("Prepare src iov failed");
2466 goto free_mdata_and_exit;
2469 ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, ¶ms, inst);
2471 goto free_mdata_and_exit;
2475 free_mdata_and_exit:
2476 if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
2477 rte_mempool_put(m_info->pool, infl_req->mdata);
2481 #endif /*_CNXK_SE_H_ */