crypto/dpaa2_sec: support scatter gather for proto offloads
[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         uint8_t *old_digest;
884         struct rte_mbuf *mbuf;
885
886         PMD_INIT_FUNC_TRACE();
887
888         mbuf = sym_op->m_src;
889         fle = (struct qbman_fle *)rte_malloc(NULL, FLE_SG_MEM_SIZE,
890                         RTE_CACHE_LINE_SIZE);
891         if (unlikely(!fle)) {
892                 DPAA2_SEC_ERR("AUTH SG: Memory alloc failed for SGE");
893                 return -1;
894         }
895         memset(fle, 0, FLE_SG_MEM_SIZE);
896         /* first FLE entry used to store mbuf and session ctxt */
897         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
898         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
899         op_fle = fle + 1;
900         ip_fle = fle + 2;
901         sge = fle + 3;
902
903         flc = &priv->flc_desc[DESC_INITFINAL].flc;
904         /* sg FD */
905         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
906         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
907         DPAA2_SET_FD_COMPOUND_FMT(fd);
908
909         /* o/p fle */
910         DPAA2_SET_FLE_ADDR(op_fle,
911                                 DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
912         op_fle->length = sess->digest_length;
913
914         /* i/p fle */
915         DPAA2_SET_FLE_SG_EXT(ip_fle);
916         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
917         /* i/p 1st seg */
918         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
919         DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset + mbuf->data_off);
920         sge->length = mbuf->data_len - sym_op->auth.data.offset;
921
922         /* i/p segs */
923         mbuf = mbuf->next;
924         while (mbuf) {
925                 sge++;
926                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
927                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
928                 sge->length = mbuf->data_len;
929                 mbuf = mbuf->next;
930         }
931         if (sess->dir == DIR_ENC) {
932                 /* Digest calculation case */
933                 sge->length -= sess->digest_length;
934                 ip_fle->length = sym_op->auth.data.length;
935         } else {
936                 /* Digest verification case */
937                 sge++;
938                 old_digest = (uint8_t *)(sge + 1);
939                 rte_memcpy(old_digest, sym_op->auth.digest.data,
940                            sess->digest_length);
941                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_digest));
942                 sge->length = sess->digest_length;
943                 ip_fle->length = sym_op->auth.data.length +
944                                 sess->digest_length;
945         }
946         DPAA2_SET_FLE_FIN(sge);
947         DPAA2_SET_FLE_FIN(ip_fle);
948         DPAA2_SET_FD_LEN(fd, ip_fle->length);
949
950         return 0;
951 }
952
953 static inline int
954 build_auth_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
955               struct qbman_fd *fd, uint16_t bpid)
956 {
957         struct rte_crypto_sym_op *sym_op = op->sym;
958         struct qbman_fle *fle, *sge;
959         struct sec_flow_context *flc;
960         struct ctxt_priv *priv = sess->ctxt;
961         uint8_t *old_digest;
962         int retval;
963
964         PMD_INIT_FUNC_TRACE();
965
966         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
967         if (retval) {
968                 DPAA2_SEC_ERR("AUTH Memory alloc failed for SGE");
969                 return -1;
970         }
971         memset(fle, 0, FLE_POOL_BUF_SIZE);
972         /* TODO we are using the first FLE entry to store Mbuf.
973          * Currently we donot know which FLE has the mbuf stored.
974          * So while retreiving we can go back 1 FLE from the FD -ADDR
975          * to get the MBUF Addr from the previous FLE.
976          * We can have a better approach to use the inline Mbuf
977          */
978         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
979         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
980         fle = fle + 1;
981
982         if (likely(bpid < MAX_BPID)) {
983                 DPAA2_SET_FD_BPID(fd, bpid);
984                 DPAA2_SET_FLE_BPID(fle, bpid);
985                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
986         } else {
987                 DPAA2_SET_FD_IVP(fd);
988                 DPAA2_SET_FLE_IVP(fle);
989                 DPAA2_SET_FLE_IVP((fle + 1));
990         }
991         flc = &priv->flc_desc[DESC_INITFINAL].flc;
992         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
993
994         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
995         fle->length = sess->digest_length;
996
997         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
998         DPAA2_SET_FD_COMPOUND_FMT(fd);
999         fle++;
1000
1001         if (sess->dir == DIR_ENC) {
1002                 DPAA2_SET_FLE_ADDR(fle,
1003                                    DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
1004                 DPAA2_SET_FLE_OFFSET(fle, sym_op->auth.data.offset +
1005                                      sym_op->m_src->data_off);
1006                 DPAA2_SET_FD_LEN(fd, sym_op->auth.data.length);
1007                 fle->length = sym_op->auth.data.length;
1008         } else {
1009                 sge = fle + 2;
1010                 DPAA2_SET_FLE_SG_EXT(fle);
1011                 DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
1012
1013                 if (likely(bpid < MAX_BPID)) {
1014                         DPAA2_SET_FLE_BPID(sge, bpid);
1015                         DPAA2_SET_FLE_BPID(sge + 1, bpid);
1016                 } else {
1017                         DPAA2_SET_FLE_IVP(sge);
1018                         DPAA2_SET_FLE_IVP((sge + 1));
1019                 }
1020                 DPAA2_SET_FLE_ADDR(sge,
1021                                    DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
1022                 DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset +
1023                                      sym_op->m_src->data_off);
1024
1025                 DPAA2_SET_FD_LEN(fd, sym_op->auth.data.length +
1026                                  sess->digest_length);
1027                 sge->length = sym_op->auth.data.length;
1028                 sge++;
1029                 old_digest = (uint8_t *)(sge + 1);
1030                 rte_memcpy(old_digest, sym_op->auth.digest.data,
1031                            sess->digest_length);
1032                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_digest));
1033                 sge->length = sess->digest_length;
1034                 fle->length = sym_op->auth.data.length +
1035                                 sess->digest_length;
1036                 DPAA2_SET_FLE_FIN(sge);
1037         }
1038         DPAA2_SET_FLE_FIN(fle);
1039
1040         return 0;
1041 }
1042
1043 static int
1044 build_cipher_sg_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
1045                 struct qbman_fd *fd, __rte_unused uint16_t bpid)
1046 {
1047         struct rte_crypto_sym_op *sym_op = op->sym;
1048         struct qbman_fle *ip_fle, *op_fle, *sge, *fle;
1049         struct sec_flow_context *flc;
1050         struct ctxt_priv *priv = sess->ctxt;
1051         struct rte_mbuf *mbuf;
1052         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1053                         sess->iv.offset);
1054
1055         PMD_INIT_FUNC_TRACE();
1056
1057         if (sym_op->m_dst)
1058                 mbuf = sym_op->m_dst;
1059         else
1060                 mbuf = sym_op->m_src;
1061
1062         fle = (struct qbman_fle *)rte_malloc(NULL, FLE_SG_MEM_SIZE,
1063                         RTE_CACHE_LINE_SIZE);
1064         if (!fle) {
1065                 DPAA2_SEC_ERR("CIPHER SG: Memory alloc failed for SGE");
1066                 return -1;
1067         }
1068         memset(fle, 0, FLE_SG_MEM_SIZE);
1069         /* first FLE entry used to store mbuf and session ctxt */
1070         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
1071         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
1072
1073         op_fle = fle + 1;
1074         ip_fle = fle + 2;
1075         sge = fle + 3;
1076
1077         flc = &priv->flc_desc[0].flc;
1078
1079         DPAA2_SEC_DP_DEBUG(
1080                 "CIPHER SG: cipher_off: 0x%x/length %d, ivlen=%d"
1081                 " data_off: 0x%x\n",
1082                 sym_op->cipher.data.offset,
1083                 sym_op->cipher.data.length,
1084                 sess->iv.length,
1085                 sym_op->m_src->data_off);
1086
1087         /* o/p fle */
1088         DPAA2_SET_FLE_ADDR(op_fle, DPAA2_VADDR_TO_IOVA(sge));
1089         op_fle->length = sym_op->cipher.data.length;
1090         DPAA2_SET_FLE_SG_EXT(op_fle);
1091
1092         /* o/p 1st seg */
1093         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1094         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset + mbuf->data_off);
1095         sge->length = mbuf->data_len - sym_op->cipher.data.offset;
1096
1097         mbuf = mbuf->next;
1098         /* o/p segs */
1099         while (mbuf) {
1100                 sge++;
1101                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1102                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
1103                 sge->length = mbuf->data_len;
1104                 mbuf = mbuf->next;
1105         }
1106         DPAA2_SET_FLE_FIN(sge);
1107
1108         DPAA2_SEC_DP_DEBUG(
1109                 "CIPHER SG: 1 - flc = %p, fle = %p FLEaddr = %x-%x, len %d\n",
1110                 flc, fle, fle->addr_hi, fle->addr_lo,
1111                 fle->length);
1112
1113         /* i/p fle */
1114         mbuf = sym_op->m_src;
1115         sge++;
1116         DPAA2_SET_FLE_ADDR(ip_fle, DPAA2_VADDR_TO_IOVA(sge));
1117         ip_fle->length = sess->iv.length + sym_op->cipher.data.length;
1118         DPAA2_SET_FLE_SG_EXT(ip_fle);
1119
1120         /* i/p IV */
1121         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
1122         DPAA2_SET_FLE_OFFSET(sge, 0);
1123         sge->length = sess->iv.length;
1124
1125         sge++;
1126
1127         /* i/p 1st seg */
1128         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1129         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset +
1130                              mbuf->data_off);
1131         sge->length = mbuf->data_len - sym_op->cipher.data.offset;
1132
1133         mbuf = mbuf->next;
1134         /* i/p segs */
1135         while (mbuf) {
1136                 sge++;
1137                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(mbuf));
1138                 DPAA2_SET_FLE_OFFSET(sge, mbuf->data_off);
1139                 sge->length = mbuf->data_len;
1140                 mbuf = mbuf->next;
1141         }
1142         DPAA2_SET_FLE_FIN(sge);
1143         DPAA2_SET_FLE_FIN(ip_fle);
1144
1145         /* sg fd */
1146         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(op_fle));
1147         DPAA2_SET_FD_LEN(fd, ip_fle->length);
1148         DPAA2_SET_FD_COMPOUND_FMT(fd);
1149         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
1150
1151         DPAA2_SEC_DP_DEBUG(
1152                 "CIPHER SG: fdaddr =%" PRIx64 " bpid =%d meta =%d"
1153                 " off =%d, len =%d\n",
1154                 DPAA2_GET_FD_ADDR(fd),
1155                 DPAA2_GET_FD_BPID(fd),
1156                 rte_dpaa2_bpid_info[bpid].meta_data_size,
1157                 DPAA2_GET_FD_OFFSET(fd),
1158                 DPAA2_GET_FD_LEN(fd));
1159         return 0;
1160 }
1161
1162 static int
1163 build_cipher_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
1164                 struct qbman_fd *fd, uint16_t bpid)
1165 {
1166         struct rte_crypto_sym_op *sym_op = op->sym;
1167         struct qbman_fle *fle, *sge;
1168         int retval;
1169         struct sec_flow_context *flc;
1170         struct ctxt_priv *priv = sess->ctxt;
1171         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1172                         sess->iv.offset);
1173         struct rte_mbuf *dst;
1174
1175         PMD_INIT_FUNC_TRACE();
1176
1177         if (sym_op->m_dst)
1178                 dst = sym_op->m_dst;
1179         else
1180                 dst = sym_op->m_src;
1181
1182         retval = rte_mempool_get(priv->fle_pool, (void **)(&fle));
1183         if (retval) {
1184                 DPAA2_SEC_ERR("CIPHER: Memory alloc failed for SGE");
1185                 return -1;
1186         }
1187         memset(fle, 0, FLE_POOL_BUF_SIZE);
1188         /* TODO we are using the first FLE entry to store Mbuf.
1189          * Currently we donot know which FLE has the mbuf stored.
1190          * So while retreiving we can go back 1 FLE from the FD -ADDR
1191          * to get the MBUF Addr from the previous FLE.
1192          * We can have a better approach to use the inline Mbuf
1193          */
1194         DPAA2_SET_FLE_ADDR(fle, (size_t)op);
1195         DPAA2_FLE_SAVE_CTXT(fle, (ptrdiff_t)priv);
1196         fle = fle + 1;
1197         sge = fle + 2;
1198
1199         if (likely(bpid < MAX_BPID)) {
1200                 DPAA2_SET_FD_BPID(fd, bpid);
1201                 DPAA2_SET_FLE_BPID(fle, bpid);
1202                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
1203                 DPAA2_SET_FLE_BPID(sge, bpid);
1204                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
1205         } else {
1206                 DPAA2_SET_FD_IVP(fd);
1207                 DPAA2_SET_FLE_IVP(fle);
1208                 DPAA2_SET_FLE_IVP((fle + 1));
1209                 DPAA2_SET_FLE_IVP(sge);
1210                 DPAA2_SET_FLE_IVP((sge + 1));
1211         }
1212
1213         flc = &priv->flc_desc[0].flc;
1214         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
1215         DPAA2_SET_FD_LEN(fd, sym_op->cipher.data.length +
1216                          sess->iv.length);
1217         DPAA2_SET_FD_COMPOUND_FMT(fd);
1218         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
1219
1220         DPAA2_SEC_DP_DEBUG(
1221                 "CIPHER: cipher_off: 0x%x/length %d, ivlen=%d,"
1222                 " data_off: 0x%x\n",
1223                 sym_op->cipher.data.offset,
1224                 sym_op->cipher.data.length,
1225                 sess->iv.length,
1226                 sym_op->m_src->data_off);
1227
1228         DPAA2_SET_FLE_ADDR(fle, DPAA2_MBUF_VADDR_TO_IOVA(dst));
1229         DPAA2_SET_FLE_OFFSET(fle, sym_op->cipher.data.offset +
1230                              dst->data_off);
1231
1232         fle->length = sym_op->cipher.data.length + sess->iv.length;
1233
1234         DPAA2_SEC_DP_DEBUG(
1235                 "CIPHER: 1 - flc = %p, fle = %p FLEaddr = %x-%x, length %d\n",
1236                 flc, fle, fle->addr_hi, fle->addr_lo,
1237                 fle->length);
1238
1239         fle++;
1240
1241         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
1242         fle->length = sym_op->cipher.data.length + sess->iv.length;
1243
1244         DPAA2_SET_FLE_SG_EXT(fle);
1245
1246         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
1247         sge->length = sess->iv.length;
1248
1249         sge++;
1250         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
1251         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset +
1252                              sym_op->m_src->data_off);
1253
1254         sge->length = sym_op->cipher.data.length;
1255         DPAA2_SET_FLE_FIN(sge);
1256         DPAA2_SET_FLE_FIN(fle);
1257
1258         DPAA2_SEC_DP_DEBUG(
1259                 "CIPHER: fdaddr =%" PRIx64 " bpid =%d meta =%d"
1260                 " off =%d, len =%d\n",
1261                 DPAA2_GET_FD_ADDR(fd),
1262                 DPAA2_GET_FD_BPID(fd),
1263                 rte_dpaa2_bpid_info[bpid].meta_data_size,
1264                 DPAA2_GET_FD_OFFSET(fd),
1265                 DPAA2_GET_FD_LEN(fd));
1266
1267         return 0;
1268 }
1269
1270 static inline int
1271 build_sec_fd(struct rte_crypto_op *op,
1272              struct qbman_fd *fd, uint16_t bpid)
1273 {
1274         int ret = -1;
1275         dpaa2_sec_session *sess;
1276
1277         PMD_INIT_FUNC_TRACE();
1278
1279         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
1280                 sess = (dpaa2_sec_session *)get_sym_session_private_data(
1281                                 op->sym->session, cryptodev_driver_id);
1282         else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1283                 sess = (dpaa2_sec_session *)get_sec_session_private_data(
1284                                 op->sym->sec_session);
1285         else
1286                 return -1;
1287
1288         /* Any of the buffer is segmented*/
1289         if (!rte_pktmbuf_is_contiguous(op->sym->m_src) ||
1290                   ((op->sym->m_dst != NULL) &&
1291                    !rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1292                 switch (sess->ctxt_type) {
1293                 case DPAA2_SEC_CIPHER:
1294                         ret = build_cipher_sg_fd(sess, op, fd, bpid);
1295                         break;
1296                 case DPAA2_SEC_AUTH:
1297                         ret = build_auth_sg_fd(sess, op, fd, bpid);
1298                         break;
1299                 case DPAA2_SEC_AEAD:
1300                         ret = build_authenc_gcm_sg_fd(sess, op, fd, bpid);
1301                         break;
1302                 case DPAA2_SEC_CIPHER_HASH:
1303                         ret = build_authenc_sg_fd(sess, op, fd, bpid);
1304                         break;
1305                 case DPAA2_SEC_IPSEC:
1306                 case DPAA2_SEC_PDCP:
1307                         ret = build_proto_compound_sg_fd(sess, op, fd, bpid);
1308                         break;
1309                 case DPAA2_SEC_HASH_CIPHER:
1310                 default:
1311                         DPAA2_SEC_ERR("error: Unsupported session");
1312                 }
1313         } else {
1314                 switch (sess->ctxt_type) {
1315                 case DPAA2_SEC_CIPHER:
1316                         ret = build_cipher_fd(sess, op, fd, bpid);
1317                         break;
1318                 case DPAA2_SEC_AUTH:
1319                         ret = build_auth_fd(sess, op, fd, bpid);
1320                         break;
1321                 case DPAA2_SEC_AEAD:
1322                         ret = build_authenc_gcm_fd(sess, op, fd, bpid);
1323                         break;
1324                 case DPAA2_SEC_CIPHER_HASH:
1325                         ret = build_authenc_fd(sess, op, fd, bpid);
1326                         break;
1327                 case DPAA2_SEC_IPSEC:
1328                         ret = build_proto_fd(sess, op, fd, bpid);
1329                         break;
1330                 case DPAA2_SEC_PDCP:
1331                         ret = build_proto_compound_fd(sess, op, fd, bpid);
1332                         break;
1333                 case DPAA2_SEC_HASH_CIPHER:
1334                 default:
1335                         DPAA2_SEC_ERR("error: Unsupported session");
1336                 }
1337         }
1338         return ret;
1339 }
1340
1341 static uint16_t
1342 dpaa2_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1343                         uint16_t nb_ops)
1344 {
1345         /* Function to transmit the frames to given device and VQ*/
1346         uint32_t loop;
1347         int32_t ret;
1348         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
1349         uint32_t frames_to_send;
1350         struct qbman_eq_desc eqdesc;
1351         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1352         struct qbman_swp *swp;
1353         uint16_t num_tx = 0;
1354         uint32_t flags[MAX_TX_RING_SLOTS] = {0};
1355         /*todo - need to support multiple buffer pools */
1356         uint16_t bpid;
1357         struct rte_mempool *mb_pool;
1358
1359         if (unlikely(nb_ops == 0))
1360                 return 0;
1361
1362         if (ops[0]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1363                 DPAA2_SEC_ERR("sessionless crypto op not supported");
1364                 return 0;
1365         }
1366         /*Prepare enqueue descriptor*/
1367         qbman_eq_desc_clear(&eqdesc);
1368         qbman_eq_desc_set_no_orp(&eqdesc, DPAA2_EQ_RESP_ERR_FQ);
1369         qbman_eq_desc_set_response(&eqdesc, 0, 0);
1370         qbman_eq_desc_set_fq(&eqdesc, dpaa2_qp->tx_vq.fqid);
1371
1372         if (!DPAA2_PER_LCORE_DPIO) {
1373                 ret = dpaa2_affine_qbman_swp();
1374                 if (ret) {
1375                         DPAA2_SEC_ERR("Failure in affining portal");
1376                         return 0;
1377                 }
1378         }
1379         swp = DPAA2_PER_LCORE_PORTAL;
1380
1381         while (nb_ops) {
1382                 frames_to_send = (nb_ops > dpaa2_eqcr_size) ?
1383                         dpaa2_eqcr_size : nb_ops;
1384
1385                 for (loop = 0; loop < frames_to_send; loop++) {
1386                         if ((*ops)->sym->m_src->seqn) {
1387                          uint8_t dqrr_index = (*ops)->sym->m_src->seqn - 1;
1388
1389                          flags[loop] = QBMAN_ENQUEUE_FLAG_DCA | dqrr_index;
1390                          DPAA2_PER_LCORE_DQRR_SIZE--;
1391                          DPAA2_PER_LCORE_DQRR_HELD &= ~(1 << dqrr_index);
1392                          (*ops)->sym->m_src->seqn = DPAA2_INVALID_MBUF_SEQN;
1393                         }
1394
1395                         /*Clear the unused FD fields before sending*/
1396                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
1397                         mb_pool = (*ops)->sym->m_src->pool;
1398                         bpid = mempool_to_bpid(mb_pool);
1399                         ret = build_sec_fd(*ops, &fd_arr[loop], bpid);
1400                         if (ret) {
1401                                 DPAA2_SEC_ERR("error: Improper packet contents"
1402                                               " for crypto operation");
1403                                 goto skip_tx;
1404                         }
1405                         ops++;
1406                 }
1407                 loop = 0;
1408                 while (loop < frames_to_send) {
1409                         loop += qbman_swp_enqueue_multiple(swp, &eqdesc,
1410                                                         &fd_arr[loop],
1411                                                         &flags[loop],
1412                                                         frames_to_send - loop);
1413                 }
1414
1415                 num_tx += frames_to_send;
1416                 nb_ops -= frames_to_send;
1417         }
1418 skip_tx:
1419         dpaa2_qp->tx_vq.tx_pkts += num_tx;
1420         dpaa2_qp->tx_vq.err_pkts += nb_ops;
1421         return num_tx;
1422 }
1423
1424 static inline struct rte_crypto_op *
1425 sec_simple_fd_to_mbuf(const struct qbman_fd *fd)
1426 {
1427         struct rte_crypto_op *op;
1428         uint16_t len = DPAA2_GET_FD_LEN(fd);
1429         uint16_t diff = 0;
1430         dpaa2_sec_session *sess_priv;
1431
1432         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
1433                 DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd)),
1434                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
1435
1436         diff = len - mbuf->pkt_len;
1437         mbuf->pkt_len += diff;
1438         mbuf->data_len += diff;
1439         op = (struct rte_crypto_op *)(size_t)mbuf->buf_iova;
1440         mbuf->buf_iova = op->sym->aead.digest.phys_addr;
1441         op->sym->aead.digest.phys_addr = 0L;
1442
1443         sess_priv = (dpaa2_sec_session *)get_sec_session_private_data(
1444                                 op->sym->sec_session);
1445         if (sess_priv->dir == DIR_ENC)
1446                 mbuf->data_off += SEC_FLC_DHR_OUTBOUND;
1447         else
1448                 mbuf->data_off += SEC_FLC_DHR_INBOUND;
1449
1450         return op;
1451 }
1452
1453 static inline struct rte_crypto_op *
1454 sec_fd_to_mbuf(const struct qbman_fd *fd)
1455 {
1456         struct qbman_fle *fle;
1457         struct rte_crypto_op *op;
1458         struct ctxt_priv *priv;
1459         struct rte_mbuf *dst, *src;
1460
1461         if (DPAA2_FD_GET_FORMAT(fd) == qbman_fd_single)
1462                 return sec_simple_fd_to_mbuf(fd);
1463
1464         fle = (struct qbman_fle *)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
1465
1466         DPAA2_SEC_DP_DEBUG("FLE addr = %x - %x, offset = %x\n",
1467                            fle->addr_hi, fle->addr_lo, fle->fin_bpid_offset);
1468
1469         /* we are using the first FLE entry to store Mbuf.
1470          * Currently we donot know which FLE has the mbuf stored.
1471          * So while retreiving we can go back 1 FLE from the FD -ADDR
1472          * to get the MBUF Addr from the previous FLE.
1473          * We can have a better approach to use the inline Mbuf
1474          */
1475
1476         if (unlikely(DPAA2_GET_FD_IVP(fd))) {
1477                 /* TODO complete it. */
1478                 DPAA2_SEC_ERR("error: non inline buffer");
1479                 return NULL;
1480         }
1481         op = (struct rte_crypto_op *)DPAA2_GET_FLE_ADDR((fle - 1));
1482
1483         /* Prefeth op */
1484         src = op->sym->m_src;
1485         rte_prefetch0(src);
1486
1487         if (op->sym->m_dst) {
1488                 dst = op->sym->m_dst;
1489                 rte_prefetch0(dst);
1490         } else
1491                 dst = src;
1492
1493         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1494                 dpaa2_sec_session *sess = (dpaa2_sec_session *)
1495                         get_sec_session_private_data(op->sym->sec_session);
1496                 if (sess->ctxt_type == DPAA2_SEC_IPSEC ||
1497                                 sess->ctxt_type == DPAA2_SEC_PDCP) {
1498                         uint16_t len = DPAA2_GET_FD_LEN(fd);
1499                         dst->pkt_len = len;
1500                         while (dst->next != NULL) {
1501                                 len -= dst->data_len;
1502                                 dst = dst->next;
1503                         }
1504                         dst->data_len = len;
1505                 }
1506         }
1507
1508         DPAA2_SEC_DP_DEBUG("mbuf %p BMAN buf addr %p,"
1509                 " fdaddr =%" PRIx64 " bpid =%d meta =%d off =%d, len =%d\n",
1510                 (void *)dst,
1511                 dst->buf_addr,
1512                 DPAA2_GET_FD_ADDR(fd),
1513                 DPAA2_GET_FD_BPID(fd),
1514                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
1515                 DPAA2_GET_FD_OFFSET(fd),
1516                 DPAA2_GET_FD_LEN(fd));
1517
1518         /* free the fle memory */
1519         if (likely(rte_pktmbuf_is_contiguous(src))) {
1520                 priv = (struct ctxt_priv *)(size_t)DPAA2_GET_FLE_CTXT(fle - 1);
1521                 rte_mempool_put(priv->fle_pool, (void *)(fle-1));
1522         } else
1523                 rte_free((void *)(fle-1));
1524
1525         return op;
1526 }
1527
1528 static uint16_t
1529 dpaa2_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1530                         uint16_t nb_ops)
1531 {
1532         /* Function is responsible to receive frames for a given device and VQ*/
1533         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1534         struct qbman_result *dq_storage;
1535         uint32_t fqid = dpaa2_qp->rx_vq.fqid;
1536         int ret, num_rx = 0;
1537         uint8_t is_last = 0, status;
1538         struct qbman_swp *swp;
1539         const struct qbman_fd *fd;
1540         struct qbman_pull_desc pulldesc;
1541
1542         if (!DPAA2_PER_LCORE_DPIO) {
1543                 ret = dpaa2_affine_qbman_swp();
1544                 if (ret) {
1545                         DPAA2_SEC_ERR("Failure in affining portal");
1546                         return 0;
1547                 }
1548         }
1549         swp = DPAA2_PER_LCORE_PORTAL;
1550         dq_storage = dpaa2_qp->rx_vq.q_storage->dq_storage[0];
1551
1552         qbman_pull_desc_clear(&pulldesc);
1553         qbman_pull_desc_set_numframes(&pulldesc,
1554                                       (nb_ops > dpaa2_dqrr_size) ?
1555                                       dpaa2_dqrr_size : nb_ops);
1556         qbman_pull_desc_set_fq(&pulldesc, fqid);
1557         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
1558                                     (dma_addr_t)DPAA2_VADDR_TO_IOVA(dq_storage),
1559                                     1);
1560
1561         /*Issue a volatile dequeue command. */
1562         while (1) {
1563                 if (qbman_swp_pull(swp, &pulldesc)) {
1564                         DPAA2_SEC_WARN(
1565                                 "SEC VDQ command is not issued : QBMAN busy");
1566                         /* Portal was busy, try again */
1567                         continue;
1568                 }
1569                 break;
1570         };
1571
1572         /* Receive the packets till Last Dequeue entry is found with
1573          * respect to the above issues PULL command.
1574          */
1575         while (!is_last) {
1576                 /* Check if the previous issued command is completed.
1577                  * Also seems like the SWP is shared between the Ethernet Driver
1578                  * and the SEC driver.
1579                  */
1580                 while (!qbman_check_command_complete(dq_storage))
1581                         ;
1582
1583                 /* Loop until the dq_storage is updated with
1584                  * new token by QBMAN
1585                  */
1586                 while (!qbman_check_new_result(dq_storage))
1587                         ;
1588                 /* Check whether Last Pull command is Expired and
1589                  * setting Condition for Loop termination
1590                  */
1591                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
1592                         is_last = 1;
1593                         /* Check for valid frame. */
1594                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
1595                         if (unlikely(
1596                                 (status & QBMAN_DQ_STAT_VALIDFRAME) == 0)) {
1597                                 DPAA2_SEC_DP_DEBUG("No frame is delivered\n");
1598                                 continue;
1599                         }
1600                 }
1601
1602                 fd = qbman_result_DQ_fd(dq_storage);
1603                 ops[num_rx] = sec_fd_to_mbuf(fd);
1604
1605                 if (unlikely(fd->simple.frc)) {
1606                         /* TODO Parse SEC errors */
1607                         DPAA2_SEC_ERR("SEC returned Error - %x",
1608                                       fd->simple.frc);
1609                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_ERROR;
1610                 } else {
1611                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1612                 }
1613
1614                 num_rx++;
1615                 dq_storage++;
1616         } /* End of Packet Rx loop */
1617
1618         dpaa2_qp->rx_vq.rx_pkts += num_rx;
1619
1620         DPAA2_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1621         /*Return the total number of packets received to DPAA2 app*/
1622         return num_rx;
1623 }
1624
1625 /** Release queue pair */
1626 static int
1627 dpaa2_sec_queue_pair_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
1628 {
1629         struct dpaa2_sec_qp *qp =
1630                 (struct dpaa2_sec_qp *)dev->data->queue_pairs[queue_pair_id];
1631
1632         PMD_INIT_FUNC_TRACE();
1633
1634         if (qp->rx_vq.q_storage) {
1635                 dpaa2_free_dq_storage(qp->rx_vq.q_storage);
1636                 rte_free(qp->rx_vq.q_storage);
1637         }
1638         rte_free(qp);
1639
1640         dev->data->queue_pairs[queue_pair_id] = NULL;
1641
1642         return 0;
1643 }
1644
1645 /** Setup a queue pair */
1646 static int
1647 dpaa2_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1648                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1649                 __rte_unused int socket_id)
1650 {
1651         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1652         struct dpaa2_sec_qp *qp;
1653         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1654         struct dpseci_rx_queue_cfg cfg;
1655         int32_t retcode;
1656
1657         PMD_INIT_FUNC_TRACE();
1658
1659         /* If qp is already in use free ring memory and qp metadata. */
1660         if (dev->data->queue_pairs[qp_id] != NULL) {
1661                 DPAA2_SEC_INFO("QP already setup");
1662                 return 0;
1663         }
1664
1665         DPAA2_SEC_DEBUG("dev =%p, queue =%d, conf =%p",
1666                     dev, qp_id, qp_conf);
1667
1668         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
1669
1670         qp = rte_malloc(NULL, sizeof(struct dpaa2_sec_qp),
1671                         RTE_CACHE_LINE_SIZE);
1672         if (!qp) {
1673                 DPAA2_SEC_ERR("malloc failed for rx/tx queues");
1674                 return -1;
1675         }
1676
1677         qp->rx_vq.crypto_data = dev->data;
1678         qp->tx_vq.crypto_data = dev->data;
1679         qp->rx_vq.q_storage = rte_malloc("sec dq storage",
1680                 sizeof(struct queue_storage_info_t),
1681                 RTE_CACHE_LINE_SIZE);
1682         if (!qp->rx_vq.q_storage) {
1683                 DPAA2_SEC_ERR("malloc failed for q_storage");
1684                 return -1;
1685         }
1686         memset(qp->rx_vq.q_storage, 0, sizeof(struct queue_storage_info_t));
1687
1688         if (dpaa2_alloc_dq_storage(qp->rx_vq.q_storage)) {
1689                 DPAA2_SEC_ERR("Unable to allocate dequeue storage");
1690                 return -1;
1691         }
1692
1693         dev->data->queue_pairs[qp_id] = qp;
1694
1695         cfg.options = cfg.options | DPSECI_QUEUE_OPT_USER_CTX;
1696         cfg.user_ctx = (size_t)(&qp->rx_vq);
1697         retcode = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
1698                                       qp_id, &cfg);
1699         return retcode;
1700 }
1701
1702 /** Return the number of allocated queue pairs */
1703 static uint32_t
1704 dpaa2_sec_queue_pair_count(struct rte_cryptodev *dev)
1705 {
1706         PMD_INIT_FUNC_TRACE();
1707
1708         return dev->data->nb_queue_pairs;
1709 }
1710
1711 /** Returns the size of the aesni gcm session structure */
1712 static unsigned int
1713 dpaa2_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1714 {
1715         PMD_INIT_FUNC_TRACE();
1716
1717         return sizeof(dpaa2_sec_session);
1718 }
1719
1720 static int
1721 dpaa2_sec_cipher_init(struct rte_cryptodev *dev,
1722                       struct rte_crypto_sym_xform *xform,
1723                       dpaa2_sec_session *session)
1724 {
1725         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1726         struct alginfo cipherdata;
1727         int bufsize, i;
1728         struct ctxt_priv *priv;
1729         struct sec_flow_context *flc;
1730
1731         PMD_INIT_FUNC_TRACE();
1732
1733         /* For SEC CIPHER only one descriptor is required. */
1734         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1735                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1736                         RTE_CACHE_LINE_SIZE);
1737         if (priv == NULL) {
1738                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1739                 return -1;
1740         }
1741
1742         priv->fle_pool = dev_priv->fle_pool;
1743
1744         flc = &priv->flc_desc[0].flc;
1745
1746         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1747                         RTE_CACHE_LINE_SIZE);
1748         if (session->cipher_key.data == NULL) {
1749                 DPAA2_SEC_ERR("No Memory for cipher key");
1750                 rte_free(priv);
1751                 return -1;
1752         }
1753         session->cipher_key.length = xform->cipher.key.length;
1754
1755         memcpy(session->cipher_key.data, xform->cipher.key.data,
1756                xform->cipher.key.length);
1757         cipherdata.key = (size_t)session->cipher_key.data;
1758         cipherdata.keylen = session->cipher_key.length;
1759         cipherdata.key_enc_flags = 0;
1760         cipherdata.key_type = RTA_DATA_IMM;
1761
1762         /* Set IV parameters */
1763         session->iv.offset = xform->cipher.iv.offset;
1764         session->iv.length = xform->cipher.iv.length;
1765
1766         switch (xform->cipher.algo) {
1767         case RTE_CRYPTO_CIPHER_AES_CBC:
1768                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1769                 cipherdata.algmode = OP_ALG_AAI_CBC;
1770                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
1771                 break;
1772         case RTE_CRYPTO_CIPHER_3DES_CBC:
1773                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
1774                 cipherdata.algmode = OP_ALG_AAI_CBC;
1775                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
1776                 break;
1777         case RTE_CRYPTO_CIPHER_AES_CTR:
1778                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1779                 cipherdata.algmode = OP_ALG_AAI_CTR;
1780                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
1781                 break;
1782         case RTE_CRYPTO_CIPHER_3DES_CTR:
1783         case RTE_CRYPTO_CIPHER_AES_ECB:
1784         case RTE_CRYPTO_CIPHER_3DES_ECB:
1785         case RTE_CRYPTO_CIPHER_AES_XTS:
1786         case RTE_CRYPTO_CIPHER_AES_F8:
1787         case RTE_CRYPTO_CIPHER_ARC4:
1788         case RTE_CRYPTO_CIPHER_KASUMI_F8:
1789         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1790         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
1791         case RTE_CRYPTO_CIPHER_NULL:
1792                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
1793                         xform->cipher.algo);
1794                 goto error_out;
1795         default:
1796                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
1797                         xform->cipher.algo);
1798                 goto error_out;
1799         }
1800         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1801                                 DIR_ENC : DIR_DEC;
1802
1803         bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
1804                                         &cipherdata, NULL, session->iv.length,
1805                                         session->dir);
1806         if (bufsize < 0) {
1807                 DPAA2_SEC_ERR("Crypto: Descriptor build failed");
1808                 goto error_out;
1809         }
1810
1811         flc->word1_sdl = (uint8_t)bufsize;
1812         session->ctxt = priv;
1813
1814         for (i = 0; i < bufsize; i++)
1815                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x", i, priv->flc_desc[0].desc[i]);
1816
1817         return 0;
1818
1819 error_out:
1820         rte_free(session->cipher_key.data);
1821         rte_free(priv);
1822         return -1;
1823 }
1824
1825 static int
1826 dpaa2_sec_auth_init(struct rte_cryptodev *dev,
1827                     struct rte_crypto_sym_xform *xform,
1828                     dpaa2_sec_session *session)
1829 {
1830         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1831         struct alginfo authdata;
1832         int bufsize, i;
1833         struct ctxt_priv *priv;
1834         struct sec_flow_context *flc;
1835
1836         PMD_INIT_FUNC_TRACE();
1837
1838         /* For SEC AUTH three descriptors are required for various stages */
1839         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1840                         sizeof(struct ctxt_priv) + 3 *
1841                         sizeof(struct sec_flc_desc),
1842                         RTE_CACHE_LINE_SIZE);
1843         if (priv == NULL) {
1844                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1845                 return -1;
1846         }
1847
1848         priv->fle_pool = dev_priv->fle_pool;
1849         flc = &priv->flc_desc[DESC_INITFINAL].flc;
1850
1851         session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1852                         RTE_CACHE_LINE_SIZE);
1853         if (session->auth_key.data == NULL) {
1854                 DPAA2_SEC_ERR("Unable to allocate memory for auth key");
1855                 rte_free(priv);
1856                 return -1;
1857         }
1858         session->auth_key.length = xform->auth.key.length;
1859
1860         memcpy(session->auth_key.data, xform->auth.key.data,
1861                xform->auth.key.length);
1862         authdata.key = (size_t)session->auth_key.data;
1863         authdata.keylen = session->auth_key.length;
1864         authdata.key_enc_flags = 0;
1865         authdata.key_type = RTA_DATA_IMM;
1866
1867         session->digest_length = xform->auth.digest_length;
1868
1869         switch (xform->auth.algo) {
1870         case RTE_CRYPTO_AUTH_SHA1_HMAC:
1871                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
1872                 authdata.algmode = OP_ALG_AAI_HMAC;
1873                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
1874                 break;
1875         case RTE_CRYPTO_AUTH_MD5_HMAC:
1876                 authdata.algtype = OP_ALG_ALGSEL_MD5;
1877                 authdata.algmode = OP_ALG_AAI_HMAC;
1878                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
1879                 break;
1880         case RTE_CRYPTO_AUTH_SHA256_HMAC:
1881                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
1882                 authdata.algmode = OP_ALG_AAI_HMAC;
1883                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
1884                 break;
1885         case RTE_CRYPTO_AUTH_SHA384_HMAC:
1886                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
1887                 authdata.algmode = OP_ALG_AAI_HMAC;
1888                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
1889                 break;
1890         case RTE_CRYPTO_AUTH_SHA512_HMAC:
1891                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
1892                 authdata.algmode = OP_ALG_AAI_HMAC;
1893                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
1894                 break;
1895         case RTE_CRYPTO_AUTH_SHA224_HMAC:
1896                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
1897                 authdata.algmode = OP_ALG_AAI_HMAC;
1898                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
1899                 break;
1900         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
1901         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
1902         case RTE_CRYPTO_AUTH_NULL:
1903         case RTE_CRYPTO_AUTH_SHA1:
1904         case RTE_CRYPTO_AUTH_SHA256:
1905         case RTE_CRYPTO_AUTH_SHA512:
1906         case RTE_CRYPTO_AUTH_SHA224:
1907         case RTE_CRYPTO_AUTH_SHA384:
1908         case RTE_CRYPTO_AUTH_MD5:
1909         case RTE_CRYPTO_AUTH_AES_GMAC:
1910         case RTE_CRYPTO_AUTH_KASUMI_F9:
1911         case RTE_CRYPTO_AUTH_AES_CMAC:
1912         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
1913         case RTE_CRYPTO_AUTH_ZUC_EIA3:
1914                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %un",
1915                               xform->auth.algo);
1916                 goto error_out;
1917         default:
1918                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
1919                               xform->auth.algo);
1920                 goto error_out;
1921         }
1922         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1923                                 DIR_ENC : DIR_DEC;
1924
1925         bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
1926                                    1, 0, SHR_NEVER, &authdata, !session->dir,
1927                                    session->digest_length);
1928         if (bufsize < 0) {
1929                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
1930                 goto error_out;
1931         }
1932
1933         flc->word1_sdl = (uint8_t)bufsize;
1934         session->ctxt = priv;
1935         for (i = 0; i < bufsize; i++)
1936                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
1937                                 i, priv->flc_desc[DESC_INITFINAL].desc[i]);
1938
1939
1940         return 0;
1941
1942 error_out:
1943         rte_free(session->auth_key.data);
1944         rte_free(priv);
1945         return -1;
1946 }
1947
1948 static int
1949 dpaa2_sec_aead_init(struct rte_cryptodev *dev,
1950                     struct rte_crypto_sym_xform *xform,
1951                     dpaa2_sec_session *session)
1952 {
1953         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
1954         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1955         struct alginfo aeaddata;
1956         int bufsize, i;
1957         struct ctxt_priv *priv;
1958         struct sec_flow_context *flc;
1959         struct rte_crypto_aead_xform *aead_xform = &xform->aead;
1960         int err;
1961
1962         PMD_INIT_FUNC_TRACE();
1963
1964         /* Set IV parameters */
1965         session->iv.offset = aead_xform->iv.offset;
1966         session->iv.length = aead_xform->iv.length;
1967         session->ctxt_type = DPAA2_SEC_AEAD;
1968
1969         /* For SEC AEAD only one descriptor is required */
1970         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1971                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1972                         RTE_CACHE_LINE_SIZE);
1973         if (priv == NULL) {
1974                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1975                 return -1;
1976         }
1977
1978         priv->fle_pool = dev_priv->fle_pool;
1979         flc = &priv->flc_desc[0].flc;
1980
1981         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
1982                                                RTE_CACHE_LINE_SIZE);
1983         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
1984                 DPAA2_SEC_ERR("No Memory for aead key");
1985                 rte_free(priv);
1986                 return -1;
1987         }
1988         memcpy(session->aead_key.data, aead_xform->key.data,
1989                aead_xform->key.length);
1990
1991         session->digest_length = aead_xform->digest_length;
1992         session->aead_key.length = aead_xform->key.length;
1993         ctxt->auth_only_len = aead_xform->aad_length;
1994
1995         aeaddata.key = (size_t)session->aead_key.data;
1996         aeaddata.keylen = session->aead_key.length;
1997         aeaddata.key_enc_flags = 0;
1998         aeaddata.key_type = RTA_DATA_IMM;
1999
2000         switch (aead_xform->algo) {
2001         case RTE_CRYPTO_AEAD_AES_GCM:
2002                 aeaddata.algtype = OP_ALG_ALGSEL_AES;
2003                 aeaddata.algmode = OP_ALG_AAI_GCM;
2004                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2005                 break;
2006         case RTE_CRYPTO_AEAD_AES_CCM:
2007                 DPAA2_SEC_ERR("Crypto: Unsupported AEAD alg %u",
2008                               aead_xform->algo);
2009                 goto error_out;
2010         default:
2011                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2012                               aead_xform->algo);
2013                 goto error_out;
2014         }
2015         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2016                                 DIR_ENC : DIR_DEC;
2017
2018         priv->flc_desc[0].desc[0] = aeaddata.keylen;
2019         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2020                                MIN_JOB_DESC_SIZE,
2021                                (unsigned int *)priv->flc_desc[0].desc,
2022                                &priv->flc_desc[0].desc[1], 1);
2023
2024         if (err < 0) {
2025                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2026                 goto error_out;
2027         }
2028         if (priv->flc_desc[0].desc[1] & 1) {
2029                 aeaddata.key_type = RTA_DATA_IMM;
2030         } else {
2031                 aeaddata.key = DPAA2_VADDR_TO_IOVA(aeaddata.key);
2032                 aeaddata.key_type = RTA_DATA_PTR;
2033         }
2034         priv->flc_desc[0].desc[0] = 0;
2035         priv->flc_desc[0].desc[1] = 0;
2036
2037         if (session->dir == DIR_ENC)
2038                 bufsize = cnstr_shdsc_gcm_encap(
2039                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2040                                 &aeaddata, session->iv.length,
2041                                 session->digest_length);
2042         else
2043                 bufsize = cnstr_shdsc_gcm_decap(
2044                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2045                                 &aeaddata, session->iv.length,
2046                                 session->digest_length);
2047         if (bufsize < 0) {
2048                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2049                 goto error_out;
2050         }
2051
2052         flc->word1_sdl = (uint8_t)bufsize;
2053         session->ctxt = priv;
2054         for (i = 0; i < bufsize; i++)
2055                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x\n",
2056                             i, priv->flc_desc[0].desc[i]);
2057
2058         return 0;
2059
2060 error_out:
2061         rte_free(session->aead_key.data);
2062         rte_free(priv);
2063         return -1;
2064 }
2065
2066
2067 static int
2068 dpaa2_sec_aead_chain_init(struct rte_cryptodev *dev,
2069                     struct rte_crypto_sym_xform *xform,
2070                     dpaa2_sec_session *session)
2071 {
2072         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
2073         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2074         struct alginfo authdata, cipherdata;
2075         int bufsize, i;
2076         struct ctxt_priv *priv;
2077         struct sec_flow_context *flc;
2078         struct rte_crypto_cipher_xform *cipher_xform;
2079         struct rte_crypto_auth_xform *auth_xform;
2080         int err;
2081
2082         PMD_INIT_FUNC_TRACE();
2083
2084         if (session->ext_params.aead_ctxt.auth_cipher_text) {
2085                 cipher_xform = &xform->cipher;
2086                 auth_xform = &xform->next->auth;
2087                 session->ctxt_type =
2088                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2089                         DPAA2_SEC_CIPHER_HASH : DPAA2_SEC_HASH_CIPHER;
2090         } else {
2091                 cipher_xform = &xform->next->cipher;
2092                 auth_xform = &xform->auth;
2093                 session->ctxt_type =
2094                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2095                         DPAA2_SEC_HASH_CIPHER : DPAA2_SEC_CIPHER_HASH;
2096         }
2097
2098         /* Set IV parameters */
2099         session->iv.offset = cipher_xform->iv.offset;
2100         session->iv.length = cipher_xform->iv.length;
2101
2102         /* For SEC AEAD only one descriptor is required */
2103         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2104                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2105                         RTE_CACHE_LINE_SIZE);
2106         if (priv == NULL) {
2107                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2108                 return -1;
2109         }
2110
2111         priv->fle_pool = dev_priv->fle_pool;
2112         flc = &priv->flc_desc[0].flc;
2113
2114         session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2115                                                RTE_CACHE_LINE_SIZE);
2116         if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2117                 DPAA2_SEC_ERR("No Memory for cipher key");
2118                 rte_free(priv);
2119                 return -1;
2120         }
2121         session->cipher_key.length = cipher_xform->key.length;
2122         session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2123                                              RTE_CACHE_LINE_SIZE);
2124         if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2125                 DPAA2_SEC_ERR("No Memory for auth key");
2126                 rte_free(session->cipher_key.data);
2127                 rte_free(priv);
2128                 return -1;
2129         }
2130         session->auth_key.length = auth_xform->key.length;
2131         memcpy(session->cipher_key.data, cipher_xform->key.data,
2132                cipher_xform->key.length);
2133         memcpy(session->auth_key.data, auth_xform->key.data,
2134                auth_xform->key.length);
2135
2136         authdata.key = (size_t)session->auth_key.data;
2137         authdata.keylen = session->auth_key.length;
2138         authdata.key_enc_flags = 0;
2139         authdata.key_type = RTA_DATA_IMM;
2140
2141         session->digest_length = auth_xform->digest_length;
2142
2143         switch (auth_xform->algo) {
2144         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2145                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2146                 authdata.algmode = OP_ALG_AAI_HMAC;
2147                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2148                 break;
2149         case RTE_CRYPTO_AUTH_MD5_HMAC:
2150                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2151                 authdata.algmode = OP_ALG_AAI_HMAC;
2152                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2153                 break;
2154         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2155                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2156                 authdata.algmode = OP_ALG_AAI_HMAC;
2157                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2158                 break;
2159         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2160                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2161                 authdata.algmode = OP_ALG_AAI_HMAC;
2162                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2163                 break;
2164         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2165                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2166                 authdata.algmode = OP_ALG_AAI_HMAC;
2167                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2168                 break;
2169         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2170                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2171                 authdata.algmode = OP_ALG_AAI_HMAC;
2172                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2173                 break;
2174         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2175         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2176         case RTE_CRYPTO_AUTH_NULL:
2177         case RTE_CRYPTO_AUTH_SHA1:
2178         case RTE_CRYPTO_AUTH_SHA256:
2179         case RTE_CRYPTO_AUTH_SHA512:
2180         case RTE_CRYPTO_AUTH_SHA224:
2181         case RTE_CRYPTO_AUTH_SHA384:
2182         case RTE_CRYPTO_AUTH_MD5:
2183         case RTE_CRYPTO_AUTH_AES_GMAC:
2184         case RTE_CRYPTO_AUTH_KASUMI_F9:
2185         case RTE_CRYPTO_AUTH_AES_CMAC:
2186         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2187         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2188                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2189                               auth_xform->algo);
2190                 goto error_out;
2191         default:
2192                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2193                               auth_xform->algo);
2194                 goto error_out;
2195         }
2196         cipherdata.key = (size_t)session->cipher_key.data;
2197         cipherdata.keylen = session->cipher_key.length;
2198         cipherdata.key_enc_flags = 0;
2199         cipherdata.key_type = RTA_DATA_IMM;
2200
2201         switch (cipher_xform->algo) {
2202         case RTE_CRYPTO_CIPHER_AES_CBC:
2203                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2204                 cipherdata.algmode = OP_ALG_AAI_CBC;
2205                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
2206                 break;
2207         case RTE_CRYPTO_CIPHER_3DES_CBC:
2208                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
2209                 cipherdata.algmode = OP_ALG_AAI_CBC;
2210                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
2211                 break;
2212         case RTE_CRYPTO_CIPHER_AES_CTR:
2213                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2214                 cipherdata.algmode = OP_ALG_AAI_CTR;
2215                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
2216                 break;
2217         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2218         case RTE_CRYPTO_CIPHER_NULL:
2219         case RTE_CRYPTO_CIPHER_3DES_ECB:
2220         case RTE_CRYPTO_CIPHER_AES_ECB:
2221         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2222                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2223                               cipher_xform->algo);
2224                 goto error_out;
2225         default:
2226                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2227                               cipher_xform->algo);
2228                 goto error_out;
2229         }
2230         session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2231                                 DIR_ENC : DIR_DEC;
2232
2233         priv->flc_desc[0].desc[0] = cipherdata.keylen;
2234         priv->flc_desc[0].desc[1] = authdata.keylen;
2235         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2236                                MIN_JOB_DESC_SIZE,
2237                                (unsigned int *)priv->flc_desc[0].desc,
2238                                &priv->flc_desc[0].desc[2], 2);
2239
2240         if (err < 0) {
2241                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2242                 goto error_out;
2243         }
2244         if (priv->flc_desc[0].desc[2] & 1) {
2245                 cipherdata.key_type = RTA_DATA_IMM;
2246         } else {
2247                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
2248                 cipherdata.key_type = RTA_DATA_PTR;
2249         }
2250         if (priv->flc_desc[0].desc[2] & (1 << 1)) {
2251                 authdata.key_type = RTA_DATA_IMM;
2252         } else {
2253                 authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
2254                 authdata.key_type = RTA_DATA_PTR;
2255         }
2256         priv->flc_desc[0].desc[0] = 0;
2257         priv->flc_desc[0].desc[1] = 0;
2258         priv->flc_desc[0].desc[2] = 0;
2259
2260         if (session->ctxt_type == DPAA2_SEC_CIPHER_HASH) {
2261                 bufsize = cnstr_shdsc_authenc(priv->flc_desc[0].desc, 1,
2262                                               0, SHR_SERIAL,
2263                                               &cipherdata, &authdata,
2264                                               session->iv.length,
2265                                               ctxt->auth_only_len,
2266                                               session->digest_length,
2267                                               session->dir);
2268                 if (bufsize < 0) {
2269                         DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2270                         goto error_out;
2271                 }
2272         } else {
2273                 DPAA2_SEC_ERR("Hash before cipher not supported");
2274                 goto error_out;
2275         }
2276
2277         flc->word1_sdl = (uint8_t)bufsize;
2278         session->ctxt = priv;
2279         for (i = 0; i < bufsize; i++)
2280                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2281                             i, priv->flc_desc[0].desc[i]);
2282
2283         return 0;
2284
2285 error_out:
2286         rte_free(session->cipher_key.data);
2287         rte_free(session->auth_key.data);
2288         rte_free(priv);
2289         return -1;
2290 }
2291
2292 static int
2293 dpaa2_sec_set_session_parameters(struct rte_cryptodev *dev,
2294                             struct rte_crypto_sym_xform *xform, void *sess)
2295 {
2296         dpaa2_sec_session *session = sess;
2297         int ret;
2298
2299         PMD_INIT_FUNC_TRACE();
2300
2301         if (unlikely(sess == NULL)) {
2302                 DPAA2_SEC_ERR("Invalid session struct");
2303                 return -1;
2304         }
2305
2306         memset(session, 0, sizeof(dpaa2_sec_session));
2307         /* Default IV length = 0 */
2308         session->iv.length = 0;
2309
2310         /* Cipher Only */
2311         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2312                 session->ctxt_type = DPAA2_SEC_CIPHER;
2313                 ret = dpaa2_sec_cipher_init(dev, xform, session);
2314
2315         /* Authentication Only */
2316         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2317                    xform->next == NULL) {
2318                 session->ctxt_type = DPAA2_SEC_AUTH;
2319                 ret = dpaa2_sec_auth_init(dev, xform, session);
2320
2321         /* Cipher then Authenticate */
2322         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2323                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2324                 session->ext_params.aead_ctxt.auth_cipher_text = true;
2325                 ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2326
2327         /* Authenticate then Cipher */
2328         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2329                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2330                 session->ext_params.aead_ctxt.auth_cipher_text = false;
2331                 ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2332
2333         /* AEAD operation for AES-GCM kind of Algorithms */
2334         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2335                    xform->next == NULL) {
2336                 ret = dpaa2_sec_aead_init(dev, xform, session);
2337
2338         } else {
2339                 DPAA2_SEC_ERR("Invalid crypto type");
2340                 return -EINVAL;
2341         }
2342
2343         return ret;
2344 }
2345
2346 static int
2347 dpaa2_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2348                         dpaa2_sec_session *session,
2349                         struct alginfo *aeaddata)
2350 {
2351         PMD_INIT_FUNC_TRACE();
2352
2353         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2354                                                RTE_CACHE_LINE_SIZE);
2355         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2356                 DPAA2_SEC_ERR("No Memory for aead key");
2357                 return -1;
2358         }
2359         memcpy(session->aead_key.data, aead_xform->key.data,
2360                aead_xform->key.length);
2361
2362         session->digest_length = aead_xform->digest_length;
2363         session->aead_key.length = aead_xform->key.length;
2364
2365         aeaddata->key = (size_t)session->aead_key.data;
2366         aeaddata->keylen = session->aead_key.length;
2367         aeaddata->key_enc_flags = 0;
2368         aeaddata->key_type = RTA_DATA_IMM;
2369
2370         switch (aead_xform->algo) {
2371         case RTE_CRYPTO_AEAD_AES_GCM:
2372                 aeaddata->algtype = OP_ALG_ALGSEL_AES;
2373                 aeaddata->algmode = OP_ALG_AAI_GCM;
2374                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2375                 break;
2376         case RTE_CRYPTO_AEAD_AES_CCM:
2377                 aeaddata->algtype = OP_ALG_ALGSEL_AES;
2378                 aeaddata->algmode = OP_ALG_AAI_CCM;
2379                 session->aead_alg = RTE_CRYPTO_AEAD_AES_CCM;
2380                 break;
2381         default:
2382                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2383                               aead_xform->algo);
2384                 return -1;
2385         }
2386         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2387                                 DIR_ENC : DIR_DEC;
2388
2389         return 0;
2390 }
2391
2392 static int
2393 dpaa2_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2394         struct rte_crypto_auth_xform *auth_xform,
2395         dpaa2_sec_session *session,
2396         struct alginfo *cipherdata,
2397         struct alginfo *authdata)
2398 {
2399         if (cipher_xform) {
2400                 session->cipher_key.data = rte_zmalloc(NULL,
2401                                                        cipher_xform->key.length,
2402                                                        RTE_CACHE_LINE_SIZE);
2403                 if (session->cipher_key.data == NULL &&
2404                                 cipher_xform->key.length > 0) {
2405                         DPAA2_SEC_ERR("No Memory for cipher key");
2406                         return -ENOMEM;
2407                 }
2408
2409                 session->cipher_key.length = cipher_xform->key.length;
2410                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2411                                 cipher_xform->key.length);
2412                 session->cipher_alg = cipher_xform->algo;
2413         } else {
2414                 session->cipher_key.data = NULL;
2415                 session->cipher_key.length = 0;
2416                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2417         }
2418
2419         if (auth_xform) {
2420                 session->auth_key.data = rte_zmalloc(NULL,
2421                                                 auth_xform->key.length,
2422                                                 RTE_CACHE_LINE_SIZE);
2423                 if (session->auth_key.data == NULL &&
2424                                 auth_xform->key.length > 0) {
2425                         DPAA2_SEC_ERR("No Memory for auth key");
2426                         return -ENOMEM;
2427                 }
2428                 session->auth_key.length = auth_xform->key.length;
2429                 memcpy(session->auth_key.data, auth_xform->key.data,
2430                                 auth_xform->key.length);
2431                 session->auth_alg = auth_xform->algo;
2432         } else {
2433                 session->auth_key.data = NULL;
2434                 session->auth_key.length = 0;
2435                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2436         }
2437
2438         authdata->key = (size_t)session->auth_key.data;
2439         authdata->keylen = session->auth_key.length;
2440         authdata->key_enc_flags = 0;
2441         authdata->key_type = RTA_DATA_IMM;
2442         switch (session->auth_alg) {
2443         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2444                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA1_96;
2445                 authdata->algmode = OP_ALG_AAI_HMAC;
2446                 break;
2447         case RTE_CRYPTO_AUTH_MD5_HMAC:
2448                 authdata->algtype = OP_PCL_IPSEC_HMAC_MD5_96;
2449                 authdata->algmode = OP_ALG_AAI_HMAC;
2450                 break;
2451         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2452                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2453                 authdata->algmode = OP_ALG_AAI_HMAC;
2454                 break;
2455         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2456                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2457                 authdata->algmode = OP_ALG_AAI_HMAC;
2458                 break;
2459         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2460                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2461                 authdata->algmode = OP_ALG_AAI_HMAC;
2462                 break;
2463         case RTE_CRYPTO_AUTH_AES_CMAC:
2464                 authdata->algtype = OP_PCL_IPSEC_AES_CMAC_96;
2465                 break;
2466         case RTE_CRYPTO_AUTH_NULL:
2467                 authdata->algtype = OP_PCL_IPSEC_HMAC_NULL;
2468                 break;
2469         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2470         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2471         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2472         case RTE_CRYPTO_AUTH_SHA1:
2473         case RTE_CRYPTO_AUTH_SHA256:
2474         case RTE_CRYPTO_AUTH_SHA512:
2475         case RTE_CRYPTO_AUTH_SHA224:
2476         case RTE_CRYPTO_AUTH_SHA384:
2477         case RTE_CRYPTO_AUTH_MD5:
2478         case RTE_CRYPTO_AUTH_AES_GMAC:
2479         case RTE_CRYPTO_AUTH_KASUMI_F9:
2480         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2481         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2482                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2483                               session->auth_alg);
2484                 return -1;
2485         default:
2486                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2487                               session->auth_alg);
2488                 return -1;
2489         }
2490         cipherdata->key = (size_t)session->cipher_key.data;
2491         cipherdata->keylen = session->cipher_key.length;
2492         cipherdata->key_enc_flags = 0;
2493         cipherdata->key_type = RTA_DATA_IMM;
2494
2495         switch (session->cipher_alg) {
2496         case RTE_CRYPTO_CIPHER_AES_CBC:
2497                 cipherdata->algtype = OP_PCL_IPSEC_AES_CBC;
2498                 cipherdata->algmode = OP_ALG_AAI_CBC;
2499                 break;
2500         case RTE_CRYPTO_CIPHER_3DES_CBC:
2501                 cipherdata->algtype = OP_PCL_IPSEC_3DES;
2502                 cipherdata->algmode = OP_ALG_AAI_CBC;
2503                 break;
2504         case RTE_CRYPTO_CIPHER_AES_CTR:
2505                 cipherdata->algtype = OP_PCL_IPSEC_AES_CTR;
2506                 cipherdata->algmode = OP_ALG_AAI_CTR;
2507                 break;
2508         case RTE_CRYPTO_CIPHER_NULL:
2509                 cipherdata->algtype = OP_PCL_IPSEC_NULL;
2510                 break;
2511         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2512         case RTE_CRYPTO_CIPHER_3DES_ECB:
2513         case RTE_CRYPTO_CIPHER_AES_ECB:
2514         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2515                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2516                               session->cipher_alg);
2517                 return -1;
2518         default:
2519                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2520                               session->cipher_alg);
2521                 return -1;
2522         }
2523
2524         return 0;
2525 }
2526
2527 #ifdef RTE_LIBRTE_SECURITY_TEST
2528 static uint8_t aes_cbc_iv[] = {
2529         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2530         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
2531 #endif
2532
2533 static int
2534 dpaa2_sec_set_ipsec_session(struct rte_cryptodev *dev,
2535                             struct rte_security_session_conf *conf,
2536                             void *sess)
2537 {
2538         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2539         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2540         struct rte_crypto_auth_xform *auth_xform = NULL;
2541         struct rte_crypto_aead_xform *aead_xform = NULL;
2542         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
2543         struct ctxt_priv *priv;
2544         struct ipsec_encap_pdb encap_pdb;
2545         struct ipsec_decap_pdb decap_pdb;
2546         struct alginfo authdata, cipherdata;
2547         int bufsize;
2548         struct sec_flow_context *flc;
2549         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2550         int ret = -1;
2551
2552         PMD_INIT_FUNC_TRACE();
2553
2554         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2555                                 sizeof(struct ctxt_priv) +
2556                                 sizeof(struct sec_flc_desc),
2557                                 RTE_CACHE_LINE_SIZE);
2558
2559         if (priv == NULL) {
2560                 DPAA2_SEC_ERR("No memory for priv CTXT");
2561                 return -ENOMEM;
2562         }
2563
2564         priv->fle_pool = dev_priv->fle_pool;
2565         flc = &priv->flc_desc[0].flc;
2566
2567         memset(session, 0, sizeof(dpaa2_sec_session));
2568
2569         if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2570                 cipher_xform = &conf->crypto_xform->cipher;
2571                 if (conf->crypto_xform->next)
2572                         auth_xform = &conf->crypto_xform->next->auth;
2573                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2574                                         session, &cipherdata, &authdata);
2575         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2576                 auth_xform = &conf->crypto_xform->auth;
2577                 if (conf->crypto_xform->next)
2578                         cipher_xform = &conf->crypto_xform->next->cipher;
2579                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2580                                         session, &cipherdata, &authdata);
2581         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
2582                 aead_xform = &conf->crypto_xform->aead;
2583                 ret = dpaa2_sec_ipsec_aead_init(aead_xform,
2584                                         session, &cipherdata);
2585         } else {
2586                 DPAA2_SEC_ERR("XFORM not specified");
2587                 ret = -EINVAL;
2588                 goto out;
2589         }
2590         if (ret) {
2591                 DPAA2_SEC_ERR("Failed to process xform");
2592                 goto out;
2593         }
2594
2595         session->ctxt_type = DPAA2_SEC_IPSEC;
2596         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2597                 uint8_t *hdr = NULL;
2598                 struct ip ip4_hdr;
2599                 struct rte_ipv6_hdr ip6_hdr;
2600
2601                 flc->dhr = SEC_FLC_DHR_OUTBOUND;
2602                 /* For Sec Proto only one descriptor is required. */
2603                 memset(&encap_pdb, 0, sizeof(struct ipsec_encap_pdb));
2604                 encap_pdb.options = (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2605                         PDBOPTS_ESP_OIHI_PDB_INL |
2606                         PDBOPTS_ESP_IVSRC |
2607                         PDBHMO_ESP_ENCAP_DTTL |
2608                         PDBHMO_ESP_SNR;
2609                 if (ipsec_xform->options.esn)
2610                         encap_pdb.options |= PDBOPTS_ESP_ESN;
2611                 encap_pdb.spi = ipsec_xform->spi;
2612                 session->dir = DIR_ENC;
2613                 if (ipsec_xform->tunnel.type ==
2614                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2615                         encap_pdb.ip_hdr_len = sizeof(struct ip);
2616                         ip4_hdr.ip_v = IPVERSION;
2617                         ip4_hdr.ip_hl = 5;
2618                         ip4_hdr.ip_len = rte_cpu_to_be_16(sizeof(ip4_hdr));
2619                         ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2620                         ip4_hdr.ip_id = 0;
2621                         ip4_hdr.ip_off = 0;
2622                         ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2623                         ip4_hdr.ip_p = IPPROTO_ESP;
2624                         ip4_hdr.ip_sum = 0;
2625                         ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
2626                         ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
2627                         ip4_hdr.ip_sum = calc_chksum((uint16_t *)(void *)
2628                                         &ip4_hdr, sizeof(struct ip));
2629                         hdr = (uint8_t *)&ip4_hdr;
2630                 } else if (ipsec_xform->tunnel.type ==
2631                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2632                         ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2633                                 DPAA2_IPv6_DEFAULT_VTC_FLOW |
2634                                 ((ipsec_xform->tunnel.ipv6.dscp <<
2635                                         RTE_IPV6_HDR_TC_SHIFT) &
2636                                         RTE_IPV6_HDR_TC_MASK) |
2637                                 ((ipsec_xform->tunnel.ipv6.flabel <<
2638                                         RTE_IPV6_HDR_FL_SHIFT) &
2639                                         RTE_IPV6_HDR_FL_MASK));
2640                         /* Payload length will be updated by HW */
2641                         ip6_hdr.payload_len = 0;
2642                         ip6_hdr.hop_limits =
2643                                         ipsec_xform->tunnel.ipv6.hlimit;
2644                         ip6_hdr.proto = (ipsec_xform->proto ==
2645                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2646                                         IPPROTO_ESP : IPPROTO_AH;
2647                         memcpy(&ip6_hdr.src_addr,
2648                                 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2649                         memcpy(&ip6_hdr.dst_addr,
2650                                 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2651                         encap_pdb.ip_hdr_len = sizeof(struct rte_ipv6_hdr);
2652                         hdr = (uint8_t *)&ip6_hdr;
2653                 }
2654
2655                 bufsize = cnstr_shdsc_ipsec_new_encap(priv->flc_desc[0].desc,
2656                                 1, 0, SHR_SERIAL, &encap_pdb,
2657                                 hdr, &cipherdata, &authdata);
2658         } else if (ipsec_xform->direction ==
2659                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2660                 flc->dhr = SEC_FLC_DHR_INBOUND;
2661                 memset(&decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2662                 decap_pdb.options = sizeof(struct ip) << 16;
2663                 if (ipsec_xform->options.esn)
2664                         decap_pdb.options |= PDBOPTS_ESP_ESN;
2665                 decap_pdb.options = (ipsec_xform->tunnel.type ==
2666                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) ?
2667                                 sizeof(struct ip) << 16 :
2668                                 sizeof(struct rte_ipv6_hdr) << 16;
2669                 session->dir = DIR_DEC;
2670                 bufsize = cnstr_shdsc_ipsec_new_decap(priv->flc_desc[0].desc,
2671                                 1, 0, SHR_SERIAL,
2672                                 &decap_pdb, &cipherdata, &authdata);
2673         } else
2674                 goto out;
2675
2676         if (bufsize < 0) {
2677                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2678                 goto out;
2679         }
2680
2681         flc->word1_sdl = (uint8_t)bufsize;
2682
2683         /* Enable the stashing control bit */
2684         DPAA2_SET_FLC_RSC(flc);
2685         flc->word2_rflc_31_0 = lower_32_bits(
2686                         (size_t)&(((struct dpaa2_sec_qp *)
2687                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
2688         flc->word3_rflc_63_32 = upper_32_bits(
2689                         (size_t)&(((struct dpaa2_sec_qp *)
2690                         dev->data->queue_pairs[0])->rx_vq));
2691
2692         /* Set EWS bit i.e. enable write-safe */
2693         DPAA2_SET_FLC_EWS(flc);
2694         /* Set BS = 1 i.e reuse input buffers as output buffers */
2695         DPAA2_SET_FLC_REUSE_BS(flc);
2696         /* Set FF = 10; reuse input buffers if they provide sufficient space */
2697         DPAA2_SET_FLC_REUSE_FF(flc);
2698
2699         session->ctxt = priv;
2700
2701         return 0;
2702 out:
2703         rte_free(session->auth_key.data);
2704         rte_free(session->cipher_key.data);
2705         rte_free(priv);
2706         return ret;
2707 }
2708
2709 static int
2710 dpaa2_sec_set_pdcp_session(struct rte_cryptodev *dev,
2711                            struct rte_security_session_conf *conf,
2712                            void *sess)
2713 {
2714         struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2715         struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2716         struct rte_crypto_auth_xform *auth_xform = NULL;
2717         struct rte_crypto_cipher_xform *cipher_xform;
2718         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
2719         struct ctxt_priv *priv;
2720         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2721         struct alginfo authdata, cipherdata;
2722         struct alginfo *p_authdata = NULL;
2723         int bufsize = -1;
2724         struct sec_flow_context *flc;
2725 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
2726         int swap = true;
2727 #else
2728         int swap = false;
2729 #endif
2730
2731         PMD_INIT_FUNC_TRACE();
2732
2733         memset(session, 0, sizeof(dpaa2_sec_session));
2734
2735         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2736                                 sizeof(struct ctxt_priv) +
2737                                 sizeof(struct sec_flc_desc),
2738                                 RTE_CACHE_LINE_SIZE);
2739
2740         if (priv == NULL) {
2741                 DPAA2_SEC_ERR("No memory for priv CTXT");
2742                 return -ENOMEM;
2743         }
2744
2745         priv->fle_pool = dev_priv->fle_pool;
2746         flc = &priv->flc_desc[0].flc;
2747
2748         /* find xfrm types */
2749         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2750                 cipher_xform = &xform->cipher;
2751         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2752                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2753                 session->ext_params.aead_ctxt.auth_cipher_text = true;
2754                 cipher_xform = &xform->cipher;
2755                 auth_xform = &xform->next->auth;
2756         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2757                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2758                 session->ext_params.aead_ctxt.auth_cipher_text = false;
2759                 cipher_xform = &xform->next->cipher;
2760                 auth_xform = &xform->auth;
2761         } else {
2762                 DPAA2_SEC_ERR("Invalid crypto type");
2763                 return -EINVAL;
2764         }
2765
2766         session->ctxt_type = DPAA2_SEC_PDCP;
2767         if (cipher_xform) {
2768                 session->cipher_key.data = rte_zmalloc(NULL,
2769                                                cipher_xform->key.length,
2770                                                RTE_CACHE_LINE_SIZE);
2771                 if (session->cipher_key.data == NULL &&
2772                                 cipher_xform->key.length > 0) {
2773                         DPAA2_SEC_ERR("No Memory for cipher key");
2774                         rte_free(priv);
2775                         return -ENOMEM;
2776                 }
2777                 session->cipher_key.length = cipher_xform->key.length;
2778                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2779                         cipher_xform->key.length);
2780                 session->dir =
2781                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2782                                         DIR_ENC : DIR_DEC;
2783                 session->cipher_alg = cipher_xform->algo;
2784         } else {
2785                 session->cipher_key.data = NULL;
2786                 session->cipher_key.length = 0;
2787                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2788                 session->dir = DIR_ENC;
2789         }
2790
2791         session->pdcp.domain = pdcp_xform->domain;
2792         session->pdcp.bearer = pdcp_xform->bearer;
2793         session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2794         session->pdcp.sn_size = pdcp_xform->sn_size;
2795         session->pdcp.hfn = pdcp_xform->hfn;
2796         session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2797         session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
2798         /* hfv ovd offset location is stored in iv.offset value*/
2799         session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2800
2801         cipherdata.key = (size_t)session->cipher_key.data;
2802         cipherdata.keylen = session->cipher_key.length;
2803         cipherdata.key_enc_flags = 0;
2804         cipherdata.key_type = RTA_DATA_IMM;
2805
2806         switch (session->cipher_alg) {
2807         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2808                 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
2809                 break;
2810         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2811                 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
2812                 break;
2813         case RTE_CRYPTO_CIPHER_AES_CTR:
2814                 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
2815                 break;
2816         case RTE_CRYPTO_CIPHER_NULL:
2817                 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
2818                 break;
2819         default:
2820                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2821                               session->cipher_alg);
2822                 goto out;
2823         }
2824
2825         if (auth_xform) {
2826                 session->auth_key.data = rte_zmalloc(NULL,
2827                                                      auth_xform->key.length,
2828                                                      RTE_CACHE_LINE_SIZE);
2829                 if (!session->auth_key.data &&
2830                     auth_xform->key.length > 0) {
2831                         DPAA2_SEC_ERR("No Memory for auth key");
2832                         rte_free(session->cipher_key.data);
2833                         rte_free(priv);
2834                         return -ENOMEM;
2835                 }
2836                 session->auth_key.length = auth_xform->key.length;
2837                 memcpy(session->auth_key.data, auth_xform->key.data,
2838                        auth_xform->key.length);
2839                 session->auth_alg = auth_xform->algo;
2840         } else {
2841                 session->auth_key.data = NULL;
2842                 session->auth_key.length = 0;
2843                 session->auth_alg = 0;
2844         }
2845         authdata.key = (size_t)session->auth_key.data;
2846         authdata.keylen = session->auth_key.length;
2847         authdata.key_enc_flags = 0;
2848         authdata.key_type = RTA_DATA_IMM;
2849
2850         if (session->auth_alg) {
2851                 switch (session->auth_alg) {
2852                 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2853                         authdata.algtype = PDCP_AUTH_TYPE_SNOW;
2854                         break;
2855                 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2856                         authdata.algtype = PDCP_AUTH_TYPE_ZUC;
2857                         break;
2858                 case RTE_CRYPTO_AUTH_AES_CMAC:
2859                         authdata.algtype = PDCP_AUTH_TYPE_AES;
2860                         break;
2861                 case RTE_CRYPTO_AUTH_NULL:
2862                         authdata.algtype = PDCP_AUTH_TYPE_NULL;
2863                         break;
2864                 default:
2865                         DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2866                                       session->auth_alg);
2867                         goto out;
2868                 }
2869
2870                 p_authdata = &authdata;
2871         } else if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2872                 DPAA2_SEC_ERR("Crypto: Integrity must for c-plane");
2873                 goto out;
2874         }
2875
2876         if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2877                 if (session->dir == DIR_ENC)
2878                         bufsize = cnstr_shdsc_pdcp_c_plane_encap(
2879                                         priv->flc_desc[0].desc, 1, swap,
2880                                         pdcp_xform->hfn,
2881                                         session->pdcp.sn_size,
2882                                         pdcp_xform->bearer,
2883                                         pdcp_xform->pkt_dir,
2884                                         pdcp_xform->hfn_threshold,
2885                                         &cipherdata, &authdata,
2886                                         0);
2887                 else if (session->dir == DIR_DEC)
2888                         bufsize = cnstr_shdsc_pdcp_c_plane_decap(
2889                                         priv->flc_desc[0].desc, 1, swap,
2890                                         pdcp_xform->hfn,
2891                                         session->pdcp.sn_size,
2892                                         pdcp_xform->bearer,
2893                                         pdcp_xform->pkt_dir,
2894                                         pdcp_xform->hfn_threshold,
2895                                         &cipherdata, &authdata,
2896                                         0);
2897         } else {
2898                 if (session->dir == DIR_ENC)
2899                         bufsize = cnstr_shdsc_pdcp_u_plane_encap(
2900                                         priv->flc_desc[0].desc, 1, swap,
2901                                         session->pdcp.sn_size,
2902                                         pdcp_xform->hfn,
2903                                         pdcp_xform->bearer,
2904                                         pdcp_xform->pkt_dir,
2905                                         pdcp_xform->hfn_threshold,
2906                                         &cipherdata, p_authdata, 0);
2907                 else if (session->dir == DIR_DEC)
2908                         bufsize = cnstr_shdsc_pdcp_u_plane_decap(
2909                                         priv->flc_desc[0].desc, 1, swap,
2910                                         session->pdcp.sn_size,
2911                                         pdcp_xform->hfn,
2912                                         pdcp_xform->bearer,
2913                                         pdcp_xform->pkt_dir,
2914                                         pdcp_xform->hfn_threshold,
2915                                         &cipherdata, p_authdata, 0);
2916         }
2917
2918         if (bufsize < 0) {
2919                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2920                 goto out;
2921         }
2922
2923         /* Enable the stashing control bit */
2924         DPAA2_SET_FLC_RSC(flc);
2925         flc->word2_rflc_31_0 = lower_32_bits(
2926                         (size_t)&(((struct dpaa2_sec_qp *)
2927                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
2928         flc->word3_rflc_63_32 = upper_32_bits(
2929                         (size_t)&(((struct dpaa2_sec_qp *)
2930                         dev->data->queue_pairs[0])->rx_vq));
2931
2932         flc->word1_sdl = (uint8_t)bufsize;
2933
2934         /* Set EWS bit i.e. enable write-safe */
2935         DPAA2_SET_FLC_EWS(flc);
2936         /* Set BS = 1 i.e reuse input buffers as output buffers */
2937         DPAA2_SET_FLC_REUSE_BS(flc);
2938         /* Set FF = 10; reuse input buffers if they provide sufficient space */
2939         DPAA2_SET_FLC_REUSE_FF(flc);
2940
2941         session->ctxt = priv;
2942
2943         return 0;
2944 out:
2945         rte_free(session->auth_key.data);
2946         rte_free(session->cipher_key.data);
2947         rte_free(priv);
2948         return -1;
2949 }
2950
2951 static int
2952 dpaa2_sec_security_session_create(void *dev,
2953                                   struct rte_security_session_conf *conf,
2954                                   struct rte_security_session *sess,
2955                                   struct rte_mempool *mempool)
2956 {
2957         void *sess_private_data;
2958         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2959         int ret;
2960
2961         if (rte_mempool_get(mempool, &sess_private_data)) {
2962                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
2963                 return -ENOMEM;
2964         }
2965
2966         switch (conf->protocol) {
2967         case RTE_SECURITY_PROTOCOL_IPSEC:
2968                 ret = dpaa2_sec_set_ipsec_session(cdev, conf,
2969                                 sess_private_data);
2970                 break;
2971         case RTE_SECURITY_PROTOCOL_MACSEC:
2972                 return -ENOTSUP;
2973         case RTE_SECURITY_PROTOCOL_PDCP:
2974                 ret = dpaa2_sec_set_pdcp_session(cdev, conf,
2975                                 sess_private_data);
2976                 break;
2977         default:
2978                 return -EINVAL;
2979         }
2980         if (ret != 0) {
2981                 DPAA2_SEC_ERR("Failed to configure session parameters");
2982                 /* Return session to mempool */
2983                 rte_mempool_put(mempool, sess_private_data);
2984                 return ret;
2985         }
2986
2987         set_sec_session_private_data(sess, sess_private_data);
2988
2989         return ret;
2990 }
2991
2992 /** Clear the memory of session so it doesn't leave key material behind */
2993 static int
2994 dpaa2_sec_security_session_destroy(void *dev __rte_unused,
2995                 struct rte_security_session *sess)
2996 {
2997         PMD_INIT_FUNC_TRACE();
2998         void *sess_priv = get_sec_session_private_data(sess);
2999
3000         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3001
3002         if (sess_priv) {
3003                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3004
3005                 rte_free(s->ctxt);
3006                 rte_free(s->cipher_key.data);
3007                 rte_free(s->auth_key.data);
3008                 memset(s, 0, sizeof(dpaa2_sec_session));
3009                 set_sec_session_private_data(sess, NULL);
3010                 rte_mempool_put(sess_mp, sess_priv);
3011         }
3012         return 0;
3013 }
3014
3015 static int
3016 dpaa2_sec_sym_session_configure(struct rte_cryptodev *dev,
3017                 struct rte_crypto_sym_xform *xform,
3018                 struct rte_cryptodev_sym_session *sess,
3019                 struct rte_mempool *mempool)
3020 {
3021         void *sess_private_data;
3022         int ret;
3023
3024         if (rte_mempool_get(mempool, &sess_private_data)) {
3025                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
3026                 return -ENOMEM;
3027         }
3028
3029         ret = dpaa2_sec_set_session_parameters(dev, xform, sess_private_data);
3030         if (ret != 0) {
3031                 DPAA2_SEC_ERR("Failed to configure session parameters");
3032                 /* Return session to mempool */
3033                 rte_mempool_put(mempool, sess_private_data);
3034                 return ret;
3035         }
3036
3037         set_sym_session_private_data(sess, dev->driver_id,
3038                 sess_private_data);
3039
3040         return 0;
3041 }
3042
3043 /** Clear the memory of session so it doesn't leave key material behind */
3044 static void
3045 dpaa2_sec_sym_session_clear(struct rte_cryptodev *dev,
3046                 struct rte_cryptodev_sym_session *sess)
3047 {
3048         PMD_INIT_FUNC_TRACE();
3049         uint8_t index = dev->driver_id;
3050         void *sess_priv = get_sym_session_private_data(sess, index);
3051         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3052
3053         if (sess_priv) {
3054                 rte_free(s->ctxt);
3055                 rte_free(s->cipher_key.data);
3056                 rte_free(s->auth_key.data);
3057                 memset(s, 0, sizeof(dpaa2_sec_session));
3058                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3059                 set_sym_session_private_data(sess, index, NULL);
3060                 rte_mempool_put(sess_mp, sess_priv);
3061         }
3062 }
3063
3064 static int
3065 dpaa2_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3066                         struct rte_cryptodev_config *config __rte_unused)
3067 {
3068         PMD_INIT_FUNC_TRACE();
3069
3070         return 0;
3071 }
3072
3073 static int
3074 dpaa2_sec_dev_start(struct rte_cryptodev *dev)
3075 {
3076         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3077         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3078         struct dpseci_attr attr;
3079         struct dpaa2_queue *dpaa2_q;
3080         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3081                                         dev->data->queue_pairs;
3082         struct dpseci_rx_queue_attr rx_attr;
3083         struct dpseci_tx_queue_attr tx_attr;
3084         int ret, i;
3085
3086         PMD_INIT_FUNC_TRACE();
3087
3088         memset(&attr, 0, sizeof(struct dpseci_attr));
3089
3090         ret = dpseci_enable(dpseci, CMD_PRI_LOW, priv->token);
3091         if (ret) {
3092                 DPAA2_SEC_ERR("DPSECI with HW_ID = %d ENABLE FAILED",
3093                               priv->hw_id);
3094                 goto get_attr_failure;
3095         }
3096         ret = dpseci_get_attributes(dpseci, CMD_PRI_LOW, priv->token, &attr);
3097         if (ret) {
3098                 DPAA2_SEC_ERR("DPSEC ATTRIBUTE READ FAILED, disabling DPSEC");
3099                 goto get_attr_failure;
3100         }
3101         for (i = 0; i < attr.num_rx_queues && qp[i]; i++) {
3102                 dpaa2_q = &qp[i]->rx_vq;
3103                 dpseci_get_rx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3104                                     &rx_attr);
3105                 dpaa2_q->fqid = rx_attr.fqid;
3106                 DPAA2_SEC_DEBUG("rx_fqid: %d", dpaa2_q->fqid);
3107         }
3108         for (i = 0; i < attr.num_tx_queues && qp[i]; i++) {
3109                 dpaa2_q = &qp[i]->tx_vq;
3110                 dpseci_get_tx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3111                                     &tx_attr);
3112                 dpaa2_q->fqid = tx_attr.fqid;
3113                 DPAA2_SEC_DEBUG("tx_fqid: %d", dpaa2_q->fqid);
3114         }
3115
3116         return 0;
3117 get_attr_failure:
3118         dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3119         return -1;
3120 }
3121
3122 static void
3123 dpaa2_sec_dev_stop(struct rte_cryptodev *dev)
3124 {
3125         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3126         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3127         int ret;
3128
3129         PMD_INIT_FUNC_TRACE();
3130
3131         ret = dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3132         if (ret) {
3133                 DPAA2_SEC_ERR("Failure in disabling dpseci %d device",
3134                              priv->hw_id);
3135                 return;
3136         }
3137
3138         ret = dpseci_reset(dpseci, CMD_PRI_LOW, priv->token);
3139         if (ret < 0) {
3140                 DPAA2_SEC_ERR("SEC Device cannot be reset:Error = %0x", ret);
3141                 return;
3142         }
3143 }
3144
3145 static int
3146 dpaa2_sec_dev_close(struct rte_cryptodev *dev)
3147 {
3148         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3149         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3150         int ret;
3151
3152         PMD_INIT_FUNC_TRACE();
3153
3154         /* Function is reverse of dpaa2_sec_dev_init.
3155          * It does the following:
3156          * 1. Detach a DPSECI from attached resources i.e. buffer pools, dpbp_id
3157          * 2. Close the DPSECI device
3158          * 3. Free the allocated resources.
3159          */
3160
3161         /*Close the device at underlying layer*/
3162         ret = dpseci_close(dpseci, CMD_PRI_LOW, priv->token);
3163         if (ret) {
3164                 DPAA2_SEC_ERR("Failure closing dpseci device: err(%d)", ret);
3165                 return -1;
3166         }
3167
3168         /*Free the allocated memory for ethernet private data and dpseci*/
3169         priv->hw = NULL;
3170         rte_free(dpseci);
3171
3172         return 0;
3173 }
3174
3175 static void
3176 dpaa2_sec_dev_infos_get(struct rte_cryptodev *dev,
3177                         struct rte_cryptodev_info *info)
3178 {
3179         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
3180
3181         PMD_INIT_FUNC_TRACE();
3182         if (info != NULL) {
3183                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3184                 info->feature_flags = dev->feature_flags;
3185                 info->capabilities = dpaa2_sec_capabilities;
3186                 /* No limit of number of sessions */
3187                 info->sym.max_nb_sessions = 0;
3188                 info->driver_id = cryptodev_driver_id;
3189         }
3190 }
3191
3192 static
3193 void dpaa2_sec_stats_get(struct rte_cryptodev *dev,
3194                          struct rte_cryptodev_stats *stats)
3195 {
3196         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3197         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3198         struct dpseci_sec_counters counters = {0};
3199         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3200                                         dev->data->queue_pairs;
3201         int ret, i;
3202
3203         PMD_INIT_FUNC_TRACE();
3204         if (stats == NULL) {
3205                 DPAA2_SEC_ERR("Invalid stats ptr NULL");
3206                 return;
3207         }
3208         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3209                 if (qp[i] == NULL) {
3210                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3211                         continue;
3212                 }
3213
3214                 stats->enqueued_count += qp[i]->tx_vq.tx_pkts;
3215                 stats->dequeued_count += qp[i]->rx_vq.rx_pkts;
3216                 stats->enqueue_err_count += qp[i]->tx_vq.err_pkts;
3217                 stats->dequeue_err_count += qp[i]->rx_vq.err_pkts;
3218         }
3219
3220         ret = dpseci_get_sec_counters(dpseci, CMD_PRI_LOW, priv->token,
3221                                       &counters);
3222         if (ret) {
3223                 DPAA2_SEC_ERR("SEC counters failed");
3224         } else {
3225                 DPAA2_SEC_INFO("dpseci hardware stats:"
3226                             "\n\tNum of Requests Dequeued = %" PRIu64
3227                             "\n\tNum of Outbound Encrypt Requests = %" PRIu64
3228                             "\n\tNum of Inbound Decrypt Requests = %" PRIu64
3229                             "\n\tNum of Outbound Bytes Encrypted = %" PRIu64
3230                             "\n\tNum of Outbound Bytes Protected = %" PRIu64
3231                             "\n\tNum of Inbound Bytes Decrypted = %" PRIu64
3232                             "\n\tNum of Inbound Bytes Validated = %" PRIu64,
3233                             counters.dequeued_requests,
3234                             counters.ob_enc_requests,
3235                             counters.ib_dec_requests,
3236                             counters.ob_enc_bytes,
3237                             counters.ob_prot_bytes,
3238                             counters.ib_dec_bytes,
3239                             counters.ib_valid_bytes);
3240         }
3241 }
3242
3243 static
3244 void dpaa2_sec_stats_reset(struct rte_cryptodev *dev)
3245 {
3246         int i;
3247         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3248                                    (dev->data->queue_pairs);
3249
3250         PMD_INIT_FUNC_TRACE();
3251
3252         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3253                 if (qp[i] == NULL) {
3254                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3255                         continue;
3256                 }
3257                 qp[i]->tx_vq.rx_pkts = 0;
3258                 qp[i]->tx_vq.tx_pkts = 0;
3259                 qp[i]->tx_vq.err_pkts = 0;
3260                 qp[i]->rx_vq.rx_pkts = 0;
3261                 qp[i]->rx_vq.tx_pkts = 0;
3262                 qp[i]->rx_vq.err_pkts = 0;
3263         }
3264 }
3265
3266 static void __attribute__((hot))
3267 dpaa2_sec_process_parallel_event(struct qbman_swp *swp,
3268                                  const struct qbman_fd *fd,
3269                                  const struct qbman_result *dq,
3270                                  struct dpaa2_queue *rxq,
3271                                  struct rte_event *ev)
3272 {
3273         /* Prefetching mbuf */
3274         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
3275                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
3276
3277         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
3278         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
3279
3280         ev->flow_id = rxq->ev.flow_id;
3281         ev->sub_event_type = rxq->ev.sub_event_type;
3282         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3283         ev->op = RTE_EVENT_OP_NEW;
3284         ev->sched_type = rxq->ev.sched_type;
3285         ev->queue_id = rxq->ev.queue_id;
3286         ev->priority = rxq->ev.priority;
3287         ev->event_ptr = sec_fd_to_mbuf(fd);
3288
3289         qbman_swp_dqrr_consume(swp, dq);
3290 }
3291 static void
3292 dpaa2_sec_process_atomic_event(struct qbman_swp *swp __attribute__((unused)),
3293                                  const struct qbman_fd *fd,
3294                                  const struct qbman_result *dq,
3295                                  struct dpaa2_queue *rxq,
3296                                  struct rte_event *ev)
3297 {
3298         uint8_t dqrr_index;
3299         struct rte_crypto_op *crypto_op = (struct rte_crypto_op *)ev->event_ptr;
3300         /* Prefetching mbuf */
3301         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
3302                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
3303
3304         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
3305         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
3306
3307         ev->flow_id = rxq->ev.flow_id;
3308         ev->sub_event_type = rxq->ev.sub_event_type;
3309         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3310         ev->op = RTE_EVENT_OP_NEW;
3311         ev->sched_type = rxq->ev.sched_type;
3312         ev->queue_id = rxq->ev.queue_id;
3313         ev->priority = rxq->ev.priority;
3314
3315         ev->event_ptr = sec_fd_to_mbuf(fd);
3316         dqrr_index = qbman_get_dqrr_idx(dq);
3317         crypto_op->sym->m_src->seqn = dqrr_index + 1;
3318         DPAA2_PER_LCORE_DQRR_SIZE++;
3319         DPAA2_PER_LCORE_DQRR_HELD |= 1 << dqrr_index;
3320         DPAA2_PER_LCORE_DQRR_MBUF(dqrr_index) = crypto_op->sym->m_src;
3321 }
3322
3323 int
3324 dpaa2_sec_eventq_attach(const struct rte_cryptodev *dev,
3325                 int qp_id,
3326                 uint16_t dpcon_id,
3327                 const struct rte_event *event)
3328 {
3329         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3330         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3331         struct dpaa2_sec_qp *qp = dev->data->queue_pairs[qp_id];
3332         struct dpseci_rx_queue_cfg cfg;
3333         int ret;
3334
3335         if (event->sched_type == RTE_SCHED_TYPE_PARALLEL)
3336                 qp->rx_vq.cb = dpaa2_sec_process_parallel_event;
3337         else if (event->sched_type == RTE_SCHED_TYPE_ATOMIC)
3338                 qp->rx_vq.cb = dpaa2_sec_process_atomic_event;
3339         else
3340                 return -EINVAL;
3341
3342         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
3343         cfg.options = DPSECI_QUEUE_OPT_DEST;
3344         cfg.dest_cfg.dest_type = DPSECI_DEST_DPCON;
3345         cfg.dest_cfg.dest_id = dpcon_id;
3346         cfg.dest_cfg.priority = event->priority;
3347
3348         cfg.options |= DPSECI_QUEUE_OPT_USER_CTX;
3349         cfg.user_ctx = (size_t)(qp);
3350         if (event->sched_type == RTE_SCHED_TYPE_ATOMIC) {
3351                 cfg.options |= DPSECI_QUEUE_OPT_ORDER_PRESERVATION;
3352                 cfg.order_preservation_en = 1;
3353         }
3354         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
3355                                   qp_id, &cfg);
3356         if (ret) {
3357                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
3358                 return ret;
3359         }
3360
3361         memcpy(&qp->rx_vq.ev, event, sizeof(struct rte_event));
3362
3363         return 0;
3364 }
3365
3366 int
3367 dpaa2_sec_eventq_detach(const struct rte_cryptodev *dev,
3368                         int qp_id)
3369 {
3370         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3371         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3372         struct dpseci_rx_queue_cfg cfg;
3373         int ret;
3374
3375         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
3376         cfg.options = DPSECI_QUEUE_OPT_DEST;
3377         cfg.dest_cfg.dest_type = DPSECI_DEST_NONE;
3378
3379         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
3380                                   qp_id, &cfg);
3381         if (ret)
3382                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
3383
3384         return ret;
3385 }
3386
3387 static struct rte_cryptodev_ops crypto_ops = {
3388         .dev_configure        = dpaa2_sec_dev_configure,
3389         .dev_start            = dpaa2_sec_dev_start,
3390         .dev_stop             = dpaa2_sec_dev_stop,
3391         .dev_close            = dpaa2_sec_dev_close,
3392         .dev_infos_get        = dpaa2_sec_dev_infos_get,
3393         .stats_get            = dpaa2_sec_stats_get,
3394         .stats_reset          = dpaa2_sec_stats_reset,
3395         .queue_pair_setup     = dpaa2_sec_queue_pair_setup,
3396         .queue_pair_release   = dpaa2_sec_queue_pair_release,
3397         .queue_pair_count     = dpaa2_sec_queue_pair_count,
3398         .sym_session_get_size     = dpaa2_sec_sym_session_get_size,
3399         .sym_session_configure    = dpaa2_sec_sym_session_configure,
3400         .sym_session_clear        = dpaa2_sec_sym_session_clear,
3401 };
3402
3403 static const struct rte_security_capability *
3404 dpaa2_sec_capabilities_get(void *device __rte_unused)
3405 {
3406         return dpaa2_sec_security_cap;
3407 }
3408
3409 static const struct rte_security_ops dpaa2_sec_security_ops = {
3410         .session_create = dpaa2_sec_security_session_create,
3411         .session_update = NULL,
3412         .session_stats_get = NULL,
3413         .session_destroy = dpaa2_sec_security_session_destroy,
3414         .set_pkt_metadata = NULL,
3415         .capabilities_get = dpaa2_sec_capabilities_get
3416 };
3417
3418 static int
3419 dpaa2_sec_uninit(const struct rte_cryptodev *dev)
3420 {
3421         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
3422
3423         rte_free(dev->security_ctx);
3424
3425         rte_mempool_free(internals->fle_pool);
3426
3427         DPAA2_SEC_INFO("Closing DPAA2_SEC device %s on numa socket %u",
3428                        dev->data->name, rte_socket_id());
3429
3430         return 0;
3431 }
3432
3433 static int
3434 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
3435 {
3436         struct dpaa2_sec_dev_private *internals;
3437         struct rte_device *dev = cryptodev->device;
3438         struct rte_dpaa2_device *dpaa2_dev;
3439         struct rte_security_ctx *security_instance;
3440         struct fsl_mc_io *dpseci;
3441         uint16_t token;
3442         struct dpseci_attr attr;
3443         int retcode, hw_id;
3444         char str[30];
3445
3446         PMD_INIT_FUNC_TRACE();
3447         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
3448         if (dpaa2_dev == NULL) {
3449                 DPAA2_SEC_ERR("DPAA2 SEC device not found");
3450                 return -1;
3451         }
3452         hw_id = dpaa2_dev->object_id;
3453
3454         cryptodev->driver_id = cryptodev_driver_id;
3455         cryptodev->dev_ops = &crypto_ops;
3456
3457         cryptodev->enqueue_burst = dpaa2_sec_enqueue_burst;
3458         cryptodev->dequeue_burst = dpaa2_sec_dequeue_burst;
3459         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3460                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
3461                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3462                         RTE_CRYPTODEV_FF_SECURITY |
3463                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
3464                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
3465                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
3466                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
3467                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3468
3469         internals = cryptodev->data->dev_private;
3470
3471         /*
3472          * For secondary processes, we don't initialise any further as primary
3473          * has already done this work. Only check we don't need a different
3474          * RX function
3475          */
3476         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3477                 DPAA2_SEC_DEBUG("Device already init by primary process");
3478                 return 0;
3479         }
3480
3481         /* Initialize security_ctx only for primary process*/
3482         security_instance = rte_malloc("rte_security_instances_ops",
3483                                 sizeof(struct rte_security_ctx), 0);
3484         if (security_instance == NULL)
3485                 return -ENOMEM;
3486         security_instance->device = (void *)cryptodev;
3487         security_instance->ops = &dpaa2_sec_security_ops;
3488         security_instance->sess_cnt = 0;
3489         cryptodev->security_ctx = security_instance;
3490
3491         /*Open the rte device via MC and save the handle for further use*/
3492         dpseci = (struct fsl_mc_io *)rte_calloc(NULL, 1,
3493                                 sizeof(struct fsl_mc_io), 0);
3494         if (!dpseci) {
3495                 DPAA2_SEC_ERR(
3496                         "Error in allocating the memory for dpsec object");
3497                 return -1;
3498         }
3499         dpseci->regs = rte_mcp_ptr_list[0];
3500
3501         retcode = dpseci_open(dpseci, CMD_PRI_LOW, hw_id, &token);
3502         if (retcode != 0) {
3503                 DPAA2_SEC_ERR("Cannot open the dpsec device: Error = %x",
3504                               retcode);
3505                 goto init_error;
3506         }
3507         retcode = dpseci_get_attributes(dpseci, CMD_PRI_LOW, token, &attr);
3508         if (retcode != 0) {
3509                 DPAA2_SEC_ERR(
3510                              "Cannot get dpsec device attributed: Error = %x",
3511                              retcode);
3512                 goto init_error;
3513         }
3514         snprintf(cryptodev->data->name, sizeof(cryptodev->data->name),
3515                         "dpsec-%u", hw_id);
3516
3517         internals->max_nb_queue_pairs = attr.num_tx_queues;
3518         cryptodev->data->nb_queue_pairs = internals->max_nb_queue_pairs;
3519         internals->hw = dpseci;
3520         internals->token = token;
3521
3522         snprintf(str, sizeof(str), "sec_fle_pool_p%d_%d",
3523                         getpid(), cryptodev->data->dev_id);
3524         internals->fle_pool = rte_mempool_create((const char *)str,
3525                         FLE_POOL_NUM_BUFS,
3526                         FLE_POOL_BUF_SIZE,
3527                         FLE_POOL_CACHE_SIZE, 0,
3528                         NULL, NULL, NULL, NULL,
3529                         SOCKET_ID_ANY, 0);
3530         if (!internals->fle_pool) {
3531                 DPAA2_SEC_ERR("Mempool (%s) creation failed", str);
3532                 goto init_error;
3533         }
3534
3535         DPAA2_SEC_INFO("driver %s: created", cryptodev->data->name);
3536         return 0;
3537
3538 init_error:
3539         DPAA2_SEC_ERR("driver %s: create failed", cryptodev->data->name);
3540
3541         /* dpaa2_sec_uninit(crypto_dev_name); */
3542         return -EFAULT;
3543 }
3544
3545 static int
3546 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
3547                           struct rte_dpaa2_device *dpaa2_dev)
3548 {
3549         struct rte_cryptodev *cryptodev;
3550         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3551
3552         int retval;
3553
3554         snprintf(cryptodev_name, sizeof(cryptodev_name), "dpsec-%d",
3555                         dpaa2_dev->object_id);
3556
3557         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3558         if (cryptodev == NULL)
3559                 return -ENOMEM;
3560
3561         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3562                 cryptodev->data->dev_private = rte_zmalloc_socket(
3563                                         "cryptodev private structure",
3564                                         sizeof(struct dpaa2_sec_dev_private),
3565                                         RTE_CACHE_LINE_SIZE,
3566                                         rte_socket_id());
3567
3568                 if (cryptodev->data->dev_private == NULL)
3569                         rte_panic("Cannot allocate memzone for private "
3570                                   "device data");
3571         }
3572
3573         dpaa2_dev->cryptodev = cryptodev;
3574         cryptodev->device = &dpaa2_dev->device;
3575
3576         /* init user callbacks */
3577         TAILQ_INIT(&(cryptodev->link_intr_cbs));
3578
3579         if (dpaa2_svr_family == SVR_LX2160A)
3580                 rta_set_sec_era(RTA_SEC_ERA_10);
3581
3582         DPAA2_SEC_INFO("2-SEC ERA is %d", rta_get_sec_era());
3583
3584         /* Invoke PMD device initialization function */
3585         retval = dpaa2_sec_dev_init(cryptodev);
3586         if (retval == 0)
3587                 return 0;
3588
3589         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3590                 rte_free(cryptodev->data->dev_private);
3591
3592         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
3593
3594         return -ENXIO;
3595 }
3596
3597 static int
3598 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
3599 {
3600         struct rte_cryptodev *cryptodev;
3601         int ret;
3602
3603         cryptodev = dpaa2_dev->cryptodev;
3604         if (cryptodev == NULL)
3605                 return -ENODEV;
3606
3607         ret = dpaa2_sec_uninit(cryptodev);
3608         if (ret)
3609                 return ret;
3610
3611         return rte_cryptodev_pmd_destroy(cryptodev);
3612 }
3613
3614 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
3615         .drv_flags = RTE_DPAA2_DRV_IOVA_AS_VA,
3616         .drv_type = DPAA2_CRYPTO,
3617         .driver = {
3618                 .name = "DPAA2 SEC PMD"
3619         },
3620         .probe = cryptodev_dpaa2_sec_probe,
3621         .remove = cryptodev_dpaa2_sec_remove,
3622 };
3623
3624 static struct cryptodev_driver dpaa2_sec_crypto_drv;
3625
3626 RTE_PMD_REGISTER_DPAA2(CRYPTODEV_NAME_DPAA2_SEC_PMD, rte_dpaa2_sec_driver);
3627 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa2_sec_crypto_drv,
3628                 rte_dpaa2_sec_driver.driver, cryptodev_driver_id);
3629
3630 RTE_INIT(dpaa2_sec_init_log)
3631 {
3632         /* Bus level logs */
3633         dpaa2_logtype_sec = rte_log_register("pmd.crypto.dpaa2");
3634         if (dpaa2_logtype_sec >= 0)
3635                 rte_log_set_level(dpaa2_logtype_sec, RTE_LOG_NOTICE);
3636 }