Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2024 Marvell.
3 : : */
4 : :
5 : : #ifndef __CN10K_TLS_OPS_H__
6 : : #define __CN10K_TLS_OPS_H__
7 : :
8 : : #include <rte_crypto_sym.h>
9 : : #include <rte_security.h>
10 : :
11 : : #include "roc_ie.h"
12 : :
13 : : #include "cn10k_cryptodev.h"
14 : : #include "cn10k_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 cn10k_sec_session *sess,
21 : : struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
22 : : struct cpt_inst_s *inst, const bool is_sg_ver2)
23 : : {
24 : 0 : struct cn10k_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_ot_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_OT_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_OT_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_OT_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_OT_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 [ # # ]: 0 : } else if (is_sg_ver2 == false) {
107 : : struct roc_sglist_comp *scatter_comp, *gather_comp;
108 : : uint32_t g_size_bytes, s_size_bytes;
109 : : uint32_t dlen;
110 : : int i;
111 : :
112 : 0 : last_seg = rte_pktmbuf_lastseg(m_src);
113 : :
114 [ # # ]: 0 : if (unlikely(rte_pktmbuf_tailroom(last_seg) < sess->max_extended_len)) {
115 : 0 : plt_dp_err("Not enough tail room (required: %d, available: %d)",
116 : : sess->max_extended_len, rte_pktmbuf_tailroom(last_seg));
117 : 0 : return -ENOMEM;
118 : : }
119 : :
120 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG1_MAX_PTRS)) {
121 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
122 : 0 : return -1;
123 : : }
124 : :
125 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
126 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
127 : 0 : plt_dp_err("Error allocating meta buffer for request");
128 : 0 : return -ENOMEM;
129 : : }
130 : :
131 : : in_buffer = (uint8_t *)m_data;
132 : 0 : ((uint16_t *)in_buffer)[0] = 0;
133 : 0 : ((uint16_t *)in_buffer)[1] = 0;
134 : :
135 : : /* Input Gather List */
136 : : i = 0;
137 : 0 : gather_comp = (struct roc_sglist_comp *)((uint8_t *)in_buffer + 8);
138 : :
139 : 0 : i = fill_sg_comp_from_pkt(gather_comp, i, m_src);
140 [ # # ]: 0 : ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
141 : :
142 : 0 : g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
143 : :
144 : : /* Output Scatter List */
145 : 0 : last_seg->data_len += sess->max_extended_len + pad_bytes;
146 : : i = 0;
147 : 0 : scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
148 : :
149 : 0 : i = fill_sg_comp_from_pkt(scatter_comp, i, m_src);
150 [ # # ]: 0 : ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
151 : :
152 : 0 : s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
153 : :
154 : 0 : dlen = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
155 : :
156 : 0 : inst->dptr = (uint64_t)in_buffer;
157 : 0 : inst->rptr = (uint64_t)in_buffer;
158 : :
159 : 0 : w4.u64 = sess->inst.w4;
160 : 0 : w4.s.dlen = dlen;
161 : 0 : w4.s.param1 = rte_pktmbuf_pkt_len(m_src);
162 : 0 : w4.s.param2 = cop->param1.tls_record.content_type;
163 : 0 : w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
164 : 0 : w4.s.opcode_minor = pad_len;
165 : :
166 : 0 : inst->w4.u64 = w4.u64;
167 : : } else {
168 : : struct roc_sg2list_comp *scatter_comp, *gather_comp;
169 : : union cpt_inst_w5 cpt_inst_w5;
170 : : union cpt_inst_w6 cpt_inst_w6;
171 : : uint32_t g_size_bytes;
172 : : int i;
173 : :
174 : 0 : last_seg = rte_pktmbuf_lastseg(m_src);
175 : :
176 [ # # ]: 0 : if (unlikely(rte_pktmbuf_tailroom(last_seg) < sess->max_extended_len)) {
177 : 0 : plt_dp_err("Not enough tail room (required: %d, available: %d)",
178 : : sess->max_extended_len, rte_pktmbuf_tailroom(last_seg));
179 : 0 : return -ENOMEM;
180 : : }
181 : :
182 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG2_MAX_PTRS)) {
183 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
184 : 0 : return -1;
185 : : }
186 : :
187 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
188 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
189 : 0 : plt_dp_err("Error allocating meta buffer for request");
190 : 0 : return -ENOMEM;
191 : : }
192 : :
193 : : in_buffer = (uint8_t *)m_data;
194 : : /* Input Gather List */
195 : : i = 0;
196 : : gather_comp = (struct roc_sg2list_comp *)((uint8_t *)in_buffer);
197 : 0 : i = fill_sg2_comp_from_pkt(gather_comp, i, m_src);
198 : :
199 : 0 : cpt_inst_w5.s.gather_sz = ((i + 2) / 3);
200 : 0 : g_size_bytes = ((i + 2) / 3) * sizeof(struct roc_sg2list_comp);
201 : :
202 : : /* Output Scatter List */
203 : 0 : last_seg->data_len += sess->max_extended_len + pad_bytes;
204 : : i = 0;
205 : 0 : scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
206 : :
207 [ # # ]: 0 : if (m_dst == NULL)
208 : : m_dst = m_src;
209 : 0 : i = fill_sg2_comp_from_pkt(scatter_comp, i, m_dst);
210 : :
211 : 0 : cpt_inst_w6.s.scatter_sz = ((i + 2) / 3);
212 : :
213 : 0 : cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
214 : 0 : cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
215 : :
216 : 0 : inst->w5.u64 = cpt_inst_w5.u64;
217 : 0 : inst->w6.u64 = cpt_inst_w6.u64;
218 : 0 : w4.u64 = sess->inst.w4;
219 : 0 : w4.s.dlen = rte_pktmbuf_pkt_len(m_src);
220 : 0 : w4.s.opcode_major &= (~(ROC_IE_OT_INPLACE_BIT));
221 : 0 : w4.s.opcode_minor = pad_len;
222 : 0 : w4.s.param1 = w4.s.dlen;
223 : 0 : w4.s.param2 = cop->param1.tls_record.content_type;
224 : 0 : inst->w4.u64 = w4.u64;
225 : : }
226 : :
227 : : return 0;
228 : : }
229 : :
230 : : static __rte_always_inline int
231 : : process_tls_read(struct rte_crypto_op *cop, struct cn10k_sec_session *sess,
232 : : struct cpt_qp_meta_info *m_info, struct cpt_inflight_req *infl_req,
233 : : struct cpt_inst_s *inst, const bool is_sg_ver2)
234 : : {
235 : : struct rte_crypto_sym_op *sym_op = cop->sym;
236 : 0 : struct rte_mbuf *m_src = sym_op->m_src;
237 : 0 : struct rte_mbuf *m_dst = sym_op->m_dst;
238 : : union cpt_inst_w4 w4;
239 : : uint8_t *in_buffer;
240 : : void *m_data;
241 : :
242 [ # # ]: 0 : if (likely(m_src->next == NULL)) {
243 : : void *vaddr;
244 : :
245 : 0 : vaddr = rte_pktmbuf_mtod(m_src, void *);
246 : :
247 : 0 : inst->dptr = (uint64_t)vaddr;
248 : 0 : inst->rptr = (uint64_t)vaddr;
249 : :
250 : 0 : w4.u64 = sess->inst.w4;
251 : 0 : w4.s.dlen = m_src->data_len;
252 : 0 : w4.s.param1 = m_src->data_len;
253 : 0 : inst->w4.u64 = w4.u64;
254 [ # # ]: 0 : } else if (is_sg_ver2 == false) {
255 : : struct roc_sglist_comp *scatter_comp, *gather_comp;
256 : 0 : int tail_len = sess->tls_opt.tail_fetch_len * 16;
257 : 0 : int pkt_len = rte_pktmbuf_pkt_len(m_src);
258 : : uint32_t g_size_bytes, s_size_bytes;
259 : : uint16_t *sg_hdr;
260 : : uint32_t dlen;
261 : : int i;
262 : :
263 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG1_MAX_PTRS)) {
264 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
265 : 0 : return -1;
266 : : }
267 : :
268 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
269 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
270 : 0 : plt_dp_err("Error allocating meta buffer for request");
271 : 0 : return -ENOMEM;
272 : : }
273 : :
274 : : /* Input Gather List */
275 : : in_buffer = (uint8_t *)m_data;
276 : : sg_hdr = (uint16_t *)(in_buffer + 32);
277 : 0 : gather_comp = (struct roc_sglist_comp *)((uint8_t *)sg_hdr + 8);
278 : : i = 0;
279 : : /* Add the last blocks as first gather component for tail fetch. */
280 [ # # ]: 0 : if (tail_len) {
281 : : const uint8_t *output;
282 : :
283 : 0 : output = rte_pktmbuf_read(m_src, pkt_len - tail_len, tail_len, in_buffer);
284 [ # # ]: 0 : if (output != in_buffer)
285 [ # # ]: 0 : rte_memcpy(in_buffer, output, tail_len);
286 [ # # ]: 0 : i = fill_sg_comp(gather_comp, i, (uint64_t)in_buffer, tail_len);
287 : : }
288 : :
289 : 0 : sg_hdr[0] = 0;
290 : 0 : sg_hdr[1] = 0;
291 : 0 : i = fill_sg_comp_from_pkt(gather_comp, i, m_src);
292 [ # # ]: 0 : sg_hdr[2] = rte_cpu_to_be_16(i);
293 : :
294 : 0 : g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
295 : :
296 : : i = 0;
297 : 0 : scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
298 : :
299 : 0 : i = fill_sg_comp_from_pkt(scatter_comp, i, m_src);
300 [ # # ]: 0 : sg_hdr[3] = rte_cpu_to_be_16(i);
301 : :
302 : 0 : s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
303 : :
304 : 0 : dlen = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
305 : :
306 : 0 : inst->dptr = (uint64_t)in_buffer;
307 : 0 : inst->rptr = (uint64_t)in_buffer;
308 : :
309 : 0 : w4.u64 = sess->inst.w4;
310 : 0 : w4.s.dlen = dlen;
311 : 0 : w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
312 : 0 : w4.s.param1 = pkt_len;
313 : 0 : inst->w4.u64 = w4.u64;
314 : : } else {
315 : : struct roc_sg2list_comp *scatter_comp, *gather_comp;
316 : 0 : int tail_len = sess->tls_opt.tail_fetch_len * 16;
317 : 0 : int pkt_len = rte_pktmbuf_pkt_len(m_src);
318 : : union cpt_inst_w5 cpt_inst_w5;
319 : : union cpt_inst_w6 cpt_inst_w6;
320 : : uint32_t g_size_bytes;
321 : : int i;
322 : :
323 [ # # ]: 0 : if (unlikely(m_src->nb_segs > ROC_SG2_MAX_PTRS)) {
324 : 0 : plt_dp_err("Exceeds max supported components. Reduce segments");
325 : 0 : return -1;
326 : : }
327 : :
328 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
329 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
330 : 0 : plt_dp_err("Error allocating meta buffer for request");
331 : 0 : return -ENOMEM;
332 : : }
333 : :
334 : : in_buffer = (uint8_t *)m_data;
335 : : /* Input Gather List */
336 : : i = 0;
337 : :
338 : : /* First 32 bytes in m_data are rsvd for tail fetch.
339 : : * SG list start from 32 byte onwards.
340 : : */
341 : 0 : gather_comp = (struct roc_sg2list_comp *)((uint8_t *)(in_buffer + 32));
342 : :
343 : : /* Add the last blocks as first gather component for tail fetch. */
344 [ # # ]: 0 : if (tail_len) {
345 : : const uint8_t *output;
346 : :
347 : 0 : output = rte_pktmbuf_read(m_src, pkt_len - tail_len, tail_len, in_buffer);
348 [ # # ]: 0 : if (output != in_buffer)
349 [ # # ]: 0 : rte_memcpy(in_buffer, output, tail_len);
350 : 0 : i = fill_sg2_comp(gather_comp, i, (uint64_t)in_buffer, tail_len);
351 : : }
352 : :
353 : 0 : i = fill_sg2_comp_from_pkt(gather_comp, i, m_src);
354 : :
355 : 0 : cpt_inst_w5.s.gather_sz = ((i + 2) / 3);
356 : 0 : g_size_bytes = ((i + 2) / 3) * sizeof(struct roc_sg2list_comp);
357 : :
358 : : i = 0;
359 : 0 : scatter_comp = (struct roc_sg2list_comp *)((uint8_t *)gather_comp + g_size_bytes);
360 : :
361 [ # # ]: 0 : if (m_dst == NULL)
362 : : m_dst = m_src;
363 : 0 : i = fill_sg2_comp_from_pkt(scatter_comp, i, m_dst);
364 : :
365 : 0 : cpt_inst_w6.s.scatter_sz = ((i + 2) / 3);
366 : :
367 : 0 : cpt_inst_w5.s.dptr = (uint64_t)gather_comp;
368 : 0 : cpt_inst_w6.s.rptr = (uint64_t)scatter_comp;
369 : :
370 : 0 : inst->w5.u64 = cpt_inst_w5.u64;
371 : 0 : inst->w6.u64 = cpt_inst_w6.u64;
372 : 0 : w4.u64 = sess->inst.w4;
373 : 0 : w4.s.dlen = pkt_len + tail_len;
374 : 0 : w4.s.param1 = w4.s.dlen;
375 : 0 : w4.s.opcode_major &= (~(ROC_IE_OT_INPLACE_BIT));
376 : 0 : inst->w4.u64 = w4.u64;
377 : : }
378 : :
379 : : return 0;
380 : : }
381 : : #endif /* __CN10K_TLS_OPS_H__ */
|