crypto/cnxk: support lookaside IPsec AES-CTR
[dpdk.git] / drivers / crypto / cnxk / cn10k_ipsec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <cryptodev_pmd.h>
6 #include <rte_esp.h>
7 #include <rte_ip.h>
8 #include <rte_malloc.h>
9 #include <rte_security.h>
10 #include <rte_security_driver.h>
11 #include <rte_udp.h>
12
13 #include "cn10k_ipsec.h"
14 #include "cnxk_cryptodev.h"
15 #include "cnxk_cryptodev_ops.h"
16 #include "cnxk_ipsec.h"
17 #include "cnxk_security.h"
18
19 #include "roc_api.h"
20
21 static uint64_t
22 ipsec_cpt_inst_w7_get(struct roc_cpt *roc_cpt, void *sa)
23 {
24         union cpt_inst_w7 w7;
25
26         w7.u64 = 0;
27         w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
28         w7.s.ctx_val = 1;
29         w7.s.cptr = (uint64_t)sa;
30         rte_mb();
31
32         return w7.u64;
33 }
34
35 static int
36 cn10k_ipsec_outb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
37                            struct rte_security_ipsec_xform *ipsec_xfrm,
38                            struct rte_crypto_sym_xform *crypto_xfrm,
39                            struct rte_security_session *sec_sess)
40 {
41         union roc_ot_ipsec_outb_param1 param1;
42         struct roc_ot_ipsec_outb_sa *sa_dptr;
43         struct cnxk_ipsec_outb_rlens rlens;
44         struct cn10k_sec_session *sess;
45         struct cn10k_ipsec_sa *sa;
46         union cpt_inst_w4 inst_w4;
47         void *out_sa;
48         int ret = 0;
49
50         sess = get_sec_session_private_data(sec_sess);
51         sa = &sess->sa;
52         out_sa = &sa->out_sa;
53
54         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
55         sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_outb_sa), 8);
56         if (sa_dptr == NULL) {
57                 plt_err("Couldn't allocate memory for SA dptr");
58                 return -ENOMEM;
59         }
60
61         /* Translate security parameters to SA */
62         ret = cnxk_ot_ipsec_outb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm);
63         if (ret) {
64                 plt_err("Could not fill outbound session parameters");
65                 goto sa_dptr_free;
66         }
67
68         sa->inst.w7 = ipsec_cpt_inst_w7_get(roc_cpt, out_sa);
69
70 #ifdef LA_IPSEC_DEBUG
71         /* Use IV from application in debug mode */
72         if (ipsec_xfrm->options.iv_gen_disable == 1) {
73                 sa_dptr->w2.s.iv_src = ROC_IE_OT_SA_IV_SRC_FROM_SA;
74                 if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
75                         sa->iv_offset = crypto_xfrm->aead.iv.offset;
76                         sa->iv_length = crypto_xfrm->aead.iv.length;
77                 } else {
78                         sa->iv_offset = crypto_xfrm->cipher.iv.offset;
79                         sa->iv_length = crypto_xfrm->cipher.iv.length;
80                 }
81         }
82 #else
83         if (ipsec_xfrm->options.iv_gen_disable != 0) {
84                 plt_err("Application provided IV not supported");
85                 ret = -ENOTSUP;
86                 goto sa_dptr_free;
87         }
88 #endif
89
90         sa->is_outbound = true;
91
92         /* Get Rlen calculation data */
93         ret = cnxk_ipsec_outb_rlens_get(&rlens, ipsec_xfrm, crypto_xfrm);
94         if (ret)
95                 goto sa_dptr_free;
96
97         sa->max_extended_len = rlens.max_extended_len;
98
99         /* pre-populate CPT INST word 4 */
100         inst_w4.u64 = 0;
101         inst_w4.s.opcode_major = ROC_IE_OT_MAJOR_OP_PROCESS_OUTBOUND_IPSEC;
102
103         param1.u16 = 0;
104
105         /* Disable IP checksum computation by default */
106         param1.s.ip_csum_disable = ROC_IE_OT_SA_INNER_PKT_IP_CSUM_DISABLE;
107
108         if (ipsec_xfrm->options.ip_csum_enable) {
109                 param1.s.ip_csum_disable =
110                         ROC_IE_OT_SA_INNER_PKT_IP_CSUM_ENABLE;
111         }
112
113         /* Disable L4 checksum computation by default */
114         param1.s.l4_csum_disable = ROC_IE_OT_SA_INNER_PKT_L4_CSUM_DISABLE;
115
116         if (ipsec_xfrm->options.l4_csum_enable) {
117                 param1.s.l4_csum_disable =
118                         ROC_IE_OT_SA_INNER_PKT_L4_CSUM_ENABLE;
119         }
120
121         inst_w4.s.param1 = param1.u16;
122
123         sa->inst.w4 = inst_w4.u64;
124
125         if (ipsec_xfrm->options.stats == 1) {
126                 /* Enable mib counters */
127                 sa_dptr->w0.s.count_mib_bytes = 1;
128                 sa_dptr->w0.s.count_mib_pkts = 1;
129         }
130
131         memset(out_sa, 0, sizeof(struct roc_ot_ipsec_outb_sa));
132
133         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
134         memcpy(out_sa, sa_dptr, 8);
135
136         plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
137
138         /* Write session using microcode opcode */
139         ret = roc_cpt_ctx_write(lf, sa_dptr, out_sa,
140                                 sizeof(struct roc_ot_ipsec_outb_sa));
141         if (ret) {
142                 plt_err("Could not write outbound session to hardware");
143                 goto sa_dptr_free;
144         }
145
146         /* Trigger CTX flush so that data is written back to DRAM */
147         roc_cpt_lf_ctx_flush(lf, out_sa, false);
148
149         plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
150
151 sa_dptr_free:
152         plt_free(sa_dptr);
153
154         return ret;
155 }
156
157 static int
158 cn10k_ipsec_inb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
159                           struct rte_security_ipsec_xform *ipsec_xfrm,
160                           struct rte_crypto_sym_xform *crypto_xfrm,
161                           struct rte_security_session *sec_sess)
162 {
163         union roc_ot_ipsec_inb_param1 param1;
164         struct roc_ot_ipsec_inb_sa *sa_dptr;
165         struct cn10k_sec_session *sess;
166         struct cn10k_ipsec_sa *sa;
167         union cpt_inst_w4 inst_w4;
168         void *in_sa;
169         int ret = 0;
170
171         sess = get_sec_session_private_data(sec_sess);
172         sa = &sess->sa;
173         in_sa = &sa->in_sa;
174
175         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
176         sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_inb_sa), 8);
177         if (sa_dptr == NULL) {
178                 plt_err("Couldn't allocate memory for SA dptr");
179                 return -ENOMEM;
180         }
181
182         /* Translate security parameters to SA */
183         ret = cnxk_ot_ipsec_inb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm);
184         if (ret) {
185                 plt_err("Could not fill inbound session parameters");
186                 goto sa_dptr_free;
187         }
188
189         sa->is_outbound = false;
190         sa->inst.w7 = ipsec_cpt_inst_w7_get(roc_cpt, in_sa);
191
192         /* pre-populate CPT INST word 4 */
193         inst_w4.u64 = 0;
194         inst_w4.s.opcode_major = ROC_IE_OT_MAJOR_OP_PROCESS_INBOUND_IPSEC;
195
196         param1.u16 = 0;
197
198         /* Disable IP checksum verification by default */
199         param1.s.ip_csum_disable = ROC_IE_OT_SA_INNER_PKT_IP_CSUM_DISABLE;
200
201         if (ipsec_xfrm->options.ip_csum_enable) {
202                 param1.s.ip_csum_disable =
203                         ROC_IE_OT_SA_INNER_PKT_IP_CSUM_ENABLE;
204                 sa->ip_csum_enable = true;
205         }
206
207         /* Disable L4 checksum verification by default */
208         param1.s.l4_csum_disable = ROC_IE_OT_SA_INNER_PKT_L4_CSUM_DISABLE;
209
210         if (ipsec_xfrm->options.l4_csum_enable) {
211                 param1.s.l4_csum_disable =
212                         ROC_IE_OT_SA_INNER_PKT_L4_CSUM_ENABLE;
213         }
214
215         param1.s.esp_trailer_disable = 1;
216
217         inst_w4.s.param1 = param1.u16;
218
219         sa->inst.w4 = inst_w4.u64;
220
221         if (ipsec_xfrm->options.stats == 1) {
222                 /* Enable mib counters */
223                 sa_dptr->w0.s.count_mib_bytes = 1;
224                 sa_dptr->w0.s.count_mib_pkts = 1;
225         }
226
227         memset(in_sa, 0, sizeof(struct roc_ot_ipsec_inb_sa));
228
229         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
230         memcpy(in_sa, sa_dptr, 8);
231
232         plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
233
234         /* Write session using microcode opcode */
235         ret = roc_cpt_ctx_write(lf, sa_dptr, in_sa,
236                                 sizeof(struct roc_ot_ipsec_inb_sa));
237         if (ret) {
238                 plt_err("Could not write inbound session to hardware");
239                 goto sa_dptr_free;
240         }
241
242         /* Trigger CTX flush so that data is written back to DRAM */
243         roc_cpt_lf_ctx_flush(lf, in_sa, false);
244
245         plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
246
247 sa_dptr_free:
248         plt_free(sa_dptr);
249
250         return ret;
251 }
252
253 static int
254 cn10k_ipsec_session_create(void *dev,
255                            struct rte_security_ipsec_xform *ipsec_xfrm,
256                            struct rte_crypto_sym_xform *crypto_xfrm,
257                            struct rte_security_session *sess)
258 {
259         struct rte_cryptodev *crypto_dev = dev;
260         struct roc_cpt *roc_cpt;
261         struct cnxk_cpt_vf *vf;
262         struct cnxk_cpt_qp *qp;
263         int ret;
264
265         qp = crypto_dev->data->queue_pairs[0];
266         if (qp == NULL) {
267                 plt_err("Setup cpt queue pair before creating security session");
268                 return -EPERM;
269         }
270
271         ret = cnxk_ipsec_xform_verify(ipsec_xfrm, crypto_xfrm);
272         if (ret)
273                 return ret;
274
275         vf = crypto_dev->data->dev_private;
276         roc_cpt = &vf->cpt;
277
278         if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
279                 return cn10k_ipsec_inb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm,
280                                                  crypto_xfrm, sess);
281         else
282                 return cn10k_ipsec_outb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm,
283                                                   crypto_xfrm, sess);
284 }
285
286 static int
287 cn10k_sec_session_create(void *device, struct rte_security_session_conf *conf,
288                          struct rte_security_session *sess,
289                          struct rte_mempool *mempool)
290 {
291         struct cn10k_sec_session *priv;
292         int ret;
293
294         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
295                 return -EINVAL;
296
297         if (rte_mempool_get(mempool, (void **)&priv)) {
298                 plt_err("Could not allocate security session private data");
299                 return -ENOMEM;
300         }
301
302         set_sec_session_private_data(sess, priv);
303
304         if (conf->protocol != RTE_SECURITY_PROTOCOL_IPSEC) {
305                 ret = -ENOTSUP;
306                 goto mempool_put;
307         }
308         ret = cn10k_ipsec_session_create(device, &conf->ipsec,
309                                          conf->crypto_xform, sess);
310         if (ret)
311                 goto mempool_put;
312
313         return 0;
314
315 mempool_put:
316         rte_mempool_put(mempool, priv);
317         set_sec_session_private_data(sess, NULL);
318         return ret;
319 }
320
321 static int
322 cn10k_sec_session_destroy(void *dev, struct rte_security_session *sec_sess)
323 {
324         struct rte_cryptodev *crypto_dev = dev;
325         union roc_ot_ipsec_sa_word2 *w2;
326         struct cn10k_sec_session *sess;
327         struct rte_mempool *sess_mp;
328         struct cn10k_ipsec_sa *sa;
329         struct cnxk_cpt_qp *qp;
330         struct roc_cpt_lf *lf;
331
332         sess = get_sec_session_private_data(sec_sess);
333         if (sess == NULL)
334                 return 0;
335
336         qp = crypto_dev->data->queue_pairs[0];
337         if (qp == NULL)
338                 return 0;
339
340         lf = &qp->lf;
341
342         sa = &sess->sa;
343
344         /* Trigger CTX flush to write dirty data back to DRAM */
345         roc_cpt_lf_ctx_flush(lf, &sa->in_sa, false);
346
347         /* Wait for 1 ms so that flush is complete */
348         rte_delay_ms(1);
349
350         w2 = (union roc_ot_ipsec_sa_word2 *)&sa->in_sa.w2;
351         w2->s.valid = 0;
352
353         plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
354
355         /* Trigger CTX reload to fetch new data from DRAM */
356         roc_cpt_lf_ctx_reload(lf, &sa->in_sa);
357
358         sess_mp = rte_mempool_from_obj(sess);
359
360         set_sec_session_private_data(sec_sess, NULL);
361         rte_mempool_put(sess_mp, sess);
362
363         return 0;
364 }
365
366 static unsigned int
367 cn10k_sec_session_get_size(void *device __rte_unused)
368 {
369         return sizeof(struct cn10k_sec_session);
370 }
371
372 static int
373 cn10k_sec_session_stats_get(void *device, struct rte_security_session *sess,
374                             struct rte_security_stats *stats)
375 {
376         struct rte_cryptodev *crypto_dev = device;
377         struct roc_ot_ipsec_outb_sa *out_sa;
378         struct roc_ot_ipsec_inb_sa *in_sa;
379         union roc_ot_ipsec_sa_word2 *w2;
380         struct cn10k_sec_session *priv;
381         struct cn10k_ipsec_sa *sa;
382         struct cnxk_cpt_qp *qp;
383
384         priv = get_sec_session_private_data(sess);
385         if (priv == NULL)
386                 return -EINVAL;
387
388         qp = crypto_dev->data->queue_pairs[0];
389         if (qp == NULL)
390                 return -EINVAL;
391
392         sa = &priv->sa;
393         w2 = (union roc_ot_ipsec_sa_word2 *)&sa->in_sa.w2;
394
395         stats->protocol = RTE_SECURITY_PROTOCOL_IPSEC;
396
397         if (w2->s.dir == ROC_IE_SA_DIR_OUTBOUND) {
398                 out_sa = &sa->out_sa;
399                 roc_cpt_lf_ctx_flush(&qp->lf, out_sa, false);
400                 rte_delay_ms(1);
401                 stats->ipsec.opackets = out_sa->ctx.mib_pkts;
402                 stats->ipsec.obytes = out_sa->ctx.mib_octs;
403         } else {
404                 in_sa = &sa->in_sa;
405                 roc_cpt_lf_ctx_flush(&qp->lf, in_sa, false);
406                 rte_delay_ms(1);
407                 stats->ipsec.ipackets = in_sa->ctx.mib_pkts;
408                 stats->ipsec.ibytes = in_sa->ctx.mib_octs;
409         }
410
411         return 0;
412 }
413
414 /* Update platform specific security ops */
415 void
416 cn10k_sec_ops_override(void)
417 {
418         /* Update platform specific ops */
419         cnxk_sec_ops.session_create = cn10k_sec_session_create;
420         cnxk_sec_ops.session_destroy = cn10k_sec_session_destroy;
421         cnxk_sec_ops.session_get_size = cn10k_sec_session_get_size;
422         cnxk_sec_ops.session_stats_get = cn10k_sec_session_stats_get;
423 }