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