bus/fslmc: switch sequence number to dynamic mbuf field
[dpdk.git] / drivers / crypto / dpaa2_sec / dpaa2_sec_dpseci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016-2020 NXP
5  *
6  */
7
8 #include <time.h>
9 #include <net/if.h>
10 #include <unistd.h>
11
12 #include <rte_ip.h>
13 #include <rte_mbuf.h>
14 #include <rte_cryptodev.h>
15 #include <rte_malloc.h>
16 #include <rte_memcpy.h>
17 #include <rte_string_fns.h>
18 #include <rte_cycles.h>
19 #include <rte_kvargs.h>
20 #include <rte_dev.h>
21 #include <rte_cryptodev_pmd.h>
22 #include <rte_common.h>
23 #include <rte_fslmc.h>
24 #include <fslmc_vfio.h>
25 #include <dpaa2_hw_pvt.h>
26 #include <dpaa2_hw_dpio.h>
27 #include <dpaa2_hw_mempool.h>
28 #include <fsl_dpopr.h>
29 #include <fsl_dpseci.h>
30 #include <fsl_mc_sys.h>
31
32 #include "dpaa2_sec_priv.h"
33 #include "dpaa2_sec_event.h"
34 #include "dpaa2_sec_logs.h"
35
36 /* RTA header files */
37 #include <desc/ipsec.h>
38 #include <desc/pdcp.h>
39 #include <desc/sdap.h>
40 #include <desc/algo.h>
41
42 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
43  * a pointer to the shared descriptor
44  */
45 #define MIN_JOB_DESC_SIZE       (CAAM_CMD_SZ + CAAM_PTR_SZ)
46 #define FSL_VENDOR_ID           0x1957
47 #define FSL_DEVICE_ID           0x410
48 #define FSL_SUBSYSTEM_SEC       1
49 #define FSL_MC_DPSECI_DEVID     3
50
51 #define NO_PREFETCH 0
52 /* FLE_POOL_NUM_BUFS is set as per the ipsec-secgw application */
53 #define FLE_POOL_NUM_BUFS       32000
54 #define FLE_POOL_BUF_SIZE       256
55 #define FLE_POOL_CACHE_SIZE     512
56 #define FLE_SG_MEM_SIZE(num)    (FLE_POOL_BUF_SIZE + ((num) * 32))
57 #define SEC_FLC_DHR_OUTBOUND    -114
58 #define SEC_FLC_DHR_INBOUND     0
59
60 static uint8_t cryptodev_driver_id;
61
62 #ifdef RTE_LIB_SECURITY
63 static inline int
64 build_proto_compound_sg_fd(dpaa2_sec_session *sess,
65                            struct rte_crypto_op *op,
66                            struct qbman_fd *fd, uint16_t bpid)
67 {
68         struct rte_crypto_sym_op *sym_op = op->sym;
69         struct ctxt_priv *priv = sess->ctxt;
70         struct qbman_fle *fle, *sge, *ip_fle, *op_fle;
71         struct sec_flow_context *flc;
72         struct rte_mbuf *mbuf;
73         uint32_t in_len = 0, out_len = 0;
74
75         if (sym_op->m_dst)
76                 mbuf = sym_op->m_dst;
77         else
78                 mbuf = sym_op->m_src;
79
80         /* first FLE entry used to store mbuf and session ctxt */
81         fle = (struct qbman_fle *)rte_malloc(NULL,
82                         FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs),
83                         RTE_CACHE_LINE_SIZE);
84         if (unlikely(!fle)) {
85                 DPAA2_SEC_DP_ERR("Proto:SG: Memory alloc failed for SGE");
86                 return -ENOMEM;
87         }
88         memset(fle, 0, FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs));
89         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
90         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
91
92         /* Save the shared descriptor */
93         flc = &priv->flc_desc[0].flc;
94
95         op_fle = fle + 1;
96         ip_fle = fle + 2;
97         sge = fle + 3;
98
99         if (likely(bpid < MAX_BPID)) {
100                 DPAA2_SET_FD_BPID(fd, bpid);
101                 DPAA2_SET_FLE_BPID(op_fle, bpid);
102                 DPAA2_SET_FLE_BPID(ip_fle, bpid);
103         } else {
104                 DPAA2_SET_FD_IVP(fd);
105                 DPAA2_SET_FLE_IVP(op_fle);
106                 DPAA2_SET_FLE_IVP(ip_fle);
107         }
108
109         /* Configure FD as a FRAME LIST */
110         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
111         DPAA2_SET_FD_COMPOUND_FMT(fd);
112         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
113
114         /* Configure Output FLE with Scatter/Gather Entry */
115         DPAA2_SET_FLE_SG_EXT(op_fle);
116         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_VADDR_TO_IOVA(sge));
117
118         /* Configure Output SGE for Encap/Decap */
119         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
120         DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
121         /* o/p segs */
122         while (mbuf->next) {
123                 sge->length = mbuf->data_len;
124                 out_len += sge->length;
125                 sge++;
126                 mbuf = mbuf->next;
127                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
128                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
129         }
130         /* using buf_len for last buf - so that extra data can be added */
131         sge->length = mbuf->buf_len - mbuf->data_off;
132         out_len += sge->length;
133
134         DPAA2_SET_FLE_FIN(sge);
135         op_fle->length = out_len;
136
137         sge++;
138         mbuf = sym_op->m_src;
139
140         /* Configure Input FLE with Scatter/Gather Entry */
141         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
142         DPAA2_SET_FLE_SG_EXT(ip_fle);
143         DPAA2_SET_FLE_FIN(ip_fle);
144
145         /* Configure input SGE for Encap/Decap */
146         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
147         DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
148         sge->length = mbuf->data_len;
149         in_len += sge->length;
150
151         mbuf = mbuf->next;
152         /* i/p segs */
153         while (mbuf) {
154                 sge++;
155                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
156                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
157                 sge->length = mbuf->data_len;
158                 in_len += sge->length;
159                 mbuf = mbuf->next;
160         }
161         ip_fle->length = in_len;
162         DPAA2_SET_FLE_FIN(sge);
163
164         /* In case of PDCP, per packet HFN is stored in
165          * mbuf priv after sym_op.
166          */
167         if (sess->ctxt_type == DPAA2_SEC_PDCP && sess->pdcp.hfn_ovd) {
168                 uint32_t hfn_ovd = *(uint32_t *)((uint8_t *)op +
169                                         sess->pdcp.hfn_ovd_offset);
170                 /*enable HFN override override */
171                 DPAA2_SET_FLE_INTERNAL_JD(ip_fle, hfn_ovd);
172                 DPAA2_SET_FLE_INTERNAL_JD(op_fle, hfn_ovd);
173                 DPAA2_SET_FD_INTERNAL_JD(fd, hfn_ovd);
174         }
175         DPAA2_SET_FD_LEN(fd, ip_fle->length);
176
177         return 0;
178 }
179
180 static inline int
181 build_proto_compound_fd(dpaa2_sec_session *sess,
182                struct rte_crypto_op *op,
183                struct qbman_fd *fd, uint16_t bpid)
184 {
185         struct rte_crypto_sym_op *sym_op = op->sym;
186         struct ctxt_priv *priv = sess->ctxt;
187         struct qbman_fle *fle, *ip_fle, *op_fle;
188         struct sec_flow_context *flc;
189         struct rte_mbuf *src_mbuf = sym_op->m_src;
190         struct rte_mbuf *dst_mbuf = sym_op->m_dst;
191         int retval;
192
193         if (!dst_mbuf)
194                 dst_mbuf = src_mbuf;
195
196         /* Save the shared descriptor */
197         flc = &priv->flc_desc[0].flc;
198
199         /* we are using the first FLE entry to store Mbuf */
200         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
201         if (retval) {
202                 DPAA2_SEC_DP_ERR("Memory alloc failed");
203                 return -ENOMEM;
204         }
205         memset(fle, 0, FLE_POOL_BUF_SIZE);
206         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
207         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
208
209         op_fle = fle + 1;
210         ip_fle = fle + 2;
211
212         if (likely(bpid < MAX_BPID)) {
213                 DPAA2_SET_FD_BPID(fd, bpid);
214                 DPAA2_SET_FLE_BPID(op_fle, bpid);
215                 DPAA2_SET_FLE_BPID(ip_fle, bpid);
216         } else {
217                 DPAA2_SET_FD_IVP(fd);
218                 DPAA2_SET_FLE_IVP(op_fle);
219                 DPAA2_SET_FLE_IVP(ip_fle);
220         }
221
222         /* Configure FD as a FRAME LIST */
223         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
224         DPAA2_SET_FD_COMPOUND_FMT(fd);
225         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
226
227         /* Configure Output FLE with dst mbuf data  */
228         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_MBUF_VADDR_TO_IOVA(dst_mbuf));
229         DPAA2_SET_FLE_OFFSET(op_fle, dst_mbuf->data_off);
230         DPAA2_SET_FLE_LEN(op_fle, dst_mbuf->buf_len);
231
232         /* Configure Input FLE with src mbuf data */
233         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_MBUF_VADDR_TO_IOVA(src_mbuf));
234         DPAA2_SET_FLE_OFFSET(ip_fle, src_mbuf->data_off);
235         DPAA2_SET_FLE_LEN(ip_fle, src_mbuf->pkt_len);
236
237         DPAA2_SET_FD_LEN(fd, ip_fle->length);
238         DPAA2_SET_FLE_FIN(ip_fle);
239
240         /* In case of PDCP, per packet HFN is stored in
241          * mbuf priv after sym_op.
242          */
243         if (sess->ctxt_type == DPAA2_SEC_PDCP && sess->pdcp.hfn_ovd) {
244                 uint32_t hfn_ovd = *(uint32_t *)((uint8_t *)op +
245                                         sess->pdcp.hfn_ovd_offset);
246                 /*enable HFN override override */
247                 DPAA2_SET_FLE_INTERNAL_JD(ip_fle, hfn_ovd);
248                 DPAA2_SET_FLE_INTERNAL_JD(op_fle, hfn_ovd);
249                 DPAA2_SET_FD_INTERNAL_JD(fd, hfn_ovd);
250         }
251
252         return 0;
253
254 }
255
256 static inline int
257 build_proto_fd(dpaa2_sec_session *sess,
258                struct rte_crypto_op *op,
259                struct qbman_fd *fd, uint16_t bpid)
260 {
261         struct rte_crypto_sym_op *sym_op = op->sym;
262         if (sym_op->m_dst)
263                 return build_proto_compound_fd(sess, op, fd, bpid);
264
265         struct ctxt_priv *priv = sess->ctxt;
266         struct sec_flow_context *flc;
267         struct rte_mbuf *mbuf = sym_op->m_src;
268
269         if (likely(bpid < MAX_BPID))
270                 DPAA2_SET_FD_BPID(fd, bpid);
271         else
272                 DPAA2_SET_FD_IVP(fd);
273
274         /* Save the shared descriptor */
275         flc = &priv->flc_desc[0].flc;
276
277         DPAA2_SET_FD_ADDR(fd, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
278         DPAA2_SET_FD_OFFSET(fd, sym_op->m_src->data_off);
279         DPAA2_SET_FD_LEN(fd, sym_op->m_src->pkt_len);
280         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
281
282         /* save physical address of mbuf */
283         op->sym->aead.digest.phys_addr = mbuf->buf_iova;
284         mbuf->buf_iova = (size_t)op;
285
286         return 0;
287 }
288 #endif
289
290 static inline int
291 build_authenc_gcm_sg_fd(dpaa2_sec_session *sess,
292                  struct rte_crypto_op *op,
293                  struct qbman_fd *fd, __rte_unused uint16_t bpid)
294 {
295         struct rte_crypto_sym_op *sym_op = op->sym;
296         struct ctxt_priv *priv = sess->ctxt;
297         struct qbman_fle *fle, *sge, *ip_fle, *op_fle;
298         struct sec_flow_context *flc;
299         uint32_t auth_only_len = sess->ext_params.aead_ctxt.auth_only_len;
300         int icv_len = sess->digest_length;
301         uint8_t *old_icv;
302         struct rte_mbuf *mbuf;
303         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
304                         sess->iv.offset);
305
306         if (sym_op->m_dst)
307                 mbuf = sym_op->m_dst;
308         else
309                 mbuf = sym_op->m_src;
310
311         /* first FLE entry used to store mbuf and session ctxt */
312         fle = (struct qbman_fle *)rte_malloc(NULL,
313                         FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs),
314                         RTE_CACHE_LINE_SIZE);
315         if (unlikely(!fle)) {
316                 DPAA2_SEC_ERR("GCM SG: Memory alloc failed for SGE");
317                 return -ENOMEM;
318         }
319         memset(fle, 0, FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs));
320         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
321         DPAA2_FLE_SAVE_CTXT(fle, (size_t)priv);
322
323         op_fle = fle + 1;
324         ip_fle = fle + 2;
325         sge = fle + 3;
326
327         /* Save the shared descriptor */
328         flc = &priv->flc_desc[0].flc;
329
330         /* Configure FD as a FRAME LIST */
331         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
332         DPAA2_SET_FD_COMPOUND_FMT(fd);
333         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
334
335         DPAA2_SEC_DP_DEBUG("GCM SG: auth_off: 0x%x/length %d, digest-len=%d\n"
336                    "iv-len=%d data_off: 0x%x\n",
337                    sym_op->aead.data.offset,
338                    sym_op->aead.data.length,
339                    sess->digest_length,
340                    sess->iv.length,
341                    sym_op->m_src->data_off);
342
343         /* Configure Output FLE with Scatter/Gather Entry */
344         DPAA2_SET_FLE_SG_EXT(op_fle);
345         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_VADDR_TO_IOVA(sge));
346
347         if (auth_only_len)
348                 DPAA2_SET_FLE_INTERNAL_JD(op_fle, auth_only_len);
349
350         op_fle->length = (sess->dir == DIR_ENC) ?
351                         (sym_op->aead.data.length + icv_len) :
352                         sym_op->aead.data.length;
353
354         /* Configure Output SGE for Encap/Decap */
355         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
356         DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off + sym_op->aead.data.offset);
357         sge->length = mbuf->data_len - sym_op->aead.data.offset;
358
359         mbuf = mbuf->next;
360         /* o/p segs */
361         while (mbuf) {
362                 sge++;
363                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
364                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
365                 sge->length = mbuf->data_len;
366                 mbuf = mbuf->next;
367         }
368         sge->length -= icv_len;
369
370         if (sess->dir == DIR_ENC) {
371                 sge++;
372                 DPAA2_SET_FLE_ADDR(sge,
373                                 DPAA2_VADDR_TO_IOVA(sym_op->aead.digest.data));
374                 sge->length = icv_len;
375         }
376         DPAA2_SET_FLE_FIN(sge);
377
378         sge++;
379         mbuf = sym_op->m_src;
380
381         /* Configure Input FLE with Scatter/Gather Entry */
382         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
383         DPAA2_SET_FLE_SG_EXT(ip_fle);
384         DPAA2_SET_FLE_FIN(ip_fle);
385         ip_fle->length = (sess->dir == DIR_ENC) ?
386                 (sym_op->aead.data.length + sess->iv.length + auth_only_len) :
387                 (sym_op->aead.data.length + sess->iv.length + auth_only_len +
388                  icv_len);
389
390         /* Configure Input SGE for Encap/Decap */
391         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(IV_ptr));
392         sge->length = sess->iv.length;
393
394         sge++;
395         if (auth_only_len) {
396                 DPAA2_SET_FLE_ADDR(sge,
397                                 DPAA2_VADDR_TO_IOVA(sym_op->aead.aad.data));
398                 sge->length = auth_only_len;
399                 sge++;
400         }
401
402         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
403         DPAA2_SET_FLE_OFFSET(sge, sym_op->aead.data.offset +
404                                 mbuf->data_off);
405         sge->length = mbuf->data_len - sym_op->aead.data.offset;
406
407         mbuf = mbuf->next;
408         /* i/p segs */
409         while (mbuf) {
410                 sge++;
411                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
412                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
413                 sge->length = mbuf->data_len;
414                 mbuf = mbuf->next;
415         }
416
417         if (sess->dir == DIR_DEC) {
418                 sge++;
419                 old_icv = (uint8_t *)(sge + 1);
420                 memcpy(old_icv, sym_op->aead.digest.data, icv_len);
421                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_icv));
422                 sge->length = icv_len;
423         }
424
425         DPAA2_SET_FLE_FIN(sge);
426         if (auth_only_len) {
427                 DPAA2_SET_FLE_INTERNAL_JD(ip_fle, auth_only_len);
428                 DPAA2_SET_FD_INTERNAL_JD(fd, auth_only_len);
429         }
430         DPAA2_SET_FD_LEN(fd, ip_fle->length);
431
432         return 0;
433 }
434
435 static inline int
436 build_authenc_gcm_fd(dpaa2_sec_session *sess,
437                      struct rte_crypto_op *op,
438                      struct qbman_fd *fd, uint16_t bpid)
439 {
440         struct rte_crypto_sym_op *sym_op = op->sym;
441         struct ctxt_priv *priv = sess->ctxt;
442         struct qbman_fle *fle, *sge;
443         struct sec_flow_context *flc;
444         uint32_t auth_only_len = sess->ext_params.aead_ctxt.auth_only_len;
445         int icv_len = sess->digest_length, retval;
446         uint8_t *old_icv;
447         struct rte_mbuf *dst;
448         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
449                         sess->iv.offset);
450
451         if (sym_op->m_dst)
452                 dst = sym_op->m_dst;
453         else
454                 dst = sym_op->m_src;
455
456         /* TODO we are using the first FLE entry to store Mbuf and session ctxt.
457          * Currently we donot know which FLE has the mbuf stored.
458          * So while retreiving we can go back 1 FLE from the FD -ADDR
459          * to get the MBUF Addr from the previous FLE.
460          * We can have a better approach to use the inline Mbuf
461          */
462         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
463         if (retval) {
464                 DPAA2_SEC_ERR("GCM: Memory alloc failed for SGE");
465                 return -ENOMEM;
466         }
467         memset(fle, 0, FLE_POOL_BUF_SIZE);
468         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
469         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
470         fle = fle + 1;
471         sge = fle + 2;
472         if (likely(bpid < MAX_BPID)) {
473                 DPAA2_SET_FD_BPID(fd, bpid);
474                 DPAA2_SET_FLE_BPID(fle, bpid);
475                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
476                 DPAA2_SET_FLE_BPID(sge, bpid);
477                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
478                 DPAA2_SET_FLE_BPID(sge + 2, bpid);
479                 DPAA2_SET_FLE_BPID(sge + 3, bpid);
480         } else {
481                 DPAA2_SET_FD_IVP(fd);
482                 DPAA2_SET_FLE_IVP(fle);
483                 DPAA2_SET_FLE_IVP((fle + 1));
484                 DPAA2_SET_FLE_IVP(sge);
485                 DPAA2_SET_FLE_IVP((sge + 1));
486                 DPAA2_SET_FLE_IVP((sge + 2));
487                 DPAA2_SET_FLE_IVP((sge + 3));
488         }
489
490         /* Save the shared descriptor */
491         flc = &priv->flc_desc[0].flc;
492         /* Configure FD as a FRAME LIST */
493         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
494         DPAA2_SET_FD_COMPOUND_FMT(fd);
495         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
496
497         DPAA2_SEC_DP_DEBUG("GCM: auth_off: 0x%x/length %d, digest-len=%d\n"
498                    "iv-len=%d data_off: 0x%x\n",
499                    sym_op->aead.data.offset,
500                    sym_op->aead.data.length,
501                    sess->digest_length,
502                    sess->iv.length,
503                    sym_op->m_src->data_off);
504
505         /* Configure Output FLE with Scatter/Gather Entry */
506         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
507         if (auth_only_len)
508                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
509         fle->length = (sess->dir == DIR_ENC) ?
510                         (sym_op->aead.data.length + icv_len) :
511                         sym_op->aead.data.length;
512
513         DPAA2_SET_FLE_SG_EXT(fle);
514
515         /* Configure Output SGE for Encap/Decap */
516         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(dst));
517         DPAA2_SET_FLE_OFFSET(sge, dst->data_off + sym_op->aead.data.offset);
518         sge->length = sym_op->aead.data.length;
519
520         if (sess->dir == DIR_ENC) {
521                 sge++;
522                 DPAA2_SET_FLE_ADDR(sge,
523                                 DPAA2_VADDR_TO_IOVA(sym_op->aead.digest.data));
524                 sge->length = sess->digest_length;
525         }
526         DPAA2_SET_FLE_FIN(sge);
527
528         sge++;
529         fle++;
530
531         /* Configure Input FLE with Scatter/Gather Entry */
532         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
533         DPAA2_SET_FLE_SG_EXT(fle);
534         DPAA2_SET_FLE_FIN(fle);
535         fle->length = (sess->dir == DIR_ENC) ?
536                 (sym_op->aead.data.length + sess->iv.length + auth_only_len) :
537                 (sym_op->aead.data.length + sess->iv.length + auth_only_len +
538                  sess->digest_length);
539
540         /* Configure Input SGE for Encap/Decap */
541         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(IV_ptr));
542         sge->length = sess->iv.length;
543         sge++;
544         if (auth_only_len) {
545                 DPAA2_SET_FLE_ADDR(sge,
546                                 DPAA2_VADDR_TO_IOVA(sym_op->aead.aad.data));
547                 sge->length = auth_only_len;
548                 DPAA2_SET_FLE_BPID(sge, bpid);
549                 sge++;
550         }
551
552         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
553         DPAA2_SET_FLE_OFFSET(sge, sym_op->aead.data.offset +
554                                 sym_op->m_src->data_off);
555         sge->length = sym_op->aead.data.length;
556         if (sess->dir == DIR_DEC) {
557                 sge++;
558                 old_icv = (uint8_t *)(sge + 1);
559                 memcpy(old_icv, sym_op->aead.digest.data,
560                        sess->digest_length);
561                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_icv));
562                 sge->length = sess->digest_length;
563         }
564         DPAA2_SET_FLE_FIN(sge);
565
566         if (auth_only_len) {
567                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
568                 DPAA2_SET_FD_INTERNAL_JD(fd, auth_only_len);
569         }
570
571         DPAA2_SET_FD_LEN(fd, fle->length);
572         return 0;
573 }
574
575 static inline int
576 build_authenc_sg_fd(dpaa2_sec_session *sess,
577                  struct rte_crypto_op *op,
578                  struct qbman_fd *fd, __rte_unused uint16_t bpid)
579 {
580         struct rte_crypto_sym_op *sym_op = op->sym;
581         struct ctxt_priv *priv = sess->ctxt;
582         struct qbman_fle *fle, *sge, *ip_fle, *op_fle;
583         struct sec_flow_context *flc;
584         uint16_t auth_hdr_len = sym_op->cipher.data.offset -
585                                 sym_op->auth.data.offset;
586         uint16_t auth_tail_len = sym_op->auth.data.length -
587                                 sym_op->cipher.data.length - auth_hdr_len;
588         uint32_t auth_only_len = (auth_tail_len << 16) | auth_hdr_len;
589         int icv_len = sess->digest_length;
590         uint8_t *old_icv;
591         struct rte_mbuf *mbuf;
592         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
593                         sess->iv.offset);
594
595         if (sym_op->m_dst)
596                 mbuf = sym_op->m_dst;
597         else
598                 mbuf = sym_op->m_src;
599
600         /* first FLE entry used to store mbuf and session ctxt */
601         fle = (struct qbman_fle *)rte_malloc(NULL,
602                         FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs),
603                         RTE_CACHE_LINE_SIZE);
604         if (unlikely(!fle)) {
605                 DPAA2_SEC_ERR("AUTHENC SG: Memory alloc failed for SGE");
606                 return -ENOMEM;
607         }
608         memset(fle, 0, FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs));
609         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
610         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
611
612         op_fle = fle + 1;
613         ip_fle = fle + 2;
614         sge = fle + 3;
615
616         /* Save the shared descriptor */
617         flc = &priv->flc_desc[0].flc;
618
619         /* Configure FD as a FRAME LIST */
620         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
621         DPAA2_SET_FD_COMPOUND_FMT(fd);
622         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
623
624         DPAA2_SEC_DP_DEBUG(
625                 "AUTHENC SG: auth_off: 0x%x/length %d, digest-len=%d\n"
626                 "cipher_off: 0x%x/length %d, iv-len=%d data_off: 0x%x\n",
627                 sym_op->auth.data.offset,
628                 sym_op->auth.data.length,
629                 sess->digest_length,
630                 sym_op->cipher.data.offset,
631                 sym_op->cipher.data.length,
632                 sess->iv.length,
633                 sym_op->m_src->data_off);
634
635         /* Configure Output FLE with Scatter/Gather Entry */
636         DPAA2_SET_FLE_SG_EXT(op_fle);
637         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_VADDR_TO_IOVA(sge));
638
639         if (auth_only_len)
640                 DPAA2_SET_FLE_INTERNAL_JD(op_fle, auth_only_len);
641
642         op_fle->length = (sess->dir == DIR_ENC) ?
643                         (sym_op->cipher.data.length + icv_len) :
644                         sym_op->cipher.data.length;
645
646         /* Configure Output SGE for Encap/Decap */
647         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
648         DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off + sym_op->auth.data.offset);
649         sge->length = mbuf->data_len - sym_op->auth.data.offset;
650
651         mbuf = mbuf->next;
652         /* o/p segs */
653         while (mbuf) {
654                 sge++;
655                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
656                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
657                 sge->length = mbuf->data_len;
658                 mbuf = mbuf->next;
659         }
660         sge->length -= icv_len;
661
662         if (sess->dir == DIR_ENC) {
663                 sge++;
664                 DPAA2_SET_FLE_ADDR(sge,
665                                 DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
666                 sge->length = icv_len;
667         }
668         DPAA2_SET_FLE_FIN(sge);
669
670         sge++;
671         mbuf = sym_op->m_src;
672
673         /* Configure Input FLE with Scatter/Gather Entry */
674         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
675         DPAA2_SET_FLE_SG_EXT(ip_fle);
676         DPAA2_SET_FLE_FIN(ip_fle);
677         ip_fle->length = (sess->dir == DIR_ENC) ?
678                         (sym_op->auth.data.length + sess->iv.length) :
679                         (sym_op->auth.data.length + sess->iv.length +
680                          icv_len);
681
682         /* Configure Input SGE for Encap/Decap */
683         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
684         sge->length = sess->iv.length;
685
686         sge++;
687         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
688         DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset +
689                                 mbuf->data_off);
690         sge->length = mbuf->data_len - sym_op->auth.data.offset;
691
692         mbuf = mbuf->next;
693         /* i/p segs */
694         while (mbuf) {
695                 sge++;
696                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
697                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
698                 sge->length = mbuf->data_len;
699                 mbuf = mbuf->next;
700         }
701         sge->length -= icv_len;
702
703         if (sess->dir == DIR_DEC) {
704                 sge++;
705                 old_icv = (uint8_t *)(sge + 1);
706                 memcpy(old_icv, sym_op->auth.digest.data,
707                        icv_len);
708                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_icv));
709                 sge->length = icv_len;
710         }
711
712         DPAA2_SET_FLE_FIN(sge);
713         if (auth_only_len) {
714                 DPAA2_SET_FLE_INTERNAL_JD(ip_fle, auth_only_len);
715                 DPAA2_SET_FD_INTERNAL_JD(fd, auth_only_len);
716         }
717         DPAA2_SET_FD_LEN(fd, ip_fle->length);
718
719         return 0;
720 }
721
722 static inline int
723 build_authenc_fd(dpaa2_sec_session *sess,
724                  struct rte_crypto_op *op,
725                  struct qbman_fd *fd, uint16_t bpid)
726 {
727         struct rte_crypto_sym_op *sym_op = op->sym;
728         struct ctxt_priv *priv = sess->ctxt;
729         struct qbman_fle *fle, *sge;
730         struct sec_flow_context *flc;
731         uint16_t auth_hdr_len = sym_op->cipher.data.offset -
732                                 sym_op->auth.data.offset;
733         uint16_t auth_tail_len = sym_op->auth.data.length -
734                                 sym_op->cipher.data.length - auth_hdr_len;
735         uint32_t auth_only_len = (auth_tail_len << 16) | auth_hdr_len;
736
737         int icv_len = sess->digest_length, retval;
738         uint8_t *old_icv;
739         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
740                         sess->iv.offset);
741         struct rte_mbuf *dst;
742
743         if (sym_op->m_dst)
744                 dst = sym_op->m_dst;
745         else
746                 dst = sym_op->m_src;
747
748         /* we are using the first FLE entry to store Mbuf.
749          * Currently we donot know which FLE has the mbuf stored.
750          * So while retreiving we can go back 1 FLE from the FD -ADDR
751          * to get the MBUF Addr from the previous FLE.
752          * We can have a better approach to use the inline Mbuf
753          */
754         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
755         if (retval) {
756                 DPAA2_SEC_ERR("Memory alloc failed for SGE");
757                 return -ENOMEM;
758         }
759         memset(fle, 0, FLE_POOL_BUF_SIZE);
760         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
761         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
762         fle = fle + 1;
763         sge = fle + 2;
764         if (likely(bpid < MAX_BPID)) {
765                 DPAA2_SET_FD_BPID(fd, bpid);
766                 DPAA2_SET_FLE_BPID(fle, bpid);
767                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
768                 DPAA2_SET_FLE_BPID(sge, bpid);
769                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
770                 DPAA2_SET_FLE_BPID(sge + 2, bpid);
771                 DPAA2_SET_FLE_BPID(sge + 3, bpid);
772         } else {
773                 DPAA2_SET_FD_IVP(fd);
774                 DPAA2_SET_FLE_IVP(fle);
775                 DPAA2_SET_FLE_IVP((fle + 1));
776                 DPAA2_SET_FLE_IVP(sge);
777                 DPAA2_SET_FLE_IVP((sge + 1));
778                 DPAA2_SET_FLE_IVP((sge + 2));
779                 DPAA2_SET_FLE_IVP((sge + 3));
780         }
781
782         /* Save the shared descriptor */
783         flc = &priv->flc_desc[0].flc;
784         /* Configure FD as a FRAME LIST */
785         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
786         DPAA2_SET_FD_COMPOUND_FMT(fd);
787         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
788
789         DPAA2_SEC_DP_DEBUG(
790                 "AUTHENC: auth_off: 0x%x/length %d, digest-len=%d\n"
791                 "cipher_off: 0x%x/length %d, iv-len=%d data_off: 0x%x\n",
792                 sym_op->auth.data.offset,
793                 sym_op->auth.data.length,
794                 sess->digest_length,
795                 sym_op->cipher.data.offset,
796                 sym_op->cipher.data.length,
797                 sess->iv.length,
798                 sym_op->m_src->data_off);
799
800         /* Configure Output FLE with Scatter/Gather Entry */
801         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
802         if (auth_only_len)
803                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
804         fle->length = (sess->dir == DIR_ENC) ?
805                         (sym_op->cipher.data.length + icv_len) :
806                         sym_op->cipher.data.length;
807
808         DPAA2_SET_FLE_SG_EXT(fle);
809
810         /* Configure Output SGE for Encap/Decap */
811         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(dst));
812         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset +
813                                 dst->data_off);
814         sge->length = sym_op->cipher.data.length;
815
816         if (sess->dir == DIR_ENC) {
817                 sge++;
818                 DPAA2_SET_FLE_ADDR(sge,
819                                 DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
820                 sge->length = sess->digest_length;
821                 DPAA2_SET_FD_LEN(fd, (sym_op->auth.data.length +
822                                         sess->iv.length));
823         }
824         DPAA2_SET_FLE_FIN(sge);
825
826         sge++;
827         fle++;
828
829         /* Configure Input FLE with Scatter/Gather Entry */
830         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
831         DPAA2_SET_FLE_SG_EXT(fle);
832         DPAA2_SET_FLE_FIN(fle);
833         fle->length = (sess->dir == DIR_ENC) ?
834                         (sym_op->auth.data.length + sess->iv.length) :
835                         (sym_op->auth.data.length + sess->iv.length +
836                          sess->digest_length);
837
838         /* Configure Input SGE for Encap/Decap */
839         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
840         sge->length = sess->iv.length;
841         sge++;
842
843         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
844         DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset +
845                                 sym_op->m_src->data_off);
846         sge->length = sym_op->auth.data.length;
847         if (sess->dir == DIR_DEC) {
848                 sge++;
849                 old_icv = (uint8_t *)(sge + 1);
850                 memcpy(old_icv, sym_op->auth.digest.data,
851                        sess->digest_length);
852                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_icv));
853                 sge->length = sess->digest_length;
854                 DPAA2_SET_FD_LEN(fd, (sym_op->auth.data.length +
855                                  sess->digest_length +
856                                  sess->iv.length));
857         }
858         DPAA2_SET_FLE_FIN(sge);
859         if (auth_only_len) {
860                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
861                 DPAA2_SET_FD_INTERNAL_JD(fd, auth_only_len);
862         }
863         return 0;
864 }
865
866 static inline int build_auth_sg_fd(
867                 dpaa2_sec_session *sess,
868                 struct rte_crypto_op *op,
869                 struct qbman_fd *fd,
870                 __rte_unused uint16_t bpid)
871 {
872         struct rte_crypto_sym_op *sym_op = op->sym;
873         struct qbman_fle *fle, *sge, *ip_fle, *op_fle;
874         struct sec_flow_context *flc;
875         struct ctxt_priv *priv = sess->ctxt;
876         int data_len, data_offset;
877         uint8_t *old_digest;
878         struct rte_mbuf *mbuf;
879
880         data_len = sym_op->auth.data.length;
881         data_offset = sym_op->auth.data.offset;
882
883         if (sess->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
884             sess->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
885                 if ((data_len & 7) || (data_offset & 7)) {
886                         DPAA2_SEC_ERR("AUTH: len/offset must be full bytes");
887                         return -ENOTSUP;
888                 }
889
890                 data_len = data_len >> 3;
891                 data_offset = data_offset >> 3;
892         }
893
894         mbuf = sym_op->m_src;
895         fle = (struct qbman_fle *)rte_malloc(NULL,
896                         FLE_SG_MEM_SIZE(mbuf->nb_segs),
897                         RTE_CACHE_LINE_SIZE);
898         if (unlikely(!fle)) {
899                 DPAA2_SEC_ERR("AUTH SG: Memory alloc failed for SGE");
900                 return -ENOMEM;
901         }
902         memset(fle, 0, FLE_SG_MEM_SIZE(mbuf->nb_segs));
903         /* first FLE entry used to store mbuf and session ctxt */
904         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
905         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
906         op_fle = fle + 1;
907         ip_fle = fle + 2;
908         sge = fle + 3;
909
910         flc = &priv->flc_desc[DESC_INITFINAL].flc;
911         /* sg FD */
912         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
913         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
914         DPAA2_SET_FD_COMPOUND_FMT(fd);
915
916         /* o/p fle */
917         DPAA2_SET_FLE_ADDR(op_fle,
918                                 DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
919         op_fle->length = sess->digest_length;
920
921         /* i/p fle */
922         DPAA2_SET_FLE_SG_EXT(ip_fle);
923         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
924         ip_fle->length = data_len;
925
926         if (sess->iv.length) {
927                 uint8_t *iv_ptr;
928
929                 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
930                                                    sess->iv.offset);
931
932                 if (sess->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
933                         iv_ptr = conv_to_snow_f9_iv(iv_ptr);
934                         sge->length = 12;
935                 } else if (sess->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
936                         iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
937                         sge->length = 8;
938                 } else {
939                         sge->length = sess->iv.length;
940                 }
941                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
942                 ip_fle->length += sge->length;
943                 sge++;
944         }
945         /* i/p 1st seg */
946         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
947         DPAA2_SET_FLE_OFFSET(sge, data_offset + mbuf->data_off);
948
949         if (data_len <= (mbuf->data_len - data_offset)) {
950                 sge->length = data_len;
951                 data_len = 0;
952         } else {
953                 sge->length = mbuf->data_len - data_offset;
954
955                 /* remaining i/p segs */
956                 while ((data_len = data_len - sge->length) &&
957                        (mbuf = mbuf->next)) {
958                         sge++;
959                         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
960                         DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
961                         if (data_len > mbuf->data_len)
962                                 sge->length = mbuf->data_len;
963                         else
964                                 sge->length = data_len;
965                 }
966         }
967
968         if (sess->dir == DIR_DEC) {
969                 /* Digest verification case */
970                 sge++;
971                 old_digest = (uint8_t *)(sge + 1);
972                 rte_memcpy(old_digest, sym_op->auth.digest.data,
973                            sess->digest_length);
974                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_digest));
975                 sge->length = sess->digest_length;
976                 ip_fle->length += sess->digest_length;
977         }
978         DPAA2_SET_FLE_FIN(sge);
979         DPAA2_SET_FLE_FIN(ip_fle);
980         DPAA2_SET_FD_LEN(fd, ip_fle->length);
981
982         return 0;
983 }
984
985 static inline int
986 build_auth_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
987               struct qbman_fd *fd, uint16_t bpid)
988 {
989         struct rte_crypto_sym_op *sym_op = op->sym;
990         struct qbman_fle *fle, *sge;
991         struct sec_flow_context *flc;
992         struct ctxt_priv *priv = sess->ctxt;
993         int data_len, data_offset;
994         uint8_t *old_digest;
995         int retval;
996
997         data_len = sym_op->auth.data.length;
998         data_offset = sym_op->auth.data.offset;
999
1000         if (sess->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
1001             sess->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1002                 if ((data_len & 7) || (data_offset & 7)) {
1003                         DPAA2_SEC_ERR("AUTH: len/offset must be full bytes");
1004                         return -ENOTSUP;
1005                 }
1006
1007                 data_len = data_len >> 3;
1008                 data_offset = data_offset >> 3;
1009         }
1010
1011         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
1012         if (retval) {
1013                 DPAA2_SEC_ERR("AUTH Memory alloc failed for SGE");
1014                 return -ENOMEM;
1015         }
1016         memset(fle, 0, FLE_POOL_BUF_SIZE);
1017         /* TODO we are using the first FLE entry to store Mbuf.
1018          * Currently we donot know which FLE has the mbuf stored.
1019          * So while retreiving we can go back 1 FLE from the FD -ADDR
1020          * to get the MBUF Addr from the previous FLE.
1021          * We can have a better approach to use the inline Mbuf
1022          */
1023         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
1024         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
1025         fle = fle + 1;
1026         sge = fle + 2;
1027
1028         if (likely(bpid < MAX_BPID)) {
1029                 DPAA2_SET_FD_BPID(fd, bpid);
1030                 DPAA2_SET_FLE_BPID(fle, bpid);
1031                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
1032                 DPAA2_SET_FLE_BPID(sge, bpid);
1033                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
1034         } else {
1035                 DPAA2_SET_FD_IVP(fd);
1036                 DPAA2_SET_FLE_IVP(fle);
1037                 DPAA2_SET_FLE_IVP((fle + 1));
1038                 DPAA2_SET_FLE_IVP(sge);
1039                 DPAA2_SET_FLE_IVP((sge + 1));
1040         }
1041
1042         flc = &priv->flc_desc[DESC_INITFINAL].flc;
1043         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
1044         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
1045         DPAA2_SET_FD_COMPOUND_FMT(fd);
1046
1047         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
1048         fle->length = sess->digest_length;
1049         fle++;
1050
1051         /* Setting input FLE */
1052         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
1053         DPAA2_SET_FLE_SG_EXT(fle);
1054         fle->length = data_len;
1055
1056         if (sess->iv.length) {
1057                 uint8_t *iv_ptr;
1058
1059                 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1060                                                    sess->iv.offset);
1061
1062                 if (sess->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
1063                         iv_ptr = conv_to_snow_f9_iv(iv_ptr);
1064                         sge->length = 12;
1065                 } else if (sess->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1066                         iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
1067                         sge->length = 8;
1068                 } else {
1069                         sge->length = sess->iv.length;
1070                 }
1071
1072                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
1073                 fle->length = fle->length + sge->length;
1074                 sge++;
1075         }
1076
1077         /* Setting data to authenticate */
1078         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
1079         DPAA2_SET_FLE_OFFSET(sge, data_offset + sym_op->m_src->data_off);
1080         sge->length = data_len;
1081
1082         if (sess->dir == DIR_DEC) {
1083                 sge++;
1084                 old_digest = (uint8_t *)(sge + 1);
1085                 rte_memcpy(old_digest, sym_op->auth.digest.data,
1086                            sess->digest_length);
1087                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_digest));
1088                 sge->length = sess->digest_length;
1089                 fle->length = fle->length + sess->digest_length;
1090         }
1091
1092         DPAA2_SET_FLE_FIN(sge);
1093         DPAA2_SET_FLE_FIN(fle);
1094         DPAA2_SET_FD_LEN(fd, fle->length);
1095
1096         return 0;
1097 }
1098
1099 static int
1100 build_cipher_sg_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
1101                 struct qbman_fd *fd, __rte_unused uint16_t bpid)
1102 {
1103         struct rte_crypto_sym_op *sym_op = op->sym;
1104         struct qbman_fle *ip_fle, *op_fle, *sge, *fle;
1105         int data_len, data_offset;
1106         struct sec_flow_context *flc;
1107         struct ctxt_priv *priv = sess->ctxt;
1108         struct rte_mbuf *mbuf;
1109         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1110                         sess->iv.offset);
1111
1112         data_len = sym_op->cipher.data.length;
1113         data_offset = sym_op->cipher.data.offset;
1114
1115         if (sess->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1116                 sess->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1117                 if ((data_len & 7) || (data_offset & 7)) {
1118                         DPAA2_SEC_ERR("CIPHER: len/offset must be full bytes");
1119                         return -ENOTSUP;
1120                 }
1121
1122                 data_len = data_len >> 3;
1123                 data_offset = data_offset >> 3;
1124         }
1125
1126         if (sym_op->m_dst)
1127                 mbuf = sym_op->m_dst;
1128         else
1129                 mbuf = sym_op->m_src;
1130
1131         /* first FLE entry used to store mbuf and session ctxt */
1132         fle = (struct qbman_fle *)rte_malloc(NULL,
1133                         FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs),
1134                         RTE_CACHE_LINE_SIZE);
1135         if (!fle) {
1136                 DPAA2_SEC_ERR("CIPHER SG: Memory alloc failed for SGE");
1137                 return -ENOMEM;
1138         }
1139         memset(fle, 0, FLE_SG_MEM_SIZE(mbuf->nb_segs + sym_op->m_src->nb_segs));
1140         /* first FLE entry used to store mbuf and session ctxt */
1141         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
1142         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
1143
1144         op_fle = fle + 1;
1145         ip_fle = fle + 2;
1146         sge = fle + 3;
1147
1148         flc = &priv->flc_desc[0].flc;
1149
1150         DPAA2_SEC_DP_DEBUG(
1151                 "CIPHER SG: cipher_off: 0x%x/length %d, ivlen=%d"
1152                 " data_off: 0x%x\n",
1153                 data_offset,
1154                 data_len,
1155                 sess->iv.length,
1156                 sym_op->m_src->data_off);
1157
1158         /* o/p fle */
1159         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_VADDR_TO_IOVA(sge));
1160         op_fle->length = data_len;
1161         DPAA2_SET_FLE_SG_EXT(op_fle);
1162
1163         /* o/p 1st seg */
1164         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1165         DPAA2_SET_FLE_OFFSET(sge, data_offset + mbuf->data_off);
1166         sge->length = mbuf->data_len - data_offset;
1167
1168         mbuf = mbuf->next;
1169         /* o/p segs */
1170         while (mbuf) {
1171                 sge++;
1172                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1173                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
1174                 sge->length = mbuf->data_len;
1175                 mbuf = mbuf->next;
1176         }
1177         DPAA2_SET_FLE_FIN(sge);
1178
1179         DPAA2_SEC_DP_DEBUG(
1180                 "CIPHER SG: 1 - flc = %p, fle = %p FLEaddr = %x-%x, len %d\n",
1181                 flc, fle, fle->addr_hi, fle->addr_lo,
1182                 fle->length);
1183
1184         /* i/p fle */
1185         mbuf = sym_op->m_src;
1186         sge++;
1187         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
1188         ip_fle->length = sess->iv.length + data_len;
1189         DPAA2_SET_FLE_SG_EXT(ip_fle);
1190
1191         /* i/p IV */
1192         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
1193         DPAA2_SET_FLE_OFFSET(sge, 0);
1194         sge->length = sess->iv.length;
1195
1196         sge++;
1197
1198         /* i/p 1st seg */
1199         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1200         DPAA2_SET_FLE_OFFSET(sge, data_offset + mbuf->data_off);
1201         sge->length = mbuf->data_len - data_offset;
1202
1203         mbuf = mbuf->next;
1204         /* i/p segs */
1205         while (mbuf) {
1206                 sge++;
1207                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1208                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
1209                 sge->length = mbuf->data_len;
1210                 mbuf = mbuf->next;
1211         }
1212         DPAA2_SET_FLE_FIN(sge);
1213         DPAA2_SET_FLE_FIN(ip_fle);
1214
1215         /* sg fd */
1216         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
1217         DPAA2_SET_FD_LEN(fd, ip_fle->length);
1218         DPAA2_SET_FD_COMPOUND_FMT(fd);
1219         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
1220
1221         DPAA2_SEC_DP_DEBUG(
1222                 "CIPHER SG: fdaddr =%" PRIx64 " bpid =%d meta =%d"
1223                 " off =%d, len =%d\n",
1224                 DPAA2_GET_FD_ADDR(fd),
1225                 DPAA2_GET_FD_BPID(fd),
1226                 rte_dpaa2_bpid_info[bpid].meta_data_size,
1227                 DPAA2_GET_FD_OFFSET(fd),
1228                 DPAA2_GET_FD_LEN(fd));
1229         return 0;
1230 }
1231
1232 static int
1233 build_cipher_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
1234                 struct qbman_fd *fd, uint16_t bpid)
1235 {
1236         struct rte_crypto_sym_op *sym_op = op->sym;
1237         struct qbman_fle *fle, *sge;
1238         int retval, data_len, data_offset;
1239         struct sec_flow_context *flc;
1240         struct ctxt_priv *priv = sess->ctxt;
1241         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1242                         sess->iv.offset);
1243         struct rte_mbuf *dst;
1244
1245         data_len = sym_op->cipher.data.length;
1246         data_offset = sym_op->cipher.data.offset;
1247
1248         if (sess->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1249                 sess->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1250                 if ((data_len & 7) || (data_offset & 7)) {
1251                         DPAA2_SEC_ERR("CIPHER: len/offset must be full bytes");
1252                         return -ENOTSUP;
1253                 }
1254
1255                 data_len = data_len >> 3;
1256                 data_offset = data_offset >> 3;
1257         }
1258
1259         if (sym_op->m_dst)
1260                 dst = sym_op->m_dst;
1261         else
1262                 dst = sym_op->m_src;
1263
1264         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
1265         if (retval) {
1266                 DPAA2_SEC_ERR("CIPHER: Memory alloc failed for SGE");
1267                 return -ENOMEM;
1268         }
1269         memset(fle, 0, FLE_POOL_BUF_SIZE);
1270         /* TODO we are using the first FLE entry to store Mbuf.
1271          * Currently we donot know which FLE has the mbuf stored.
1272          * So while retreiving we can go back 1 FLE from the FD -ADDR
1273          * to get the MBUF Addr from the previous FLE.
1274          * We can have a better approach to use the inline Mbuf
1275          */
1276         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
1277         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
1278         fle = fle + 1;
1279         sge = fle + 2;
1280
1281         if (likely(bpid < MAX_BPID)) {
1282                 DPAA2_SET_FD_BPID(fd, bpid);
1283                 DPAA2_SET_FLE_BPID(fle, bpid);
1284                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
1285                 DPAA2_SET_FLE_BPID(sge, bpid);
1286                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
1287         } else {
1288                 DPAA2_SET_FD_IVP(fd);
1289                 DPAA2_SET_FLE_IVP(fle);
1290                 DPAA2_SET_FLE_IVP((fle + 1));
1291                 DPAA2_SET_FLE_IVP(sge);
1292                 DPAA2_SET_FLE_IVP((sge + 1));
1293         }
1294
1295         flc = &priv->flc_desc[0].flc;
1296         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
1297         DPAA2_SET_FD_LEN(fd, data_len + sess->iv.length);
1298         DPAA2_SET_FD_COMPOUND_FMT(fd);
1299         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
1300
1301         DPAA2_SEC_DP_DEBUG(
1302                 "CIPHER: cipher_off: 0x%x/length %d, ivlen=%d,"
1303                 " data_off: 0x%x\n",
1304                 data_offset,
1305                 data_len,
1306                 sess->iv.length,
1307                 sym_op->m_src->data_off);
1308
1309         DPAA2_SET_FLE_ADDR(fle, DPAA2_MBUF_VADDR_TO_IOVA(dst));
1310         DPAA2_SET_FLE_OFFSET(fle, data_offset + dst->data_off);
1311
1312         fle->length = data_len + sess->iv.length;
1313
1314         DPAA2_SEC_DP_DEBUG(
1315                 "CIPHER: 1 - flc = %p, fle = %p FLEaddr = %x-%x, length %d\n",
1316                 flc, fle, fle->addr_hi, fle->addr_lo,
1317                 fle->length);
1318
1319         fle++;
1320
1321         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
1322         fle->length = data_len + sess->iv.length;
1323
1324         DPAA2_SET_FLE_SG_EXT(fle);
1325
1326         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
1327         sge->length = sess->iv.length;
1328
1329         sge++;
1330         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
1331         DPAA2_SET_FLE_OFFSET(sge, data_offset + sym_op->m_src->data_off);
1332
1333         sge->length = data_len;
1334         DPAA2_SET_FLE_FIN(sge);
1335         DPAA2_SET_FLE_FIN(fle);
1336
1337         DPAA2_SEC_DP_DEBUG(
1338                 "CIPHER: fdaddr =%" PRIx64 " bpid =%d meta =%d"
1339                 " off =%d, len =%d\n",
1340                 DPAA2_GET_FD_ADDR(fd),
1341                 DPAA2_GET_FD_BPID(fd),
1342                 rte_dpaa2_bpid_info[bpid].meta_data_size,
1343                 DPAA2_GET_FD_OFFSET(fd),
1344                 DPAA2_GET_FD_LEN(fd));
1345
1346         return 0;
1347 }
1348
1349 static inline int
1350 build_sec_fd(struct rte_crypto_op *op,
1351              struct qbman_fd *fd, uint16_t bpid)
1352 {
1353         int ret = -1;
1354         dpaa2_sec_session *sess;
1355
1356         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
1357                 sess = (dpaa2_sec_session *)get_sym_session_private_data(
1358                                 op->sym->session, cryptodev_driver_id);
1359 #ifdef RTE_LIB_SECURITY
1360         else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1361                 sess = (dpaa2_sec_session *)get_sec_session_private_data(
1362                                 op->sym->sec_session);
1363 #endif
1364         else
1365                 return -ENOTSUP;
1366
1367         if (!sess)
1368                 return -EINVAL;
1369
1370         /* Any of the buffer is segmented*/
1371         if (!rte_pktmbuf_is_contiguous(op->sym->m_src) ||
1372                   ((op->sym->m_dst != NULL) &&
1373                    !rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1374                 switch (sess->ctxt_type) {
1375                 case DPAA2_SEC_CIPHER:
1376                         ret = build_cipher_sg_fd(sess, op, fd, bpid);
1377                         break;
1378                 case DPAA2_SEC_AUTH:
1379                         ret = build_auth_sg_fd(sess, op, fd, bpid);
1380                         break;
1381                 case DPAA2_SEC_AEAD:
1382                         ret = build_authenc_gcm_sg_fd(sess, op, fd, bpid);
1383                         break;
1384                 case DPAA2_SEC_CIPHER_HASH:
1385                         ret = build_authenc_sg_fd(sess, op, fd, bpid);
1386                         break;
1387 #ifdef RTE_LIB_SECURITY
1388                 case DPAA2_SEC_IPSEC:
1389                 case DPAA2_SEC_PDCP:
1390                         ret = build_proto_compound_sg_fd(sess, op, fd, bpid);
1391                         break;
1392 #endif
1393                 case DPAA2_SEC_HASH_CIPHER:
1394                 default:
1395                         DPAA2_SEC_ERR("error: Unsupported session");
1396                 }
1397         } else {
1398                 switch (sess->ctxt_type) {
1399                 case DPAA2_SEC_CIPHER:
1400                         ret = build_cipher_fd(sess, op, fd, bpid);
1401                         break;
1402                 case DPAA2_SEC_AUTH:
1403                         ret = build_auth_fd(sess, op, fd, bpid);
1404                         break;
1405                 case DPAA2_SEC_AEAD:
1406                         ret = build_authenc_gcm_fd(sess, op, fd, bpid);
1407                         break;
1408                 case DPAA2_SEC_CIPHER_HASH:
1409                         ret = build_authenc_fd(sess, op, fd, bpid);
1410                         break;
1411 #ifdef RTE_LIB_SECURITY
1412                 case DPAA2_SEC_IPSEC:
1413                         ret = build_proto_fd(sess, op, fd, bpid);
1414                         break;
1415                 case DPAA2_SEC_PDCP:
1416                         ret = build_proto_compound_fd(sess, op, fd, bpid);
1417                         break;
1418 #endif
1419                 case DPAA2_SEC_HASH_CIPHER:
1420                 default:
1421                         DPAA2_SEC_ERR("error: Unsupported session");
1422                         ret = -ENOTSUP;
1423                 }
1424         }
1425         return ret;
1426 }
1427
1428 static uint16_t
1429 dpaa2_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1430                         uint16_t nb_ops)
1431 {
1432         /* Function to transmit the frames to given device and VQ*/
1433         uint32_t loop;
1434         int32_t ret;
1435         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
1436         uint32_t frames_to_send, retry_count;
1437         struct qbman_eq_desc eqdesc;
1438         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1439         struct qbman_swp *swp;
1440         uint16_t num_tx = 0;
1441         uint32_t flags[MAX_TX_RING_SLOTS] = {0};
1442         /*todo - need to support multiple buffer pools */
1443         uint16_t bpid;
1444         struct rte_mempool *mb_pool;
1445
1446         if (unlikely(nb_ops == 0))
1447                 return 0;
1448
1449         if (ops[0]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1450                 DPAA2_SEC_ERR("sessionless crypto op not supported");
1451                 return 0;
1452         }
1453         /*Prepare enqueue descriptor*/
1454         qbman_eq_desc_clear(&eqdesc);
1455         qbman_eq_desc_set_no_orp(&eqdesc, DPAA2_EQ_RESP_ERR_FQ);
1456         qbman_eq_desc_set_response(&eqdesc, 0, 0);
1457         qbman_eq_desc_set_fq(&eqdesc, dpaa2_qp->tx_vq.fqid);
1458
1459         if (!DPAA2_PER_LCORE_DPIO) {
1460                 ret = dpaa2_affine_qbman_swp();
1461                 if (ret) {
1462                         DPAA2_SEC_ERR(
1463                                 "Failed to allocate IO portal, tid: %d\n",
1464                                 rte_gettid());
1465                         return 0;
1466                 }
1467         }
1468         swp = DPAA2_PER_LCORE_PORTAL;
1469
1470         while (nb_ops) {
1471                 frames_to_send = (nb_ops > dpaa2_eqcr_size) ?
1472                         dpaa2_eqcr_size : nb_ops;
1473
1474                 for (loop = 0; loop < frames_to_send; loop++) {
1475                         if (*dpaa2_seqn((*ops)->sym->m_src)) {
1476                                 uint8_t dqrr_index =
1477                                         *dpaa2_seqn((*ops)->sym->m_src) - 1;
1478
1479                                 flags[loop] = QBMAN_ENQUEUE_FLAG_DCA | dqrr_index;
1480                                 DPAA2_PER_LCORE_DQRR_SIZE--;
1481                                 DPAA2_PER_LCORE_DQRR_HELD &= ~(1 << dqrr_index);
1482                                 *dpaa2_seqn((*ops)->sym->m_src) =
1483                                         DPAA2_INVALID_MBUF_SEQN;
1484                         }
1485
1486                         /*Clear the unused FD fields before sending*/
1487                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
1488                         mb_pool = (*ops)->sym->m_src->pool;
1489                         bpid = mempool_to_bpid(mb_pool);
1490                         ret = build_sec_fd(*ops, &fd_arr[loop], bpid);
1491                         if (ret) {
1492                                 DPAA2_SEC_ERR("error: Improper packet contents"
1493                                               " for crypto operation");
1494                                 goto skip_tx;
1495                         }
1496                         ops++;
1497                 }
1498
1499                 loop = 0;
1500                 retry_count = 0;
1501                 while (loop < frames_to_send) {
1502                         ret = qbman_swp_enqueue_multiple(swp, &eqdesc,
1503                                                          &fd_arr[loop],
1504                                                          &flags[loop],
1505                                                          frames_to_send - loop);
1506                         if (unlikely(ret < 0)) {
1507                                 retry_count++;
1508                                 if (retry_count > DPAA2_MAX_TX_RETRY_COUNT) {
1509                                         num_tx += loop;
1510                                         nb_ops -= loop;
1511                                         goto skip_tx;
1512                                 }
1513                         } else {
1514                                 loop += ret;
1515                                 retry_count = 0;
1516                         }
1517                 }
1518
1519                 num_tx += loop;
1520                 nb_ops -= loop;
1521         }
1522 skip_tx:
1523         dpaa2_qp->tx_vq.tx_pkts += num_tx;
1524         dpaa2_qp->tx_vq.err_pkts += nb_ops;
1525         return num_tx;
1526 }
1527
1528 #ifdef RTE_LIB_SECURITY
1529 static inline struct rte_crypto_op *
1530 sec_simple_fd_to_mbuf(const struct qbman_fd *fd)
1531 {
1532         struct rte_crypto_op *op;
1533         uint16_t len = DPAA2_GET_FD_LEN(fd);
1534         int16_t diff = 0;
1535         dpaa2_sec_session *sess_priv __rte_unused;
1536
1537         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
1538                 DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd)),
1539                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
1540
1541         diff = len - mbuf->pkt_len;
1542         mbuf->pkt_len += diff;
1543         mbuf->data_len += diff;
1544         op = (struct rte_crypto_op *)(size_t)mbuf->buf_iova;
1545         mbuf->buf_iova = op->sym->aead.digest.phys_addr;
1546         op->sym->aead.digest.phys_addr = 0L;
1547
1548         sess_priv = (dpaa2_sec_session *)get_sec_session_private_data(
1549                                 op->sym->sec_session);
1550         if (sess_priv->dir == DIR_ENC)
1551                 mbuf->data_off += SEC_FLC_DHR_OUTBOUND;
1552         else
1553                 mbuf->data_off += SEC_FLC_DHR_INBOUND;
1554
1555         return op;
1556 }
1557 #endif
1558
1559 static inline struct rte_crypto_op *
1560 sec_fd_to_mbuf(const struct qbman_fd *fd)
1561 {
1562         struct qbman_fle *fle;
1563         struct rte_crypto_op *op;
1564         struct ctxt_priv *priv;
1565         struct rte_mbuf *dst, *src;
1566
1567 #ifdef RTE_LIB_SECURITY
1568         if (DPAA2_FD_GET_FORMAT(fd) == qbman_fd_single)
1569                 return sec_simple_fd_to_mbuf(fd);
1570 #endif
1571         fle = (struct qbman_fle *)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
1572
1573         DPAA2_SEC_DP_DEBUG("FLE addr = %x - %x, offset = %x\n",
1574                            fle->addr_hi, fle->addr_lo, fle->fin_bpid_offset);
1575
1576         /* we are using the first FLE entry to store Mbuf.
1577          * Currently we donot know which FLE has the mbuf stored.
1578          * So while retreiving we can go back 1 FLE from the FD -ADDR
1579          * to get the MBUF Addr from the previous FLE.
1580          * We can have a better approach to use the inline Mbuf
1581          */
1582
1583         if (unlikely(DPAA2_GET_FD_IVP(fd))) {
1584                 /* TODO complete it. */
1585                 DPAA2_SEC_ERR("error: non inline buffer");
1586                 return NULL;
1587         }
1588         op = (struct rte_crypto_op *)DPAA2_GET_FLE_ADDR((fle - 1));
1589
1590         /* Prefeth op */
1591         src = op->sym->m_src;
1592         rte_prefetch0(src);
1593
1594         if (op->sym->m_dst) {
1595                 dst = op->sym->m_dst;
1596                 rte_prefetch0(dst);
1597         } else
1598                 dst = src;
1599
1600 #ifdef RTE_LIB_SECURITY
1601         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1602                 uint16_t len = DPAA2_GET_FD_LEN(fd);
1603                 dst->pkt_len = len;
1604                 while (dst->next != NULL) {
1605                         len -= dst->data_len;
1606                         dst = dst->next;
1607                 }
1608                 dst->data_len = len;
1609         }
1610 #endif
1611         DPAA2_SEC_DP_DEBUG("mbuf %p BMAN buf addr %p,"
1612                 " fdaddr =%" PRIx64 " bpid =%d meta =%d off =%d, len =%d\n",
1613                 (void *)dst,
1614                 dst->buf_addr,
1615                 DPAA2_GET_FD_ADDR(fd),
1616                 DPAA2_GET_FD_BPID(fd),
1617                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
1618                 DPAA2_GET_FD_OFFSET(fd),
1619                 DPAA2_GET_FD_LEN(fd));
1620
1621         /* free the fle memory */
1622         if (likely(rte_pktmbuf_is_contiguous(src))) {
1623                 priv = (struct ctxt_priv *)(size_t)DPAA2_GET_FLE_CTXT(fle - 1);
1624                 rte_mempool_put(priv->fle_pool, (void *)(fle-1));
1625         } else
1626                 rte_free((void *)(fle-1));
1627
1628         return op;
1629 }
1630
1631 static uint16_t
1632 dpaa2_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1633                         uint16_t nb_ops)
1634 {
1635         /* Function is responsible to receive frames for a given device and VQ*/
1636         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1637         struct qbman_result *dq_storage;
1638         uint32_t fqid = dpaa2_qp->rx_vq.fqid;
1639         int ret, num_rx = 0;
1640         uint8_t is_last = 0, status;
1641         struct qbman_swp *swp;
1642         const struct qbman_fd *fd;
1643         struct qbman_pull_desc pulldesc;
1644
1645         if (!DPAA2_PER_LCORE_DPIO) {
1646                 ret = dpaa2_affine_qbman_swp();
1647                 if (ret) {
1648                         DPAA2_SEC_ERR(
1649                                 "Failed to allocate IO portal, tid: %d\n",
1650                                 rte_gettid());
1651                         return 0;
1652                 }
1653         }
1654         swp = DPAA2_PER_LCORE_PORTAL;
1655         dq_storage = dpaa2_qp->rx_vq.q_storage->dq_storage[0];
1656
1657         qbman_pull_desc_clear(&pulldesc);
1658         qbman_pull_desc_set_numframes(&pulldesc,
1659                                       (nb_ops > dpaa2_dqrr_size) ?
1660                                       dpaa2_dqrr_size : nb_ops);
1661         qbman_pull_desc_set_fq(&pulldesc, fqid);
1662         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
1663                                     (dma_addr_t)DPAA2_VADDR_TO_IOVA(dq_storage),
1664                                     1);
1665
1666         /*Issue a volatile dequeue command. */
1667         while (1) {
1668                 if (qbman_swp_pull(swp, &pulldesc)) {
1669                         DPAA2_SEC_WARN(
1670                                 "SEC VDQ command is not issued : QBMAN busy");
1671                         /* Portal was busy, try again */
1672                         continue;
1673                 }
1674                 break;
1675         };
1676
1677         /* Receive the packets till Last Dequeue entry is found with
1678          * respect to the above issues PULL command.
1679          */
1680         while (!is_last) {
1681                 /* Check if the previous issued command is completed.
1682                  * Also seems like the SWP is shared between the Ethernet Driver
1683                  * and the SEC driver.
1684                  */
1685                 while (!qbman_check_command_complete(dq_storage))
1686                         ;
1687
1688                 /* Loop until the dq_storage is updated with
1689                  * new token by QBMAN
1690                  */
1691                 while (!qbman_check_new_result(dq_storage))
1692                         ;
1693                 /* Check whether Last Pull command is Expired and
1694                  * setting Condition for Loop termination
1695                  */
1696                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
1697                         is_last = 1;
1698                         /* Check for valid frame. */
1699                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
1700                         if (unlikely(
1701                                 (status & QBMAN_DQ_STAT_VALIDFRAME) == 0)) {
1702                                 DPAA2_SEC_DP_DEBUG("No frame is delivered\n");
1703                                 continue;
1704                         }
1705                 }
1706
1707                 fd = qbman_result_DQ_fd(dq_storage);
1708                 ops[num_rx] = sec_fd_to_mbuf(fd);
1709
1710                 if (unlikely(fd->simple.frc)) {
1711                         /* TODO Parse SEC errors */
1712                         DPAA2_SEC_ERR("SEC returned Error - %x",
1713                                       fd->simple.frc);
1714                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_ERROR;
1715                 } else {
1716                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1717                 }
1718
1719                 num_rx++;
1720                 dq_storage++;
1721         } /* End of Packet Rx loop */
1722
1723         dpaa2_qp->rx_vq.rx_pkts += num_rx;
1724
1725         DPAA2_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1726         /*Return the total number of packets received to DPAA2 app*/
1727         return num_rx;
1728 }
1729
1730 /** Release queue pair */
1731 static int
1732 dpaa2_sec_queue_pair_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
1733 {
1734         struct dpaa2_sec_qp *qp =
1735                 (struct dpaa2_sec_qp *)dev->data->queue_pairs[queue_pair_id];
1736
1737         PMD_INIT_FUNC_TRACE();
1738
1739         if (qp->rx_vq.q_storage) {
1740                 dpaa2_free_dq_storage(qp->rx_vq.q_storage);
1741                 rte_free(qp->rx_vq.q_storage);
1742         }
1743         rte_free(qp);
1744
1745         dev->data->queue_pairs[queue_pair_id] = NULL;
1746
1747         return 0;
1748 }
1749
1750 /** Setup a queue pair */
1751 static int
1752 dpaa2_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1753                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1754                 __rte_unused int socket_id)
1755 {
1756         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1757         struct dpaa2_sec_qp *qp;
1758         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1759         struct dpseci_rx_queue_cfg cfg;
1760         int32_t retcode;
1761
1762         PMD_INIT_FUNC_TRACE();
1763
1764         /* If qp is already in use free ring memory and qp metadata. */
1765         if (dev->data->queue_pairs[qp_id] != NULL) {
1766                 DPAA2_SEC_INFO("QP already setup");
1767                 return 0;
1768         }
1769
1770         DPAA2_SEC_DEBUG("dev =%p, queue =%d, conf =%p",
1771                     dev, qp_id, qp_conf);
1772
1773         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
1774
1775         qp = rte_malloc(NULL, sizeof(struct dpaa2_sec_qp),
1776                         RTE_CACHE_LINE_SIZE);
1777         if (!qp) {
1778                 DPAA2_SEC_ERR("malloc failed for rx/tx queues");
1779                 return -ENOMEM;
1780         }
1781
1782         qp->rx_vq.crypto_data = dev->data;
1783         qp->tx_vq.crypto_data = dev->data;
1784         qp->rx_vq.q_storage = rte_malloc("sec dq storage",
1785                 sizeof(struct queue_storage_info_t),
1786                 RTE_CACHE_LINE_SIZE);
1787         if (!qp->rx_vq.q_storage) {
1788                 DPAA2_SEC_ERR("malloc failed for q_storage");
1789                 return -ENOMEM;
1790         }
1791         memset(qp->rx_vq.q_storage, 0, sizeof(struct queue_storage_info_t));
1792
1793         if (dpaa2_alloc_dq_storage(qp->rx_vq.q_storage)) {
1794                 DPAA2_SEC_ERR("Unable to allocate dequeue storage");
1795                 return -ENOMEM;
1796         }
1797
1798         dev->data->queue_pairs[qp_id] = qp;
1799
1800         cfg.options = cfg.options | DPSECI_QUEUE_OPT_USER_CTX;
1801         cfg.user_ctx = (size_t)(&qp->rx_vq);
1802         retcode = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
1803                                       qp_id, &cfg);
1804         return retcode;
1805 }
1806
1807 /** Returns the size of the aesni gcm session structure */
1808 static unsigned int
1809 dpaa2_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1810 {
1811         PMD_INIT_FUNC_TRACE();
1812
1813         return sizeof(dpaa2_sec_session);
1814 }
1815
1816 static int
1817 dpaa2_sec_cipher_init(struct rte_cryptodev *dev,
1818                       struct rte_crypto_sym_xform *xform,
1819                       dpaa2_sec_session *session)
1820 {
1821         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1822         struct alginfo cipherdata;
1823         int bufsize, ret = 0;
1824         struct ctxt_priv *priv;
1825         struct sec_flow_context *flc;
1826
1827         PMD_INIT_FUNC_TRACE();
1828
1829         /* For SEC CIPHER only one descriptor is required. */
1830         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1831                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1832                         RTE_CACHE_LINE_SIZE);
1833         if (priv == NULL) {
1834                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1835                 return -ENOMEM;
1836         }
1837
1838         priv->fle_pool = dev_priv->fle_pool;
1839
1840         flc = &priv->flc_desc[0].flc;
1841
1842         session->ctxt_type = DPAA2_SEC_CIPHER;
1843         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1844                         RTE_CACHE_LINE_SIZE);
1845         if (session->cipher_key.data == NULL) {
1846                 DPAA2_SEC_ERR("No Memory for cipher key");
1847                 rte_free(priv);
1848                 return -ENOMEM;
1849         }
1850         session->cipher_key.length = xform->cipher.key.length;
1851
1852         memcpy(session->cipher_key.data, xform->cipher.key.data,
1853                xform->cipher.key.length);
1854         cipherdata.key = (size_t)session->cipher_key.data;
1855         cipherdata.keylen = session->cipher_key.length;
1856         cipherdata.key_enc_flags = 0;
1857         cipherdata.key_type = RTA_DATA_IMM;
1858
1859         /* Set IV parameters */
1860         session->iv.offset = xform->cipher.iv.offset;
1861         session->iv.length = xform->cipher.iv.length;
1862         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1863                                 DIR_ENC : DIR_DEC;
1864
1865         switch (xform->cipher.algo) {
1866         case RTE_CRYPTO_CIPHER_AES_CBC:
1867                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1868                 cipherdata.algmode = OP_ALG_AAI_CBC;
1869                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
1870                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
1871                                                 SHR_NEVER, &cipherdata,
1872                                                 session->iv.length,
1873                                                 session->dir);
1874                 break;
1875         case RTE_CRYPTO_CIPHER_3DES_CBC:
1876                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
1877                 cipherdata.algmode = OP_ALG_AAI_CBC;
1878                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
1879                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
1880                                                 SHR_NEVER, &cipherdata,
1881                                                 session->iv.length,
1882                                                 session->dir);
1883                 break;
1884         case RTE_CRYPTO_CIPHER_DES_CBC:
1885                 cipherdata.algtype = OP_ALG_ALGSEL_DES;
1886                 cipherdata.algmode = OP_ALG_AAI_CBC;
1887                 session->cipher_alg = RTE_CRYPTO_CIPHER_DES_CBC;
1888                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
1889                                                 SHR_NEVER, &cipherdata,
1890                                                 session->iv.length,
1891                                                 session->dir);
1892                 break;
1893         case RTE_CRYPTO_CIPHER_AES_CTR:
1894                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1895                 cipherdata.algmode = OP_ALG_AAI_CTR;
1896                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
1897                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
1898                                                 SHR_NEVER, &cipherdata,
1899                                                 session->iv.length,
1900                                                 session->dir);
1901                 break;
1902         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1903                 cipherdata.algtype = OP_ALG_ALGSEL_SNOW_F8;
1904                 session->cipher_alg = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
1905                 bufsize = cnstr_shdsc_snow_f8(priv->flc_desc[0].desc, 1, 0,
1906                                               &cipherdata,
1907                                               session->dir);
1908                 break;
1909         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
1910                 cipherdata.algtype = OP_ALG_ALGSEL_ZUCE;
1911                 session->cipher_alg = RTE_CRYPTO_CIPHER_ZUC_EEA3;
1912                 bufsize = cnstr_shdsc_zuce(priv->flc_desc[0].desc, 1, 0,
1913                                               &cipherdata,
1914                                               session->dir);
1915                 break;
1916         case RTE_CRYPTO_CIPHER_KASUMI_F8:
1917         case RTE_CRYPTO_CIPHER_AES_F8:
1918         case RTE_CRYPTO_CIPHER_AES_ECB:
1919         case RTE_CRYPTO_CIPHER_3DES_ECB:
1920         case RTE_CRYPTO_CIPHER_3DES_CTR:
1921         case RTE_CRYPTO_CIPHER_AES_XTS:
1922         case RTE_CRYPTO_CIPHER_ARC4:
1923         case RTE_CRYPTO_CIPHER_NULL:
1924                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
1925                         xform->cipher.algo);
1926                 ret = -ENOTSUP;
1927                 goto error_out;
1928         default:
1929                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
1930                         xform->cipher.algo);
1931                 ret = -ENOTSUP;
1932                 goto error_out;
1933         }
1934
1935         if (bufsize < 0) {
1936                 DPAA2_SEC_ERR("Crypto: Descriptor build failed");
1937                 ret = -EINVAL;
1938                 goto error_out;
1939         }
1940
1941         flc->word1_sdl = (uint8_t)bufsize;
1942         session->ctxt = priv;
1943
1944 #ifdef CAAM_DESC_DEBUG
1945         int i;
1946         for (i = 0; i < bufsize; i++)
1947                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x", i, priv->flc_desc[0].desc[i]);
1948 #endif
1949         return ret;
1950
1951 error_out:
1952         rte_free(session->cipher_key.data);
1953         rte_free(priv);
1954         return ret;
1955 }
1956
1957 static int
1958 dpaa2_sec_auth_init(struct rte_cryptodev *dev,
1959                     struct rte_crypto_sym_xform *xform,
1960                     dpaa2_sec_session *session)
1961 {
1962         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1963         struct alginfo authdata;
1964         int bufsize, ret = 0;
1965         struct ctxt_priv *priv;
1966         struct sec_flow_context *flc;
1967
1968         PMD_INIT_FUNC_TRACE();
1969
1970         /* For SEC AUTH three descriptors are required for various stages */
1971         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1972                         sizeof(struct ctxt_priv) + 3 *
1973                         sizeof(struct sec_flc_desc),
1974                         RTE_CACHE_LINE_SIZE);
1975         if (priv == NULL) {
1976                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1977                 return -ENOMEM;
1978         }
1979
1980         priv->fle_pool = dev_priv->fle_pool;
1981         flc = &priv->flc_desc[DESC_INITFINAL].flc;
1982
1983         session->ctxt_type = DPAA2_SEC_AUTH;
1984         session->auth_key.length = xform->auth.key.length;
1985         if (xform->auth.key.length) {
1986                 session->auth_key.data = rte_zmalloc(NULL,
1987                         xform->auth.key.length,
1988                         RTE_CACHE_LINE_SIZE);
1989                 if (session->auth_key.data == NULL) {
1990                         DPAA2_SEC_ERR("Unable to allocate memory for auth key");
1991                         rte_free(priv);
1992                         return -ENOMEM;
1993                 }
1994                 memcpy(session->auth_key.data, xform->auth.key.data,
1995                        xform->auth.key.length);
1996                 authdata.key = (size_t)session->auth_key.data;
1997                 authdata.key_enc_flags = 0;
1998                 authdata.key_type = RTA_DATA_IMM;
1999         }
2000         authdata.keylen = session->auth_key.length;
2001
2002         session->digest_length = xform->auth.digest_length;
2003         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2004                                 DIR_ENC : DIR_DEC;
2005
2006         switch (xform->auth.algo) {
2007         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2008                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2009                 authdata.algmode = OP_ALG_AAI_HMAC;
2010                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2011                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2012                                            1, 0, SHR_NEVER, &authdata,
2013                                            !session->dir,
2014                                            session->digest_length);
2015                 break;
2016         case RTE_CRYPTO_AUTH_MD5_HMAC:
2017                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2018                 authdata.algmode = OP_ALG_AAI_HMAC;
2019                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2020                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2021                                            1, 0, SHR_NEVER, &authdata,
2022                                            !session->dir,
2023                                            session->digest_length);
2024                 break;
2025         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2026                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2027                 authdata.algmode = OP_ALG_AAI_HMAC;
2028                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2029                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2030                                            1, 0, SHR_NEVER, &authdata,
2031                                            !session->dir,
2032                                            session->digest_length);
2033                 break;
2034         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2035                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2036                 authdata.algmode = OP_ALG_AAI_HMAC;
2037                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2038                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2039                                            1, 0, SHR_NEVER, &authdata,
2040                                            !session->dir,
2041                                            session->digest_length);
2042                 break;
2043         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2044                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2045                 authdata.algmode = OP_ALG_AAI_HMAC;
2046                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2047                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2048                                            1, 0, SHR_NEVER, &authdata,
2049                                            !session->dir,
2050                                            session->digest_length);
2051                 break;
2052         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2053                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2054                 authdata.algmode = OP_ALG_AAI_HMAC;
2055                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2056                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2057                                            1, 0, SHR_NEVER, &authdata,
2058                                            !session->dir,
2059                                            session->digest_length);
2060                 break;
2061         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2062                 authdata.algtype = OP_ALG_ALGSEL_SNOW_F9;
2063                 authdata.algmode = OP_ALG_AAI_F9;
2064                 session->auth_alg = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
2065                 session->iv.offset = xform->auth.iv.offset;
2066                 session->iv.length = xform->auth.iv.length;
2067                 bufsize = cnstr_shdsc_snow_f9(priv->flc_desc[DESC_INITFINAL].desc,
2068                                               1, 0, &authdata,
2069                                               !session->dir,
2070                                               session->digest_length);
2071                 break;
2072         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2073                 authdata.algtype = OP_ALG_ALGSEL_ZUCA;
2074                 authdata.algmode = OP_ALG_AAI_F9;
2075                 session->auth_alg = RTE_CRYPTO_AUTH_ZUC_EIA3;
2076                 session->iv.offset = xform->auth.iv.offset;
2077                 session->iv.length = xform->auth.iv.length;
2078                 bufsize = cnstr_shdsc_zuca(priv->flc_desc[DESC_INITFINAL].desc,
2079                                            1, 0, &authdata,
2080                                            !session->dir,
2081                                            session->digest_length);
2082                 break;
2083         case RTE_CRYPTO_AUTH_SHA1:
2084                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2085                 authdata.algmode = OP_ALG_AAI_HASH;
2086                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1;
2087                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2088                                            1, 0, SHR_NEVER, &authdata,
2089                                            !session->dir,
2090                                            session->digest_length);
2091                 break;
2092         case RTE_CRYPTO_AUTH_MD5:
2093                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2094                 authdata.algmode = OP_ALG_AAI_HASH;
2095                 session->auth_alg = RTE_CRYPTO_AUTH_MD5;
2096                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2097                                            1, 0, SHR_NEVER, &authdata,
2098                                            !session->dir,
2099                                            session->digest_length);
2100                 break;
2101         case RTE_CRYPTO_AUTH_SHA256:
2102                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2103                 authdata.algmode = OP_ALG_AAI_HASH;
2104                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256;
2105                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2106                                            1, 0, SHR_NEVER, &authdata,
2107                                            !session->dir,
2108                                            session->digest_length);
2109                 break;
2110         case RTE_CRYPTO_AUTH_SHA384:
2111                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2112                 authdata.algmode = OP_ALG_AAI_HASH;
2113                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384;
2114                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2115                                            1, 0, SHR_NEVER, &authdata,
2116                                            !session->dir,
2117                                            session->digest_length);
2118                 break;
2119         case RTE_CRYPTO_AUTH_SHA512:
2120                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2121                 authdata.algmode = OP_ALG_AAI_HASH;
2122                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512;
2123                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2124                                            1, 0, SHR_NEVER, &authdata,
2125                                            !session->dir,
2126                                            session->digest_length);
2127                 break;
2128         case RTE_CRYPTO_AUTH_SHA224:
2129                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2130                 authdata.algmode = OP_ALG_AAI_HASH;
2131                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224;
2132                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2133                                            1, 0, SHR_NEVER, &authdata,
2134                                            !session->dir,
2135                                            session->digest_length);
2136                 break;
2137         case RTE_CRYPTO_AUTH_AES_GMAC:
2138         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2139         case RTE_CRYPTO_AUTH_AES_CMAC:
2140         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2141         case RTE_CRYPTO_AUTH_KASUMI_F9:
2142         case RTE_CRYPTO_AUTH_NULL:
2143                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %un",
2144                               xform->auth.algo);
2145                 ret = -ENOTSUP;
2146                 goto error_out;
2147         default:
2148                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2149                               xform->auth.algo);
2150                 ret = -ENOTSUP;
2151                 goto error_out;
2152         }
2153
2154         if (bufsize < 0) {
2155                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2156                 ret = -EINVAL;
2157                 goto error_out;
2158         }
2159
2160         flc->word1_sdl = (uint8_t)bufsize;
2161         session->ctxt = priv;
2162 #ifdef CAAM_DESC_DEBUG
2163         int i;
2164         for (i = 0; i < bufsize; i++)
2165                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2166                                 i, priv->flc_desc[DESC_INITFINAL].desc[i]);
2167 #endif
2168
2169         return ret;
2170
2171 error_out:
2172         rte_free(session->auth_key.data);
2173         rte_free(priv);
2174         return ret;
2175 }
2176
2177 static int
2178 dpaa2_sec_aead_init(struct rte_cryptodev *dev,
2179                     struct rte_crypto_sym_xform *xform,
2180                     dpaa2_sec_session *session)
2181 {
2182         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
2183         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2184         struct alginfo aeaddata;
2185         int bufsize;
2186         struct ctxt_priv *priv;
2187         struct sec_flow_context *flc;
2188         struct rte_crypto_aead_xform *aead_xform = &xform->aead;
2189         int err, ret = 0;
2190
2191         PMD_INIT_FUNC_TRACE();
2192
2193         /* Set IV parameters */
2194         session->iv.offset = aead_xform->iv.offset;
2195         session->iv.length = aead_xform->iv.length;
2196         session->ctxt_type = DPAA2_SEC_AEAD;
2197
2198         /* For SEC AEAD only one descriptor is required */
2199         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2200                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2201                         RTE_CACHE_LINE_SIZE);
2202         if (priv == NULL) {
2203                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2204                 return -ENOMEM;
2205         }
2206
2207         priv->fle_pool = dev_priv->fle_pool;
2208         flc = &priv->flc_desc[0].flc;
2209
2210         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2211                                                RTE_CACHE_LINE_SIZE);
2212         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2213                 DPAA2_SEC_ERR("No Memory for aead key");
2214                 rte_free(priv);
2215                 return -ENOMEM;
2216         }
2217         memcpy(session->aead_key.data, aead_xform->key.data,
2218                aead_xform->key.length);
2219
2220         session->digest_length = aead_xform->digest_length;
2221         session->aead_key.length = aead_xform->key.length;
2222         ctxt->auth_only_len = aead_xform->aad_length;
2223
2224         aeaddata.key = (size_t)session->aead_key.data;
2225         aeaddata.keylen = session->aead_key.length;
2226         aeaddata.key_enc_flags = 0;
2227         aeaddata.key_type = RTA_DATA_IMM;
2228
2229         switch (aead_xform->algo) {
2230         case RTE_CRYPTO_AEAD_AES_GCM:
2231                 aeaddata.algtype = OP_ALG_ALGSEL_AES;
2232                 aeaddata.algmode = OP_ALG_AAI_GCM;
2233                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2234                 break;
2235         case RTE_CRYPTO_AEAD_AES_CCM:
2236                 DPAA2_SEC_ERR("Crypto: Unsupported AEAD alg %u",
2237                               aead_xform->algo);
2238                 ret = -ENOTSUP;
2239                 goto error_out;
2240         default:
2241                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2242                               aead_xform->algo);
2243                 ret = -ENOTSUP;
2244                 goto error_out;
2245         }
2246         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2247                                 DIR_ENC : DIR_DEC;
2248
2249         priv->flc_desc[0].desc[0] = aeaddata.keylen;
2250         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2251                                DESC_JOB_IO_LEN,
2252                                (unsigned int *)priv->flc_desc[0].desc,
2253                                &priv->flc_desc[0].desc[1], 1);
2254
2255         if (err < 0) {
2256                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2257                 ret = -EINVAL;
2258                 goto error_out;
2259         }
2260         if (priv->flc_desc[0].desc[1] & 1) {
2261                 aeaddata.key_type = RTA_DATA_IMM;
2262         } else {
2263                 aeaddata.key = DPAA2_VADDR_TO_IOVA(aeaddata.key);
2264                 aeaddata.key_type = RTA_DATA_PTR;
2265         }
2266         priv->flc_desc[0].desc[0] = 0;
2267         priv->flc_desc[0].desc[1] = 0;
2268
2269         if (session->dir == DIR_ENC)
2270                 bufsize = cnstr_shdsc_gcm_encap(
2271                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2272                                 &aeaddata, session->iv.length,
2273                                 session->digest_length);
2274         else
2275                 bufsize = cnstr_shdsc_gcm_decap(
2276                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2277                                 &aeaddata, session->iv.length,
2278                                 session->digest_length);
2279         if (bufsize < 0) {
2280                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2281                 ret = -EINVAL;
2282                 goto error_out;
2283         }
2284
2285         flc->word1_sdl = (uint8_t)bufsize;
2286         session->ctxt = priv;
2287 #ifdef CAAM_DESC_DEBUG
2288         int i;
2289         for (i = 0; i < bufsize; i++)
2290                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x\n",
2291                             i, priv->flc_desc[0].desc[i]);
2292 #endif
2293         return ret;
2294
2295 error_out:
2296         rte_free(session->aead_key.data);
2297         rte_free(priv);
2298         return ret;
2299 }
2300
2301
2302 static int
2303 dpaa2_sec_aead_chain_init(struct rte_cryptodev *dev,
2304                     struct rte_crypto_sym_xform *xform,
2305                     dpaa2_sec_session *session)
2306 {
2307         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2308         struct alginfo authdata, cipherdata;
2309         int bufsize;
2310         struct ctxt_priv *priv;
2311         struct sec_flow_context *flc;
2312         struct rte_crypto_cipher_xform *cipher_xform;
2313         struct rte_crypto_auth_xform *auth_xform;
2314         int err, ret = 0;
2315
2316         PMD_INIT_FUNC_TRACE();
2317
2318         if (session->ext_params.aead_ctxt.auth_cipher_text) {
2319                 cipher_xform = &xform->cipher;
2320                 auth_xform = &xform->next->auth;
2321                 session->ctxt_type =
2322                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2323                         DPAA2_SEC_CIPHER_HASH : DPAA2_SEC_HASH_CIPHER;
2324         } else {
2325                 cipher_xform = &xform->next->cipher;
2326                 auth_xform = &xform->auth;
2327                 session->ctxt_type =
2328                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2329                         DPAA2_SEC_HASH_CIPHER : DPAA2_SEC_CIPHER_HASH;
2330         }
2331
2332         /* Set IV parameters */
2333         session->iv.offset = cipher_xform->iv.offset;
2334         session->iv.length = cipher_xform->iv.length;
2335
2336         /* For SEC AEAD only one descriptor is required */
2337         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2338                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2339                         RTE_CACHE_LINE_SIZE);
2340         if (priv == NULL) {
2341                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2342                 return -ENOMEM;
2343         }
2344
2345         priv->fle_pool = dev_priv->fle_pool;
2346         flc = &priv->flc_desc[0].flc;
2347
2348         session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2349                                                RTE_CACHE_LINE_SIZE);
2350         if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2351                 DPAA2_SEC_ERR("No Memory for cipher key");
2352                 rte_free(priv);
2353                 return -ENOMEM;
2354         }
2355         session->cipher_key.length = cipher_xform->key.length;
2356         session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2357                                              RTE_CACHE_LINE_SIZE);
2358         if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2359                 DPAA2_SEC_ERR("No Memory for auth key");
2360                 rte_free(session->cipher_key.data);
2361                 rte_free(priv);
2362                 return -ENOMEM;
2363         }
2364         session->auth_key.length = auth_xform->key.length;
2365         memcpy(session->cipher_key.data, cipher_xform->key.data,
2366                cipher_xform->key.length);
2367         memcpy(session->auth_key.data, auth_xform->key.data,
2368                auth_xform->key.length);
2369
2370         authdata.key = (size_t)session->auth_key.data;
2371         authdata.keylen = session->auth_key.length;
2372         authdata.key_enc_flags = 0;
2373         authdata.key_type = RTA_DATA_IMM;
2374
2375         session->digest_length = auth_xform->digest_length;
2376
2377         switch (auth_xform->algo) {
2378         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2379                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2380                 authdata.algmode = OP_ALG_AAI_HMAC;
2381                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2382                 break;
2383         case RTE_CRYPTO_AUTH_MD5_HMAC:
2384                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2385                 authdata.algmode = OP_ALG_AAI_HMAC;
2386                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2387                 break;
2388         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2389                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2390                 authdata.algmode = OP_ALG_AAI_HMAC;
2391                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2392                 break;
2393         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2394                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2395                 authdata.algmode = OP_ALG_AAI_HMAC;
2396                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2397                 break;
2398         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2399                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2400                 authdata.algmode = OP_ALG_AAI_HMAC;
2401                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2402                 break;
2403         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2404                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2405                 authdata.algmode = OP_ALG_AAI_HMAC;
2406                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2407                 break;
2408         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2409         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2410         case RTE_CRYPTO_AUTH_NULL:
2411         case RTE_CRYPTO_AUTH_SHA1:
2412         case RTE_CRYPTO_AUTH_SHA256:
2413         case RTE_CRYPTO_AUTH_SHA512:
2414         case RTE_CRYPTO_AUTH_SHA224:
2415         case RTE_CRYPTO_AUTH_SHA384:
2416         case RTE_CRYPTO_AUTH_MD5:
2417         case RTE_CRYPTO_AUTH_AES_GMAC:
2418         case RTE_CRYPTO_AUTH_KASUMI_F9:
2419         case RTE_CRYPTO_AUTH_AES_CMAC:
2420         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2421         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2422                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2423                               auth_xform->algo);
2424                 ret = -ENOTSUP;
2425                 goto error_out;
2426         default:
2427                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2428                               auth_xform->algo);
2429                 ret = -ENOTSUP;
2430                 goto error_out;
2431         }
2432         cipherdata.key = (size_t)session->cipher_key.data;
2433         cipherdata.keylen = session->cipher_key.length;
2434         cipherdata.key_enc_flags = 0;
2435         cipherdata.key_type = RTA_DATA_IMM;
2436
2437         switch (cipher_xform->algo) {
2438         case RTE_CRYPTO_CIPHER_AES_CBC:
2439                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2440                 cipherdata.algmode = OP_ALG_AAI_CBC;
2441                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
2442                 break;
2443         case RTE_CRYPTO_CIPHER_3DES_CBC:
2444                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
2445                 cipherdata.algmode = OP_ALG_AAI_CBC;
2446                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
2447                 break;
2448         case RTE_CRYPTO_CIPHER_DES_CBC:
2449                 cipherdata.algtype = OP_ALG_ALGSEL_DES;
2450                 cipherdata.algmode = OP_ALG_AAI_CBC;
2451                 session->cipher_alg = RTE_CRYPTO_CIPHER_DES_CBC;
2452                 break;
2453         case RTE_CRYPTO_CIPHER_AES_CTR:
2454                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2455                 cipherdata.algmode = OP_ALG_AAI_CTR;
2456                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
2457                 break;
2458         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2459         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2460         case RTE_CRYPTO_CIPHER_NULL:
2461         case RTE_CRYPTO_CIPHER_3DES_ECB:
2462         case RTE_CRYPTO_CIPHER_3DES_CTR:
2463         case RTE_CRYPTO_CIPHER_AES_ECB:
2464         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2465                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2466                               cipher_xform->algo);
2467                 ret = -ENOTSUP;
2468                 goto error_out;
2469         default:
2470                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2471                               cipher_xform->algo);
2472                 ret = -ENOTSUP;
2473                 goto error_out;
2474         }
2475         session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2476                                 DIR_ENC : DIR_DEC;
2477
2478         priv->flc_desc[0].desc[0] = cipherdata.keylen;
2479         priv->flc_desc[0].desc[1] = authdata.keylen;
2480         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2481                                DESC_JOB_IO_LEN,
2482                                (unsigned int *)priv->flc_desc[0].desc,
2483                                &priv->flc_desc[0].desc[2], 2);
2484
2485         if (err < 0) {
2486                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2487                 ret = -EINVAL;
2488                 goto error_out;
2489         }
2490         if (priv->flc_desc[0].desc[2] & 1) {
2491                 cipherdata.key_type = RTA_DATA_IMM;
2492         } else {
2493                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
2494                 cipherdata.key_type = RTA_DATA_PTR;
2495         }
2496         if (priv->flc_desc[0].desc[2] & (1 << 1)) {
2497                 authdata.key_type = RTA_DATA_IMM;
2498         } else {
2499                 authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
2500                 authdata.key_type = RTA_DATA_PTR;
2501         }
2502         priv->flc_desc[0].desc[0] = 0;
2503         priv->flc_desc[0].desc[1] = 0;
2504         priv->flc_desc[0].desc[2] = 0;
2505
2506         if (session->ctxt_type == DPAA2_SEC_CIPHER_HASH) {
2507                 bufsize = cnstr_shdsc_authenc(priv->flc_desc[0].desc, 1,
2508                                               0, SHR_SERIAL,
2509                                               &cipherdata, &authdata,
2510                                               session->iv.length,
2511                                               session->digest_length,
2512                                               session->dir);
2513                 if (bufsize < 0) {
2514                         DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2515                         ret = -EINVAL;
2516                         goto error_out;
2517                 }
2518         } else {
2519                 DPAA2_SEC_ERR("Hash before cipher not supported");
2520                 ret = -ENOTSUP;
2521                 goto error_out;
2522         }
2523
2524         flc->word1_sdl = (uint8_t)bufsize;
2525         session->ctxt = priv;
2526 #ifdef CAAM_DESC_DEBUG
2527         int i;
2528         for (i = 0; i < bufsize; i++)
2529                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2530                             i, priv->flc_desc[0].desc[i]);
2531 #endif
2532
2533         return ret;
2534
2535 error_out:
2536         rte_free(session->cipher_key.data);
2537         rte_free(session->auth_key.data);
2538         rte_free(priv);
2539         return ret;
2540 }
2541
2542 static int
2543 dpaa2_sec_set_session_parameters(struct rte_cryptodev *dev,
2544                             struct rte_crypto_sym_xform *xform, void *sess)
2545 {
2546         dpaa2_sec_session *session = sess;
2547         int ret;
2548
2549         PMD_INIT_FUNC_TRACE();
2550
2551         if (unlikely(sess == NULL)) {
2552                 DPAA2_SEC_ERR("Invalid session struct");
2553                 return -EINVAL;
2554         }
2555
2556         memset(session, 0, sizeof(dpaa2_sec_session));
2557         /* Default IV length = 0 */
2558         session->iv.length = 0;
2559
2560         /* Cipher Only */
2561         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2562                 ret = dpaa2_sec_cipher_init(dev, xform, session);
2563
2564         /* Authentication Only */
2565         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2566                    xform->next == NULL) {
2567                 ret = dpaa2_sec_auth_init(dev, xform, session);
2568
2569         /* Cipher then Authenticate */
2570         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2571                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2572                 session->ext_params.aead_ctxt.auth_cipher_text = true;
2573                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2574                         ret = dpaa2_sec_auth_init(dev, xform, session);
2575                 else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2576                         ret = dpaa2_sec_cipher_init(dev, xform, session);
2577                 else
2578                         ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2579         /* Authenticate then Cipher */
2580         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2581                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2582                 session->ext_params.aead_ctxt.auth_cipher_text = false;
2583                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2584                         ret = dpaa2_sec_cipher_init(dev, xform, session);
2585                 else if (xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2586                         ret = dpaa2_sec_auth_init(dev, xform, session);
2587                 else
2588                         ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2589         /* AEAD operation for AES-GCM kind of Algorithms */
2590         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2591                    xform->next == NULL) {
2592                 ret = dpaa2_sec_aead_init(dev, xform, session);
2593
2594         } else {
2595                 DPAA2_SEC_ERR("Invalid crypto type");
2596                 return -EINVAL;
2597         }
2598
2599         return ret;
2600 }
2601
2602 #ifdef RTE_LIB_SECURITY
2603 static int
2604 dpaa2_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2605                         dpaa2_sec_session *session,
2606                         struct alginfo *aeaddata)
2607 {
2608         PMD_INIT_FUNC_TRACE();
2609
2610         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2611                                                RTE_CACHE_LINE_SIZE);
2612         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2613                 DPAA2_SEC_ERR("No Memory for aead key");
2614                 return -ENOMEM;
2615         }
2616         memcpy(session->aead_key.data, aead_xform->key.data,
2617                aead_xform->key.length);
2618
2619         session->digest_length = aead_xform->digest_length;
2620         session->aead_key.length = aead_xform->key.length;
2621
2622         aeaddata->key = (size_t)session->aead_key.data;
2623         aeaddata->keylen = session->aead_key.length;
2624         aeaddata->key_enc_flags = 0;
2625         aeaddata->key_type = RTA_DATA_IMM;
2626
2627         switch (aead_xform->algo) {
2628         case RTE_CRYPTO_AEAD_AES_GCM:
2629                 switch (session->digest_length) {
2630                 case 8:
2631                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM8;
2632                         break;
2633                 case 12:
2634                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM12;
2635                         break;
2636                 case 16:
2637                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM16;
2638                         break;
2639                 default:
2640                         DPAA2_SEC_ERR("Crypto: Undefined GCM digest %d",
2641                                       session->digest_length);
2642                         return -EINVAL;
2643                 }
2644                 aeaddata->algmode = OP_ALG_AAI_GCM;
2645                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2646                 break;
2647         case RTE_CRYPTO_AEAD_AES_CCM:
2648                 switch (session->digest_length) {
2649                 case 8:
2650                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM8;
2651                         break;
2652                 case 12:
2653                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM12;
2654                         break;
2655                 case 16:
2656                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM16;
2657                         break;
2658                 default:
2659                         DPAA2_SEC_ERR("Crypto: Undefined CCM digest %d",
2660                                       session->digest_length);
2661                         return -EINVAL;
2662                 }
2663                 aeaddata->algmode = OP_ALG_AAI_CCM;
2664                 session->aead_alg = RTE_CRYPTO_AEAD_AES_CCM;
2665                 break;
2666         default:
2667                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2668                               aead_xform->algo);
2669                 return -ENOTSUP;
2670         }
2671         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2672                                 DIR_ENC : DIR_DEC;
2673
2674         return 0;
2675 }
2676
2677 static int
2678 dpaa2_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2679         struct rte_crypto_auth_xform *auth_xform,
2680         dpaa2_sec_session *session,
2681         struct alginfo *cipherdata,
2682         struct alginfo *authdata)
2683 {
2684         if (cipher_xform) {
2685                 session->cipher_key.data = rte_zmalloc(NULL,
2686                                                        cipher_xform->key.length,
2687                                                        RTE_CACHE_LINE_SIZE);
2688                 if (session->cipher_key.data == NULL &&
2689                                 cipher_xform->key.length > 0) {
2690                         DPAA2_SEC_ERR("No Memory for cipher key");
2691                         return -ENOMEM;
2692                 }
2693
2694                 session->cipher_key.length = cipher_xform->key.length;
2695                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2696                                 cipher_xform->key.length);
2697                 session->cipher_alg = cipher_xform->algo;
2698         } else {
2699                 session->cipher_key.data = NULL;
2700                 session->cipher_key.length = 0;
2701                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2702         }
2703
2704         if (auth_xform) {
2705                 session->auth_key.data = rte_zmalloc(NULL,
2706                                                 auth_xform->key.length,
2707                                                 RTE_CACHE_LINE_SIZE);
2708                 if (session->auth_key.data == NULL &&
2709                                 auth_xform->key.length > 0) {
2710                         DPAA2_SEC_ERR("No Memory for auth key");
2711                         return -ENOMEM;
2712                 }
2713                 session->auth_key.length = auth_xform->key.length;
2714                 memcpy(session->auth_key.data, auth_xform->key.data,
2715                                 auth_xform->key.length);
2716                 session->auth_alg = auth_xform->algo;
2717                 session->digest_length = auth_xform->digest_length;
2718         } else {
2719                 session->auth_key.data = NULL;
2720                 session->auth_key.length = 0;
2721                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2722         }
2723
2724         authdata->key = (size_t)session->auth_key.data;
2725         authdata->keylen = session->auth_key.length;
2726         authdata->key_enc_flags = 0;
2727         authdata->key_type = RTA_DATA_IMM;
2728         switch (session->auth_alg) {
2729         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2730                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA1_96;
2731                 authdata->algmode = OP_ALG_AAI_HMAC;
2732                 break;
2733         case RTE_CRYPTO_AUTH_MD5_HMAC:
2734                 authdata->algtype = OP_PCL_IPSEC_HMAC_MD5_96;
2735                 authdata->algmode = OP_ALG_AAI_HMAC;
2736                 break;
2737         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2738                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2739                 authdata->algmode = OP_ALG_AAI_HMAC;
2740                 if (session->digest_length != 16)
2741                         DPAA2_SEC_WARN(
2742                         "+++Using sha256-hmac truncated len is non-standard,"
2743                         "it will not work with lookaside proto");
2744                 break;
2745         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2746                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2747                 authdata->algmode = OP_ALG_AAI_HMAC;
2748                 break;
2749         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2750                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2751                 authdata->algmode = OP_ALG_AAI_HMAC;
2752                 break;
2753         case RTE_CRYPTO_AUTH_AES_CMAC:
2754                 authdata->algtype = OP_PCL_IPSEC_AES_CMAC_96;
2755                 break;
2756         case RTE_CRYPTO_AUTH_NULL:
2757                 authdata->algtype = OP_PCL_IPSEC_HMAC_NULL;
2758                 break;
2759         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2760         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2761         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2762         case RTE_CRYPTO_AUTH_SHA1:
2763         case RTE_CRYPTO_AUTH_SHA256:
2764         case RTE_CRYPTO_AUTH_SHA512:
2765         case RTE_CRYPTO_AUTH_SHA224:
2766         case RTE_CRYPTO_AUTH_SHA384:
2767         case RTE_CRYPTO_AUTH_MD5:
2768         case RTE_CRYPTO_AUTH_AES_GMAC:
2769         case RTE_CRYPTO_AUTH_KASUMI_F9:
2770         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2771         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2772                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2773                               session->auth_alg);
2774                 return -ENOTSUP;
2775         default:
2776                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2777                               session->auth_alg);
2778                 return -ENOTSUP;
2779         }
2780         cipherdata->key = (size_t)session->cipher_key.data;
2781         cipherdata->keylen = session->cipher_key.length;
2782         cipherdata->key_enc_flags = 0;
2783         cipherdata->key_type = RTA_DATA_IMM;
2784
2785         switch (session->cipher_alg) {
2786         case RTE_CRYPTO_CIPHER_AES_CBC:
2787                 cipherdata->algtype = OP_PCL_IPSEC_AES_CBC;
2788                 cipherdata->algmode = OP_ALG_AAI_CBC;
2789                 break;
2790         case RTE_CRYPTO_CIPHER_3DES_CBC:
2791                 cipherdata->algtype = OP_PCL_IPSEC_3DES;
2792                 cipherdata->algmode = OP_ALG_AAI_CBC;
2793                 break;
2794         case RTE_CRYPTO_CIPHER_DES_CBC:
2795                 cipherdata->algtype = OP_PCL_IPSEC_DES;
2796                 cipherdata->algmode = OP_ALG_AAI_CBC;
2797                 break;
2798         case RTE_CRYPTO_CIPHER_AES_CTR:
2799                 cipherdata->algtype = OP_PCL_IPSEC_AES_CTR;
2800                 cipherdata->algmode = OP_ALG_AAI_CTR;
2801                 break;
2802         case RTE_CRYPTO_CIPHER_NULL:
2803                 cipherdata->algtype = OP_PCL_IPSEC_NULL;
2804                 break;
2805         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2806         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2807         case RTE_CRYPTO_CIPHER_3DES_ECB:
2808         case RTE_CRYPTO_CIPHER_3DES_CTR:
2809         case RTE_CRYPTO_CIPHER_AES_ECB:
2810         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2811                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2812                               session->cipher_alg);
2813                 return -ENOTSUP;
2814         default:
2815                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2816                               session->cipher_alg);
2817                 return -ENOTSUP;
2818         }
2819
2820         return 0;
2821 }
2822
2823 #ifdef RTE_LIBRTE_SECURITY_TEST
2824 static uint8_t aes_cbc_iv[] = {
2825         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2826         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
2827 #endif
2828
2829 static int
2830 dpaa2_sec_set_ipsec_session(struct rte_cryptodev *dev,
2831                             struct rte_security_session_conf *conf,
2832                             void *sess)
2833 {
2834         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2835         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2836         struct rte_crypto_auth_xform *auth_xform = NULL;
2837         struct rte_crypto_aead_xform *aead_xform = NULL;
2838         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
2839         struct ctxt_priv *priv;
2840         struct alginfo authdata, cipherdata;
2841         int bufsize;
2842         struct sec_flow_context *flc;
2843         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2844         int ret = -1;
2845
2846         PMD_INIT_FUNC_TRACE();
2847
2848         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2849                                 sizeof(struct ctxt_priv) +
2850                                 sizeof(struct sec_flc_desc),
2851                                 RTE_CACHE_LINE_SIZE);
2852
2853         if (priv == NULL) {
2854                 DPAA2_SEC_ERR("No memory for priv CTXT");
2855                 return -ENOMEM;
2856         }
2857
2858         priv->fle_pool = dev_priv->fle_pool;
2859         flc = &priv->flc_desc[0].flc;
2860
2861         memset(session, 0, sizeof(dpaa2_sec_session));
2862
2863         if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2864                 cipher_xform = &conf->crypto_xform->cipher;
2865                 if (conf->crypto_xform->next)
2866                         auth_xform = &conf->crypto_xform->next->auth;
2867                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2868                                         session, &cipherdata, &authdata);
2869         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2870                 auth_xform = &conf->crypto_xform->auth;
2871                 if (conf->crypto_xform->next)
2872                         cipher_xform = &conf->crypto_xform->next->cipher;
2873                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2874                                         session, &cipherdata, &authdata);
2875         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
2876                 aead_xform = &conf->crypto_xform->aead;
2877                 ret = dpaa2_sec_ipsec_aead_init(aead_xform,
2878                                         session, &cipherdata);
2879                 authdata.keylen = 0;
2880                 authdata.algtype = 0;
2881         } else {
2882                 DPAA2_SEC_ERR("XFORM not specified");
2883                 ret = -EINVAL;
2884                 goto out;
2885         }
2886         if (ret) {
2887                 DPAA2_SEC_ERR("Failed to process xform");
2888                 goto out;
2889         }
2890
2891         session->ctxt_type = DPAA2_SEC_IPSEC;
2892         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2893                 uint8_t *hdr = NULL;
2894                 struct ip ip4_hdr;
2895                 struct rte_ipv6_hdr ip6_hdr;
2896                 struct ipsec_encap_pdb encap_pdb;
2897
2898                 flc->dhr = SEC_FLC_DHR_OUTBOUND;
2899                 /* For Sec Proto only one descriptor is required. */
2900                 memset(&encap_pdb, 0, sizeof(struct ipsec_encap_pdb));
2901
2902                 /* copy algo specific data to PDB */
2903                 switch (cipherdata.algtype) {
2904                 case OP_PCL_IPSEC_AES_CTR:
2905                         encap_pdb.ctr.ctr_initial = 0x00000001;
2906                         encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2907                         break;
2908                 case OP_PCL_IPSEC_AES_GCM8:
2909                 case OP_PCL_IPSEC_AES_GCM12:
2910                 case OP_PCL_IPSEC_AES_GCM16:
2911                         memcpy(encap_pdb.gcm.salt,
2912                                 (uint8_t *)&(ipsec_xform->salt), 4);
2913                         break;
2914                 }
2915
2916                 encap_pdb.options = (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2917                         PDBOPTS_ESP_OIHI_PDB_INL |
2918                         PDBOPTS_ESP_IVSRC |
2919                         PDBHMO_ESP_ENCAP_DTTL |
2920                         PDBHMO_ESP_SNR;
2921                 if (ipsec_xform->options.esn)
2922                         encap_pdb.options |= PDBOPTS_ESP_ESN;
2923                 encap_pdb.spi = ipsec_xform->spi;
2924                 session->dir = DIR_ENC;
2925                 if (ipsec_xform->tunnel.type ==
2926                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2927                         encap_pdb.ip_hdr_len = sizeof(struct ip);
2928                         ip4_hdr.ip_v = IPVERSION;
2929                         ip4_hdr.ip_hl = 5;
2930                         ip4_hdr.ip_len = rte_cpu_to_be_16(sizeof(ip4_hdr));
2931                         ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2932                         ip4_hdr.ip_id = 0;
2933                         ip4_hdr.ip_off = 0;
2934                         ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2935                         ip4_hdr.ip_p = IPPROTO_ESP;
2936                         ip4_hdr.ip_sum = 0;
2937                         ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
2938                         ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
2939                         ip4_hdr.ip_sum = calc_chksum((uint16_t *)(void *)
2940                                         &ip4_hdr, sizeof(struct ip));
2941                         hdr = (uint8_t *)&ip4_hdr;
2942                 } else if (ipsec_xform->tunnel.type ==
2943                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2944                         ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2945                                 DPAA2_IPv6_DEFAULT_VTC_FLOW |
2946                                 ((ipsec_xform->tunnel.ipv6.dscp <<
2947                                         RTE_IPV6_HDR_TC_SHIFT) &
2948                                         RTE_IPV6_HDR_TC_MASK) |
2949                                 ((ipsec_xform->tunnel.ipv6.flabel <<
2950                                         RTE_IPV6_HDR_FL_SHIFT) &
2951                                         RTE_IPV6_HDR_FL_MASK));
2952                         /* Payload length will be updated by HW */
2953                         ip6_hdr.payload_len = 0;
2954                         ip6_hdr.hop_limits =
2955                                         ipsec_xform->tunnel.ipv6.hlimit;
2956                         ip6_hdr.proto = (ipsec_xform->proto ==
2957                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2958                                         IPPROTO_ESP : IPPROTO_AH;
2959                         memcpy(&ip6_hdr.src_addr,
2960                                 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2961                         memcpy(&ip6_hdr.dst_addr,
2962                                 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2963                         encap_pdb.ip_hdr_len = sizeof(struct rte_ipv6_hdr);
2964                         hdr = (uint8_t *)&ip6_hdr;
2965                 }
2966
2967                 bufsize = cnstr_shdsc_ipsec_new_encap(priv->flc_desc[0].desc,
2968                                 1, 0, (rta_sec_era >= RTA_SEC_ERA_10) ?
2969                                 SHR_WAIT : SHR_SERIAL, &encap_pdb,
2970                                 hdr, &cipherdata, &authdata);
2971         } else if (ipsec_xform->direction ==
2972                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2973                 struct ipsec_decap_pdb decap_pdb;
2974
2975                 flc->dhr = SEC_FLC_DHR_INBOUND;
2976                 memset(&decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2977                 /* copy algo specific data to PDB */
2978                 switch (cipherdata.algtype) {
2979                 case OP_PCL_IPSEC_AES_CTR:
2980                         decap_pdb.ctr.ctr_initial = 0x00000001;
2981                         decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2982                         break;
2983                 case OP_PCL_IPSEC_AES_GCM8:
2984                 case OP_PCL_IPSEC_AES_GCM12:
2985                 case OP_PCL_IPSEC_AES_GCM16:
2986                         memcpy(decap_pdb.gcm.salt,
2987                                 (uint8_t *)&(ipsec_xform->salt), 4);
2988                         break;
2989                 }
2990
2991                 decap_pdb.options = (ipsec_xform->tunnel.type ==
2992                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) ?
2993                                 sizeof(struct ip) << 16 :
2994                                 sizeof(struct rte_ipv6_hdr) << 16;
2995                 if (ipsec_xform->options.esn)
2996                         decap_pdb.options |= PDBOPTS_ESP_ESN;
2997
2998                 if (ipsec_xform->replay_win_sz) {
2999                         uint32_t win_sz;
3000                         win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
3001
3002                         if (rta_sec_era < RTA_SEC_ERA_10 && win_sz > 128) {
3003                                 DPAA2_SEC_INFO("Max Anti replay Win sz = 128");
3004                                 win_sz = 128;
3005                         }
3006                         switch (win_sz) {
3007                         case 1:
3008                         case 2:
3009                         case 4:
3010                         case 8:
3011                         case 16:
3012                         case 32:
3013                                 decap_pdb.options |= PDBOPTS_ESP_ARS32;
3014                                 break;
3015                         case 64:
3016                                 decap_pdb.options |= PDBOPTS_ESP_ARS64;
3017                                 break;
3018                         case 256:
3019                                 decap_pdb.options |= PDBOPTS_ESP_ARS256;
3020                                 break;
3021                         case 512:
3022                                 decap_pdb.options |= PDBOPTS_ESP_ARS512;
3023                                 break;
3024                         case 1024:
3025                                 decap_pdb.options |= PDBOPTS_ESP_ARS1024;
3026                                 break;
3027                         case 128:
3028                         default:
3029                                 decap_pdb.options |= PDBOPTS_ESP_ARS128;
3030                         }
3031                 }
3032                 session->dir = DIR_DEC;
3033                 bufsize = cnstr_shdsc_ipsec_new_decap(priv->flc_desc[0].desc,
3034                                 1, 0, (rta_sec_era >= RTA_SEC_ERA_10) ?
3035                                 SHR_WAIT : SHR_SERIAL,
3036                                 &decap_pdb, &cipherdata, &authdata);
3037         } else
3038                 goto out;
3039
3040         if (bufsize < 0) {
3041                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
3042                 goto out;
3043         }
3044
3045         flc->word1_sdl = (uint8_t)bufsize;
3046
3047         /* Enable the stashing control bit */
3048         DPAA2_SET_FLC_RSC(flc);
3049         flc->word2_rflc_31_0 = lower_32_bits(
3050                         (size_t)&(((struct dpaa2_sec_qp *)
3051                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
3052         flc->word3_rflc_63_32 = upper_32_bits(
3053                         (size_t)&(((struct dpaa2_sec_qp *)
3054                         dev->data->queue_pairs[0])->rx_vq));
3055
3056         /* Set EWS bit i.e. enable write-safe */
3057         DPAA2_SET_FLC_EWS(flc);
3058         /* Set BS = 1 i.e reuse input buffers as output buffers */
3059         DPAA2_SET_FLC_REUSE_BS(flc);
3060         /* Set FF = 10; reuse input buffers if they provide sufficient space */
3061         DPAA2_SET_FLC_REUSE_FF(flc);
3062
3063         session->ctxt = priv;
3064
3065         return 0;
3066 out:
3067         rte_free(session->auth_key.data);
3068         rte_free(session->cipher_key.data);
3069         rte_free(priv);
3070         return ret;
3071 }
3072
3073 static int
3074 dpaa2_sec_set_pdcp_session(struct rte_cryptodev *dev,
3075                            struct rte_security_session_conf *conf,
3076                            void *sess)
3077 {
3078         struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
3079         struct rte_crypto_sym_xform *xform = conf->crypto_xform;
3080         struct rte_crypto_auth_xform *auth_xform = NULL;
3081         struct rte_crypto_cipher_xform *cipher_xform;
3082         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
3083         struct ctxt_priv *priv;
3084         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
3085         struct alginfo authdata, cipherdata;
3086         struct alginfo *p_authdata = NULL;
3087         int bufsize = -1;
3088         struct sec_flow_context *flc;
3089 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
3090         int swap = true;
3091 #else
3092         int swap = false;
3093 #endif
3094
3095         PMD_INIT_FUNC_TRACE();
3096
3097         memset(session, 0, sizeof(dpaa2_sec_session));
3098
3099         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
3100                                 sizeof(struct ctxt_priv) +
3101                                 sizeof(struct sec_flc_desc),
3102                                 RTE_CACHE_LINE_SIZE);
3103
3104         if (priv == NULL) {
3105                 DPAA2_SEC_ERR("No memory for priv CTXT");
3106                 return -ENOMEM;
3107         }
3108
3109         priv->fle_pool = dev_priv->fle_pool;
3110         flc = &priv->flc_desc[0].flc;
3111
3112         /* find xfrm types */
3113         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
3114                 cipher_xform = &xform->cipher;
3115         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
3116                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3117                 session->ext_params.aead_ctxt.auth_cipher_text = true;
3118                 cipher_xform = &xform->cipher;
3119                 auth_xform = &xform->next->auth;
3120         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
3121                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3122                 session->ext_params.aead_ctxt.auth_cipher_text = false;
3123                 cipher_xform = &xform->next->cipher;
3124                 auth_xform = &xform->auth;
3125         } else {
3126                 DPAA2_SEC_ERR("Invalid crypto type");
3127                 return -EINVAL;
3128         }
3129
3130         session->ctxt_type = DPAA2_SEC_PDCP;
3131         if (cipher_xform) {
3132                 session->cipher_key.data = rte_zmalloc(NULL,
3133                                                cipher_xform->key.length,
3134                                                RTE_CACHE_LINE_SIZE);
3135                 if (session->cipher_key.data == NULL &&
3136                                 cipher_xform->key.length > 0) {
3137                         DPAA2_SEC_ERR("No Memory for cipher key");
3138                         rte_free(priv);
3139                         return -ENOMEM;
3140                 }
3141                 session->cipher_key.length = cipher_xform->key.length;
3142                 memcpy(session->cipher_key.data, cipher_xform->key.data,
3143                         cipher_xform->key.length);
3144                 session->dir =
3145                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3146                                         DIR_ENC : DIR_DEC;
3147                 session->cipher_alg = cipher_xform->algo;
3148         } else {
3149                 session->cipher_key.data = NULL;
3150                 session->cipher_key.length = 0;
3151                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3152                 session->dir = DIR_ENC;
3153         }
3154
3155         session->pdcp.domain = pdcp_xform->domain;
3156         session->pdcp.bearer = pdcp_xform->bearer;
3157         session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3158         session->pdcp.sn_size = pdcp_xform->sn_size;
3159         session->pdcp.hfn = pdcp_xform->hfn;
3160         session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
3161         session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
3162         /* hfv ovd offset location is stored in iv.offset value*/
3163         session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3164
3165         cipherdata.key = (size_t)session->cipher_key.data;
3166         cipherdata.keylen = session->cipher_key.length;
3167         cipherdata.key_enc_flags = 0;
3168         cipherdata.key_type = RTA_DATA_IMM;
3169
3170         switch (session->cipher_alg) {
3171         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
3172                 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
3173                 break;
3174         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
3175                 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
3176                 break;
3177         case RTE_CRYPTO_CIPHER_AES_CTR:
3178                 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
3179                 break;
3180         case RTE_CRYPTO_CIPHER_NULL:
3181                 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
3182                 break;
3183         default:
3184                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
3185                               session->cipher_alg);
3186                 goto out;
3187         }
3188
3189         if (auth_xform) {
3190                 session->auth_key.data = rte_zmalloc(NULL,
3191                                                      auth_xform->key.length,
3192                                                      RTE_CACHE_LINE_SIZE);
3193                 if (!session->auth_key.data &&
3194                     auth_xform->key.length > 0) {
3195                         DPAA2_SEC_ERR("No Memory for auth key");
3196                         rte_free(session->cipher_key.data);
3197                         rte_free(priv);
3198                         return -ENOMEM;
3199                 }
3200                 session->auth_key.length = auth_xform->key.length;
3201                 memcpy(session->auth_key.data, auth_xform->key.data,
3202                        auth_xform->key.length);
3203                 session->auth_alg = auth_xform->algo;
3204         } else {
3205                 session->auth_key.data = NULL;
3206                 session->auth_key.length = 0;
3207                 session->auth_alg = 0;
3208         }
3209         authdata.key = (size_t)session->auth_key.data;
3210         authdata.keylen = session->auth_key.length;
3211         authdata.key_enc_flags = 0;
3212         authdata.key_type = RTA_DATA_IMM;
3213
3214         if (session->auth_alg) {
3215                 switch (session->auth_alg) {
3216                 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
3217                         authdata.algtype = PDCP_AUTH_TYPE_SNOW;
3218                         break;
3219                 case RTE_CRYPTO_AUTH_ZUC_EIA3:
3220                         authdata.algtype = PDCP_AUTH_TYPE_ZUC;
3221                         break;
3222                 case RTE_CRYPTO_AUTH_AES_CMAC:
3223                         authdata.algtype = PDCP_AUTH_TYPE_AES;
3224                         break;
3225                 case RTE_CRYPTO_AUTH_NULL:
3226                         authdata.algtype = PDCP_AUTH_TYPE_NULL;
3227                         break;
3228                 default:
3229                         DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
3230                                       session->auth_alg);
3231                         goto out;
3232                 }
3233
3234                 p_authdata = &authdata;
3235         } else if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3236                 DPAA2_SEC_ERR("Crypto: Integrity must for c-plane");
3237                 goto out;
3238         }
3239
3240         if (rta_inline_pdcp_query(authdata.algtype,
3241                                 cipherdata.algtype,
3242                                 session->pdcp.sn_size,
3243                                 session->pdcp.hfn_ovd)) {
3244                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
3245                 cipherdata.key_type = RTA_DATA_PTR;
3246         }
3247
3248         if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3249                 if (session->dir == DIR_ENC)
3250                         bufsize = cnstr_shdsc_pdcp_c_plane_encap(
3251                                         priv->flc_desc[0].desc, 1, swap,
3252                                         pdcp_xform->hfn,
3253                                         session->pdcp.sn_size,
3254                                         pdcp_xform->bearer,
3255                                         pdcp_xform->pkt_dir,
3256                                         pdcp_xform->hfn_threshold,
3257                                         &cipherdata, &authdata,
3258                                         0);
3259                 else if (session->dir == DIR_DEC)
3260                         bufsize = cnstr_shdsc_pdcp_c_plane_decap(
3261                                         priv->flc_desc[0].desc, 1, swap,
3262                                         pdcp_xform->hfn,
3263                                         session->pdcp.sn_size,
3264                                         pdcp_xform->bearer,
3265                                         pdcp_xform->pkt_dir,
3266                                         pdcp_xform->hfn_threshold,
3267                                         &cipherdata, &authdata,
3268                                         0);
3269         } else {
3270                 if (session->dir == DIR_ENC) {
3271                         if (pdcp_xform->sdap_enabled)
3272                                 bufsize = cnstr_shdsc_pdcp_sdap_u_plane_encap(
3273                                         priv->flc_desc[0].desc, 1, swap,
3274                                         session->pdcp.sn_size,
3275                                         pdcp_xform->hfn,
3276                                         pdcp_xform->bearer,
3277                                         pdcp_xform->pkt_dir,
3278                                         pdcp_xform->hfn_threshold,
3279                                         &cipherdata, p_authdata, 0);
3280                         else
3281                                 bufsize = cnstr_shdsc_pdcp_u_plane_encap(
3282                                         priv->flc_desc[0].desc, 1, swap,
3283                                         session->pdcp.sn_size,
3284                                         pdcp_xform->hfn,
3285                                         pdcp_xform->bearer,
3286                                         pdcp_xform->pkt_dir,
3287                                         pdcp_xform->hfn_threshold,
3288                                         &cipherdata, p_authdata, 0);
3289                 } else if (session->dir == DIR_DEC) {
3290                         if (pdcp_xform->sdap_enabled)
3291                                 bufsize = cnstr_shdsc_pdcp_sdap_u_plane_decap(
3292                                         priv->flc_desc[0].desc, 1, swap,
3293                                         session->pdcp.sn_size,
3294                                         pdcp_xform->hfn,
3295                                         pdcp_xform->bearer,
3296                                         pdcp_xform->pkt_dir,
3297                                         pdcp_xform->hfn_threshold,
3298                                         &cipherdata, p_authdata, 0);
3299                         else
3300                                 bufsize = cnstr_shdsc_pdcp_u_plane_decap(
3301                                         priv->flc_desc[0].desc, 1, swap,
3302                                         session->pdcp.sn_size,
3303                                         pdcp_xform->hfn,
3304                                         pdcp_xform->bearer,
3305                                         pdcp_xform->pkt_dir,
3306                                         pdcp_xform->hfn_threshold,
3307                                         &cipherdata, p_authdata, 0);
3308                 }
3309         }
3310
3311         if (bufsize < 0) {
3312                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
3313                 goto out;
3314         }
3315
3316         /* Enable the stashing control bit */
3317         DPAA2_SET_FLC_RSC(flc);
3318         flc->word2_rflc_31_0 = lower_32_bits(
3319                         (size_t)&(((struct dpaa2_sec_qp *)
3320                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
3321         flc->word3_rflc_63_32 = upper_32_bits(
3322                         (size_t)&(((struct dpaa2_sec_qp *)
3323                         dev->data->queue_pairs[0])->rx_vq));
3324
3325         flc->word1_sdl = (uint8_t)bufsize;
3326
3327         /* TODO - check the perf impact or
3328          * align as per descriptor type
3329          * Set EWS bit i.e. enable write-safe
3330          * DPAA2_SET_FLC_EWS(flc);
3331          */
3332
3333         /* Set BS = 1 i.e reuse input buffers as output buffers */
3334         DPAA2_SET_FLC_REUSE_BS(flc);
3335         /* Set FF = 10; reuse input buffers if they provide sufficient space */
3336         DPAA2_SET_FLC_REUSE_FF(flc);
3337
3338         session->ctxt = priv;
3339
3340         return 0;
3341 out:
3342         rte_free(session->auth_key.data);
3343         rte_free(session->cipher_key.data);
3344         rte_free(priv);
3345         return -EINVAL;
3346 }
3347
3348 static int
3349 dpaa2_sec_security_session_create(void *dev,
3350                                   struct rte_security_session_conf *conf,
3351                                   struct rte_security_session *sess,
3352                                   struct rte_mempool *mempool)
3353 {
3354         void *sess_private_data;
3355         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
3356         int ret;
3357
3358         if (rte_mempool_get(mempool, &sess_private_data)) {
3359                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
3360                 return -ENOMEM;
3361         }
3362
3363         switch (conf->protocol) {
3364         case RTE_SECURITY_PROTOCOL_IPSEC:
3365                 ret = dpaa2_sec_set_ipsec_session(cdev, conf,
3366                                 sess_private_data);
3367                 break;
3368         case RTE_SECURITY_PROTOCOL_MACSEC:
3369                 return -ENOTSUP;
3370         case RTE_SECURITY_PROTOCOL_PDCP:
3371                 ret = dpaa2_sec_set_pdcp_session(cdev, conf,
3372                                 sess_private_data);
3373                 break;
3374         default:
3375                 return -EINVAL;
3376         }
3377         if (ret != 0) {
3378                 DPAA2_SEC_ERR("Failed to configure session parameters");
3379                 /* Return session to mempool */
3380                 rte_mempool_put(mempool, sess_private_data);
3381                 return ret;
3382         }
3383
3384         set_sec_session_private_data(sess, sess_private_data);
3385
3386         return ret;
3387 }
3388
3389 /** Clear the memory of session so it doesn't leave key material behind */
3390 static int
3391 dpaa2_sec_security_session_destroy(void *dev __rte_unused,
3392                 struct rte_security_session *sess)
3393 {
3394         PMD_INIT_FUNC_TRACE();
3395         void *sess_priv = get_sec_session_private_data(sess);
3396
3397         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3398
3399         if (sess_priv) {
3400                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3401
3402                 rte_free(s->ctxt);
3403                 rte_free(s->cipher_key.data);
3404                 rte_free(s->auth_key.data);
3405                 memset(s, 0, sizeof(dpaa2_sec_session));
3406                 set_sec_session_private_data(sess, NULL);
3407                 rte_mempool_put(sess_mp, sess_priv);
3408         }
3409         return 0;
3410 }
3411 #endif
3412 static int
3413 dpaa2_sec_sym_session_configure(struct rte_cryptodev *dev,
3414                 struct rte_crypto_sym_xform *xform,
3415                 struct rte_cryptodev_sym_session *sess,
3416                 struct rte_mempool *mempool)
3417 {
3418         void *sess_private_data;
3419         int ret;
3420
3421         if (rte_mempool_get(mempool, &sess_private_data)) {
3422                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
3423                 return -ENOMEM;
3424         }
3425
3426         ret = dpaa2_sec_set_session_parameters(dev, xform, sess_private_data);
3427         if (ret != 0) {
3428                 DPAA2_SEC_ERR("Failed to configure session parameters");
3429                 /* Return session to mempool */
3430                 rte_mempool_put(mempool, sess_private_data);
3431                 return ret;
3432         }
3433
3434         set_sym_session_private_data(sess, dev->driver_id,
3435                 sess_private_data);
3436
3437         return 0;
3438 }
3439
3440 /** Clear the memory of session so it doesn't leave key material behind */
3441 static void
3442 dpaa2_sec_sym_session_clear(struct rte_cryptodev *dev,
3443                 struct rte_cryptodev_sym_session *sess)
3444 {
3445         PMD_INIT_FUNC_TRACE();
3446         uint8_t index = dev->driver_id;
3447         void *sess_priv = get_sym_session_private_data(sess, index);
3448         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3449
3450         if (sess_priv) {
3451                 rte_free(s->ctxt);
3452                 rte_free(s->cipher_key.data);
3453                 rte_free(s->auth_key.data);
3454                 memset(s, 0, sizeof(dpaa2_sec_session));
3455                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3456                 set_sym_session_private_data(sess, index, NULL);
3457                 rte_mempool_put(sess_mp, sess_priv);
3458         }
3459 }
3460
3461 static int
3462 dpaa2_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3463                         struct rte_cryptodev_config *config __rte_unused)
3464 {
3465         PMD_INIT_FUNC_TRACE();
3466
3467         return 0;
3468 }
3469
3470 static int
3471 dpaa2_sec_dev_start(struct rte_cryptodev *dev)
3472 {
3473         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3474         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3475         struct dpseci_attr attr;
3476         struct dpaa2_queue *dpaa2_q;
3477         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3478                                         dev->data->queue_pairs;
3479         struct dpseci_rx_queue_attr rx_attr;
3480         struct dpseci_tx_queue_attr tx_attr;
3481         int ret, i;
3482
3483         PMD_INIT_FUNC_TRACE();
3484
3485         memset(&attr, 0, sizeof(struct dpseci_attr));
3486
3487         ret = dpseci_enable(dpseci, CMD_PRI_LOW, priv->token);
3488         if (ret) {
3489                 DPAA2_SEC_ERR("DPSECI with HW_ID = %d ENABLE FAILED",
3490                               priv->hw_id);
3491                 goto get_attr_failure;
3492         }
3493         ret = dpseci_get_attributes(dpseci, CMD_PRI_LOW, priv->token, &attr);
3494         if (ret) {
3495                 DPAA2_SEC_ERR("DPSEC ATTRIBUTE READ FAILED, disabling DPSEC");
3496                 goto get_attr_failure;
3497         }
3498         for (i = 0; i < attr.num_rx_queues && qp[i]; i++) {
3499                 dpaa2_q = &qp[i]->rx_vq;
3500                 dpseci_get_rx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3501                                     &rx_attr);
3502                 dpaa2_q->fqid = rx_attr.fqid;
3503                 DPAA2_SEC_DEBUG("rx_fqid: %d", dpaa2_q->fqid);
3504         }
3505         for (i = 0; i < attr.num_tx_queues && qp[i]; i++) {
3506                 dpaa2_q = &qp[i]->tx_vq;
3507                 dpseci_get_tx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3508                                     &tx_attr);
3509                 dpaa2_q->fqid = tx_attr.fqid;
3510                 DPAA2_SEC_DEBUG("tx_fqid: %d", dpaa2_q->fqid);
3511         }
3512
3513         return 0;
3514 get_attr_failure:
3515         dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3516         return -1;
3517 }
3518
3519 static void
3520 dpaa2_sec_dev_stop(struct rte_cryptodev *dev)
3521 {
3522         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3523         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3524         int ret;
3525
3526         PMD_INIT_FUNC_TRACE();
3527
3528         ret = dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3529         if (ret) {
3530                 DPAA2_SEC_ERR("Failure in disabling dpseci %d device",
3531                              priv->hw_id);
3532                 return;
3533         }
3534
3535         ret = dpseci_reset(dpseci, CMD_PRI_LOW, priv->token);
3536         if (ret < 0) {
3537                 DPAA2_SEC_ERR("SEC Device cannot be reset:Error = %0x", ret);
3538                 return;
3539         }
3540 }
3541
3542 static int
3543 dpaa2_sec_dev_close(struct rte_cryptodev *dev)
3544 {
3545         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3546         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3547         int ret;
3548
3549         PMD_INIT_FUNC_TRACE();
3550
3551         /* Function is reverse of dpaa2_sec_dev_init.
3552          * It does the following:
3553          * 1. Detach a DPSECI from attached resources i.e. buffer pools, dpbp_id
3554          * 2. Close the DPSECI device
3555          * 3. Free the allocated resources.
3556          */
3557
3558         /*Close the device at underlying layer*/
3559         ret = dpseci_close(dpseci, CMD_PRI_LOW, priv->token);
3560         if (ret) {
3561                 DPAA2_SEC_ERR("Failure closing dpseci device: err(%d)", ret);
3562                 return -1;
3563         }
3564
3565         /*Free the allocated memory for ethernet private data and dpseci*/
3566         priv->hw = NULL;
3567         rte_free(dpseci);
3568
3569         return 0;
3570 }
3571
3572 static void
3573 dpaa2_sec_dev_infos_get(struct rte_cryptodev *dev,
3574                         struct rte_cryptodev_info *info)
3575 {
3576         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
3577
3578         PMD_INIT_FUNC_TRACE();
3579         if (info != NULL) {
3580                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3581                 info->feature_flags = dev->feature_flags;
3582                 info->capabilities = dpaa2_sec_capabilities;
3583                 /* No limit of number of sessions */
3584                 info->sym.max_nb_sessions = 0;
3585                 info->driver_id = cryptodev_driver_id;
3586         }
3587 }
3588
3589 static
3590 void dpaa2_sec_stats_get(struct rte_cryptodev *dev,
3591                          struct rte_cryptodev_stats *stats)
3592 {
3593         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3594         struct fsl_mc_io dpseci;
3595         struct dpseci_sec_counters counters = {0};
3596         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3597                                         dev->data->queue_pairs;
3598         int ret, i;
3599
3600         PMD_INIT_FUNC_TRACE();
3601         if (stats == NULL) {
3602                 DPAA2_SEC_ERR("Invalid stats ptr NULL");
3603                 return;
3604         }
3605         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3606                 if (qp == NULL || qp[i] == NULL) {
3607                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3608                         continue;
3609                 }
3610
3611                 stats->enqueued_count += qp[i]->tx_vq.tx_pkts;
3612                 stats->dequeued_count += qp[i]->rx_vq.rx_pkts;
3613                 stats->enqueue_err_count += qp[i]->tx_vq.err_pkts;
3614                 stats->dequeue_err_count += qp[i]->rx_vq.err_pkts;
3615         }
3616
3617         /* In case as secondary process access stats, MCP portal in priv-hw
3618          * may have primary process address. Need the secondary process
3619          * based MCP portal address for this object.
3620          */
3621         dpseci.regs = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
3622         ret = dpseci_get_sec_counters(&dpseci, CMD_PRI_LOW, priv->token,
3623                                       &counters);
3624         if (ret) {
3625                 DPAA2_SEC_ERR("SEC counters failed");
3626         } else {
3627                 DPAA2_SEC_INFO("dpseci hardware stats:"
3628                             "\n\tNum of Requests Dequeued = %" PRIu64
3629                             "\n\tNum of Outbound Encrypt Requests = %" PRIu64
3630                             "\n\tNum of Inbound Decrypt Requests = %" PRIu64
3631                             "\n\tNum of Outbound Bytes Encrypted = %" PRIu64
3632                             "\n\tNum of Outbound Bytes Protected = %" PRIu64
3633                             "\n\tNum of Inbound Bytes Decrypted = %" PRIu64
3634                             "\n\tNum of Inbound Bytes Validated = %" PRIu64,
3635                             counters.dequeued_requests,
3636                             counters.ob_enc_requests,
3637                             counters.ib_dec_requests,
3638                             counters.ob_enc_bytes,
3639                             counters.ob_prot_bytes,
3640                             counters.ib_dec_bytes,
3641                             counters.ib_valid_bytes);
3642         }
3643 }
3644
3645 static
3646 void dpaa2_sec_stats_reset(struct rte_cryptodev *dev)
3647 {
3648         int i;
3649         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3650                                    (dev->data->queue_pairs);
3651
3652         PMD_INIT_FUNC_TRACE();
3653
3654         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3655                 if (qp[i] == NULL) {
3656                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3657                         continue;
3658                 }
3659                 qp[i]->tx_vq.rx_pkts = 0;
3660                 qp[i]->tx_vq.tx_pkts = 0;
3661                 qp[i]->tx_vq.err_pkts = 0;
3662                 qp[i]->rx_vq.rx_pkts = 0;
3663                 qp[i]->rx_vq.tx_pkts = 0;
3664                 qp[i]->rx_vq.err_pkts = 0;
3665         }
3666 }
3667
3668 static void __rte_hot
3669 dpaa2_sec_process_parallel_event(struct qbman_swp *swp,
3670                                  const struct qbman_fd *fd,
3671                                  const struct qbman_result *dq,
3672                                  struct dpaa2_queue *rxq,
3673                                  struct rte_event *ev)
3674 {
3675         /* Prefetching mbuf */
3676         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
3677                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
3678
3679         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
3680         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
3681
3682         ev->flow_id = rxq->ev.flow_id;
3683         ev->sub_event_type = rxq->ev.sub_event_type;
3684         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3685         ev->op = RTE_EVENT_OP_NEW;
3686         ev->sched_type = rxq->ev.sched_type;
3687         ev->queue_id = rxq->ev.queue_id;
3688         ev->priority = rxq->ev.priority;
3689         ev->event_ptr = sec_fd_to_mbuf(fd);
3690
3691         qbman_swp_dqrr_consume(swp, dq);
3692 }
3693 static void
3694 dpaa2_sec_process_atomic_event(struct qbman_swp *swp __rte_unused,
3695                                  const struct qbman_fd *fd,
3696                                  const struct qbman_result *dq,
3697                                  struct dpaa2_queue *rxq,
3698                                  struct rte_event *ev)
3699 {
3700         uint8_t dqrr_index;
3701         struct rte_crypto_op *crypto_op = (struct rte_crypto_op *)ev->event_ptr;
3702         /* Prefetching mbuf */
3703         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
3704                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
3705
3706         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
3707         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
3708
3709         ev->flow_id = rxq->ev.flow_id;
3710         ev->sub_event_type = rxq->ev.sub_event_type;
3711         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3712         ev->op = RTE_EVENT_OP_NEW;
3713         ev->sched_type = rxq->ev.sched_type;
3714         ev->queue_id = rxq->ev.queue_id;
3715         ev->priority = rxq->ev.priority;
3716
3717         ev->event_ptr = sec_fd_to_mbuf(fd);
3718         dqrr_index = qbman_get_dqrr_idx(dq);
3719         *dpaa2_seqn(crypto_op->sym->m_src) = dqrr_index + 1;
3720         DPAA2_PER_LCORE_DQRR_SIZE++;
3721         DPAA2_PER_LCORE_DQRR_HELD |= 1 << dqrr_index;
3722         DPAA2_PER_LCORE_DQRR_MBUF(dqrr_index) = crypto_op->sym->m_src;
3723 }
3724
3725 int
3726 dpaa2_sec_eventq_attach(const struct rte_cryptodev *dev,
3727                 int qp_id,
3728                 struct dpaa2_dpcon_dev *dpcon,
3729                 const struct rte_event *event)
3730 {
3731         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3732         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3733         struct dpaa2_sec_qp *qp = dev->data->queue_pairs[qp_id];
3734         struct dpseci_rx_queue_cfg cfg;
3735         uint8_t priority;
3736         int ret;
3737
3738         if (event->sched_type == RTE_SCHED_TYPE_PARALLEL)
3739                 qp->rx_vq.cb = dpaa2_sec_process_parallel_event;
3740         else if (event->sched_type == RTE_SCHED_TYPE_ATOMIC)
3741                 qp->rx_vq.cb = dpaa2_sec_process_atomic_event;
3742         else
3743                 return -EINVAL;
3744
3745         priority = (RTE_EVENT_DEV_PRIORITY_LOWEST / event->priority) *
3746                    (dpcon->num_priorities - 1);
3747
3748         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
3749         cfg.options = DPSECI_QUEUE_OPT_DEST;
3750         cfg.dest_cfg.dest_type = DPSECI_DEST_DPCON;
3751         cfg.dest_cfg.dest_id = dpcon->dpcon_id;
3752         cfg.dest_cfg.priority = priority;
3753
3754         cfg.options |= DPSECI_QUEUE_OPT_USER_CTX;
3755         cfg.user_ctx = (size_t)(qp);
3756         if (event->sched_type == RTE_SCHED_TYPE_ATOMIC) {
3757                 cfg.options |= DPSECI_QUEUE_OPT_ORDER_PRESERVATION;
3758                 cfg.order_preservation_en = 1;
3759         }
3760         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
3761                                   qp_id, &cfg);
3762         if (ret) {
3763                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
3764                 return ret;
3765         }
3766
3767         memcpy(&qp->rx_vq.ev, event, sizeof(struct rte_event));
3768
3769         return 0;
3770 }
3771
3772 int
3773 dpaa2_sec_eventq_detach(const struct rte_cryptodev *dev,
3774                         int qp_id)
3775 {
3776         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3777         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3778         struct dpseci_rx_queue_cfg cfg;
3779         int ret;
3780
3781         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
3782         cfg.options = DPSECI_QUEUE_OPT_DEST;
3783         cfg.dest_cfg.dest_type = DPSECI_DEST_NONE;
3784
3785         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
3786                                   qp_id, &cfg);
3787         if (ret)
3788                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
3789
3790         return ret;
3791 }
3792
3793 static struct rte_cryptodev_ops crypto_ops = {
3794         .dev_configure        = dpaa2_sec_dev_configure,
3795         .dev_start            = dpaa2_sec_dev_start,
3796         .dev_stop             = dpaa2_sec_dev_stop,
3797         .dev_close            = dpaa2_sec_dev_close,
3798         .dev_infos_get        = dpaa2_sec_dev_infos_get,
3799         .stats_get            = dpaa2_sec_stats_get,
3800         .stats_reset          = dpaa2_sec_stats_reset,
3801         .queue_pair_setup     = dpaa2_sec_queue_pair_setup,
3802         .queue_pair_release   = dpaa2_sec_queue_pair_release,
3803         .sym_session_get_size     = dpaa2_sec_sym_session_get_size,
3804         .sym_session_configure    = dpaa2_sec_sym_session_configure,
3805         .sym_session_clear        = dpaa2_sec_sym_session_clear,
3806 };
3807
3808 #ifdef RTE_LIB_SECURITY
3809 static const struct rte_security_capability *
3810 dpaa2_sec_capabilities_get(void *device __rte_unused)
3811 {
3812         return dpaa2_sec_security_cap;
3813 }
3814
3815 static const struct rte_security_ops dpaa2_sec_security_ops = {
3816         .session_create = dpaa2_sec_security_session_create,
3817         .session_update = NULL,
3818         .session_stats_get = NULL,
3819         .session_destroy = dpaa2_sec_security_session_destroy,
3820         .set_pkt_metadata = NULL,
3821         .capabilities_get = dpaa2_sec_capabilities_get
3822 };
3823 #endif
3824
3825 static int
3826 dpaa2_sec_uninit(const struct rte_cryptodev *dev)
3827 {
3828         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
3829
3830         rte_free(dev->security_ctx);
3831
3832         rte_mempool_free(internals->fle_pool);
3833
3834         DPAA2_SEC_INFO("Closing DPAA2_SEC device %s on numa socket %u",
3835                        dev->data->name, rte_socket_id());
3836
3837         return 0;
3838 }
3839
3840 static int
3841 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
3842 {
3843         struct dpaa2_sec_dev_private *internals;
3844         struct rte_device *dev = cryptodev->device;
3845         struct rte_dpaa2_device *dpaa2_dev;
3846 #ifdef RTE_LIB_SECURITY
3847         struct rte_security_ctx *security_instance;
3848 #endif
3849         struct fsl_mc_io *dpseci;
3850         uint16_t token;
3851         struct dpseci_attr attr;
3852         int retcode, hw_id;
3853         char str[30];
3854
3855         PMD_INIT_FUNC_TRACE();
3856         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
3857         hw_id = dpaa2_dev->object_id;
3858
3859         cryptodev->driver_id = cryptodev_driver_id;
3860         cryptodev->dev_ops = &crypto_ops;
3861
3862         cryptodev->enqueue_burst = dpaa2_sec_enqueue_burst;
3863         cryptodev->dequeue_burst = dpaa2_sec_dequeue_burst;
3864         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3865                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
3866                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3867                         RTE_CRYPTODEV_FF_SECURITY |
3868                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
3869                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
3870                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
3871                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
3872                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3873
3874         internals = cryptodev->data->dev_private;
3875
3876         /*
3877          * For secondary processes, we don't initialise any further as primary
3878          * has already done this work. Only check we don't need a different
3879          * RX function
3880          */
3881         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3882                 DPAA2_SEC_DEBUG("Device already init by primary process");
3883                 return 0;
3884         }
3885 #ifdef RTE_LIB_SECURITY
3886         /* Initialize security_ctx only for primary process*/
3887         security_instance = rte_malloc("rte_security_instances_ops",
3888                                 sizeof(struct rte_security_ctx), 0);
3889         if (security_instance == NULL)
3890                 return -ENOMEM;
3891         security_instance->device = (void *)cryptodev;
3892         security_instance->ops = &dpaa2_sec_security_ops;
3893         security_instance->sess_cnt = 0;
3894         cryptodev->security_ctx = security_instance;
3895 #endif
3896         /*Open the rte device via MC and save the handle for further use*/
3897         dpseci = (struct fsl_mc_io *)rte_calloc(NULL, 1,
3898                                 sizeof(struct fsl_mc_io), 0);
3899         if (!dpseci) {
3900                 DPAA2_SEC_ERR(
3901                         "Error in allocating the memory for dpsec object");
3902                 return -ENOMEM;
3903         }
3904         dpseci->regs = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
3905
3906         retcode = dpseci_open(dpseci, CMD_PRI_LOW, hw_id, &token);
3907         if (retcode != 0) {
3908                 DPAA2_SEC_ERR("Cannot open the dpsec device: Error = %x",
3909                               retcode);
3910                 goto init_error;
3911         }
3912         retcode = dpseci_get_attributes(dpseci, CMD_PRI_LOW, token, &attr);
3913         if (retcode != 0) {
3914                 DPAA2_SEC_ERR(
3915                              "Cannot get dpsec device attributed: Error = %x",
3916                              retcode);
3917                 goto init_error;
3918         }
3919         snprintf(cryptodev->data->name, sizeof(cryptodev->data->name),
3920                         "dpsec-%u", hw_id);
3921
3922         internals->max_nb_queue_pairs = attr.num_tx_queues;
3923         cryptodev->data->nb_queue_pairs = internals->max_nb_queue_pairs;
3924         internals->hw = dpseci;
3925         internals->token = token;
3926
3927         snprintf(str, sizeof(str), "sec_fle_pool_p%d_%d",
3928                         getpid(), cryptodev->data->dev_id);
3929         internals->fle_pool = rte_mempool_create((const char *)str,
3930                         FLE_POOL_NUM_BUFS,
3931                         FLE_POOL_BUF_SIZE,
3932                         FLE_POOL_CACHE_SIZE, 0,
3933                         NULL, NULL, NULL, NULL,
3934                         SOCKET_ID_ANY, 0);
3935         if (!internals->fle_pool) {
3936                 DPAA2_SEC_ERR("Mempool (%s) creation failed", str);
3937                 goto init_error;
3938         }
3939
3940         DPAA2_SEC_INFO("driver %s: created", cryptodev->data->name);
3941         return 0;
3942
3943 init_error:
3944         DPAA2_SEC_ERR("driver %s: create failed", cryptodev->data->name);
3945
3946         /* dpaa2_sec_uninit(crypto_dev_name); */
3947         return -EFAULT;
3948 }
3949
3950 static int
3951 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
3952                           struct rte_dpaa2_device *dpaa2_dev)
3953 {
3954         struct rte_cryptodev *cryptodev;
3955         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3956
3957         int retval;
3958
3959         snprintf(cryptodev_name, sizeof(cryptodev_name), "dpsec-%d",
3960                         dpaa2_dev->object_id);
3961
3962         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3963         if (cryptodev == NULL)
3964                 return -ENOMEM;
3965
3966         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3967                 cryptodev->data->dev_private = rte_zmalloc_socket(
3968                                         "cryptodev private structure",
3969                                         sizeof(struct dpaa2_sec_dev_private),
3970                                         RTE_CACHE_LINE_SIZE,
3971                                         rte_socket_id());
3972
3973                 if (cryptodev->data->dev_private == NULL)
3974                         rte_panic("Cannot allocate memzone for private "
3975                                   "device data");
3976         }
3977
3978         dpaa2_dev->cryptodev = cryptodev;
3979         cryptodev->device = &dpaa2_dev->device;
3980
3981         /* init user callbacks */
3982         TAILQ_INIT(&(cryptodev->link_intr_cbs));
3983
3984         if (dpaa2_svr_family == SVR_LX2160A)
3985                 rta_set_sec_era(RTA_SEC_ERA_10);
3986         else
3987                 rta_set_sec_era(RTA_SEC_ERA_8);
3988
3989         DPAA2_SEC_INFO("2-SEC ERA is %d", rta_get_sec_era());
3990
3991         /* Invoke PMD device initialization function */
3992         retval = dpaa2_sec_dev_init(cryptodev);
3993         if (retval == 0)
3994                 return 0;
3995
3996         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3997                 rte_free(cryptodev->data->dev_private);
3998
3999         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
4000
4001         return -ENXIO;
4002 }
4003
4004 static int
4005 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
4006 {
4007         struct rte_cryptodev *cryptodev;
4008         int ret;
4009
4010         cryptodev = dpaa2_dev->cryptodev;
4011         if (cryptodev == NULL)
4012                 return -ENODEV;
4013
4014         ret = dpaa2_sec_uninit(cryptodev);
4015         if (ret)
4016                 return ret;
4017
4018         return rte_cryptodev_pmd_destroy(cryptodev);
4019 }
4020
4021 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
4022         .drv_flags = RTE_DPAA2_DRV_IOVA_AS_VA,
4023         .drv_type = DPAA2_CRYPTO,
4024         .driver = {
4025                 .name = "DPAA2 SEC PMD"
4026         },
4027         .probe = cryptodev_dpaa2_sec_probe,
4028         .remove = cryptodev_dpaa2_sec_remove,
4029 };
4030
4031 static struct cryptodev_driver dpaa2_sec_crypto_drv;
4032
4033 RTE_PMD_REGISTER_DPAA2(CRYPTODEV_NAME_DPAA2_SEC_PMD, rte_dpaa2_sec_driver);
4034 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa2_sec_crypto_drv,
4035                 rte_dpaa2_sec_driver.driver, cryptodev_driver_id);
4036 RTE_LOG_REGISTER(dpaa2_logtype_sec, pmd.crypto.dpaa2, NOTICE);