1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
5 #ifndef __OTX2_ETHDEV_SEC_TX_H__
6 #define __OTX2_ETHDEV_SEC_TX_H__
8 #include <rte_security.h>
11 #include "otx2_ethdev_sec.h"
13 struct otx2_ipsec_fp_out_hdr {
19 static __rte_always_inline int32_t
20 otx2_ipsec_fp_out_rlen_get(struct otx2_sec_session_ipsec_ip *sess,
23 uint32_t enc_payload_len;
25 enc_payload_len = RTE_ALIGN_CEIL(plen + sess->roundup_len,
28 return sess->partial_len + enc_payload_len;
31 static __rte_always_inline void
32 otx2_ssogws_head_wait(struct otx2_ssogws *ws);
34 static __rte_always_inline int
35 otx2_sec_event_tx(struct otx2_ssogws *ws, struct rte_event *ev,
36 struct rte_mbuf *m, const struct otx2_eth_txq *txq,
37 const uint32_t offload_flags)
39 uint32_t dlen, rlen, desc_headroom, extend_head, extend_tail;
40 struct otx2_sec_session_ipsec_ip *sess;
41 struct otx2_ipsec_fp_out_hdr *hdr;
42 struct otx2_ipsec_fp_out_sa *sa;
43 uint64_t data_addr, desc_addr;
44 struct otx2_sec_session *priv;
45 struct otx2_cpt_inst_s inst;
50 struct otx2_cpt_res cpt_res __rte_aligned(OTX2_CPT_RES_ALIGN);
51 struct nix_send_hdr_s nix_hdr
52 __rte_aligned(OTX2_NIX_SEND_DESC_ALIGN);
53 union nix_send_sg_s nix_sg;
54 struct nix_iova_s nix_iova;
57 priv = get_sec_session_private_data((void *)(m->udata64));
58 sess = &priv->ipsec.ip;
61 RTE_ASSERT(sess->cpt_lmtline != NULL);
62 RTE_ASSERT(!(offload_flags & (NIX_TX_OFFLOAD_MBUF_NOFF_F |
63 NIX_TX_OFFLOAD_VLAN_QINQ_F)));
65 dlen = rte_pktmbuf_pkt_len(m) + sizeof(*hdr) - RTE_ETHER_HDR_LEN;
66 rlen = otx2_ipsec_fp_out_rlen_get(sess, dlen - sizeof(*hdr));
68 RTE_BUILD_BUG_ON(OTX2_CPT_RES_ALIGN % OTX2_NIX_SEND_DESC_ALIGN);
69 RTE_BUILD_BUG_ON(sizeof(sd->cpt_res) % OTX2_NIX_SEND_DESC_ALIGN);
71 extend_head = sizeof(*hdr);
72 extend_tail = rlen - dlen;
74 desc_headroom = (OTX2_CPT_RES_ALIGN - 1) + sizeof(*sd);
76 if (unlikely(!rte_pktmbuf_is_contiguous(m)) ||
77 unlikely(rte_pktmbuf_headroom(m) < extend_head + desc_headroom) ||
78 unlikely(rte_pktmbuf_tailroom(m) < extend_tail)) {
83 * Extend mbuf data to point to the expected packet buffer for NIX.
84 * This includes the Ethernet header followed by the encrypted IPsec
87 rte_pktmbuf_append(m, extend_tail);
88 data = rte_pktmbuf_prepend(m, extend_head);
89 data_addr = rte_pktmbuf_mtophys(m);
92 * Move the Ethernet header, to insert otx2_ipsec_fp_out_hdr prior
95 memcpy(data, data + sizeof(*hdr), RTE_ETHER_HDR_LEN);
97 hdr = (struct otx2_ipsec_fp_out_hdr *)(data + RTE_ETHER_HDR_LEN);
99 if (sa->ctl.enc_type == OTX2_IPSEC_FP_SA_ENC_AES_GCM) {
101 memcpy(hdr->iv, &sa->nonce, 4);
102 memset(hdr->iv + 4, 0, 12); //TODO: make it random
104 /* AES-128-[CBC] + [SHA1] */
105 memset(hdr->iv, 0, 16); //TODO: make it random
108 /* Keep CPT result and NIX send descriptors in headroom */
109 sd = (void *)RTE_PTR_ALIGN(data - desc_headroom, OTX2_CPT_RES_ALIGN);
110 desc_addr = data_addr - RTE_PTR_DIFF(data, sd);
112 /* Prepare CPT instruction */
114 inst.nixtx_addr = (desc_addr + offsetof(struct desc, nix_hdr)) >> 4;
117 inst.res_addr = desc_addr + offsetof(struct desc, cpt_res);
120 inst.wqe_ptr = desc_addr >> 3; /* FIXME: Handle errors */
122 inst.opcode = OTX2_CPT_OP_INLINE_IPSEC_OUTB;
124 inst.dptr = data_addr + RTE_ETHER_HDR_LEN;
125 inst.u64[7] = sess->inst_w7;
127 /* First word contains 8 bit completion code & 8 bit uc comp code */
128 sd->cpt_res.u16[0] = 0;
130 /* Prepare NIX send descriptors for output expected from CPT */
132 sd->nix_hdr.w0.u = 0;
133 sd->nix_hdr.w1.u = 0;
134 sd->nix_hdr.w0.sq = txq->sq;
135 sd->nix_hdr.w0.sizem1 = 1;
136 sd->nix_hdr.w0.total = rte_pktmbuf_data_len(m);
137 sd->nix_hdr.w0.aura = npa_lf_aura_handle_to_aura(m->pool->pool_id);
140 sd->nix_sg.subdc = NIX_SUBDC_SG;
141 sd->nix_sg.ld_type = NIX_SENDLDTYPE_LDD;
143 sd->nix_sg.seg1_size = rte_pktmbuf_data_len(m);
145 sd->nix_iova.addr = rte_mbuf_data_iova(m);
147 /* Mark mempool object as "put" since it is freed by NIX */
148 __mempool_check_cookies(m->pool, (void **)&m, 1, 0);
151 otx2_ssogws_head_wait(ws);
153 inst.param1 = sess->esn_hi >> 16;
154 inst.param2 = sess->esn_hi & 0xffff;
156 hdr->seq = rte_cpu_to_be_32(sess->seq);
157 hdr->ip_id = rte_cpu_to_be_32(sess->ip_id);
165 otx2_lmt_mov(sess->cpt_lmtline, &inst, 2);
166 lmt_status = otx2_lmt_submit(sess->cpt_nq_reg);
167 } while (lmt_status == 0);
172 if (offload_flags & NIX_TX_OFFLOAD_MBUF_NOFF_F) {
173 /* Don't free if reference count > 1 */
174 if (rte_pktmbuf_prefree_seg(m) == NULL)
181 #endif /* __OTX2_ETHDEV_SEC_TX_H__ */