Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2025 Marvell.
3 : : */
4 : :
5 : : #ifndef __CN20K_TLS_OPS_H__
6 : : #define __CN20K_TLS_OPS_H__
7 : :
8 : : #include <rte_crypto_sym.h>
9 : : #include <rte_security.h>
10 : :
11 : : #include "roc_ie.h"
12 : :
13 : : #include "cn20k_cryptodev.h"
14 : : #include "cn20k_cryptodev_sec.h"
15 : : #include "cnxk_cryptodev.h"
16 : : #include "cnxk_cryptodev_ops.h"
17 : : #include "cnxk_sg.h"
18 : :
19 : : static __rte_always_inline int
20 : : process_tls_write(struct roc_cpt_lf *lf, struct rte_crypto_op *cop, struct cn20k_sec_session *sess,
21 : : struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
22 : : struct cpt_inst_s *inst)
23 : : {
24 : 0 : struct cn20k_tls_opt tls_opt = sess->tls_opt;
25 : : struct rte_crypto_sym_op *sym_op = cop->sym;
26 : : #ifdef LA_IPSEC_DEBUG
27 : : struct roc_ie_ow_tls_write_sa *write_sa;
28 : : #endif
29 : 0 : struct rte_mbuf *m_src = sym_op->m_src;
30 : 0 : struct rte_mbuf *m_dst = sym_op->m_dst;
31 : : uint32_t pad_len, pad_bytes;
32 : : struct rte_mbuf *last_seg;
33 : : union cpt_inst_w4 w4;
34 : : void *m_data = NULL;
35 : : uint8_t *in_buffer;
36 : :
37 : 0 : pad_bytes = (cop->aux_flags * 8) > 0xff ? 0xff : (cop->aux_flags * 8);
38 : 0 : pad_len = (pad_bytes >> tls_opt.pad_shift) * tls_opt.enable_padding;
39 : :
40 : : #ifdef LA_IPSEC_DEBUG
41 : : write_sa = &sess->tls_rec.write_sa;
42 : : if (write_sa->w2.s.iv_at_cptr == ROC_IE_OW_TLS_IV_SRC_FROM_SA) {
43 : :
44 : : uint8_t *iv = PLT_PTR_ADD(write_sa->cipher_key, 32);
45 : :
46 : : if (write_sa->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_AES_GCM) {
47 : : uint32_t *tmp;
48 : :
49 : : /* For GCM, the IV and salt format will be like below:
50 : : * iv[0-3]: lower bytes of IV in BE format.
51 : : * iv[4-7]: salt / nonce.
52 : : * iv[12-15]: upper bytes of IV in BE format.
53 : : */
54 : : memcpy(iv, rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset), 4);
55 : : tmp = (uint32_t *)iv;
56 : : *tmp = rte_be_to_cpu_32(*tmp);
57 : :
58 : : memcpy(iv + 12,
59 : : rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset + 4), 4);
60 : : tmp = (uint32_t *)(iv + 12);
61 : : *tmp = rte_be_to_cpu_32(*tmp);
62 : : } else if (write_sa->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_AES_CBC) {
63 : : uint64_t *tmp;
64 : :
65 : : memcpy(iv, rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset), 16);
66 : : tmp = (uint64_t *)iv;
67 : : *tmp = rte_be_to_cpu_64(*tmp);
68 : : tmp = (uint64_t *)(iv + 8);
69 : : *tmp = rte_be_to_cpu_64(*tmp);
70 : : } else if (write_sa->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_3DES) {
71 : : uint64_t *tmp;
72 : :
73 : : memcpy(iv, rte_crypto_op_ctod_offset(cop, uint8_t *, sess->iv_offset), 8);
74 : : tmp = (uint64_t *)iv;
75 : : *tmp = rte_be_to_cpu_64(*tmp);
76 : : }
77 : :
78 : : /* Trigger CTX reload to fetch new data from DRAM */
79 : : roc_cpt_lf_ctx_reload(lf, write_sa);
80 : : rte_delay_ms(1);
81 : : }
82 : : #else
83 : : RTE_SET_USED(lf);
84 : : #endif
85 : : /* Single buffer direct mode */
86 [ # # ]: 0 : if (likely(m_src->next == NULL)) {
87 : : void *vaddr;
88 : :
89 [ # # ]: 0 : if (unlikely(rte_pktmbuf_tailroom(m_src) < sess->max_extended_len)) {
90 : 0 : plt_dp_err("Not enough tail room");
91 : 0 : return -ENOMEM;
92 : : }
93 : :
94 : 0 : vaddr = rte_pktmbuf_mtod(m_src, void *);
95 : 0 : inst->dptr = (uint64_t)vaddr;
96 : 0 : inst->rptr = (uint64_t)vaddr;
97 : :
98 : 0 : w4.u64 = sess->inst.w4;
99 : 0 : w4.s.param1 = m_src->data_len;
100 : 0 : w4.s.dlen = m_src->data_len;
101 : :
102 : 0 : w4.s.param2 = cop->param1.tls_record.content_type;
103 : 0 : w4.s.opcode_minor = pad_len;
104 : :
105 : 0 : inst->w4.u64 = w4.u64;
106 : : } else {
107 : : struct roc_sg2list_comp *scatter_comp, *gather_comp;
108 : : union cpt_inst_w5 cpt_inst_w5;
109 : : union cpt_inst_w6 cpt_inst_w6;
110 : : uint32_t g_size_bytes;
111 : : int i;
112 : :
113 : 0 : last_seg = rte_pktmbuf_lastseg(m_src);
114 : :
115 [ # # ]: 0 : if (unlikely(rte_pktmbuf_tailroom(last_seg) < sess->max_extended_len)) {
116 : 0 : plt_dp_err("Not enough tail room (required: %d, available: %d)",
117 : : sess->max_extended_len, rte_pktmbuf_tailroom(last_seg));
118 : 0 : return -ENOMEM;
119 : : }
120 : :
121 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG2_MAX_PTRS)) {
122 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
123 : 0 : return -1;
124 : : }
125 : :
126 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
127 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
128 : 0 : plt_dp_err("Error allocating meta buffer for request");
129 : 0 : return -ENOMEM;
130 : : }
131 : :
132 : : in_buffer = (uint8_t *)m_data;
133 : : /* Input Gather List */
134 : : i = 0;
135 : : gather_comp = (struct roc_sg2list_comp *)((uint8_t *)in_buffer);
136 : 0 : i = fill_sg2_comp_from_pkt(gather_comp, i, m_src);
137 : :
138 : 0 : cpt_inst_w5.s.gather_sz = ((i + 2) / 3);
139 : 0 : g_size_bytes = ((i + 2) / 3) * sizeof(struct roc_sg2list_comp);
140 : :
141 : : /* Output Scatter List */
142 : 0 : last_seg->data_len += sess->max_extended_len + pad_bytes;
143 : : i = 0;
144 : 0 : scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
145 : :
146 [ # # ]: 0 : if (m_dst == NULL)
147 : : m_dst = m_src;
148 : 0 : i = fill_sg2_comp_from_pkt(scatter_comp, i, m_dst);
149 : :
150 : 0 : cpt_inst_w6.s.scatter_sz = ((i + 2) / 3);
151 : :
152 : 0 : cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
153 : 0 : cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
154 : :
155 : 0 : inst->w5.u64 = cpt_inst_w5.u64;
156 : 0 : inst->w6.u64 = cpt_inst_w6.u64;
157 : 0 : w4.u64 = sess->inst.w4;
158 : 0 : w4.s.dlen = rte_pktmbuf_pkt_len(m_src);
159 : 0 : w4.s.opcode_major &= (~(ROC_IE_OW_INPLACE_BIT));
160 : 0 : w4.s.opcode_minor = pad_len;
161 : 0 : w4.s.param1 = w4.s.dlen;
162 : 0 : w4.s.param2 = cop->param1.tls_record.content_type;
163 : 0 : inst->w4.u64 = w4.u64;
164 : : }
165 : :
166 : : return 0;
167 : : }
168 : :
169 : : static __rte_always_inline int
170 : : process_tls_read(struct rte_crypto_op *cop, struct cn20k_sec_session *sess,
171 : : struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
172 : : struct cpt_inst_s *inst)
173 : : {
174 : : struct rte_crypto_sym_op *sym_op = cop->sym;
175 : 0 : struct rte_mbuf *m_src = sym_op->m_src;
176 : 0 : struct rte_mbuf *m_dst = sym_op->m_dst;
177 : : union cpt_inst_w4 w4;
178 : : uint8_t *in_buffer;
179 : : void *m_data;
180 : :
181 [ # # ]: 0 : if (likely(m_src->next == NULL)) {
182 : : void *vaddr;
183 : :
184 : 0 : vaddr = rte_pktmbuf_mtod(m_src, void *);
185 : :
186 : 0 : inst->dptr = (uint64_t)vaddr;
187 : 0 : inst->rptr = (uint64_t)vaddr;
188 : :
189 : 0 : w4.u64 = sess->inst.w4;
190 : 0 : w4.s.dlen = m_src->data_len;
191 : 0 : w4.s.param1 = m_src->data_len;
192 : 0 : inst->w4.u64 = w4.u64;
193 : : } else {
194 : : struct roc_sg2list_comp *scatter_comp, *gather_comp;
195 : 0 : int tail_len = sess->tls_opt.tail_fetch_len * 16;
196 : 0 : int pkt_len = rte_pktmbuf_pkt_len(m_src);
197 : : union cpt_inst_w5 cpt_inst_w5;
198 : : union cpt_inst_w6 cpt_inst_w6;
199 : : uint32_t g_size_bytes;
200 : : int i;
201 : :
202 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG2_MAX_PTRS)) {
203 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
204 : 0 : return -1;
205 : : }
206 : :
207 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
208 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
209 : 0 : plt_dp_err("Error allocating meta buffer for request");
210 : 0 : return -ENOMEM;
211 : : }
212 : :
213 : : in_buffer = (uint8_t *)m_data;
214 : : /* Input Gather List */
215 : : i = 0;
216 : :
217 : : /* First 32 bytes in m_data are rsvd for tail fetch.
218 : : * SG list start from 32 byte onwards.
219 : : */
220 : 0 : gather_comp = (struct roc_sg2list_comp *)((uint8_t *)(in_buffer + 32));
221 : :
222 : : /* Add the last blocks as first gather component for tail fetch. */
223 [ # # ]: 0 : if (tail_len) {
224 : : const uint8_t *output;
225 : :
226 : 0 : output = rte_pktmbuf_read(m_src, pkt_len - tail_len, tail_len, in_buffer);
227 [ # # ]: 0 : if (output != in_buffer)
228 [ # # ]: 0 : rte_memcpy(in_buffer, output, tail_len);
229 : 0 : i = fill_sg2_comp(gather_comp, i, (uint64_t)in_buffer, tail_len);
230 : : }
231 : :
232 : 0 : i = fill_sg2_comp_from_pkt(gather_comp, i, m_src);
233 : :
234 : 0 : cpt_inst_w5.s.gather_sz = ((i + 2) / 3);
235 : 0 : g_size_bytes = ((i + 2) / 3) * sizeof(struct roc_sg2list_comp);
236 : :
237 : : i = 0;
238 : 0 : scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
239 : :
240 [ # # ]: 0 : if (m_dst == NULL)
241 : : m_dst = m_src;
242 : 0 : i = fill_sg2_comp_from_pkt(scatter_comp, i, m_dst);
243 : :
244 : 0 : cpt_inst_w6.s.scatter_sz = ((i + 2) / 3);
245 : :
246 : 0 : cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
247 : 0 : cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
248 : :
249 : 0 : inst->w5.u64 = cpt_inst_w5.u64;
250 : 0 : inst->w6.u64 = cpt_inst_w6.u64;
251 : 0 : w4.u64 = sess->inst.w4;
252 : 0 : w4.s.dlen = pkt_len + tail_len;
253 : 0 : w4.s.param1 = w4.s.dlen;
254 : 0 : w4.s.opcode_major &= (~(ROC_IE_OW_INPLACE_BIT));
255 : 0 : inst->w4.u64 = w4.u64;
256 : : }
257 : :
258 : : return 0;
259 : : }
260 : : #endif /* __CN20K_TLS_OPS_H__ */
|