doc: add tested platforms with Mellanox NICs
[dpdk.git] / drivers / crypto / octeontx2 / otx2_ipsec_po_ops.h
1
2 /* SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(C) 2019 Marvell International Ltd.
4  */
5
6 #ifndef __OTX2_IPSEC_PO_OPS_H__
7 #define __OTX2_IPSEC_PO_OPS_H__
8
9 #include <rte_crypto_sym.h>
10 #include <rte_security.h>
11
12 #include "otx2_cryptodev.h"
13 #include "otx2_security.h"
14
15 static __rte_always_inline int32_t
16 otx2_ipsec_po_out_rlen_get(struct otx2_sec_session_ipsec_lp *sess,
17                            uint32_t plen)
18 {
19         uint32_t enc_payload_len;
20
21         enc_payload_len = RTE_ALIGN_CEIL(plen + sess->roundup_len,
22                         sess->roundup_byte);
23
24         return sess->partial_len + enc_payload_len;
25 }
26
27 static __rte_always_inline struct cpt_request_info *
28 alloc_request_struct(char *maddr, void *cop, int mdata_len)
29 {
30         struct cpt_request_info *req;
31         struct cpt_meta_info *meta;
32         uint8_t *resp_addr;
33         uintptr_t *op;
34
35         meta = (void *)RTE_PTR_ALIGN((uint8_t *)maddr, 16);
36
37         op = (uintptr_t *)meta->deq_op_info;
38         req = &meta->cpt_req;
39         resp_addr = (uint8_t *)&meta->cpt_res;
40
41         req->completion_addr = (uint64_t *)((uint8_t *)resp_addr);
42         *req->completion_addr = COMPLETION_CODE_INIT;
43         req->comp_baddr = rte_mem_virt2iova(resp_addr);
44         req->op = op;
45
46         op[0] = (uintptr_t)((uint64_t)meta | 1ull);
47         op[1] = (uintptr_t)cop;
48         op[2] = (uintptr_t)req;
49         op[3] = mdata_len;
50
51         return req;
52 }
53
54 static __rte_always_inline int
55 process_outb_sa(struct rte_crypto_op *cop,
56                struct otx2_sec_session_ipsec_lp *sess,
57                struct cpt_qp_meta_info *m_info, void **prep_req)
58 {
59         uint32_t dlen, rlen, extend_head, extend_tail;
60         struct rte_crypto_sym_op *sym_op = cop->sym;
61         struct rte_mbuf *m_src = sym_op->m_src;
62         struct cpt_request_info *req = NULL;
63         struct otx2_ipsec_po_out_hdr *hdr;
64         struct otx2_ipsec_po_out_sa *sa;
65         int hdr_len, mdata_len, ret = 0;
66         vq_cmd_word0_t word0;
67         char *mdata, *data;
68
69         sa = &sess->out_sa;
70         hdr_len = sizeof(*hdr);
71
72         dlen = rte_pktmbuf_pkt_len(m_src) + hdr_len;
73         rlen = otx2_ipsec_po_out_rlen_get(sess, dlen - hdr_len);
74
75         extend_head = hdr_len + RTE_ETHER_HDR_LEN;
76         extend_tail = rlen - dlen;
77         mdata_len = m_info->lb_mlen + 8;
78
79         mdata = rte_pktmbuf_append(m_src, extend_tail + mdata_len);
80         if (unlikely(mdata == NULL)) {
81                 otx2_err("Not enough tail room\n");
82                 ret = -ENOMEM;
83                 goto exit;
84         }
85
86         mdata += extend_tail; /* mdata follows encrypted data */
87         req = alloc_request_struct(mdata, (void *)cop, mdata_len);
88
89         data = rte_pktmbuf_prepend(m_src, extend_head);
90         if (unlikely(data == NULL)) {
91                 otx2_err("Not enough head room\n");
92                 ret = -ENOMEM;
93                 goto exit;
94         }
95
96         /*
97          * Move the Ethernet header, to insert otx2_ipsec_po_out_hdr prior
98          * to the IP header
99          */
100         memcpy(data, data + hdr_len, RTE_ETHER_HDR_LEN);
101
102         hdr = (struct otx2_ipsec_po_out_hdr *)rte_pktmbuf_adj(m_src,
103                                                         RTE_ETHER_HDR_LEN);
104
105         memcpy(&hdr->iv[0], rte_crypto_op_ctod_offset(cop, uint8_t *,
106                 sess->iv_offset), sess->iv_length);
107
108         /* Prepare CPT instruction */
109         word0.u64 = sess->ucmd_w0;
110         word0.s.dlen = dlen;
111
112         req->ist.ei0 = word0.u64;
113         req->ist.ei1 = rte_pktmbuf_iova(m_src);
114         req->ist.ei2 = req->ist.ei1;
115
116         sa->esn_hi = sess->seq_hi;
117
118         hdr->seq = rte_cpu_to_be_32(sess->seq_lo);
119         hdr->ip_id = rte_cpu_to_be_32(sess->ip_id);
120
121         sess->ip_id++;
122         sess->esn++;
123
124 exit:
125         *prep_req = req;
126
127         return ret;
128 }
129
130 static __rte_always_inline int
131 process_inb_sa(struct rte_crypto_op *cop,
132               struct otx2_sec_session_ipsec_lp *sess,
133               struct cpt_qp_meta_info *m_info, void **prep_req)
134 {
135         struct rte_crypto_sym_op *sym_op = cop->sym;
136         struct rte_mbuf *m_src = sym_op->m_src;
137         struct cpt_request_info *req = NULL;
138         int mdata_len, ret = 0;
139         vq_cmd_word0_t word0;
140         uint32_t dlen;
141         char *mdata;
142
143         dlen = rte_pktmbuf_pkt_len(m_src);
144         mdata_len = m_info->lb_mlen + 8;
145
146         mdata = rte_pktmbuf_append(m_src, mdata_len);
147         if (unlikely(mdata == NULL)) {
148                 otx2_err("Not enough tail room\n");
149                 ret = -ENOMEM;
150                 goto exit;
151         }
152
153         req = alloc_request_struct(mdata, (void *)cop, mdata_len);
154
155         /* Prepare CPT instruction */
156         word0.u64 = sess->ucmd_w0;
157         word0.s.dlen   = dlen;
158
159         req->ist.ei0 = word0.u64;
160         req->ist.ei1 = rte_pktmbuf_iova(m_src);
161         req->ist.ei2 = req->ist.ei1;
162
163 exit:
164         *prep_req = req;
165         return ret;
166 }
167 #endif /* __OTX2_IPSEC_PO_OPS_H__ */