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