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 cpt_inst_w4.s.opcode_minor = se_ctx->template_w4.s.opcode_minor;
1000 * Microcode expects offsets in bytes
1001 * TODO: Rounding off
1003 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
1006 auth_offset = ROC_SE_AUTH_OFFSET(d_offs);
1007 auth_offset = auth_offset / 8;
1009 /* consider iv len */
1010 auth_offset += iv_len;
1012 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
1013 outputlen = mac_len;
1015 offset_ctrl = rte_cpu_to_be_64((uint64_t)auth_offset);
1020 iv_s = params->auth_iv_buf;
1024 * Microcode expects offsets in bytes
1025 * TODO: Rounding off
1027 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1029 encr_offset = ROC_SE_ENCR_OFFSET(d_offs);
1030 encr_offset = encr_offset / 8;
1031 /* consider iv len */
1032 encr_offset += iv_len;
1034 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
1035 outputlen = inputlen;
1037 /* iv offset is 0 */
1038 offset_ctrl = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1043 iv_s = params->iv_buf;
1046 if (unlikely((encr_offset >> 16) || (auth_offset >> 8))) {
1047 plt_dp_err("Offset not supported");
1048 plt_dp_err("enc_offset: %d", encr_offset);
1049 plt_dp_err("auth_offset: %d", auth_offset);
1054 * GP op header, lengths are expected in bits.
1056 cpt_inst_w4.s.param1 = encr_data_len;
1057 cpt_inst_w4.s.param2 = auth_data_len;
1060 * In cn9k, cn10k since we have a limitation of
1061 * IV & Offset control word not part of instruction
1062 * and need to be part of Data Buffer, we check if
1063 * head room is there and then only do the Direct mode processing
1065 if (likely((req_flags & ROC_SE_SINGLE_BUF_INPLACE) &&
1066 (req_flags & ROC_SE_SINGLE_BUF_HEADROOM))) {
1067 void *dm_vaddr = params->bufs[0].vaddr;
1069 /* Use Direct mode */
1071 offset_vaddr = (uint64_t *)((uint8_t *)dm_vaddr -
1072 ROC_SE_OFF_CTRL_LEN - iv_len);
1075 inst->dptr = (uint64_t)offset_vaddr;
1076 /* RPTR should just exclude offset control word */
1077 inst->rptr = (uint64_t)dm_vaddr - iv_len;
1079 cpt_inst_w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
1081 uint8_t *iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
1082 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type);
1084 *offset_vaddr = offset_ctrl;
1086 void *m_vaddr = params->meta_buf.vaddr;
1087 uint32_t i, g_size_bytes, s_size_bytes;
1088 struct roc_se_sglist_comp *gather_comp;
1089 struct roc_se_sglist_comp *scatter_comp;
1093 /* save space for iv */
1094 offset_vaddr = m_vaddr;
1096 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1098 cpt_inst_w4.s.opcode_major |= (uint64_t)ROC_SE_DMA_MODE;
1100 /* DPTR has SG list */
1101 in_buffer = m_vaddr;
1103 ((uint16_t *)in_buffer)[0] = 0;
1104 ((uint16_t *)in_buffer)[1] = 0;
1106 /* TODO Add error check if space will be sufficient */
1108 (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1115 /* Offset control word followed by iv */
1117 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1118 ROC_SE_OFF_CTRL_LEN + iv_len);
1120 /* iv offset is 0 */
1121 *offset_vaddr = offset_ctrl;
1123 iv_d = ((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN);
1124 pdcp_iv_copy(iv_d, iv_s, pdcp_alg_type);
1127 size = inputlen - iv_len;
1129 i = fill_sg_comp_from_iov(gather_comp, i,
1130 params->src_iov, 0, &size,
1132 if (unlikely(size)) {
1133 plt_dp_err("Insufficient buffer space,"
1139 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1141 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1144 * Output Scatter List
1149 (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1153 /* IV in SLIST only for EEA3 & UEA2 */
1158 i = fill_sg_comp(scatter_comp, i,
1159 (uint64_t)offset_vaddr +
1160 ROC_SE_OFF_CTRL_LEN,
1164 /* Add output data */
1165 if (req_flags & ROC_SE_VALID_MAC_BUF) {
1166 size = outputlen - iv_len - mac_len;
1168 i = fill_sg_comp_from_iov(scatter_comp, i,
1172 if (unlikely(size)) {
1173 plt_dp_err("Insufficient buffer space,"
1182 i = fill_sg_comp_from_buf(scatter_comp, i,
1186 /* Output including mac */
1187 size = outputlen - iv_len;
1189 i = fill_sg_comp_from_iov(scatter_comp, i,
1193 if (unlikely(size)) {
1194 plt_dp_err("Insufficient buffer space,"
1201 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1203 ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1205 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1207 /* This is DPTR len in case of SG mode */
1208 cpt_inst_w4.s.dlen = size;
1210 inst->dptr = (uint64_t)in_buffer;
1213 inst->w4.u64 = cpt_inst_w4.u64;
1218 static __rte_always_inline int
1219 cpt_kasumi_enc_prep(uint32_t req_flags, uint64_t d_offs, uint64_t d_lens,
1220 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
1222 void *m_vaddr = params->meta_buf.vaddr;
1224 int32_t inputlen = 0, outputlen = 0;
1225 struct roc_se_ctx *se_ctx;
1226 uint32_t mac_len = 0;
1228 uint32_t encr_offset, auth_offset;
1229 uint32_t encr_data_len, auth_data_len;
1231 uint8_t *iv_s, *iv_d, iv_len = 8;
1233 uint64_t *offset_vaddr;
1234 union cpt_inst_w4 cpt_inst_w4;
1236 uint32_t g_size_bytes, s_size_bytes;
1237 struct roc_se_sglist_comp *gather_comp;
1238 struct roc_se_sglist_comp *scatter_comp;
1240 encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
1241 auth_offset = ROC_SE_AUTH_OFFSET(d_offs) / 8;
1242 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1243 auth_data_len = ROC_SE_AUTH_DLEN(d_lens);
1245 se_ctx = params->ctx_buf.vaddr;
1246 flags = se_ctx->zsk_flags;
1247 mac_len = se_ctx->mac_len;
1250 iv_s = params->iv_buf;
1252 iv_s = params->auth_iv_buf;
1254 dir = iv_s[8] & 0x1;
1256 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_SE_DMA_MODE;
1258 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
1259 cpt_inst_w4.s.opcode_minor = ((1 << 6) | (se_ctx->k_ecb << 5) |
1260 (dir << 4) | (0 << 3) | (flags & 0x7));
1263 * GP op header, lengths are expected in bits.
1265 cpt_inst_w4.s.param1 = encr_data_len;
1266 cpt_inst_w4.s.param2 = auth_data_len;
1268 /* consider iv len */
1270 encr_offset += iv_len;
1271 auth_offset += iv_len;
1274 /* save space for offset ctrl and iv */
1275 offset_vaddr = m_vaddr;
1277 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1279 /* DPTR has SG list */
1280 in_buffer = m_vaddr;
1282 ((uint16_t *)in_buffer)[0] = 0;
1283 ((uint16_t *)in_buffer)[1] = 0;
1285 /* TODO Add error check if space will be sufficient */
1286 gather_comp = (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1293 /* Offset control word followed by iv */
1296 inputlen = encr_offset + (RTE_ALIGN(encr_data_len, 8) / 8);
1297 outputlen = inputlen;
1298 /* iv offset is 0 */
1299 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1300 if (unlikely((encr_offset >> 16))) {
1301 plt_dp_err("Offset not supported");
1302 plt_dp_err("enc_offset: %d", encr_offset);
1306 inputlen = auth_offset + (RTE_ALIGN(auth_data_len, 8) / 8);
1307 outputlen = mac_len;
1308 /* iv offset is 0 */
1309 *offset_vaddr = rte_cpu_to_be_64((uint64_t)auth_offset);
1310 if (unlikely((auth_offset >> 8))) {
1311 plt_dp_err("Offset not supported");
1312 plt_dp_err("auth_offset: %d", auth_offset);
1317 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1318 ROC_SE_OFF_CTRL_LEN + iv_len);
1321 iv_d = (uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN;
1322 memcpy(iv_d, iv_s, iv_len);
1325 size = inputlen - iv_len;
1327 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0,
1330 if (unlikely(size)) {
1331 plt_dp_err("Insufficient buffer space,"
1337 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1338 g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1341 * Output Scatter List
1345 scatter_comp = (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1349 /* IV in SLIST only for F8 */
1355 i = fill_sg_comp(scatter_comp, i,
1356 (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN,
1360 /* Add output data */
1361 if (req_flags & ROC_SE_VALID_MAC_BUF) {
1362 size = outputlen - iv_len - mac_len;
1364 i = fill_sg_comp_from_iov(scatter_comp, i,
1365 params->dst_iov, 0, &size,
1368 if (unlikely(size)) {
1369 plt_dp_err("Insufficient buffer space,"
1378 i = fill_sg_comp_from_buf(scatter_comp, i,
1382 /* Output including mac */
1383 size = outputlen - iv_len;
1385 i = fill_sg_comp_from_iov(scatter_comp, i,
1386 params->dst_iov, 0, &size,
1389 if (unlikely(size)) {
1390 plt_dp_err("Insufficient buffer space,"
1397 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1398 s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1400 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1402 /* This is DPTR len in case of SG mode */
1403 cpt_inst_w4.s.dlen = size;
1405 inst->dptr = (uint64_t)in_buffer;
1406 inst->w4.u64 = cpt_inst_w4.u64;
1411 static __rte_always_inline int
1412 cpt_kasumi_dec_prep(uint64_t d_offs, uint64_t d_lens,
1413 struct roc_se_fc_params *params, struct cpt_inst_s *inst)
1415 void *m_vaddr = params->meta_buf.vaddr;
1417 int32_t inputlen = 0, outputlen;
1418 struct roc_se_ctx *se_ctx;
1419 uint8_t i = 0, iv_len = 8;
1420 uint32_t encr_offset;
1421 uint32_t encr_data_len;
1424 uint64_t *offset_vaddr;
1425 union cpt_inst_w4 cpt_inst_w4;
1427 uint32_t g_size_bytes, s_size_bytes;
1428 struct roc_se_sglist_comp *gather_comp;
1429 struct roc_se_sglist_comp *scatter_comp;
1431 encr_offset = ROC_SE_ENCR_OFFSET(d_offs) / 8;
1432 encr_data_len = ROC_SE_ENCR_DLEN(d_lens);
1434 se_ctx = params->ctx_buf.vaddr;
1435 flags = se_ctx->zsk_flags;
1437 cpt_inst_w4.u64 = 0;
1438 cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_KASUMI | ROC_SE_DMA_MODE;
1440 /* indicates ECB/CBC, direction, ctx from cptr, iv from dptr */
1441 cpt_inst_w4.s.opcode_minor = ((1 << 6) | (se_ctx->k_ecb << 5) |
1442 (dir << 4) | (0 << 3) | (flags & 0x7));
1445 * GP op header, lengths are expected in bits.
1447 cpt_inst_w4.s.param1 = encr_data_len;
1449 /* consider iv len */
1450 encr_offset += iv_len;
1452 inputlen = iv_len + (RTE_ALIGN(encr_data_len, 8) / 8);
1453 outputlen = inputlen;
1455 /* save space for offset ctrl & iv */
1456 offset_vaddr = m_vaddr;
1458 m_vaddr = (uint8_t *)m_vaddr + ROC_SE_OFF_CTRL_LEN + iv_len;
1460 /* DPTR has SG list */
1461 in_buffer = m_vaddr;
1463 ((uint16_t *)in_buffer)[0] = 0;
1464 ((uint16_t *)in_buffer)[1] = 0;
1466 /* TODO Add error check if space will be sufficient */
1467 gather_comp = (struct roc_se_sglist_comp *)((uint8_t *)m_vaddr + 8);
1474 /* Offset control word followed by iv */
1475 *offset_vaddr = rte_cpu_to_be_64((uint64_t)encr_offset << 16);
1476 if (unlikely((encr_offset >> 16))) {
1477 plt_dp_err("Offset not supported");
1478 plt_dp_err("enc_offset: %d", encr_offset);
1482 i = fill_sg_comp(gather_comp, i, (uint64_t)offset_vaddr,
1483 ROC_SE_OFF_CTRL_LEN + iv_len);
1486 memcpy((uint8_t *)offset_vaddr + ROC_SE_OFF_CTRL_LEN, params->iv_buf,
1489 /* Add input data */
1490 size = inputlen - iv_len;
1492 i = fill_sg_comp_from_iov(gather_comp, i, params->src_iov, 0,
1494 if (unlikely(size)) {
1495 plt_dp_err("Insufficient buffer space,"
1501 ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
1502 g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1505 * Output Scatter List
1509 scatter_comp = (struct roc_se_sglist_comp *)((uint8_t *)gather_comp +
1513 i = fill_sg_comp(scatter_comp, i,
1514 (uint64_t)offset_vaddr + ROC_SE_OFF_CTRL_LEN, iv_len);
1516 /* Add output data */
1517 size = outputlen - iv_len;
1519 i = fill_sg_comp_from_iov(scatter_comp, i, params->dst_iov, 0,
1521 if (unlikely(size)) {
1522 plt_dp_err("Insufficient buffer space,"
1528 ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
1529 s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_se_sglist_comp);
1531 size = g_size_bytes + s_size_bytes + ROC_SE_SG_LIST_HDR_SIZE;
1533 /* This is DPTR len in case of SG mode */
1534 cpt_inst_w4.s.dlen = size;
1536 inst->dptr = (uint64_t)in_buffer;
1537 inst->w4.u64 = cpt_inst_w4.u64;
1542 static __rte_always_inline int
1543 cpt_fc_dec_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
1544 struct roc_se_fc_params *fc_params,
1545 struct cpt_inst_s *inst)
1547 struct roc_se_ctx *ctx = fc_params->ctx_buf.vaddr;
1551 fc_type = ctx->fc_type;
1553 if (likely(fc_type == ROC_SE_FC_GEN)) {
1554 ret = cpt_dec_hmac_prep(flags, d_offs, d_lens, fc_params, inst);
1555 } else if (fc_type == ROC_SE_PDCP) {
1556 ret = cpt_zuc_snow3g_prep(flags, d_offs, d_lens, fc_params,
1558 } else if (fc_type == ROC_SE_KASUMI) {
1559 ret = cpt_kasumi_dec_prep(d_offs, d_lens, fc_params, inst);
1563 * For AUTH_ONLY case,
1564 * MC only supports digest generation and verification
1565 * should be done in software by memcmp()
1571 static __rte_always_inline int
1572 cpt_fc_enc_hmac_prep(uint32_t flags, uint64_t d_offs, uint64_t d_lens,
1573 struct roc_se_fc_params *fc_params,
1574 struct cpt_inst_s *inst)
1576 struct roc_se_ctx *ctx = fc_params->ctx_buf.vaddr;
1580 fc_type = ctx->fc_type;
1582 if (likely(fc_type == ROC_SE_FC_GEN)) {
1583 ret = cpt_enc_hmac_prep(flags, d_offs, d_lens, fc_params, inst);
1584 } else if (fc_type == ROC_SE_PDCP) {
1585 ret = cpt_zuc_snow3g_prep(flags, d_offs, d_lens, fc_params,
1587 } else if (fc_type == ROC_SE_KASUMI) {
1588 ret = cpt_kasumi_enc_prep(flags, d_offs, d_lens, fc_params,
1590 } else if (fc_type == ROC_SE_HASH_HMAC) {
1591 ret = cpt_digest_gen_prep(flags, d_lens, fc_params, inst);
1597 static __rte_always_inline int
1598 fill_sess_aead(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1600 struct rte_crypto_aead_xform *aead_form;
1601 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1602 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1603 uint32_t cipher_key_len = 0;
1604 uint8_t aes_gcm = 0;
1605 aead_form = &xform->aead;
1607 if (aead_form->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
1608 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
1609 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
1610 } else if (aead_form->op == RTE_CRYPTO_AEAD_OP_DECRYPT) {
1611 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
1612 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
1614 plt_dp_err("Unknown aead operation\n");
1617 switch (aead_form->algo) {
1618 case RTE_CRYPTO_AEAD_AES_GCM:
1619 enc_type = ROC_SE_AES_GCM;
1620 cipher_key_len = 16;
1623 case RTE_CRYPTO_AEAD_AES_CCM:
1624 plt_dp_err("Crypto: Unsupported cipher algo %u",
1627 case RTE_CRYPTO_AEAD_CHACHA20_POLY1305:
1628 enc_type = ROC_SE_CHACHA20;
1629 auth_type = ROC_SE_POLY1305;
1630 cipher_key_len = 32;
1631 sess->chacha_poly = 1;
1634 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1638 if (aead_form->key.length < cipher_key_len) {
1639 plt_dp_err("Invalid cipher params keylen %u",
1640 aead_form->key.length);
1644 sess->aes_gcm = aes_gcm;
1645 sess->mac_len = aead_form->digest_length;
1646 sess->iv_offset = aead_form->iv.offset;
1647 sess->iv_length = aead_form->iv.length;
1648 sess->aad_length = aead_form->aad_length;
1650 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1651 aead_form->key.data,
1652 aead_form->key.length, NULL)))
1655 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
1656 aead_form->digest_length)))
1662 static __rte_always_inline int
1663 fill_sess_cipher(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1665 struct rte_crypto_cipher_xform *c_form;
1666 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1667 uint32_t cipher_key_len = 0;
1668 uint8_t zsk_flag = 0, aes_ctr = 0, is_null = 0;
1670 c_form = &xform->cipher;
1672 if (c_form->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1673 sess->cpt_op |= ROC_SE_OP_CIPHER_ENCRYPT;
1674 else if (c_form->op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1675 sess->cpt_op |= ROC_SE_OP_CIPHER_DECRYPT;
1676 if (xform->next != NULL &&
1677 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1678 /* Perform decryption followed by auth verify */
1679 sess->roc_se_ctx.template_w4.s.opcode_minor =
1680 ROC_SE_FC_MINOR_OP_HMAC_FIRST;
1683 plt_dp_err("Unknown cipher operation\n");
1687 switch (c_form->algo) {
1688 case RTE_CRYPTO_CIPHER_AES_CBC:
1689 enc_type = ROC_SE_AES_CBC;
1690 cipher_key_len = 16;
1692 case RTE_CRYPTO_CIPHER_3DES_CBC:
1693 enc_type = ROC_SE_DES3_CBC;
1694 cipher_key_len = 24;
1696 case RTE_CRYPTO_CIPHER_DES_CBC:
1697 /* DES is implemented using 3DES in hardware */
1698 enc_type = ROC_SE_DES3_CBC;
1701 case RTE_CRYPTO_CIPHER_AES_CTR:
1702 enc_type = ROC_SE_AES_CTR;
1703 cipher_key_len = 16;
1706 case RTE_CRYPTO_CIPHER_NULL:
1710 case RTE_CRYPTO_CIPHER_KASUMI_F8:
1711 enc_type = ROC_SE_KASUMI_F8_ECB;
1712 cipher_key_len = 16;
1713 zsk_flag = ROC_SE_K_F8;
1715 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1716 enc_type = ROC_SE_SNOW3G_UEA2;
1717 cipher_key_len = 16;
1718 zsk_flag = ROC_SE_ZS_EA;
1720 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
1721 enc_type = ROC_SE_ZUC_EEA3;
1722 cipher_key_len = 16;
1723 zsk_flag = ROC_SE_ZS_EA;
1725 case RTE_CRYPTO_CIPHER_AES_XTS:
1726 enc_type = ROC_SE_AES_XTS;
1727 cipher_key_len = 16;
1729 case RTE_CRYPTO_CIPHER_3DES_ECB:
1730 enc_type = ROC_SE_DES3_ECB;
1731 cipher_key_len = 24;
1733 case RTE_CRYPTO_CIPHER_AES_ECB:
1734 enc_type = ROC_SE_AES_ECB;
1735 cipher_key_len = 16;
1737 case RTE_CRYPTO_CIPHER_3DES_CTR:
1738 case RTE_CRYPTO_CIPHER_AES_F8:
1739 case RTE_CRYPTO_CIPHER_ARC4:
1740 plt_dp_err("Crypto: Unsupported cipher algo %u", c_form->algo);
1743 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1748 if (c_form->key.length < cipher_key_len) {
1749 plt_dp_err("Invalid cipher params keylen %u",
1750 c_form->key.length);
1754 sess->zsk_flag = zsk_flag;
1756 sess->aes_ctr = aes_ctr;
1757 sess->iv_offset = c_form->iv.offset;
1758 sess->iv_length = c_form->iv.length;
1759 sess->is_null = is_null;
1761 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1762 c_form->key.data, c_form->key.length,
1766 if ((enc_type >= ROC_SE_ZUC_EEA3) && (enc_type <= ROC_SE_AES_CTR_EEA2))
1767 roc_se_ctx_swap(&sess->roc_se_ctx);
1771 static __rte_always_inline int
1772 fill_sess_auth(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1774 struct rte_crypto_auth_xform *a_form;
1775 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1776 uint8_t zsk_flag = 0, aes_gcm = 0, is_null = 0;
1778 if (xform->next != NULL &&
1779 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1780 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1781 /* Perform auth followed by encryption */
1782 sess->roc_se_ctx.template_w4.s.opcode_minor =
1783 ROC_SE_FC_MINOR_OP_HMAC_FIRST;
1786 a_form = &xform->auth;
1788 if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
1789 sess->cpt_op |= ROC_SE_OP_AUTH_VERIFY;
1790 else if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
1791 sess->cpt_op |= ROC_SE_OP_AUTH_GENERATE;
1793 plt_dp_err("Unknown auth operation");
1797 switch (a_form->algo) {
1798 case RTE_CRYPTO_AUTH_SHA1_HMAC:
1800 case RTE_CRYPTO_AUTH_SHA1:
1801 auth_type = ROC_SE_SHA1_TYPE;
1803 case RTE_CRYPTO_AUTH_SHA256_HMAC:
1804 case RTE_CRYPTO_AUTH_SHA256:
1805 auth_type = ROC_SE_SHA2_SHA256;
1807 case RTE_CRYPTO_AUTH_SHA512_HMAC:
1808 case RTE_CRYPTO_AUTH_SHA512:
1809 auth_type = ROC_SE_SHA2_SHA512;
1811 case RTE_CRYPTO_AUTH_AES_GMAC:
1812 auth_type = ROC_SE_GMAC_TYPE;
1815 case RTE_CRYPTO_AUTH_SHA224_HMAC:
1816 case RTE_CRYPTO_AUTH_SHA224:
1817 auth_type = ROC_SE_SHA2_SHA224;
1819 case RTE_CRYPTO_AUTH_SHA384_HMAC:
1820 case RTE_CRYPTO_AUTH_SHA384:
1821 auth_type = ROC_SE_SHA2_SHA384;
1823 case RTE_CRYPTO_AUTH_MD5_HMAC:
1824 case RTE_CRYPTO_AUTH_MD5:
1825 auth_type = ROC_SE_MD5_TYPE;
1827 case RTE_CRYPTO_AUTH_KASUMI_F9:
1828 auth_type = ROC_SE_KASUMI_F9_ECB;
1830 * Indicate that direction needs to be taken out
1833 zsk_flag = ROC_SE_K_F9;
1835 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
1836 auth_type = ROC_SE_SNOW3G_UIA2;
1837 zsk_flag = ROC_SE_ZS_IA;
1839 case RTE_CRYPTO_AUTH_ZUC_EIA3:
1840 auth_type = ROC_SE_ZUC_EIA3;
1841 zsk_flag = ROC_SE_ZS_IA;
1843 case RTE_CRYPTO_AUTH_NULL:
1847 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
1848 case RTE_CRYPTO_AUTH_AES_CMAC:
1849 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
1850 plt_dp_err("Crypto: Unsupported hash algo %u", a_form->algo);
1853 plt_dp_err("Crypto: Undefined Hash algo %u specified",
1858 sess->zsk_flag = zsk_flag;
1859 sess->aes_gcm = aes_gcm;
1860 sess->mac_len = a_form->digest_length;
1861 sess->is_null = is_null;
1863 sess->auth_iv_offset = a_form->iv.offset;
1864 sess->auth_iv_length = a_form->iv.length;
1866 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type,
1867 a_form->key.data, a_form->key.length,
1868 a_form->digest_length)))
1871 if ((auth_type >= ROC_SE_ZUC_EIA3) &&
1872 (auth_type <= ROC_SE_AES_CMAC_EIA2))
1873 roc_se_ctx_swap(&sess->roc_se_ctx);
1878 static __rte_always_inline int
1879 fill_sess_gmac(struct rte_crypto_sym_xform *xform, struct cnxk_se_sess *sess)
1881 struct rte_crypto_auth_xform *a_form;
1882 roc_se_cipher_type enc_type = 0; /* NULL Cipher type */
1883 roc_se_auth_type auth_type = 0; /* NULL Auth type */
1885 a_form = &xform->auth;
1887 if (a_form->op == RTE_CRYPTO_AUTH_OP_GENERATE)
1888 sess->cpt_op |= ROC_SE_OP_ENCODE;
1889 else if (a_form->op == RTE_CRYPTO_AUTH_OP_VERIFY)
1890 sess->cpt_op |= ROC_SE_OP_DECODE;
1892 plt_dp_err("Unknown auth operation");
1896 switch (a_form->algo) {
1897 case RTE_CRYPTO_AUTH_AES_GMAC:
1898 enc_type = ROC_SE_AES_GCM;
1899 auth_type = ROC_SE_GMAC_TYPE;
1902 plt_dp_err("Crypto: Undefined cipher algo %u specified",
1910 sess->iv_offset = a_form->iv.offset;
1911 sess->iv_length = a_form->iv.length;
1912 sess->mac_len = a_form->digest_length;
1914 if (unlikely(roc_se_ciph_key_set(&sess->roc_se_ctx, enc_type,
1915 a_form->key.data, a_form->key.length,
1919 if (unlikely(roc_se_auth_key_set(&sess->roc_se_ctx, auth_type, NULL, 0,
1920 a_form->digest_length)))
1926 static __rte_always_inline void *
1927 alloc_op_meta(struct roc_se_buf_ptr *buf, int32_t len,
1928 struct rte_mempool *cpt_meta_pool,
1929 struct cpt_inflight_req *infl_req)
1933 if (unlikely(rte_mempool_get(cpt_meta_pool, (void **)&mdata) < 0))
1939 infl_req->mdata = mdata;
1940 infl_req->op_flags |= CPT_OP_FLAGS_METABUF;
1945 static __rte_always_inline uint32_t
1946 prepare_iov_from_pkt(struct rte_mbuf *pkt, struct roc_se_iov_ptr *iovec,
1947 uint32_t start_offset)
1950 void *seg_data = NULL;
1951 int32_t seg_size = 0;
1958 if (!start_offset) {
1959 seg_data = rte_pktmbuf_mtod(pkt, void *);
1960 seg_size = pkt->data_len;
1962 while (start_offset >= pkt->data_len) {
1963 start_offset -= pkt->data_len;
1967 seg_data = rte_pktmbuf_mtod_offset(pkt, void *, start_offset);
1968 seg_size = pkt->data_len - start_offset;
1974 iovec->bufs[index].vaddr = seg_data;
1975 iovec->bufs[index].size = seg_size;
1979 while (unlikely(pkt != NULL)) {
1980 seg_data = rte_pktmbuf_mtod(pkt, void *);
1981 seg_size = pkt->data_len;
1985 iovec->bufs[index].vaddr = seg_data;
1986 iovec->bufs[index].size = seg_size;
1993 iovec->buf_cnt = index;
1997 static __rte_always_inline uint32_t
1998 prepare_iov_from_pkt_inplace(struct rte_mbuf *pkt,
1999 struct roc_se_fc_params *param, uint32_t *flags)
2002 void *seg_data = NULL;
2003 uint32_t seg_size = 0;
2004 struct roc_se_iov_ptr *iovec;
2006 seg_data = rte_pktmbuf_mtod(pkt, void *);
2007 seg_size = pkt->data_len;
2010 if (likely(!pkt->next)) {
2013 *flags |= ROC_SE_SINGLE_BUF_INPLACE;
2014 headroom = rte_pktmbuf_headroom(pkt);
2015 if (likely(headroom >= 24))
2016 *flags |= ROC_SE_SINGLE_BUF_HEADROOM;
2018 param->bufs[0].vaddr = seg_data;
2019 param->bufs[0].size = seg_size;
2022 iovec = param->src_iov;
2023 iovec->bufs[index].vaddr = seg_data;
2024 iovec->bufs[index].size = seg_size;
2028 while (unlikely(pkt != NULL)) {
2029 seg_data = rte_pktmbuf_mtod(pkt, void *);
2030 seg_size = pkt->data_len;
2035 iovec->bufs[index].vaddr = seg_data;
2036 iovec->bufs[index].size = seg_size;
2043 iovec->buf_cnt = index;
2047 static __rte_always_inline int
2048 fill_fc_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
2049 struct cpt_qp_meta_info *m_info,
2050 struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst)
2052 struct roc_se_ctx *ctx = &sess->roc_se_ctx;
2053 uint8_t op_minor = ctx->template_w4.s.opcode_minor;
2054 struct rte_crypto_sym_op *sym_op = cop->sym;
2056 uint32_t mc_hash_off;
2058 uint64_t d_offs, d_lens;
2059 struct rte_mbuf *m_src, *m_dst;
2060 uint8_t cpt_op = sess->cpt_op;
2061 #ifdef CPT_ALWAYS_USE_SG_MODE
2062 uint8_t inplace = 0;
2064 uint8_t inplace = 1;
2066 struct roc_se_fc_params fc_params;
2067 char src[SRC_IOV_SIZE];
2068 char dst[SRC_IOV_SIZE];
2072 if (likely(sess->iv_length)) {
2073 flags |= ROC_SE_VALID_IV_BUF;
2074 fc_params.iv_buf = rte_crypto_op_ctod_offset(cop, uint8_t *,
2076 if (sess->aes_ctr && unlikely(sess->iv_length != 16)) {
2077 memcpy((uint8_t *)iv_buf,
2078 rte_crypto_op_ctod_offset(cop, uint8_t *,
2081 iv_buf[3] = rte_cpu_to_be_32(0x1);
2082 fc_params.iv_buf = iv_buf;
2086 if (sess->zsk_flag) {
2087 fc_params.auth_iv_buf = rte_crypto_op_ctod_offset(
2088 cop, uint8_t *, sess->auth_iv_offset);
2089 if (sess->zsk_flag != ROC_SE_ZS_EA)
2092 m_src = sym_op->m_src;
2093 m_dst = sym_op->m_dst;
2095 if (sess->aes_gcm || sess->chacha_poly) {
2100 d_offs = sym_op->aead.data.offset;
2101 d_lens = sym_op->aead.data.length;
2103 sym_op->aead.data.offset + sym_op->aead.data.length;
2105 aad_data = sym_op->aead.aad.data;
2106 aad_len = sess->aad_length;
2107 if (likely((aad_data + aad_len) ==
2108 rte_pktmbuf_mtod_offset(m_src, uint8_t *,
2109 sym_op->aead.data.offset))) {
2110 d_offs = (d_offs - aad_len) | (d_offs << 16);
2111 d_lens = (d_lens + aad_len) | (d_lens << 32);
2113 fc_params.aad_buf.vaddr = sym_op->aead.aad.data;
2114 fc_params.aad_buf.size = aad_len;
2115 flags |= ROC_SE_VALID_AAD_BUF;
2117 d_offs = d_offs << 16;
2118 d_lens = d_lens << 32;
2121 salt = fc_params.iv_buf;
2122 if (unlikely(*(uint32_t *)salt != sess->salt)) {
2123 cpt_fc_salt_update(&sess->roc_se_ctx, salt);
2124 sess->salt = *(uint32_t *)salt;
2126 fc_params.iv_buf = salt + 4;
2127 if (likely(sess->mac_len)) {
2128 struct rte_mbuf *m =
2129 (cpt_op & ROC_SE_OP_ENCODE) ? m_dst : m_src;
2134 /* hmac immediately following data is best case */
2135 if (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
2137 (uint8_t *)sym_op->aead.digest.data)) {
2138 flags |= ROC_SE_VALID_MAC_BUF;
2139 fc_params.mac_buf.size = sess->mac_len;
2140 fc_params.mac_buf.vaddr =
2141 sym_op->aead.digest.data;
2146 d_offs = sym_op->cipher.data.offset;
2147 d_lens = sym_op->cipher.data.length;
2149 sym_op->cipher.data.offset + sym_op->cipher.data.length;
2150 d_offs = (d_offs << 16) | sym_op->auth.data.offset;
2151 d_lens = (d_lens << 32) | sym_op->auth.data.length;
2154 (sym_op->auth.data.offset + sym_op->auth.data.length)) {
2155 mc_hash_off = (sym_op->auth.data.offset +
2156 sym_op->auth.data.length);
2158 /* for gmac, salt should be updated like in gcm */
2159 if (unlikely(sess->is_gmac)) {
2161 salt = fc_params.iv_buf;
2162 if (unlikely(*(uint32_t *)salt != sess->salt)) {
2163 cpt_fc_salt_update(&sess->roc_se_ctx, salt);
2164 sess->salt = *(uint32_t *)salt;
2166 fc_params.iv_buf = salt + 4;
2168 if (likely(sess->mac_len)) {
2171 m = (cpt_op & ROC_SE_OP_ENCODE) ? m_dst : m_src;
2175 /* hmac immediately following data is best case */
2176 if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST) &&
2177 (unlikely(rte_pktmbuf_mtod(m, uint8_t *) +
2179 (uint8_t *)sym_op->auth.digest.data))) {
2180 flags |= ROC_SE_VALID_MAC_BUF;
2181 fc_params.mac_buf.size = sess->mac_len;
2182 fc_params.mac_buf.vaddr =
2183 sym_op->auth.digest.data;
2188 fc_params.ctx_buf.vaddr = &sess->roc_se_ctx;
2190 if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST) &&
2191 unlikely(sess->is_null || sess->cpt_op == ROC_SE_OP_DECODE))
2194 if (likely(!m_dst && inplace)) {
2195 /* Case of single buffer without AAD buf or
2196 * separate mac buf in place and
2199 fc_params.dst_iov = fc_params.src_iov = (void *)src;
2201 if (unlikely(prepare_iov_from_pkt_inplace(m_src, &fc_params,
2203 plt_dp_err("Prepare inplace src iov failed");
2209 /* Out of place processing */
2210 fc_params.src_iov = (void *)src;
2211 fc_params.dst_iov = (void *)dst;
2213 /* Store SG I/O in the api for reuse */
2214 if (prepare_iov_from_pkt(m_src, fc_params.src_iov, 0)) {
2215 plt_dp_err("Prepare src iov failed");
2220 if (unlikely(m_dst != NULL)) {
2223 /* Try to make room as much as src has */
2224 pkt_len = rte_pktmbuf_pkt_len(m_dst);
2226 if (unlikely(pkt_len < rte_pktmbuf_pkt_len(m_src))) {
2227 pkt_len = rte_pktmbuf_pkt_len(m_src) - pkt_len;
2228 if (!rte_pktmbuf_append(m_dst, pkt_len)) {
2229 plt_dp_err("Not enough space in "
2238 if (prepare_iov_from_pkt(m_dst, fc_params.dst_iov, 0)) {
2239 plt_dp_err("Prepare dst iov failed for "
2246 fc_params.dst_iov = (void *)src;
2250 if (unlikely(!((flags & ROC_SE_SINGLE_BUF_INPLACE) &&
2251 (flags & ROC_SE_SINGLE_BUF_HEADROOM) &&
2252 ((ctx->fc_type == ROC_SE_FC_GEN) ||
2253 (ctx->fc_type == ROC_SE_PDCP))))) {
2254 mdata = alloc_op_meta(&fc_params.meta_buf, m_info->mlen,
2255 m_info->pool, infl_req);
2256 if (mdata == NULL) {
2257 plt_dp_err("Error allocating meta buffer for request");
2262 /* Finally prepare the instruction */
2263 if (cpt_op & ROC_SE_OP_ENCODE)
2264 ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, &fc_params,
2267 ret = cpt_fc_dec_hmac_prep(flags, d_offs, d_lens, &fc_params,
2270 if (unlikely(ret)) {
2271 plt_dp_err("Preparing request failed due to bad input arg");
2272 goto free_mdata_and_exit;
2277 free_mdata_and_exit:
2278 if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
2279 rte_mempool_put(m_info->pool, infl_req->mdata);
2284 static __rte_always_inline void
2285 compl_auth_verify(struct rte_crypto_op *op, uint8_t *gen_mac, uint64_t mac_len)
2288 struct rte_crypto_sym_op *sym_op = op->sym;
2290 if (sym_op->auth.digest.data)
2291 mac = sym_op->auth.digest.data;
2293 mac = rte_pktmbuf_mtod_offset(sym_op->m_src, uint8_t *,
2294 sym_op->auth.data.length +
2295 sym_op->auth.data.offset);
2297 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2301 if (memcmp(mac, gen_mac, mac_len))
2302 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
2304 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2307 static __rte_always_inline void
2308 find_kasumif9_direction_and_length(uint8_t *src, uint32_t counter_num_bytes,
2309 uint32_t *addr_length_in_bits,
2310 uint8_t *addr_direction)
2315 while (!found && counter_num_bytes > 0) {
2316 counter_num_bytes--;
2317 if (src[counter_num_bytes] == 0x00)
2319 pos = rte_bsf32(src[counter_num_bytes]);
2321 if (likely(counter_num_bytes > 0)) {
2322 last_byte = src[counter_num_bytes - 1];
2323 *addr_direction = last_byte & 0x1;
2324 *addr_length_in_bits =
2325 counter_num_bytes * 8 - 1;
2328 last_byte = src[counter_num_bytes];
2329 *addr_direction = (last_byte >> (pos + 1)) & 0x1;
2330 *addr_length_in_bits =
2331 counter_num_bytes * 8 + (8 - (pos + 2));
2338 * This handles all auth only except AES_GMAC
2340 static __rte_always_inline int
2341 fill_digest_params(struct rte_crypto_op *cop, struct cnxk_se_sess *sess,
2342 struct cpt_qp_meta_info *m_info,
2343 struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst)
2346 struct rte_crypto_sym_op *sym_op = cop->sym;
2348 uint32_t auth_range_off;
2350 uint64_t d_offs = 0, d_lens;
2351 struct rte_mbuf *m_src, *m_dst;
2352 uint16_t auth_op = sess->cpt_op & ROC_SE_OP_AUTH_MASK;
2353 uint16_t mac_len = sess->mac_len;
2354 struct roc_se_fc_params params;
2355 char src[SRC_IOV_SIZE];
2359 memset(¶ms, 0, sizeof(struct roc_se_fc_params));
2361 m_src = sym_op->m_src;
2363 mdata = alloc_op_meta(¶ms.meta_buf, m_info->mlen, m_info->pool,
2365 if (mdata == NULL) {
2370 auth_range_off = sym_op->auth.data.offset;
2372 flags = ROC_SE_VALID_MAC_BUF;
2373 params.src_iov = (void *)src;
2374 if (unlikely(sess->zsk_flag)) {
2376 * Since for Zuc, Kasumi, Snow3g offsets are in bits
2377 * we will send pass through even for auth only case,
2380 d_offs = auth_range_off;
2382 params.auth_iv_buf = rte_crypto_op_ctod_offset(
2383 cop, uint8_t *, sess->auth_iv_offset);
2384 if (sess->zsk_flag == ROC_SE_K_F9) {
2385 uint32_t length_in_bits, num_bytes;
2386 uint8_t *src, direction = 0;
2389 rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *), 8);
2391 * This is kasumi f9, take direction from
2394 length_in_bits = cop->sym->auth.data.length;
2395 num_bytes = (length_in_bits >> 3);
2396 src = rte_pktmbuf_mtod(cop->sym->m_src, uint8_t *);
2397 find_kasumif9_direction_and_length(
2398 src, num_bytes, &length_in_bits, &direction);
2399 length_in_bits -= 64;
2400 cop->sym->auth.data.offset += 64;
2401 d_offs = cop->sym->auth.data.offset;
2402 auth_range_off = d_offs / 8;
2403 cop->sym->auth.data.length = length_in_bits;
2405 /* Store it at end of auth iv */
2406 iv_buf[8] = direction;
2407 params.auth_iv_buf = iv_buf;
2411 d_lens = sym_op->auth.data.length;
2413 params.ctx_buf.vaddr = &sess->roc_se_ctx;
2415 if (auth_op == ROC_SE_OP_AUTH_GENERATE) {
2416 if (sym_op->auth.digest.data) {
2418 * Digest to be generated
2419 * in separate buffer
2421 params.mac_buf.size = sess->mac_len;
2422 params.mac_buf.vaddr = sym_op->auth.digest.data;
2424 uint32_t off = sym_op->auth.data.offset +
2425 sym_op->auth.data.length;
2426 int32_t dlen, space;
2428 m_dst = sym_op->m_dst ? sym_op->m_dst : sym_op->m_src;
2429 dlen = rte_pktmbuf_pkt_len(m_dst);
2431 space = off + mac_len - dlen;
2433 if (!rte_pktmbuf_append(m_dst, space)) {
2434 plt_dp_err("Failed to extend "
2438 goto free_mdata_and_exit;
2441 params.mac_buf.vaddr =
2442 rte_pktmbuf_mtod_offset(m_dst, void *, off);
2443 params.mac_buf.size = mac_len;
2446 uint64_t *op = mdata;
2448 /* Need space for storing generated mac */
2449 space += 2 * sizeof(uint64_t);
2451 params.mac_buf.vaddr = (uint8_t *)mdata + space;
2452 params.mac_buf.size = mac_len;
2453 space += RTE_ALIGN_CEIL(mac_len, 8);
2454 op[0] = (uintptr_t)params.mac_buf.vaddr;
2456 infl_req->op_flags |= CPT_OP_FLAGS_AUTH_VERIFY;
2459 params.meta_buf.vaddr = (uint8_t *)mdata + space;
2460 params.meta_buf.size -= space;
2462 /* Out of place processing */
2463 params.src_iov = (void *)src;
2465 /*Store SG I/O in the api for reuse */
2466 if (prepare_iov_from_pkt(m_src, params.src_iov, auth_range_off)) {
2467 plt_dp_err("Prepare src iov failed");
2469 goto free_mdata_and_exit;
2472 ret = cpt_fc_enc_hmac_prep(flags, d_offs, d_lens, ¶ms, inst);
2474 goto free_mdata_and_exit;
2478 free_mdata_and_exit:
2479 if (infl_req->op_flags & CPT_OP_FLAGS_METABUF)
2480 rte_mempool_put(m_info->pool, infl_req->mdata);
2484 #endif /*_CNXK_SE_H_ */