net/txgbe: fix queue statistics mapping
[dpdk.git] / drivers / crypto / cnxk / cn9k_ipsec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <cryptodev_pmd.h>
6 #include <rte_ip.h>
7 #include <rte_security.h>
8 #include <rte_security_driver.h>
9
10 #include "cnxk_cryptodev.h"
11 #include "cnxk_cryptodev_ops.h"
12 #include "cnxk_ipsec.h"
13 #include "cnxk_security.h"
14 #include "cn9k_ipsec.h"
15
16 #include "roc_api.h"
17
18 static inline int
19 cn9k_cpt_enq_sa_write(struct cn9k_ipsec_sa *sa, struct cnxk_cpt_qp *qp,
20                       uint8_t opcode, size_t ctx_len)
21 {
22         struct roc_cpt *roc_cpt = qp->lf.roc_cpt;
23         uint64_t lmtline = qp->lmtline.lmt_base;
24         uint64_t io_addr = qp->lmtline.io_addr;
25         uint64_t lmt_status, time_out;
26         struct cpt_cn9k_res_s *res;
27         struct cpt_inst_s inst;
28         uint64_t *mdata;
29         int ret = 0;
30
31         if (unlikely(rte_mempool_get(qp->meta_info.pool, (void **)&mdata) < 0))
32                 return -ENOMEM;
33
34         res = (struct cpt_cn9k_res_s *)RTE_PTR_ALIGN(mdata, 16);
35         res->compcode = CPT_COMP_NOT_DONE;
36
37         inst.w4.s.opcode_major = opcode;
38         inst.w4.s.opcode_minor = ctx_len >> 3;
39         inst.w4.s.param1 = 0;
40         inst.w4.s.param2 = 0;
41         inst.w4.s.dlen = ctx_len;
42         inst.dptr = rte_mempool_virt2iova(sa);
43         inst.rptr = 0;
44         inst.w7.s.cptr = rte_mempool_virt2iova(sa);
45         inst.w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
46
47         inst.w0.u64 = 0;
48         inst.w2.u64 = 0;
49         inst.w3.u64 = 0;
50         inst.res_addr = rte_mempool_virt2iova(res);
51
52         rte_io_wmb();
53
54         do {
55                 /* Copy CPT command to LMTLINE */
56                 roc_lmt_mov64((void *)lmtline, &inst);
57                 lmt_status = roc_lmt_submit_ldeor(io_addr);
58         } while (lmt_status == 0);
59
60         time_out = rte_get_timer_cycles() +
61                    DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
62
63         while (res->compcode == CPT_COMP_NOT_DONE) {
64                 if (rte_get_timer_cycles() > time_out) {
65                         rte_mempool_put(qp->meta_info.pool, mdata);
66                         plt_err("Request timed out");
67                         return -ETIMEDOUT;
68                 }
69                 rte_io_rmb();
70         }
71
72         if (unlikely(res->compcode != CPT_COMP_GOOD)) {
73                 ret = res->compcode;
74                 switch (ret) {
75                 case CPT_COMP_INSTERR:
76                         plt_err("Request failed with instruction error");
77                         break;
78                 case CPT_COMP_FAULT:
79                         plt_err("Request failed with DMA fault");
80                         break;
81                 case CPT_COMP_HWERR:
82                         plt_err("Request failed with hardware error");
83                         break;
84                 default:
85                         plt_err("Request failed with unknown hardware "
86                                 "completion code : 0x%x",
87                                 ret);
88                 }
89                 ret = -EINVAL;
90                 goto mempool_put;
91         }
92
93         if (unlikely(res->uc_compcode != ROC_IE_ON_UCC_SUCCESS)) {
94                 ret = res->uc_compcode;
95                 switch (ret) {
96                 case ROC_IE_ON_AUTH_UNSUPPORTED:
97                         plt_err("Invalid auth type");
98                         break;
99                 case ROC_IE_ON_ENCRYPT_UNSUPPORTED:
100                         plt_err("Invalid encrypt type");
101                         break;
102                 default:
103                         plt_err("Request failed with unknown microcode "
104                                 "completion code : 0x%x",
105                                 ret);
106                 }
107                 ret = -ENOTSUP;
108         }
109
110 mempool_put:
111         rte_mempool_put(qp->meta_info.pool, mdata);
112         return ret;
113 }
114
115 static inline int
116 ipsec_sa_ctl_set(struct rte_security_ipsec_xform *ipsec,
117                  struct rte_crypto_sym_xform *crypto_xform,
118                  struct roc_ie_on_sa_ctl *ctl)
119 {
120         struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
121         int aes_key_len = 0;
122
123         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
124                 ctl->direction = ROC_IE_SA_DIR_OUTBOUND;
125                 cipher_xform = crypto_xform;
126                 auth_xform = crypto_xform->next;
127         } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
128                 ctl->direction = ROC_IE_SA_DIR_INBOUND;
129                 auth_xform = crypto_xform;
130                 cipher_xform = crypto_xform->next;
131         } else {
132                 return -EINVAL;
133         }
134
135         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
136                 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
137                         ctl->outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
138                 else if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV6)
139                         ctl->outer_ip_ver = ROC_IE_SA_IP_VERSION_6;
140                 else
141                         return -EINVAL;
142         }
143
144         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT) {
145                 ctl->ipsec_mode = ROC_IE_SA_MODE_TRANSPORT;
146                 ctl->outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
147         } else if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
148                 ctl->ipsec_mode = ROC_IE_SA_MODE_TUNNEL;
149         else
150                 return -EINVAL;
151
152         if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH)
153                 ctl->ipsec_proto = ROC_IE_SA_PROTOCOL_AH;
154         else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP)
155                 ctl->ipsec_proto = ROC_IE_SA_PROTOCOL_ESP;
156         else
157                 return -EINVAL;
158
159         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
160                 switch (crypto_xform->aead.algo) {
161                 case RTE_CRYPTO_AEAD_AES_GCM:
162                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_GCM;
163                         aes_key_len = crypto_xform->aead.key.length;
164                         break;
165                 default:
166                         plt_err("Unsupported AEAD algorithm");
167                         return -ENOTSUP;
168                 }
169         } else {
170                 switch (cipher_xform->cipher.algo) {
171                 case RTE_CRYPTO_CIPHER_NULL:
172                         ctl->enc_type = ROC_IE_ON_SA_ENC_NULL;
173                         break;
174                 case RTE_CRYPTO_CIPHER_AES_CBC:
175                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CBC;
176                         aes_key_len = cipher_xform->cipher.key.length;
177                         break;
178                 case RTE_CRYPTO_CIPHER_AES_CTR:
179                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CTR;
180                         aes_key_len = cipher_xform->cipher.key.length;
181                         break;
182                 default:
183                         plt_err("Unsupported cipher algorithm");
184                         return -ENOTSUP;
185                 }
186
187                 switch (auth_xform->auth.algo) {
188                 case RTE_CRYPTO_AUTH_NULL:
189                         ctl->auth_type = ROC_IE_ON_SA_AUTH_NULL;
190                         break;
191                 case RTE_CRYPTO_AUTH_MD5_HMAC:
192                         ctl->auth_type = ROC_IE_ON_SA_AUTH_MD5;
193                         break;
194                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
195                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA1;
196                         break;
197                 case RTE_CRYPTO_AUTH_SHA224_HMAC:
198                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA2_224;
199                         break;
200                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
201                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA2_256;
202                         break;
203                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
204                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA2_384;
205                         break;
206                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
207                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA2_512;
208                         break;
209                 case RTE_CRYPTO_AUTH_AES_GMAC:
210                         ctl->auth_type = ROC_IE_ON_SA_AUTH_AES_GMAC;
211                         break;
212                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
213                         ctl->auth_type = ROC_IE_ON_SA_AUTH_AES_XCBC_128;
214                         break;
215                 default:
216                         plt_err("Unsupported auth algorithm");
217                         return -ENOTSUP;
218                 }
219         }
220
221         /* Set AES key length */
222         if (ctl->enc_type == ROC_IE_ON_SA_ENC_AES_CBC ||
223             ctl->enc_type == ROC_IE_ON_SA_ENC_AES_CCM ||
224             ctl->enc_type == ROC_IE_ON_SA_ENC_AES_CTR ||
225             ctl->enc_type == ROC_IE_ON_SA_ENC_AES_GCM ||
226             ctl->auth_type == ROC_IE_ON_SA_AUTH_AES_GMAC) {
227                 switch (aes_key_len) {
228                 case 16:
229                         ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_128;
230                         break;
231                 case 24:
232                         ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_192;
233                         break;
234                 case 32:
235                         ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_256;
236                         break;
237                 default:
238                         plt_err("Invalid AES key length");
239                         return -EINVAL;
240                 }
241         }
242
243         if (ipsec->options.esn)
244                 ctl->esn_en = 1;
245
246         if (ipsec->options.udp_encap == 1)
247                 ctl->encap_type = ROC_IE_ON_SA_ENCAP_UDP;
248
249         ctl->copy_df = ipsec->options.copy_df;
250
251         ctl->spi = rte_cpu_to_be_32(ipsec->spi);
252
253         rte_io_wmb();
254
255         ctl->valid = 1;
256
257         return 0;
258 }
259
260 static inline int
261 fill_ipsec_common_sa(struct rte_security_ipsec_xform *ipsec,
262                      struct rte_crypto_sym_xform *crypto_xform,
263                      struct roc_ie_on_common_sa *common_sa)
264 {
265         struct rte_crypto_sym_xform *cipher_xform;
266         const uint8_t *cipher_key;
267         int cipher_key_len = 0;
268         int ret;
269
270         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
271                 cipher_xform = crypto_xform->next;
272         else
273                 cipher_xform = crypto_xform;
274
275         ret = ipsec_sa_ctl_set(ipsec, crypto_xform, &common_sa->ctl);
276         if (ret)
277                 return ret;
278
279         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
280                 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
281                         memcpy(common_sa->iv.gcm.nonce, &ipsec->salt, 4);
282                 cipher_key = crypto_xform->aead.key.data;
283                 cipher_key_len = crypto_xform->aead.key.length;
284         } else {
285                 cipher_key = cipher_xform->cipher.key.data;
286                 cipher_key_len = cipher_xform->cipher.key.length;
287         }
288
289         if (cipher_key_len != 0)
290                 memcpy(common_sa->cipher_key, cipher_key, cipher_key_len);
291
292         if (ipsec->esn.value) {
293                 common_sa->esn_low = ipsec->esn.low;
294                 common_sa->esn_hi = ipsec->esn.hi;
295         }
296
297         return 0;
298 }
299
300 static int
301 cn9k_ipsec_outb_sa_create(struct cnxk_cpt_qp *qp,
302                           struct rte_security_ipsec_xform *ipsec,
303                           struct rte_crypto_sym_xform *crypto_xform,
304                           struct rte_security_session *sec_sess)
305 {
306         struct rte_crypto_sym_xform *auth_xform = crypto_xform->next;
307         struct roc_ie_on_ip_template *template = NULL;
308         struct roc_cpt *roc_cpt = qp->lf.roc_cpt;
309         union roc_on_ipsec_outb_param1 param1;
310         struct cnxk_cpt_inst_tmpl *inst_tmpl;
311         struct roc_ie_on_outb_sa *out_sa;
312         struct cn9k_sec_session *sess;
313         struct roc_ie_on_sa_ctl *ctl;
314         struct cn9k_ipsec_sa *sa;
315         struct rte_ipv6_hdr *ip6;
316         struct rte_ipv4_hdr *ip4;
317         const uint8_t *auth_key;
318         union cpt_inst_w4 w4;
319         union cpt_inst_w7 w7;
320         int auth_key_len = 0;
321         size_t ctx_len;
322         int ret;
323
324         sess = get_sec_session_private_data(sec_sess);
325         sa = &sess->sa;
326         out_sa = &sa->out_sa;
327         ctl = &out_sa->common_sa.ctl;
328
329         memset(sa, 0, sizeof(struct cn9k_ipsec_sa));
330
331         /* Initialize lookaside IPsec private data */
332         sa->dir = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
333         /* Start ip id from 1 */
334         sa->ip_id = 1;
335         sa->seq_lo = 1;
336         sa->seq_hi = 0;
337
338         if (ipsec->esn.value)
339                 sa->esn = ipsec->esn.value;
340
341         ret = fill_ipsec_common_sa(ipsec, crypto_xform, &out_sa->common_sa);
342         if (ret)
343                 return ret;
344
345         ret = cnxk_ipsec_outb_rlens_get(&sa->rlens, ipsec, crypto_xform);
346         if (ret)
347                 return ret;
348
349         if (ctl->enc_type == ROC_IE_ON_SA_ENC_AES_GCM ||
350             ctl->auth_type == ROC_IE_ON_SA_AUTH_NULL) {
351                 template = &out_sa->aes_gcm.template;
352                 ctx_len = offsetof(struct roc_ie_on_outb_sa, aes_gcm.template);
353         } else {
354                 switch (ctl->auth_type) {
355                 case ROC_IE_ON_SA_AUTH_SHA1:
356                         template = &out_sa->sha1.template;
357                         ctx_len = offsetof(struct roc_ie_on_outb_sa,
358                                            sha1.template);
359                         break;
360                 case ROC_IE_ON_SA_AUTH_SHA2_256:
361                 case ROC_IE_ON_SA_AUTH_SHA2_384:
362                 case ROC_IE_ON_SA_AUTH_SHA2_512:
363                         template = &out_sa->sha2.template;
364                         ctx_len = offsetof(struct roc_ie_on_outb_sa,
365                                            sha2.template);
366                         break;
367                 case ROC_IE_ON_SA_AUTH_AES_XCBC_128:
368                         template = &out_sa->aes_xcbc.template;
369                         ctx_len = offsetof(struct roc_ie_on_outb_sa,
370                                            aes_xcbc.template);
371                         break;
372                 default:
373                         plt_err("Unsupported auth algorithm");
374                         return -EINVAL;
375                 }
376         }
377
378         ip4 = (struct rte_ipv4_hdr *)&template->ip4.ipv4_hdr;
379         if (ipsec->options.udp_encap) {
380                 ip4->next_proto_id = IPPROTO_UDP;
381                 template->ip4.udp_src = rte_be_to_cpu_16(4500);
382                 template->ip4.udp_dst = rte_be_to_cpu_16(4500);
383         } else {
384                 ip4->next_proto_id = IPPROTO_ESP;
385         }
386
387         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
388                 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
389                         uint16_t frag_off = 0;
390                         ctx_len += sizeof(template->ip4);
391
392                         ip4->version_ihl = RTE_IPV4_VHL_DEF;
393                         ip4->time_to_live = ipsec->tunnel.ipv4.ttl;
394                         ip4->type_of_service |= (ipsec->tunnel.ipv4.dscp << 2);
395                         if (ipsec->tunnel.ipv4.df)
396                                 frag_off |= RTE_IPV4_HDR_DF_FLAG;
397                         ip4->fragment_offset = rte_cpu_to_be_16(frag_off);
398
399                         memcpy(&ip4->src_addr, &ipsec->tunnel.ipv4.src_ip,
400                                sizeof(struct in_addr));
401                         memcpy(&ip4->dst_addr, &ipsec->tunnel.ipv4.dst_ip,
402                                sizeof(struct in_addr));
403                 } else if (ipsec->tunnel.type ==
404                            RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
405                         ctx_len += sizeof(template->ip6);
406
407                         ip6 = (struct rte_ipv6_hdr *)&template->ip6.ipv6_hdr;
408                         if (ipsec->options.udp_encap) {
409                                 ip6->proto = IPPROTO_UDP;
410                                 template->ip6.udp_src = rte_be_to_cpu_16(4500);
411                                 template->ip6.udp_dst = rte_be_to_cpu_16(4500);
412                         } else {
413                                 ip6->proto = (ipsec->proto ==
414                                               RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
415                                                      IPPROTO_ESP :
416                                                      IPPROTO_AH;
417                         }
418                         ip6->vtc_flow =
419                                 rte_cpu_to_be_32(0x60000000 |
420                                                  ((ipsec->tunnel.ipv6.dscp
421                                                    << RTE_IPV6_HDR_TC_SHIFT) &
422                                                   RTE_IPV6_HDR_TC_MASK) |
423                                                  ((ipsec->tunnel.ipv6.flabel
424                                                    << RTE_IPV6_HDR_FL_SHIFT) &
425                                                   RTE_IPV6_HDR_FL_MASK));
426                         ip6->hop_limits = ipsec->tunnel.ipv6.hlimit;
427                         memcpy(&ip6->src_addr, &ipsec->tunnel.ipv6.src_addr,
428                                sizeof(struct in6_addr));
429                         memcpy(&ip6->dst_addr, &ipsec->tunnel.ipv6.dst_addr,
430                                sizeof(struct in6_addr));
431                 }
432         } else
433                 ctx_len += sizeof(template->ip4);
434
435         ctx_len += RTE_ALIGN_CEIL(ctx_len, 8);
436
437         if (crypto_xform->type != RTE_CRYPTO_SYM_XFORM_AEAD) {
438                 auth_key = auth_xform->auth.key.data;
439                 auth_key_len = auth_xform->auth.key.length;
440
441                 switch (auth_xform->auth.algo) {
442                 case RTE_CRYPTO_AUTH_NULL:
443                         break;
444                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
445                         memcpy(out_sa->sha1.hmac_key, auth_key, auth_key_len);
446                         break;
447                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
448                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
449                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
450                         memcpy(out_sa->sha2.hmac_key, auth_key, auth_key_len);
451                         break;
452                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
453                         memcpy(out_sa->aes_xcbc.key, auth_key, auth_key_len);
454                         break;
455                 default:
456                         plt_err("Unsupported auth algorithm %u",
457                                 auth_xform->auth.algo);
458                         return -ENOTSUP;
459                 }
460         }
461
462         inst_tmpl = &sa->inst;
463
464         w4.u64 = 0;
465         w4.s.opcode_major = ROC_IE_ON_MAJOR_OP_PROCESS_OUTBOUND_IPSEC;
466         w4.s.opcode_minor = ctx_len >> 3;
467
468         param1.u16 = 0;
469         param1.s.ikev2 = 1;
470
471         sa->custom_hdr_len = sizeof(struct roc_ie_on_outb_hdr) -
472                              ROC_IE_ON_MAX_IV_LEN;
473
474 #ifdef LA_IPSEC_DEBUG
475         /* Use IV from application in debug mode */
476         if (ipsec->options.iv_gen_disable == 1) {
477                 param1.s.per_pkt_iv = ROC_IE_ON_IV_SRC_FROM_DPTR;
478                 sa->custom_hdr_len = sizeof(struct roc_ie_on_outb_hdr);
479
480                 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
481                         sa->cipher_iv_off = crypto_xform->aead.iv.offset;
482                         sa->cipher_iv_len = crypto_xform->aead.iv.length;
483                 } else {
484                         sa->cipher_iv_off = crypto_xform->cipher.iv.offset;
485                         sa->cipher_iv_len = crypto_xform->cipher.iv.length;
486                 }
487         }
488 #else
489         if (ipsec->options.iv_gen_disable != 0) {
490                 plt_err("Application provided IV is not supported");
491                 return -ENOTSUP;
492         }
493 #endif
494
495         w4.s.param1 = param1.u16;
496
497         inst_tmpl->w4 = w4.u64;
498
499         w7.u64 = 0;
500         w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
501         w7.s.cptr = rte_mempool_virt2iova(out_sa);
502         inst_tmpl->w7 = w7.u64;
503
504         return cn9k_cpt_enq_sa_write(
505                 sa, qp, ROC_IE_ON_MAJOR_OP_WRITE_IPSEC_OUTBOUND, ctx_len);
506 }
507
508 static int
509 cn9k_ipsec_inb_sa_create(struct cnxk_cpt_qp *qp,
510                          struct rte_security_ipsec_xform *ipsec,
511                          struct rte_crypto_sym_xform *crypto_xform,
512                          struct rte_security_session *sec_sess)
513 {
514         struct rte_crypto_sym_xform *auth_xform = crypto_xform;
515         struct roc_cpt *roc_cpt = qp->lf.roc_cpt;
516         union roc_on_ipsec_inb_param2 param2;
517         struct cnxk_cpt_inst_tmpl *inst_tmpl;
518         struct roc_ie_on_inb_sa *in_sa;
519         struct cn9k_sec_session *sess;
520         struct cn9k_ipsec_sa *sa;
521         const uint8_t *auth_key;
522         union cpt_inst_w4 w4;
523         union cpt_inst_w7 w7;
524         int auth_key_len = 0;
525         size_t ctx_len = 0;
526         int ret;
527
528         sess = get_sec_session_private_data(sec_sess);
529         sa = &sess->sa;
530         in_sa = &sa->in_sa;
531
532         memset(sa, 0, sizeof(struct cn9k_ipsec_sa));
533
534         sa->dir = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
535         sa->replay_win_sz = ipsec->replay_win_sz;
536
537         ret = fill_ipsec_common_sa(ipsec, crypto_xform, &in_sa->common_sa);
538         if (ret)
539                 return ret;
540
541         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD ||
542             auth_xform->auth.algo == RTE_CRYPTO_AUTH_NULL) {
543                 ctx_len = offsetof(struct roc_ie_on_inb_sa,
544                                    sha1_or_gcm.hmac_key[0]);
545         } else {
546                 auth_key = auth_xform->auth.key.data;
547                 auth_key_len = auth_xform->auth.key.length;
548
549                 switch (auth_xform->auth.algo) {
550                 case RTE_CRYPTO_AUTH_NULL:
551                         break;
552                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
553                         memcpy(in_sa->sha1_or_gcm.hmac_key, auth_key,
554                                auth_key_len);
555                         ctx_len = offsetof(struct roc_ie_on_inb_sa,
556                                            sha1_or_gcm.selector);
557                         break;
558                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
559                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
560                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
561                         memcpy(in_sa->sha2.hmac_key, auth_key, auth_key_len);
562                         ctx_len = offsetof(struct roc_ie_on_inb_sa,
563                                            sha2.selector);
564                         break;
565                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
566                         memcpy(in_sa->aes_xcbc.key, auth_key, auth_key_len);
567                         ctx_len = offsetof(struct roc_ie_on_inb_sa,
568                                            aes_xcbc.selector);
569                         break;
570                 default:
571                         plt_err("Unsupported auth algorithm %u",
572                                 auth_xform->auth.algo);
573                         return -ENOTSUP;
574                 }
575         }
576
577         inst_tmpl = &sa->inst;
578
579         w4.u64 = 0;
580         w4.s.opcode_major = ROC_IE_ON_MAJOR_OP_PROCESS_INBOUND_IPSEC;
581         w4.s.opcode_minor = ctx_len >> 3;
582
583         param2.u16 = 0;
584         param2.s.ikev2 = 1;
585         w4.s.param2 = param2.u16;
586
587         inst_tmpl->w4 = w4.u64;
588
589         w7.u64 = 0;
590         w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
591         w7.s.cptr = rte_mempool_virt2iova(in_sa);
592         inst_tmpl->w7 = w7.u64;
593
594         if (sa->replay_win_sz) {
595                 if (sa->replay_win_sz > CNXK_ON_AR_WIN_SIZE_MAX) {
596                         plt_err("Replay window size:%u is not supported",
597                                 sa->replay_win_sz);
598                         return -ENOTSUP;
599                 }
600
601                 /* Set window bottom to 1, base and top to size of window */
602                 sa->ar.winb = 1;
603                 sa->ar.wint = sa->replay_win_sz;
604                 sa->ar.base = sa->replay_win_sz;
605
606                 in_sa->common_sa.esn_low = sa->seq_lo;
607                 in_sa->common_sa.esn_hi = sa->seq_hi;
608         }
609
610         return cn9k_cpt_enq_sa_write(
611                 sa, qp, ROC_IE_ON_MAJOR_OP_WRITE_IPSEC_INBOUND, ctx_len);
612 }
613
614 static inline int
615 cn9k_ipsec_xform_verify(struct rte_security_ipsec_xform *ipsec,
616                         struct rte_crypto_sym_xform *crypto)
617 {
618         if (ipsec->life.bytes_hard_limit != 0 ||
619             ipsec->life.bytes_soft_limit != 0 ||
620             ipsec->life.packets_hard_limit != 0 ||
621             ipsec->life.packets_soft_limit != 0)
622                 return -ENOTSUP;
623
624         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT) {
625                 enum rte_crypto_sym_xform_type type = crypto->type;
626
627                 if (type == RTE_CRYPTO_SYM_XFORM_AEAD) {
628                         if ((crypto->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) &&
629                             (crypto->aead.key.length == 32)) {
630                                 plt_err("Transport mode AES-256-GCM is not supported");
631                                 return -ENOTSUP;
632                         }
633                 } else {
634                         struct rte_crypto_cipher_xform *cipher;
635                         struct rte_crypto_auth_xform *auth;
636
637                         if (crypto->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
638                                 cipher = &crypto->cipher;
639                                 auth = &crypto->next->auth;
640                         } else {
641                                 cipher = &crypto->next->cipher;
642                                 auth = &crypto->auth;
643                         }
644
645                         if ((cipher->algo == RTE_CRYPTO_CIPHER_AES_CBC) &&
646                             (auth->algo == RTE_CRYPTO_AUTH_SHA256_HMAC)) {
647                                 plt_err("Transport mode AES-CBC SHA2 HMAC 256 is not supported");
648                                 return -ENOTSUP;
649                         }
650
651                         if ((cipher->algo == RTE_CRYPTO_CIPHER_AES_CBC) &&
652                             (auth->algo == RTE_CRYPTO_AUTH_SHA384_HMAC)) {
653                                 plt_err("Transport mode AES-CBC SHA2 HMAC 384 is not supported");
654                                 return -ENOTSUP;
655                         }
656
657                         if ((cipher->algo == RTE_CRYPTO_CIPHER_AES_CBC) &&
658                             (auth->algo == RTE_CRYPTO_AUTH_SHA512_HMAC)) {
659                                 plt_err("Transport mode AES-CBC SHA2 HMAC 512 is not supported");
660                                 return -ENOTSUP;
661                         }
662
663                         if ((cipher->algo == RTE_CRYPTO_CIPHER_AES_CBC) &&
664                             (auth->algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC)) {
665                                 plt_err("Transport mode AES-CBC AES-XCBC is not supported");
666                                 return -ENOTSUP;
667                         }
668                 }
669         }
670
671         return 0;
672 }
673
674 static int
675 cn9k_ipsec_session_create(void *dev,
676                           struct rte_security_ipsec_xform *ipsec_xform,
677                           struct rte_crypto_sym_xform *crypto_xform,
678                           struct rte_security_session *sess)
679 {
680         struct rte_cryptodev *crypto_dev = dev;
681         struct cnxk_cpt_qp *qp;
682         int ret;
683
684         qp = crypto_dev->data->queue_pairs[0];
685         if (qp == NULL) {
686                 plt_err("CPT queue pairs need to be setup for creating security"
687                         " session");
688                 return -EPERM;
689         }
690
691         ret = cnxk_ipsec_xform_verify(ipsec_xform, crypto_xform);
692         if (ret)
693                 return ret;
694
695         ret = cn9k_ipsec_xform_verify(ipsec_xform, crypto_xform);
696         if (ret)
697                 return ret;
698
699         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
700                 return cn9k_ipsec_inb_sa_create(qp, ipsec_xform, crypto_xform,
701                                                 sess);
702         else
703                 return cn9k_ipsec_outb_sa_create(qp, ipsec_xform, crypto_xform,
704                                                  sess);
705 }
706
707 static int
708 cn9k_sec_session_create(void *device, struct rte_security_session_conf *conf,
709                         struct rte_security_session *sess,
710                         struct rte_mempool *mempool)
711 {
712         struct cn9k_sec_session *priv;
713         int ret;
714
715         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
716                 return -EINVAL;
717
718         if (rte_mempool_get(mempool, (void **)&priv)) {
719                 plt_err("Could not allocate security session private data");
720                 return -ENOMEM;
721         }
722
723         memset(priv, 0, sizeof(*priv));
724
725         set_sec_session_private_data(sess, priv);
726
727         if (conf->protocol != RTE_SECURITY_PROTOCOL_IPSEC) {
728                 ret = -ENOTSUP;
729                 goto mempool_put;
730         }
731
732         ret = cn9k_ipsec_session_create(device, &conf->ipsec,
733                                         conf->crypto_xform, sess);
734         if (ret)
735                 goto mempool_put;
736
737         return 0;
738
739 mempool_put:
740         rte_mempool_put(mempool, priv);
741         set_sec_session_private_data(sess, NULL);
742         return ret;
743 }
744
745 static int
746 cn9k_sec_session_destroy(void *device __rte_unused,
747                          struct rte_security_session *sess)
748 {
749         struct roc_ie_on_outb_sa *out_sa;
750         struct cn9k_sec_session *priv;
751         struct rte_mempool *sess_mp;
752         struct roc_ie_on_sa_ctl *ctl;
753         struct cn9k_ipsec_sa *sa;
754
755         priv = get_sec_session_private_data(sess);
756         if (priv == NULL)
757                 return 0;
758
759         sa = &priv->sa;
760         out_sa = &sa->out_sa;
761
762         ctl = &out_sa->common_sa.ctl;
763         ctl->valid = 0;
764
765         rte_io_wmb();
766
767         sess_mp = rte_mempool_from_obj(priv);
768
769         memset(priv, 0, sizeof(*priv));
770
771         set_sec_session_private_data(sess, NULL);
772         rte_mempool_put(sess_mp, priv);
773
774         return 0;
775 }
776
777 static unsigned int
778 cn9k_sec_session_get_size(void *device __rte_unused)
779 {
780         return sizeof(struct cn9k_sec_session);
781 }
782
783 static int
784 cn9k_sec_session_update(void *device, struct rte_security_session *sec_sess,
785                         struct rte_security_session_conf *conf)
786 {
787         struct rte_cryptodev *crypto_dev = device;
788         struct cnxk_cpt_qp *qp;
789         int ret;
790
791         qp = crypto_dev->data->queue_pairs[0];
792         if (qp == NULL) {
793                 plt_err("CPT queue pairs need to be setup for updating security"
794                         " session");
795                 return -EPERM;
796         }
797
798         if (conf->ipsec.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
799                 return -ENOTSUP;
800
801         ret = cnxk_ipsec_xform_verify(&conf->ipsec, conf->crypto_xform);
802         if (ret)
803                 return ret;
804
805         ret = cn9k_ipsec_xform_verify(&conf->ipsec, conf->crypto_xform);
806         if (ret)
807                 return ret;
808
809         return cn9k_ipsec_outb_sa_create(qp, &conf->ipsec, conf->crypto_xform,
810                                          sec_sess);
811 }
812
813 /* Update platform specific security ops */
814 void
815 cn9k_sec_ops_override(void)
816 {
817         /* Update platform specific ops */
818         cnxk_sec_ops.session_create = cn9k_sec_session_create;
819         cnxk_sec_ops.session_destroy = cn9k_sec_session_destroy;
820         cnxk_sec_ops.session_get_size = cn9k_sec_session_get_size;
821         cnxk_sec_ops.session_update = cn9k_sec_session_update;
822 }