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