eal/windows: add missing SPDX license tag
[dpdk.git] / drivers / crypto / octeontx2 / otx2_cryptodev_sec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2020 Marvell International Ltd.
3  */
4
5 #include <rte_cryptodev.h>
6 #include <rte_esp.h>
7 #include <rte_ethdev.h>
8 #include <rte_ip.h>
9 #include <rte_malloc.h>
10 #include <rte_security.h>
11 #include <rte_security_driver.h>
12 #include <rte_udp.h>
13
14 #include "otx2_cryptodev.h"
15 #include "otx2_cryptodev_capabilities.h"
16 #include "otx2_cryptodev_hw_access.h"
17 #include "otx2_cryptodev_ops.h"
18 #include "otx2_cryptodev_sec.h"
19 #include "otx2_security.h"
20
21 static int
22 ipsec_lp_len_precalc(struct rte_security_ipsec_xform *ipsec,
23                 struct rte_crypto_sym_xform *xform,
24                 struct otx2_sec_session_ipsec_lp *lp)
25 {
26         struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
27
28         if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
29                 lp->partial_len = sizeof(struct rte_ipv4_hdr);
30         else if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV6)
31                 lp->partial_len = sizeof(struct rte_ipv6_hdr);
32         else
33                 return -EINVAL;
34
35         if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
36                 lp->partial_len += sizeof(struct rte_esp_hdr);
37                 lp->roundup_len = sizeof(struct rte_esp_tail);
38         } else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
39                 lp->partial_len += OTX2_SEC_AH_HDR_LEN;
40         } else {
41                 return -EINVAL;
42         }
43
44         if (ipsec->options.udp_encap)
45                 lp->partial_len += sizeof(struct rte_udp_hdr);
46
47         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
48                 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
49                         lp->partial_len += OTX2_SEC_AES_GCM_IV_LEN;
50                         lp->partial_len += OTX2_SEC_AES_GCM_MAC_LEN;
51                         lp->roundup_byte = OTX2_SEC_AES_GCM_ROUNDUP_BYTE_LEN;
52                         return 0;
53                 } else {
54                         return -EINVAL;
55                 }
56         }
57
58         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
59                 cipher_xform = xform;
60                 auth_xform = xform->next;
61         } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
62                 auth_xform = xform;
63                 cipher_xform = xform->next;
64         } else {
65                 return -EINVAL;
66         }
67
68         if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
69                 lp->partial_len += OTX2_SEC_AES_CBC_IV_LEN;
70                 lp->roundup_byte = OTX2_SEC_AES_CBC_ROUNDUP_BYTE_LEN;
71         } else {
72                 return -EINVAL;
73         }
74
75         if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
76                 lp->partial_len += OTX2_SEC_SHA1_HMAC_LEN;
77         else if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
78                 lp->partial_len += OTX2_SEC_SHA2_HMAC_LEN;
79         else
80                 return -EINVAL;
81
82         return 0;
83 }
84
85 static int
86 otx2_cpt_enq_sa_write(struct otx2_sec_session_ipsec_lp *lp,
87                       struct otx2_cpt_qp *qptr, uint8_t opcode)
88 {
89         uint64_t lmt_status, time_out;
90         void *lmtline = qptr->lmtline;
91         struct otx2_cpt_inst_s inst;
92         struct otx2_cpt_res *res;
93         uint64_t *mdata;
94         int ret = 0;
95
96         if (unlikely(rte_mempool_get(qptr->meta_info.pool,
97                                      (void **)&mdata) < 0))
98                 return -ENOMEM;
99
100         res = (struct otx2_cpt_res *)RTE_PTR_ALIGN(mdata, 16);
101         res->compcode = CPT_9X_COMP_E_NOTDONE;
102
103         inst.opcode = opcode | (lp->ctx_len << 8);
104         inst.param1 = 0;
105         inst.param2 = 0;
106         inst.dlen = lp->ctx_len << 3;
107         inst.dptr = rte_mempool_virt2iova(lp);
108         inst.rptr = 0;
109         inst.cptr = rte_mempool_virt2iova(lp);
110         inst.egrp  = OTX2_CPT_EGRP_SE;
111
112         inst.u64[0] = 0;
113         inst.u64[2] = 0;
114         inst.u64[3] = 0;
115         inst.res_addr = rte_mempool_virt2iova(res);
116
117         rte_io_wmb();
118
119         do {
120                 /* Copy CPT command to LMTLINE */
121                 otx2_lmt_mov(lmtline, &inst, 2);
122                 lmt_status = otx2_lmt_submit(qptr->lf_nq_reg);
123         } while (lmt_status == 0);
124
125         time_out = rte_get_timer_cycles() +
126                         DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
127
128         while (res->compcode == CPT_9X_COMP_E_NOTDONE) {
129                 if (rte_get_timer_cycles() > time_out) {
130                         rte_mempool_put(qptr->meta_info.pool, mdata);
131                         otx2_err("Request timed out");
132                         return -ETIMEDOUT;
133                 }
134             rte_io_rmb();
135         }
136
137         if (unlikely(res->compcode != CPT_9X_COMP_E_GOOD)) {
138                 ret = res->compcode;
139                 switch (ret) {
140                 case CPT_9X_COMP_E_INSTERR:
141                         otx2_err("Request failed with instruction error");
142                         break;
143                 case CPT_9X_COMP_E_FAULT:
144                         otx2_err("Request failed with DMA fault");
145                         break;
146                 case CPT_9X_COMP_E_HWERR:
147                         otx2_err("Request failed with hardware error");
148                         break;
149                 default:
150                         otx2_err("Request failed with unknown hardware "
151                                  "completion code : 0x%x", ret);
152                 }
153                 goto mempool_put;
154         }
155
156         if (unlikely(res->uc_compcode != OTX2_IPSEC_PO_CC_SUCCESS)) {
157                 ret = res->uc_compcode;
158                 switch (ret) {
159                 case OTX2_IPSEC_PO_CC_AUTH_UNSUPPORTED:
160                         otx2_err("Invalid auth type");
161                         break;
162                 case OTX2_IPSEC_PO_CC_ENCRYPT_UNSUPPORTED:
163                         otx2_err("Invalid encrypt type");
164                         break;
165                 default:
166                         otx2_err("Request failed with unknown microcode "
167                                  "completion code : 0x%x", ret);
168                 }
169         }
170
171 mempool_put:
172         rte_mempool_put(qptr->meta_info.pool, mdata);
173         return ret;
174 }
175
176 static void
177 set_session_misc_attributes(struct otx2_sec_session_ipsec_lp *sess,
178                             struct rte_crypto_sym_xform *crypto_xform,
179                             struct rte_crypto_sym_xform *auth_xform,
180                             struct rte_crypto_sym_xform *cipher_xform)
181 {
182         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
183                 sess->iv_offset = crypto_xform->aead.iv.offset;
184                 sess->iv_length = crypto_xform->aead.iv.length;
185                 sess->aad_length = crypto_xform->aead.aad_length;
186                 sess->mac_len = crypto_xform->aead.digest_length;
187         } else {
188                 sess->iv_offset = cipher_xform->cipher.iv.offset;
189                 sess->iv_length = cipher_xform->cipher.iv.length;
190                 sess->auth_iv_offset = auth_xform->auth.iv.offset;
191                 sess->auth_iv_length = auth_xform->auth.iv.length;
192                 sess->mac_len = auth_xform->auth.digest_length;
193         }
194
195         sess->ucmd_param1 = OTX2_IPSEC_PO_PER_PKT_IV;
196         sess->ucmd_param2 = 0;
197 }
198
199 static int
200 crypto_sec_ipsec_outb_session_create(struct rte_cryptodev *crypto_dev,
201                                      struct rte_security_ipsec_xform *ipsec,
202                                      struct rte_crypto_sym_xform *crypto_xform,
203                                      struct rte_security_session *sec_sess)
204 {
205         struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
206         const uint8_t *cipher_key, *auth_key;
207         struct otx2_sec_session_ipsec_lp *lp;
208         struct otx2_ipsec_po_sa_ctl *ctl;
209         int cipher_key_len, auth_key_len;
210         struct otx2_ipsec_po_out_sa *sa;
211         struct otx2_sec_session *sess;
212         struct otx2_cpt_inst_s inst;
213         struct rte_ipv6_hdr *ip6;
214         struct rte_ipv4_hdr *ip;
215         int ret, ctx_len;
216
217         sess = get_sec_session_private_data(sec_sess);
218         sess->ipsec.dir = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
219         lp = &sess->ipsec.lp;
220
221         sa = &lp->out_sa;
222         ctl = &sa->ctl;
223         if (ctl->valid) {
224                 otx2_err("SA already registered");
225                 return -EINVAL;
226         }
227
228         memset(sa, 0, sizeof(struct otx2_ipsec_po_out_sa));
229
230         /* Initialize lookaside ipsec private data */
231         lp->ip_id = 0;
232         lp->seq_lo = 1;
233         lp->seq_hi = 0;
234         lp->tunnel_type = ipsec->tunnel.type;
235
236         ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
237         if (ret)
238                 return ret;
239
240         ret = ipsec_lp_len_precalc(ipsec, crypto_xform, lp);
241         if (ret)
242                 return ret;
243
244         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
245                 /* Start ip id from 1 */
246                 lp->ip_id = 1;
247
248                 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
249
250                         if (ctl->enc_type == OTX2_IPSEC_PO_SA_ENC_AES_GCM) {
251                                 if (ipsec->options.udp_encap) {
252                                         sa->aes_gcm.template.ip4.udp_src = 4500;
253                                         sa->aes_gcm.template.ip4.udp_dst = 4500;
254                                 }
255                                 ip = &sa->aes_gcm.template.ip4.ipv4_hdr;
256                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
257                                                 aes_gcm.template) + sizeof(
258                                                 sa->aes_gcm.template.ip4);
259                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
260                                 lp->ctx_len = ctx_len >> 3;
261                         } else if (ctl->auth_type ==
262                                         OTX2_IPSEC_PO_SA_AUTH_SHA1) {
263                                 if (ipsec->options.udp_encap) {
264                                         sa->sha1.template.ip4.udp_src = 4500;
265                                         sa->sha1.template.ip4.udp_dst = 4500;
266                                 }
267                                 ip = &sa->sha1.template.ip4.ipv4_hdr;
268                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
269                                                 sha1.template) + sizeof(
270                                                 sa->sha1.template.ip4);
271                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
272                                 lp->ctx_len = ctx_len >> 3;
273                         } else if (ctl->auth_type ==
274                                         OTX2_IPSEC_PO_SA_AUTH_SHA2_256) {
275                                 if (ipsec->options.udp_encap) {
276                                         sa->sha2.template.ip4.udp_src = 4500;
277                                         sa->sha2.template.ip4.udp_dst = 4500;
278                                 }
279                                 ip = &sa->sha2.template.ip4.ipv4_hdr;
280                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
281                                                 sha2.template) + sizeof(
282                                                 sa->sha2.template.ip4);
283                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
284                                 lp->ctx_len = ctx_len >> 3;
285                         } else {
286                                 return -EINVAL;
287                         }
288                         ip->version_ihl = RTE_IPV4_VHL_DEF;
289                         ip->next_proto_id = IPPROTO_ESP;
290                         ip->time_to_live = ipsec->tunnel.ipv4.ttl;
291                         ip->type_of_service |= (ipsec->tunnel.ipv4.dscp << 2);
292                         if (ipsec->tunnel.ipv4.df)
293                                 ip->fragment_offset = BIT(14);
294                         memcpy(&ip->src_addr, &ipsec->tunnel.ipv4.src_ip,
295                                 sizeof(struct in_addr));
296                         memcpy(&ip->dst_addr, &ipsec->tunnel.ipv4.dst_ip,
297                                 sizeof(struct in_addr));
298                 } else if (ipsec->tunnel.type ==
299                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
300
301                         if (ctl->enc_type == OTX2_IPSEC_PO_SA_ENC_AES_GCM) {
302                                 if (ipsec->options.udp_encap) {
303                                         sa->aes_gcm.template.ip6.udp_src = 4500;
304                                         sa->aes_gcm.template.ip6.udp_dst = 4500;
305                                 }
306                                 ip6 = &sa->aes_gcm.template.ip6.ipv6_hdr;
307                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
308                                                 aes_gcm.template) + sizeof(
309                                                 sa->aes_gcm.template.ip6);
310                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
311                                 lp->ctx_len = ctx_len >> 3;
312                         } else if (ctl->auth_type ==
313                                         OTX2_IPSEC_PO_SA_AUTH_SHA1) {
314                                 if (ipsec->options.udp_encap) {
315                                         sa->sha1.template.ip6.udp_src = 4500;
316                                         sa->sha1.template.ip6.udp_dst = 4500;
317                                 }
318                                 ip6 = &sa->sha1.template.ip6.ipv6_hdr;
319                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
320                                                 sha1.template) + sizeof(
321                                                 sa->sha1.template.ip6);
322                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
323                                 lp->ctx_len = ctx_len >> 3;
324                         } else if (ctl->auth_type ==
325                                         OTX2_IPSEC_PO_SA_AUTH_SHA2_256) {
326                                 if (ipsec->options.udp_encap) {
327                                         sa->sha2.template.ip6.udp_src = 4500;
328                                         sa->sha2.template.ip6.udp_dst = 4500;
329                                 }
330                                 ip6 = &sa->sha2.template.ip6.ipv6_hdr;
331                                 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
332                                                 sha2.template) + sizeof(
333                                                 sa->sha2.template.ip6);
334                                 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
335                                 lp->ctx_len = ctx_len >> 3;
336                         } else {
337                                 return -EINVAL;
338                         }
339
340                         ip6->vtc_flow = rte_cpu_to_be_32(0x60000000 |
341                                 ((ipsec->tunnel.ipv6.dscp <<
342                                         RTE_IPV6_HDR_TC_SHIFT) &
343                                         RTE_IPV6_HDR_TC_MASK) |
344                                 ((ipsec->tunnel.ipv6.flabel <<
345                                         RTE_IPV6_HDR_FL_SHIFT) &
346                                         RTE_IPV6_HDR_FL_MASK));
347                         ip6->hop_limits = ipsec->tunnel.ipv6.hlimit;
348                         ip6->proto = (ipsec->proto ==
349                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
350                                         IPPROTO_ESP : IPPROTO_AH;
351                         memcpy(&ip6->src_addr, &ipsec->tunnel.ipv6.src_addr,
352                                 sizeof(struct in6_addr));
353                         memcpy(&ip6->dst_addr, &ipsec->tunnel.ipv6.dst_addr,
354                                 sizeof(struct in6_addr));
355                 } else {
356                         return -EINVAL;
357                 }
358         } else {
359                 return -EINVAL;
360         }
361
362         cipher_xform = crypto_xform;
363         auth_xform = crypto_xform->next;
364
365         cipher_key_len = 0;
366         auth_key_len = 0;
367
368         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
369                 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
370                         memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);
371                 cipher_key = crypto_xform->aead.key.data;
372                 cipher_key_len = crypto_xform->aead.key.length;
373         } else {
374                 cipher_key = cipher_xform->cipher.key.data;
375                 cipher_key_len = cipher_xform->cipher.key.length;
376                 auth_key = auth_xform->auth.key.data;
377                 auth_key_len = auth_xform->auth.key.length;
378
379                 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
380                         memcpy(sa->sha1.hmac_key, auth_key, auth_key_len);
381                 else if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
382                         memcpy(sa->sha2.hmac_key, auth_key, auth_key_len);
383         }
384
385         if (cipher_key_len != 0)
386                 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
387         else
388                 return -EINVAL;
389
390         inst.u64[7] = 0;
391         inst.egrp = OTX2_CPT_EGRP_SE;
392         inst.cptr = rte_mempool_virt2iova(sa);
393
394         lp->cpt_inst_w7 = inst.u64[7];
395         lp->ucmd_opcode = (lp->ctx_len << 8) |
396                                 (OTX2_IPSEC_PO_PROCESS_IPSEC_OUTB);
397
398         set_session_misc_attributes(lp, crypto_xform,
399                                     auth_xform, cipher_xform);
400
401         return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
402                                      OTX2_IPSEC_PO_WRITE_IPSEC_OUTB);
403 }
404
405 static int
406 crypto_sec_ipsec_inb_session_create(struct rte_cryptodev *crypto_dev,
407                                     struct rte_security_ipsec_xform *ipsec,
408                                     struct rte_crypto_sym_xform *crypto_xform,
409                                     struct rte_security_session *sec_sess)
410 {
411         struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
412         const uint8_t *cipher_key, *auth_key;
413         struct otx2_sec_session_ipsec_lp *lp;
414         struct otx2_ipsec_po_sa_ctl *ctl;
415         int cipher_key_len, auth_key_len;
416         struct otx2_ipsec_po_in_sa *sa;
417         struct otx2_sec_session *sess;
418         struct otx2_cpt_inst_s inst;
419         int ret;
420
421         sess = get_sec_session_private_data(sec_sess);
422         sess->ipsec.dir = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
423         lp = &sess->ipsec.lp;
424
425         sa = &lp->in_sa;
426         ctl = &sa->ctl;
427
428         if (ctl->valid) {
429                 otx2_err("SA already registered");
430                 return -EINVAL;
431         }
432
433         memset(sa, 0, sizeof(struct otx2_ipsec_po_in_sa));
434         sa->replay_win_sz = ipsec->replay_win_sz;
435
436         ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
437         if (ret)
438                 return ret;
439
440         lp->tunnel_type = ipsec->tunnel.type;
441         auth_xform = crypto_xform;
442         cipher_xform = crypto_xform->next;
443
444         cipher_key_len = 0;
445         auth_key_len = 0;
446
447         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
448                 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
449                         memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);
450                 cipher_key = crypto_xform->aead.key.data;
451                 cipher_key_len = crypto_xform->aead.key.length;
452
453                 lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
454                                             aes_gcm.hmac_key[0]) >> 3;
455                 RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_AES_GCM_INB_CTX_LEN);
456         } else {
457                 cipher_key = cipher_xform->cipher.key.data;
458                 cipher_key_len = cipher_xform->cipher.key.length;
459                 auth_key = auth_xform->auth.key.data;
460                 auth_key_len = auth_xform->auth.key.length;
461
462                 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC) {
463                         memcpy(sa->aes_gcm.hmac_key, auth_key, auth_key_len);
464                         lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
465                                                     aes_gcm.selector) >> 3;
466                 } else if (auth_xform->auth.algo ==
467                                 RTE_CRYPTO_AUTH_SHA256_HMAC) {
468                         memcpy(sa->sha2.hmac_key, auth_key, auth_key_len);
469                         lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
470                                                     sha2.selector) >> 3;
471                 }
472         }
473
474         if (cipher_key_len != 0)
475                 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
476         else
477                 return -EINVAL;
478
479         inst.u64[7] = 0;
480         inst.egrp = OTX2_CPT_EGRP_SE;
481         inst.cptr = rte_mempool_virt2iova(sa);
482
483         lp->cpt_inst_w7 = inst.u64[7];
484         lp->ucmd_opcode = (lp->ctx_len << 8) |
485                                 (OTX2_IPSEC_PO_PROCESS_IPSEC_INB);
486
487         set_session_misc_attributes(lp, crypto_xform,
488                                     auth_xform, cipher_xform);
489
490         if (sa->replay_win_sz) {
491                 if (sa->replay_win_sz > OTX2_IPSEC_MAX_REPLAY_WIN_SZ) {
492                         otx2_err("Replay window size is not supported");
493                         return -ENOTSUP;
494                 }
495                 sa->replay = rte_zmalloc(NULL, sizeof(struct otx2_ipsec_replay),
496                                 0);
497                 if (sa->replay == NULL)
498                         return -ENOMEM;
499
500                 /* Set window bottom to 1, base and top to size of window */
501                 sa->replay->winb = 1;
502                 sa->replay->wint = sa->replay_win_sz;
503                 sa->replay->base = sa->replay_win_sz;
504                 sa->esn_low = 0;
505                 sa->esn_hi = 0;
506         }
507
508         return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
509                                      OTX2_IPSEC_PO_WRITE_IPSEC_INB);
510 }
511
512 static int
513 crypto_sec_ipsec_session_create(struct rte_cryptodev *crypto_dev,
514                                 struct rte_security_ipsec_xform *ipsec,
515                                 struct rte_crypto_sym_xform *crypto_xform,
516                                 struct rte_security_session *sess)
517 {
518         int ret;
519
520         if (crypto_dev->data->queue_pairs[0] == NULL) {
521                 otx2_err("Setup cpt queue pair before creating sec session");
522                 return -EPERM;
523         }
524
525         ret = ipsec_po_xform_verify(ipsec, crypto_xform);
526         if (ret)
527                 return ret;
528
529         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
530                 return crypto_sec_ipsec_inb_session_create(crypto_dev, ipsec,
531                                                            crypto_xform, sess);
532         else
533                 return crypto_sec_ipsec_outb_session_create(crypto_dev, ipsec,
534                                                             crypto_xform, sess);
535 }
536
537 static int
538 otx2_crypto_sec_session_create(void *device,
539                                struct rte_security_session_conf *conf,
540                                struct rte_security_session *sess,
541                                struct rte_mempool *mempool)
542 {
543         struct otx2_sec_session *priv;
544         int ret;
545
546         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
547                 return -ENOTSUP;
548
549         if (rte_security_dynfield_register() < 0)
550                 return -rte_errno;
551
552         if (rte_mempool_get(mempool, (void **)&priv)) {
553                 otx2_err("Could not allocate security session private data");
554                 return -ENOMEM;
555         }
556
557         set_sec_session_private_data(sess, priv);
558
559         priv->userdata = conf->userdata;
560
561         if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
562                 ret = crypto_sec_ipsec_session_create(device, &conf->ipsec,
563                                                       conf->crypto_xform,
564                                                       sess);
565         else
566                 ret = -ENOTSUP;
567
568         if (ret)
569                 goto mempool_put;
570
571         return 0;
572
573 mempool_put:
574         rte_mempool_put(mempool, priv);
575         set_sec_session_private_data(sess, NULL);
576         return ret;
577 }
578
579 static int
580 otx2_crypto_sec_session_destroy(void *device __rte_unused,
581                                 struct rte_security_session *sess)
582 {
583         struct otx2_sec_session *priv;
584         struct rte_mempool *sess_mp;
585
586         priv = get_sec_session_private_data(sess);
587
588         if (priv == NULL)
589                 return 0;
590
591         sess_mp = rte_mempool_from_obj(priv);
592
593         set_sec_session_private_data(sess, NULL);
594         rte_mempool_put(sess_mp, priv);
595
596         return 0;
597 }
598
599 static unsigned int
600 otx2_crypto_sec_session_get_size(void *device __rte_unused)
601 {
602         return sizeof(struct otx2_sec_session);
603 }
604
605 static int
606 otx2_crypto_sec_set_pkt_mdata(void *device __rte_unused,
607                               struct rte_security_session *session,
608                               struct rte_mbuf *m, void *params __rte_unused)
609 {
610         /* Set security session as the pkt metadata */
611         *rte_security_dynfield(m) = (rte_security_dynfield_t)session;
612
613         return 0;
614 }
615
616 static int
617 otx2_crypto_sec_get_userdata(void *device __rte_unused, uint64_t md,
618                              void **userdata)
619 {
620         /* Retrieve userdata  */
621         *userdata = (void *)md;
622
623         return 0;
624 }
625
626 static struct rte_security_ops otx2_crypto_sec_ops = {
627         .session_create         = otx2_crypto_sec_session_create,
628         .session_destroy        = otx2_crypto_sec_session_destroy,
629         .session_get_size       = otx2_crypto_sec_session_get_size,
630         .set_pkt_metadata       = otx2_crypto_sec_set_pkt_mdata,
631         .get_userdata           = otx2_crypto_sec_get_userdata,
632         .capabilities_get       = otx2_crypto_sec_capabilities_get
633 };
634
635 int
636 otx2_crypto_sec_ctx_create(struct rte_cryptodev *cdev)
637 {
638         struct rte_security_ctx *ctx;
639
640         ctx = rte_malloc("otx2_cpt_dev_sec_ctx",
641                          sizeof(struct rte_security_ctx), 0);
642
643         if (ctx == NULL)
644                 return -ENOMEM;
645
646         /* Populate ctx */
647         ctx->device = cdev;
648         ctx->ops = &otx2_crypto_sec_ops;
649         ctx->sess_cnt = 0;
650
651         cdev->security_ctx = ctx;
652
653         return 0;
654 }
655
656 void
657 otx2_crypto_sec_ctx_destroy(struct rte_cryptodev *cdev)
658 {
659         rte_free(cdev->security_ctx);
660 }