crypto/dpaa2_sec: upgarde mc FW APIs to 10.10.0
[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 >> 3) ? MAX_TX_RING_SLOTS : nb_ops;
1241
1242                 for (loop = 0; loop < frames_to_send; loop++) {
1243                         /*Clear the unused FD fields before sending*/
1244                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
1245                         mb_pool = (*ops)->sym->m_src->pool;
1246                         bpid = mempool_to_bpid(mb_pool);
1247                         ret = build_sec_fd(*ops, &fd_arr[loop], bpid);
1248                         if (ret) {
1249                                 DPAA2_SEC_ERR("error: Improper packet contents"
1250                                               " for crypto operation");
1251                                 goto skip_tx;
1252                         }
1253                         ops++;
1254                 }
1255                 loop = 0;
1256                 while (loop < frames_to_send) {
1257                         loop += qbman_swp_enqueue_multiple(swp, &eqdesc,
1258                                                         &fd_arr[loop],
1259                                                         NULL,
1260                                                         frames_to_send - loop);
1261                 }
1262
1263                 num_tx += frames_to_send;
1264                 nb_ops -= frames_to_send;
1265         }
1266 skip_tx:
1267         dpaa2_qp->tx_vq.tx_pkts += num_tx;
1268         dpaa2_qp->tx_vq.err_pkts += nb_ops;
1269         return num_tx;
1270 }
1271
1272 static inline struct rte_crypto_op *
1273 sec_simple_fd_to_mbuf(const struct qbman_fd *fd, __rte_unused uint8_t id)
1274 {
1275         struct rte_crypto_op *op;
1276         uint16_t len = DPAA2_GET_FD_LEN(fd);
1277         uint16_t diff = 0;
1278         dpaa2_sec_session *sess_priv;
1279
1280         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
1281                 DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd)),
1282                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
1283
1284         op = (struct rte_crypto_op *)(size_t)mbuf->buf_iova;
1285         mbuf->buf_iova = op->sym->aead.digest.phys_addr;
1286         op->sym->aead.digest.phys_addr = 0L;
1287
1288         sess_priv = (dpaa2_sec_session *)get_sec_session_private_data(
1289                                 op->sym->sec_session);
1290         if (sess_priv->dir == DIR_ENC)
1291                 mbuf->data_off += SEC_FLC_DHR_OUTBOUND;
1292         else
1293                 mbuf->data_off += SEC_FLC_DHR_INBOUND;
1294         diff = len - mbuf->pkt_len;
1295         mbuf->pkt_len += diff;
1296         mbuf->data_len += diff;
1297
1298         return op;
1299 }
1300
1301 static inline struct rte_crypto_op *
1302 sec_fd_to_mbuf(const struct qbman_fd *fd, uint8_t driver_id)
1303 {
1304         struct qbman_fle *fle;
1305         struct rte_crypto_op *op;
1306         struct ctxt_priv *priv;
1307         struct rte_mbuf *dst, *src;
1308
1309         if (DPAA2_FD_GET_FORMAT(fd) == qbman_fd_single)
1310                 return sec_simple_fd_to_mbuf(fd, driver_id);
1311
1312         fle = (struct qbman_fle *)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
1313
1314         DPAA2_SEC_DP_DEBUG("FLE addr = %x - %x, offset = %x\n",
1315                            fle->addr_hi, fle->addr_lo, fle->fin_bpid_offset);
1316
1317         /* we are using the first FLE entry to store Mbuf.
1318          * Currently we donot know which FLE has the mbuf stored.
1319          * So while retreiving we can go back 1 FLE from the FD -ADDR
1320          * to get the MBUF Addr from the previous FLE.
1321          * We can have a better approach to use the inline Mbuf
1322          */
1323
1324         if (unlikely(DPAA2_GET_FD_IVP(fd))) {
1325                 /* TODO complete it. */
1326                 DPAA2_SEC_ERR("error: non inline buffer");
1327                 return NULL;
1328         }
1329         op = (struct rte_crypto_op *)DPAA2_GET_FLE_ADDR((fle - 1));
1330
1331         /* Prefeth op */
1332         src = op->sym->m_src;
1333         rte_prefetch0(src);
1334
1335         if (op->sym->m_dst) {
1336                 dst = op->sym->m_dst;
1337                 rte_prefetch0(dst);
1338         } else
1339                 dst = src;
1340
1341         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1342                 dpaa2_sec_session *sess = (dpaa2_sec_session *)
1343                         get_sec_session_private_data(op->sym->sec_session);
1344                 if (sess->ctxt_type == DPAA2_SEC_IPSEC) {
1345                         uint16_t len = DPAA2_GET_FD_LEN(fd);
1346                         dst->pkt_len = len;
1347                         dst->data_len = len;
1348                 }
1349         }
1350
1351         DPAA2_SEC_DP_DEBUG("mbuf %p BMAN buf addr %p,"
1352                 " fdaddr =%" PRIx64 " bpid =%d meta =%d off =%d, len =%d\n",
1353                 (void *)dst,
1354                 dst->buf_addr,
1355                 DPAA2_GET_FD_ADDR(fd),
1356                 DPAA2_GET_FD_BPID(fd),
1357                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
1358                 DPAA2_GET_FD_OFFSET(fd),
1359                 DPAA2_GET_FD_LEN(fd));
1360
1361         /* free the fle memory */
1362         if (likely(rte_pktmbuf_is_contiguous(src))) {
1363                 priv = (struct ctxt_priv *)(size_t)DPAA2_GET_FLE_CTXT(fle - 1);
1364                 rte_mempool_put(priv->fle_pool, (void *)(fle-1));
1365         } else
1366                 rte_free((void *)(fle-1));
1367
1368         return op;
1369 }
1370
1371 static uint16_t
1372 dpaa2_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1373                         uint16_t nb_ops)
1374 {
1375         /* Function is responsible to receive frames for a given device and VQ*/
1376         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1377         struct rte_cryptodev *dev =
1378                         (struct rte_cryptodev *)(dpaa2_qp->rx_vq.dev);
1379         struct qbman_result *dq_storage;
1380         uint32_t fqid = dpaa2_qp->rx_vq.fqid;
1381         int ret, num_rx = 0;
1382         uint8_t is_last = 0, status;
1383         struct qbman_swp *swp;
1384         const struct qbman_fd *fd;
1385         struct qbman_pull_desc pulldesc;
1386
1387         if (!DPAA2_PER_LCORE_DPIO) {
1388                 ret = dpaa2_affine_qbman_swp();
1389                 if (ret) {
1390                         DPAA2_SEC_ERR("Failure in affining portal");
1391                         return 0;
1392                 }
1393         }
1394         swp = DPAA2_PER_LCORE_PORTAL;
1395         dq_storage = dpaa2_qp->rx_vq.q_storage->dq_storage[0];
1396
1397         qbman_pull_desc_clear(&pulldesc);
1398         qbman_pull_desc_set_numframes(&pulldesc,
1399                                       (nb_ops > DPAA2_DQRR_RING_SIZE) ?
1400                                       DPAA2_DQRR_RING_SIZE : nb_ops);
1401         qbman_pull_desc_set_fq(&pulldesc, fqid);
1402         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
1403                                     (dma_addr_t)DPAA2_VADDR_TO_IOVA(dq_storage),
1404                                     1);
1405
1406         /*Issue a volatile dequeue command. */
1407         while (1) {
1408                 if (qbman_swp_pull(swp, &pulldesc)) {
1409                         DPAA2_SEC_WARN(
1410                                 "SEC VDQ command is not issued : QBMAN busy");
1411                         /* Portal was busy, try again */
1412                         continue;
1413                 }
1414                 break;
1415         };
1416
1417         /* Receive the packets till Last Dequeue entry is found with
1418          * respect to the above issues PULL command.
1419          */
1420         while (!is_last) {
1421                 /* Check if the previous issued command is completed.
1422                  * Also seems like the SWP is shared between the Ethernet Driver
1423                  * and the SEC driver.
1424                  */
1425                 while (!qbman_check_command_complete(dq_storage))
1426                         ;
1427
1428                 /* Loop until the dq_storage is updated with
1429                  * new token by QBMAN
1430                  */
1431                 while (!qbman_check_new_result(dq_storage))
1432                         ;
1433                 /* Check whether Last Pull command is Expired and
1434                  * setting Condition for Loop termination
1435                  */
1436                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
1437                         is_last = 1;
1438                         /* Check for valid frame. */
1439                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
1440                         if (unlikely(
1441                                 (status & QBMAN_DQ_STAT_VALIDFRAME) == 0)) {
1442                                 DPAA2_SEC_DP_DEBUG("No frame is delivered\n");
1443                                 continue;
1444                         }
1445                 }
1446
1447                 fd = qbman_result_DQ_fd(dq_storage);
1448                 ops[num_rx] = sec_fd_to_mbuf(fd, dev->driver_id);
1449
1450                 if (unlikely(fd->simple.frc)) {
1451                         /* TODO Parse SEC errors */
1452                         DPAA2_SEC_ERR("SEC returned Error - %x",
1453                                       fd->simple.frc);
1454                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_ERROR;
1455                 } else {
1456                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1457                 }
1458
1459                 num_rx++;
1460                 dq_storage++;
1461         } /* End of Packet Rx loop */
1462
1463         dpaa2_qp->rx_vq.rx_pkts += num_rx;
1464
1465         DPAA2_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1466         /*Return the total number of packets received to DPAA2 app*/
1467         return num_rx;
1468 }
1469
1470 /** Release queue pair */
1471 static int
1472 dpaa2_sec_queue_pair_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
1473 {
1474         struct dpaa2_sec_qp *qp =
1475                 (struct dpaa2_sec_qp *)dev->data->queue_pairs[queue_pair_id];
1476
1477         PMD_INIT_FUNC_TRACE();
1478
1479         if (qp->rx_vq.q_storage) {
1480                 dpaa2_free_dq_storage(qp->rx_vq.q_storage);
1481                 rte_free(qp->rx_vq.q_storage);
1482         }
1483         rte_free(qp);
1484
1485         dev->data->queue_pairs[queue_pair_id] = NULL;
1486
1487         return 0;
1488 }
1489
1490 /** Setup a queue pair */
1491 static int
1492 dpaa2_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1493                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1494                 __rte_unused int socket_id,
1495                 __rte_unused struct rte_mempool *session_pool)
1496 {
1497         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1498         struct dpaa2_sec_qp *qp;
1499         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1500         struct dpseci_rx_queue_cfg cfg;
1501         int32_t retcode;
1502
1503         PMD_INIT_FUNC_TRACE();
1504
1505         /* If qp is already in use free ring memory and qp metadata. */
1506         if (dev->data->queue_pairs[qp_id] != NULL) {
1507                 DPAA2_SEC_INFO("QP already setup");
1508                 return 0;
1509         }
1510
1511         DPAA2_SEC_DEBUG("dev =%p, queue =%d, conf =%p",
1512                     dev, qp_id, qp_conf);
1513
1514         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
1515
1516         qp = rte_malloc(NULL, sizeof(struct dpaa2_sec_qp),
1517                         RTE_CACHE_LINE_SIZE);
1518         if (!qp) {
1519                 DPAA2_SEC_ERR("malloc failed for rx/tx queues");
1520                 return -1;
1521         }
1522
1523         qp->rx_vq.dev = dev;
1524         qp->tx_vq.dev = dev;
1525         qp->rx_vq.q_storage = rte_malloc("sec dq storage",
1526                 sizeof(struct queue_storage_info_t),
1527                 RTE_CACHE_LINE_SIZE);
1528         if (!qp->rx_vq.q_storage) {
1529                 DPAA2_SEC_ERR("malloc failed for q_storage");
1530                 return -1;
1531         }
1532         memset(qp->rx_vq.q_storage, 0, sizeof(struct queue_storage_info_t));
1533
1534         if (dpaa2_alloc_dq_storage(qp->rx_vq.q_storage)) {
1535                 DPAA2_SEC_ERR("Unable to allocate dequeue storage");
1536                 return -1;
1537         }
1538
1539         dev->data->queue_pairs[qp_id] = qp;
1540
1541         cfg.options = cfg.options | DPSECI_QUEUE_OPT_USER_CTX;
1542         cfg.user_ctx = (size_t)(&qp->rx_vq);
1543         retcode = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
1544                                       qp_id, &cfg);
1545         return retcode;
1546 }
1547
1548 /** Return the number of allocated queue pairs */
1549 static uint32_t
1550 dpaa2_sec_queue_pair_count(struct rte_cryptodev *dev)
1551 {
1552         PMD_INIT_FUNC_TRACE();
1553
1554         return dev->data->nb_queue_pairs;
1555 }
1556
1557 /** Returns the size of the aesni gcm session structure */
1558 static unsigned int
1559 dpaa2_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1560 {
1561         PMD_INIT_FUNC_TRACE();
1562
1563         return sizeof(dpaa2_sec_session);
1564 }
1565
1566 static int
1567 dpaa2_sec_cipher_init(struct rte_cryptodev *dev,
1568                       struct rte_crypto_sym_xform *xform,
1569                       dpaa2_sec_session *session)
1570 {
1571         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1572         struct alginfo cipherdata;
1573         int bufsize, i;
1574         struct ctxt_priv *priv;
1575         struct sec_flow_context *flc;
1576
1577         PMD_INIT_FUNC_TRACE();
1578
1579         /* For SEC CIPHER only one descriptor is required. */
1580         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1581                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1582                         RTE_CACHE_LINE_SIZE);
1583         if (priv == NULL) {
1584                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1585                 return -1;
1586         }
1587
1588         priv->fle_pool = dev_priv->fle_pool;
1589
1590         flc = &priv->flc_desc[0].flc;
1591
1592         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1593                         RTE_CACHE_LINE_SIZE);
1594         if (session->cipher_key.data == NULL) {
1595                 DPAA2_SEC_ERR("No Memory for cipher key");
1596                 rte_free(priv);
1597                 return -1;
1598         }
1599         session->cipher_key.length = xform->cipher.key.length;
1600
1601         memcpy(session->cipher_key.data, xform->cipher.key.data,
1602                xform->cipher.key.length);
1603         cipherdata.key = (size_t)session->cipher_key.data;
1604         cipherdata.keylen = session->cipher_key.length;
1605         cipherdata.key_enc_flags = 0;
1606         cipherdata.key_type = RTA_DATA_IMM;
1607
1608         /* Set IV parameters */
1609         session->iv.offset = xform->cipher.iv.offset;
1610         session->iv.length = xform->cipher.iv.length;
1611
1612         switch (xform->cipher.algo) {
1613         case RTE_CRYPTO_CIPHER_AES_CBC:
1614                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1615                 cipherdata.algmode = OP_ALG_AAI_CBC;
1616                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
1617                 break;
1618         case RTE_CRYPTO_CIPHER_3DES_CBC:
1619                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
1620                 cipherdata.algmode = OP_ALG_AAI_CBC;
1621                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
1622                 break;
1623         case RTE_CRYPTO_CIPHER_AES_CTR:
1624                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1625                 cipherdata.algmode = OP_ALG_AAI_CTR;
1626                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
1627                 break;
1628         case RTE_CRYPTO_CIPHER_3DES_CTR:
1629         case RTE_CRYPTO_CIPHER_AES_ECB:
1630         case RTE_CRYPTO_CIPHER_3DES_ECB:
1631         case RTE_CRYPTO_CIPHER_AES_XTS:
1632         case RTE_CRYPTO_CIPHER_AES_F8:
1633         case RTE_CRYPTO_CIPHER_ARC4:
1634         case RTE_CRYPTO_CIPHER_KASUMI_F8:
1635         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1636         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
1637         case RTE_CRYPTO_CIPHER_NULL:
1638                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
1639                         xform->cipher.algo);
1640                 goto error_out;
1641         default:
1642                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
1643                         xform->cipher.algo);
1644                 goto error_out;
1645         }
1646         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1647                                 DIR_ENC : DIR_DEC;
1648
1649         bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
1650                                         &cipherdata, NULL, session->iv.length,
1651                                         session->dir);
1652         if (bufsize < 0) {
1653                 DPAA2_SEC_ERR("Crypto: Descriptor build failed");
1654                 goto error_out;
1655         }
1656         flc->dhr = 0;
1657         flc->bpv0 = 0x1;
1658         flc->mode_bits = 0x8000;
1659
1660         flc->word1_sdl = (uint8_t)bufsize;
1661         flc->word2_rflc_31_0 = lower_32_bits(
1662                         (size_t)&(((struct dpaa2_sec_qp *)
1663                         dev->data->queue_pairs[0])->rx_vq));
1664         flc->word3_rflc_63_32 = upper_32_bits(
1665                         (size_t)&(((struct dpaa2_sec_qp *)
1666                         dev->data->queue_pairs[0])->rx_vq));
1667         session->ctxt = priv;
1668
1669         for (i = 0; i < bufsize; i++)
1670                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x", i, priv->flc_desc[0].desc[i]);
1671
1672         return 0;
1673
1674 error_out:
1675         rte_free(session->cipher_key.data);
1676         rte_free(priv);
1677         return -1;
1678 }
1679
1680 static int
1681 dpaa2_sec_auth_init(struct rte_cryptodev *dev,
1682                     struct rte_crypto_sym_xform *xform,
1683                     dpaa2_sec_session *session)
1684 {
1685         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1686         struct alginfo authdata;
1687         int bufsize, i;
1688         struct ctxt_priv *priv;
1689         struct sec_flow_context *flc;
1690
1691         PMD_INIT_FUNC_TRACE();
1692
1693         /* For SEC AUTH three descriptors are required for various stages */
1694         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1695                         sizeof(struct ctxt_priv) + 3 *
1696                         sizeof(struct sec_flc_desc),
1697                         RTE_CACHE_LINE_SIZE);
1698         if (priv == NULL) {
1699                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1700                 return -1;
1701         }
1702
1703         priv->fle_pool = dev_priv->fle_pool;
1704         flc = &priv->flc_desc[DESC_INITFINAL].flc;
1705
1706         session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1707                         RTE_CACHE_LINE_SIZE);
1708         if (session->auth_key.data == NULL) {
1709                 DPAA2_SEC_ERR("Unable to allocate memory for auth key");
1710                 rte_free(priv);
1711                 return -1;
1712         }
1713         session->auth_key.length = xform->auth.key.length;
1714
1715         memcpy(session->auth_key.data, xform->auth.key.data,
1716                xform->auth.key.length);
1717         authdata.key = (size_t)session->auth_key.data;
1718         authdata.keylen = session->auth_key.length;
1719         authdata.key_enc_flags = 0;
1720         authdata.key_type = RTA_DATA_IMM;
1721
1722         session->digest_length = xform->auth.digest_length;
1723
1724         switch (xform->auth.algo) {
1725         case RTE_CRYPTO_AUTH_SHA1_HMAC:
1726                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
1727                 authdata.algmode = OP_ALG_AAI_HMAC;
1728                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
1729                 break;
1730         case RTE_CRYPTO_AUTH_MD5_HMAC:
1731                 authdata.algtype = OP_ALG_ALGSEL_MD5;
1732                 authdata.algmode = OP_ALG_AAI_HMAC;
1733                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
1734                 break;
1735         case RTE_CRYPTO_AUTH_SHA256_HMAC:
1736                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
1737                 authdata.algmode = OP_ALG_AAI_HMAC;
1738                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
1739                 break;
1740         case RTE_CRYPTO_AUTH_SHA384_HMAC:
1741                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
1742                 authdata.algmode = OP_ALG_AAI_HMAC;
1743                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
1744                 break;
1745         case RTE_CRYPTO_AUTH_SHA512_HMAC:
1746                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
1747                 authdata.algmode = OP_ALG_AAI_HMAC;
1748                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
1749                 break;
1750         case RTE_CRYPTO_AUTH_SHA224_HMAC:
1751                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
1752                 authdata.algmode = OP_ALG_AAI_HMAC;
1753                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
1754                 break;
1755         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
1756         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
1757         case RTE_CRYPTO_AUTH_NULL:
1758         case RTE_CRYPTO_AUTH_SHA1:
1759         case RTE_CRYPTO_AUTH_SHA256:
1760         case RTE_CRYPTO_AUTH_SHA512:
1761         case RTE_CRYPTO_AUTH_SHA224:
1762         case RTE_CRYPTO_AUTH_SHA384:
1763         case RTE_CRYPTO_AUTH_MD5:
1764         case RTE_CRYPTO_AUTH_AES_GMAC:
1765         case RTE_CRYPTO_AUTH_KASUMI_F9:
1766         case RTE_CRYPTO_AUTH_AES_CMAC:
1767         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
1768         case RTE_CRYPTO_AUTH_ZUC_EIA3:
1769                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %un",
1770                               xform->auth.algo);
1771                 goto error_out;
1772         default:
1773                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
1774                               xform->auth.algo);
1775                 goto error_out;
1776         }
1777         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1778                                 DIR_ENC : DIR_DEC;
1779
1780         bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
1781                                    1, 0, &authdata, !session->dir,
1782                                    session->digest_length);
1783         if (bufsize < 0) {
1784                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
1785                 goto error_out;
1786         }
1787
1788         flc->word1_sdl = (uint8_t)bufsize;
1789         flc->word2_rflc_31_0 = lower_32_bits(
1790                         (size_t)&(((struct dpaa2_sec_qp *)
1791                         dev->data->queue_pairs[0])->rx_vq));
1792         flc->word3_rflc_63_32 = upper_32_bits(
1793                         (size_t)&(((struct dpaa2_sec_qp *)
1794                         dev->data->queue_pairs[0])->rx_vq));
1795         session->ctxt = priv;
1796         for (i = 0; i < bufsize; i++)
1797                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
1798                                 i, priv->flc_desc[DESC_INITFINAL].desc[i]);
1799
1800
1801         return 0;
1802
1803 error_out:
1804         rte_free(session->auth_key.data);
1805         rte_free(priv);
1806         return -1;
1807 }
1808
1809 static int
1810 dpaa2_sec_aead_init(struct rte_cryptodev *dev,
1811                     struct rte_crypto_sym_xform *xform,
1812                     dpaa2_sec_session *session)
1813 {
1814         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
1815         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1816         struct alginfo aeaddata;
1817         int bufsize, i;
1818         struct ctxt_priv *priv;
1819         struct sec_flow_context *flc;
1820         struct rte_crypto_aead_xform *aead_xform = &xform->aead;
1821         int err;
1822
1823         PMD_INIT_FUNC_TRACE();
1824
1825         /* Set IV parameters */
1826         session->iv.offset = aead_xform->iv.offset;
1827         session->iv.length = aead_xform->iv.length;
1828         session->ctxt_type = DPAA2_SEC_AEAD;
1829
1830         /* For SEC AEAD only one descriptor is required */
1831         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1832                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1833                         RTE_CACHE_LINE_SIZE);
1834         if (priv == NULL) {
1835                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1836                 return -1;
1837         }
1838
1839         priv->fle_pool = dev_priv->fle_pool;
1840         flc = &priv->flc_desc[0].flc;
1841
1842         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
1843                                                RTE_CACHE_LINE_SIZE);
1844         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
1845                 DPAA2_SEC_ERR("No Memory for aead key");
1846                 rte_free(priv);
1847                 return -1;
1848         }
1849         memcpy(session->aead_key.data, aead_xform->key.data,
1850                aead_xform->key.length);
1851
1852         session->digest_length = aead_xform->digest_length;
1853         session->aead_key.length = aead_xform->key.length;
1854         ctxt->auth_only_len = aead_xform->aad_length;
1855
1856         aeaddata.key = (size_t)session->aead_key.data;
1857         aeaddata.keylen = session->aead_key.length;
1858         aeaddata.key_enc_flags = 0;
1859         aeaddata.key_type = RTA_DATA_IMM;
1860
1861         switch (aead_xform->algo) {
1862         case RTE_CRYPTO_AEAD_AES_GCM:
1863                 aeaddata.algtype = OP_ALG_ALGSEL_AES;
1864                 aeaddata.algmode = OP_ALG_AAI_GCM;
1865                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
1866                 break;
1867         case RTE_CRYPTO_AEAD_AES_CCM:
1868                 DPAA2_SEC_ERR("Crypto: Unsupported AEAD alg %u",
1869                               aead_xform->algo);
1870                 goto error_out;
1871         default:
1872                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
1873                               aead_xform->algo);
1874                 goto error_out;
1875         }
1876         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1877                                 DIR_ENC : DIR_DEC;
1878
1879         priv->flc_desc[0].desc[0] = aeaddata.keylen;
1880         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
1881                                MIN_JOB_DESC_SIZE,
1882                                (unsigned int *)priv->flc_desc[0].desc,
1883                                &priv->flc_desc[0].desc[1], 1);
1884
1885         if (err < 0) {
1886                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
1887                 goto error_out;
1888         }
1889         if (priv->flc_desc[0].desc[1] & 1) {
1890                 aeaddata.key_type = RTA_DATA_IMM;
1891         } else {
1892                 aeaddata.key = DPAA2_VADDR_TO_IOVA(aeaddata.key);
1893                 aeaddata.key_type = RTA_DATA_PTR;
1894         }
1895         priv->flc_desc[0].desc[0] = 0;
1896         priv->flc_desc[0].desc[1] = 0;
1897
1898         if (session->dir == DIR_ENC)
1899                 bufsize = cnstr_shdsc_gcm_encap(
1900                                 priv->flc_desc[0].desc, 1, 0,
1901                                 &aeaddata, session->iv.length,
1902                                 session->digest_length);
1903         else
1904                 bufsize = cnstr_shdsc_gcm_decap(
1905                                 priv->flc_desc[0].desc, 1, 0,
1906                                 &aeaddata, session->iv.length,
1907                                 session->digest_length);
1908         if (bufsize < 0) {
1909                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
1910                 goto error_out;
1911         }
1912
1913         flc->word1_sdl = (uint8_t)bufsize;
1914         flc->word2_rflc_31_0 = lower_32_bits(
1915                         (size_t)&(((struct dpaa2_sec_qp *)
1916                         dev->data->queue_pairs[0])->rx_vq));
1917         flc->word3_rflc_63_32 = upper_32_bits(
1918                         (size_t)&(((struct dpaa2_sec_qp *)
1919                         dev->data->queue_pairs[0])->rx_vq));
1920         session->ctxt = priv;
1921         for (i = 0; i < bufsize; i++)
1922                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x\n",
1923                             i, priv->flc_desc[0].desc[i]);
1924
1925         return 0;
1926
1927 error_out:
1928         rte_free(session->aead_key.data);
1929         rte_free(priv);
1930         return -1;
1931 }
1932
1933
1934 static int
1935 dpaa2_sec_aead_chain_init(struct rte_cryptodev *dev,
1936                     struct rte_crypto_sym_xform *xform,
1937                     dpaa2_sec_session *session)
1938 {
1939         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
1940         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
1941         struct alginfo authdata, cipherdata;
1942         int bufsize, i;
1943         struct ctxt_priv *priv;
1944         struct sec_flow_context *flc;
1945         struct rte_crypto_cipher_xform *cipher_xform;
1946         struct rte_crypto_auth_xform *auth_xform;
1947         int err;
1948
1949         PMD_INIT_FUNC_TRACE();
1950
1951         if (session->ext_params.aead_ctxt.auth_cipher_text) {
1952                 cipher_xform = &xform->cipher;
1953                 auth_xform = &xform->next->auth;
1954                 session->ctxt_type =
1955                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1956                         DPAA2_SEC_CIPHER_HASH : DPAA2_SEC_HASH_CIPHER;
1957         } else {
1958                 cipher_xform = &xform->next->cipher;
1959                 auth_xform = &xform->auth;
1960                 session->ctxt_type =
1961                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1962                         DPAA2_SEC_HASH_CIPHER : DPAA2_SEC_CIPHER_HASH;
1963         }
1964
1965         /* Set IV parameters */
1966         session->iv.offset = cipher_xform->iv.offset;
1967         session->iv.length = cipher_xform->iv.length;
1968
1969         /* For SEC AEAD only one descriptor is required */
1970         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1971                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1972                         RTE_CACHE_LINE_SIZE);
1973         if (priv == NULL) {
1974                 DPAA2_SEC_ERR("No Memory for priv CTXT");
1975                 return -1;
1976         }
1977
1978         priv->fle_pool = dev_priv->fle_pool;
1979         flc = &priv->flc_desc[0].flc;
1980
1981         session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
1982                                                RTE_CACHE_LINE_SIZE);
1983         if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
1984                 DPAA2_SEC_ERR("No Memory for cipher key");
1985                 rte_free(priv);
1986                 return -1;
1987         }
1988         session->cipher_key.length = cipher_xform->key.length;
1989         session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
1990                                              RTE_CACHE_LINE_SIZE);
1991         if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
1992                 DPAA2_SEC_ERR("No Memory for auth key");
1993                 rte_free(session->cipher_key.data);
1994                 rte_free(priv);
1995                 return -1;
1996         }
1997         session->auth_key.length = auth_xform->key.length;
1998         memcpy(session->cipher_key.data, cipher_xform->key.data,
1999                cipher_xform->key.length);
2000         memcpy(session->auth_key.data, auth_xform->key.data,
2001                auth_xform->key.length);
2002
2003         authdata.key = (size_t)session->auth_key.data;
2004         authdata.keylen = session->auth_key.length;
2005         authdata.key_enc_flags = 0;
2006         authdata.key_type = RTA_DATA_IMM;
2007
2008         session->digest_length = auth_xform->digest_length;
2009
2010         switch (auth_xform->algo) {
2011         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2012                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2013                 authdata.algmode = OP_ALG_AAI_HMAC;
2014                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2015                 break;
2016         case RTE_CRYPTO_AUTH_MD5_HMAC:
2017                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2018                 authdata.algmode = OP_ALG_AAI_HMAC;
2019                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2020                 break;
2021         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2022                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2023                 authdata.algmode = OP_ALG_AAI_HMAC;
2024                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2025                 break;
2026         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2027                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2028                 authdata.algmode = OP_ALG_AAI_HMAC;
2029                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2030                 break;
2031         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2032                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2033                 authdata.algmode = OP_ALG_AAI_HMAC;
2034                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2035                 break;
2036         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2037                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2038                 authdata.algmode = OP_ALG_AAI_HMAC;
2039                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2040                 break;
2041         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2042         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2043         case RTE_CRYPTO_AUTH_NULL:
2044         case RTE_CRYPTO_AUTH_SHA1:
2045         case RTE_CRYPTO_AUTH_SHA256:
2046         case RTE_CRYPTO_AUTH_SHA512:
2047         case RTE_CRYPTO_AUTH_SHA224:
2048         case RTE_CRYPTO_AUTH_SHA384:
2049         case RTE_CRYPTO_AUTH_MD5:
2050         case RTE_CRYPTO_AUTH_AES_GMAC:
2051         case RTE_CRYPTO_AUTH_KASUMI_F9:
2052         case RTE_CRYPTO_AUTH_AES_CMAC:
2053         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2054         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2055                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2056                               auth_xform->algo);
2057                 goto error_out;
2058         default:
2059                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2060                               auth_xform->algo);
2061                 goto error_out;
2062         }
2063         cipherdata.key = (size_t)session->cipher_key.data;
2064         cipherdata.keylen = session->cipher_key.length;
2065         cipherdata.key_enc_flags = 0;
2066         cipherdata.key_type = RTA_DATA_IMM;
2067
2068         switch (cipher_xform->algo) {
2069         case RTE_CRYPTO_CIPHER_AES_CBC:
2070                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2071                 cipherdata.algmode = OP_ALG_AAI_CBC;
2072                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
2073                 break;
2074         case RTE_CRYPTO_CIPHER_3DES_CBC:
2075                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
2076                 cipherdata.algmode = OP_ALG_AAI_CBC;
2077                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
2078                 break;
2079         case RTE_CRYPTO_CIPHER_AES_CTR:
2080                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2081                 cipherdata.algmode = OP_ALG_AAI_CTR;
2082                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
2083                 break;
2084         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2085         case RTE_CRYPTO_CIPHER_NULL:
2086         case RTE_CRYPTO_CIPHER_3DES_ECB:
2087         case RTE_CRYPTO_CIPHER_AES_ECB:
2088         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2089                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2090                               cipher_xform->algo);
2091                 goto error_out;
2092         default:
2093                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2094                               cipher_xform->algo);
2095                 goto error_out;
2096         }
2097         session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2098                                 DIR_ENC : DIR_DEC;
2099
2100         priv->flc_desc[0].desc[0] = cipherdata.keylen;
2101         priv->flc_desc[0].desc[1] = authdata.keylen;
2102         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2103                                MIN_JOB_DESC_SIZE,
2104                                (unsigned int *)priv->flc_desc[0].desc,
2105                                &priv->flc_desc[0].desc[2], 2);
2106
2107         if (err < 0) {
2108                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2109                 goto error_out;
2110         }
2111         if (priv->flc_desc[0].desc[2] & 1) {
2112                 cipherdata.key_type = RTA_DATA_IMM;
2113         } else {
2114                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
2115                 cipherdata.key_type = RTA_DATA_PTR;
2116         }
2117         if (priv->flc_desc[0].desc[2] & (1 << 1)) {
2118                 authdata.key_type = RTA_DATA_IMM;
2119         } else {
2120                 authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
2121                 authdata.key_type = RTA_DATA_PTR;
2122         }
2123         priv->flc_desc[0].desc[0] = 0;
2124         priv->flc_desc[0].desc[1] = 0;
2125         priv->flc_desc[0].desc[2] = 0;
2126
2127         if (session->ctxt_type == DPAA2_SEC_CIPHER_HASH) {
2128                 bufsize = cnstr_shdsc_authenc(priv->flc_desc[0].desc, 1,
2129                                               0, &cipherdata, &authdata,
2130                                               session->iv.length,
2131                                               ctxt->auth_only_len,
2132                                               session->digest_length,
2133                                               session->dir);
2134                 if (bufsize < 0) {
2135                         DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2136                         goto error_out;
2137                 }
2138         } else {
2139                 DPAA2_SEC_ERR("Hash before cipher not supported");
2140                 goto error_out;
2141         }
2142
2143         flc->word1_sdl = (uint8_t)bufsize;
2144         flc->word2_rflc_31_0 = lower_32_bits(
2145                         (size_t)&(((struct dpaa2_sec_qp *)
2146                         dev->data->queue_pairs[0])->rx_vq));
2147         flc->word3_rflc_63_32 = upper_32_bits(
2148                         (size_t)&(((struct dpaa2_sec_qp *)
2149                         dev->data->queue_pairs[0])->rx_vq));
2150         session->ctxt = priv;
2151         for (i = 0; i < bufsize; i++)
2152                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2153                             i, priv->flc_desc[0].desc[i]);
2154
2155         return 0;
2156
2157 error_out:
2158         rte_free(session->cipher_key.data);
2159         rte_free(session->auth_key.data);
2160         rte_free(priv);
2161         return -1;
2162 }
2163
2164 static int
2165 dpaa2_sec_set_session_parameters(struct rte_cryptodev *dev,
2166                             struct rte_crypto_sym_xform *xform, void *sess)
2167 {
2168         dpaa2_sec_session *session = sess;
2169
2170         PMD_INIT_FUNC_TRACE();
2171
2172         if (unlikely(sess == NULL)) {
2173                 DPAA2_SEC_ERR("Invalid session struct");
2174                 return -1;
2175         }
2176
2177         memset(session, 0, sizeof(dpaa2_sec_session));
2178         /* Default IV length = 0 */
2179         session->iv.length = 0;
2180
2181         /* Cipher Only */
2182         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2183                 session->ctxt_type = DPAA2_SEC_CIPHER;
2184                 dpaa2_sec_cipher_init(dev, xform, session);
2185
2186         /* Authentication Only */
2187         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2188                    xform->next == NULL) {
2189                 session->ctxt_type = DPAA2_SEC_AUTH;
2190                 dpaa2_sec_auth_init(dev, xform, session);
2191
2192         /* Cipher then Authenticate */
2193         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2194                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2195                 session->ext_params.aead_ctxt.auth_cipher_text = true;
2196                 dpaa2_sec_aead_chain_init(dev, xform, session);
2197
2198         /* Authenticate then Cipher */
2199         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2200                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2201                 session->ext_params.aead_ctxt.auth_cipher_text = false;
2202                 dpaa2_sec_aead_chain_init(dev, xform, session);
2203
2204         /* AEAD operation for AES-GCM kind of Algorithms */
2205         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2206                    xform->next == NULL) {
2207                 dpaa2_sec_aead_init(dev, xform, session);
2208
2209         } else {
2210                 DPAA2_SEC_ERR("Invalid crypto type");
2211                 return -EINVAL;
2212         }
2213
2214         return 0;
2215 }
2216
2217 static int
2218 dpaa2_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2219                         dpaa2_sec_session *session,
2220                         struct alginfo *aeaddata)
2221 {
2222         PMD_INIT_FUNC_TRACE();
2223
2224         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2225                                                RTE_CACHE_LINE_SIZE);
2226         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2227                 DPAA2_SEC_ERR("No Memory for aead key");
2228                 return -1;
2229         }
2230         memcpy(session->aead_key.data, aead_xform->key.data,
2231                aead_xform->key.length);
2232
2233         session->digest_length = aead_xform->digest_length;
2234         session->aead_key.length = aead_xform->key.length;
2235
2236         aeaddata->key = (size_t)session->aead_key.data;
2237         aeaddata->keylen = session->aead_key.length;
2238         aeaddata->key_enc_flags = 0;
2239         aeaddata->key_type = RTA_DATA_IMM;
2240
2241         switch (aead_xform->algo) {
2242         case RTE_CRYPTO_AEAD_AES_GCM:
2243                 aeaddata->algtype = OP_ALG_ALGSEL_AES;
2244                 aeaddata->algmode = OP_ALG_AAI_GCM;
2245                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2246                 break;
2247         case RTE_CRYPTO_AEAD_AES_CCM:
2248                 aeaddata->algtype = OP_ALG_ALGSEL_AES;
2249                 aeaddata->algmode = OP_ALG_AAI_CCM;
2250                 session->aead_alg = RTE_CRYPTO_AEAD_AES_CCM;
2251                 break;
2252         default:
2253                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2254                               aead_xform->algo);
2255                 return -1;
2256         }
2257         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2258                                 DIR_ENC : DIR_DEC;
2259
2260         return 0;
2261 }
2262
2263 static int
2264 dpaa2_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2265         struct rte_crypto_auth_xform *auth_xform,
2266         dpaa2_sec_session *session,
2267         struct alginfo *cipherdata,
2268         struct alginfo *authdata)
2269 {
2270         if (cipher_xform) {
2271                 session->cipher_key.data = rte_zmalloc(NULL,
2272                                                        cipher_xform->key.length,
2273                                                        RTE_CACHE_LINE_SIZE);
2274                 if (session->cipher_key.data == NULL &&
2275                                 cipher_xform->key.length > 0) {
2276                         DPAA2_SEC_ERR("No Memory for cipher key");
2277                         return -ENOMEM;
2278                 }
2279
2280                 session->cipher_key.length = cipher_xform->key.length;
2281                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2282                                 cipher_xform->key.length);
2283                 session->cipher_alg = cipher_xform->algo;
2284         } else {
2285                 session->cipher_key.data = NULL;
2286                 session->cipher_key.length = 0;
2287                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2288         }
2289
2290         if (auth_xform) {
2291                 session->auth_key.data = rte_zmalloc(NULL,
2292                                                 auth_xform->key.length,
2293                                                 RTE_CACHE_LINE_SIZE);
2294                 if (session->auth_key.data == NULL &&
2295                                 auth_xform->key.length > 0) {
2296                         DPAA2_SEC_ERR("No Memory for auth key");
2297                         return -ENOMEM;
2298                 }
2299                 session->auth_key.length = auth_xform->key.length;
2300                 memcpy(session->auth_key.data, auth_xform->key.data,
2301                                 auth_xform->key.length);
2302                 session->auth_alg = auth_xform->algo;
2303         } else {
2304                 session->auth_key.data = NULL;
2305                 session->auth_key.length = 0;
2306                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2307         }
2308
2309         authdata->key = (size_t)session->auth_key.data;
2310         authdata->keylen = session->auth_key.length;
2311         authdata->key_enc_flags = 0;
2312         authdata->key_type = RTA_DATA_IMM;
2313         switch (session->auth_alg) {
2314         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2315                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA1_96;
2316                 authdata->algmode = OP_ALG_AAI_HMAC;
2317                 break;
2318         case RTE_CRYPTO_AUTH_MD5_HMAC:
2319                 authdata->algtype = OP_PCL_IPSEC_HMAC_MD5_96;
2320                 authdata->algmode = OP_ALG_AAI_HMAC;
2321                 break;
2322         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2323                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2324                 authdata->algmode = OP_ALG_AAI_HMAC;
2325                 break;
2326         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2327                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2328                 authdata->algmode = OP_ALG_AAI_HMAC;
2329                 break;
2330         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2331                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2332                 authdata->algmode = OP_ALG_AAI_HMAC;
2333                 break;
2334         case RTE_CRYPTO_AUTH_AES_CMAC:
2335                 authdata->algtype = OP_PCL_IPSEC_AES_CMAC_96;
2336                 break;
2337         case RTE_CRYPTO_AUTH_NULL:
2338                 authdata->algtype = OP_PCL_IPSEC_HMAC_NULL;
2339                 break;
2340         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2341         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2342         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2343         case RTE_CRYPTO_AUTH_SHA1:
2344         case RTE_CRYPTO_AUTH_SHA256:
2345         case RTE_CRYPTO_AUTH_SHA512:
2346         case RTE_CRYPTO_AUTH_SHA224:
2347         case RTE_CRYPTO_AUTH_SHA384:
2348         case RTE_CRYPTO_AUTH_MD5:
2349         case RTE_CRYPTO_AUTH_AES_GMAC:
2350         case RTE_CRYPTO_AUTH_KASUMI_F9:
2351         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2352         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2353                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2354                               session->auth_alg);
2355                 return -1;
2356         default:
2357                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2358                               session->auth_alg);
2359                 return -1;
2360         }
2361         cipherdata->key = (size_t)session->cipher_key.data;
2362         cipherdata->keylen = session->cipher_key.length;
2363         cipherdata->key_enc_flags = 0;
2364         cipherdata->key_type = RTA_DATA_IMM;
2365
2366         switch (session->cipher_alg) {
2367         case RTE_CRYPTO_CIPHER_AES_CBC:
2368                 cipherdata->algtype = OP_PCL_IPSEC_AES_CBC;
2369                 cipherdata->algmode = OP_ALG_AAI_CBC;
2370                 break;
2371         case RTE_CRYPTO_CIPHER_3DES_CBC:
2372                 cipherdata->algtype = OP_PCL_IPSEC_3DES;
2373                 cipherdata->algmode = OP_ALG_AAI_CBC;
2374                 break;
2375         case RTE_CRYPTO_CIPHER_AES_CTR:
2376                 cipherdata->algtype = OP_PCL_IPSEC_AES_CTR;
2377                 cipherdata->algmode = OP_ALG_AAI_CTR;
2378                 break;
2379         case RTE_CRYPTO_CIPHER_NULL:
2380                 cipherdata->algtype = OP_PCL_IPSEC_NULL;
2381                 break;
2382         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2383         case RTE_CRYPTO_CIPHER_3DES_ECB:
2384         case RTE_CRYPTO_CIPHER_AES_ECB:
2385         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2386                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2387                               session->cipher_alg);
2388                 return -1;
2389         default:
2390                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2391                               session->cipher_alg);
2392                 return -1;
2393         }
2394
2395         return 0;
2396 }
2397
2398 #ifdef RTE_LIBRTE_SECURITY_TEST
2399 static uint8_t aes_cbc_iv[] = {
2400         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2401         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
2402 #endif
2403
2404 static int
2405 dpaa2_sec_set_ipsec_session(struct rte_cryptodev *dev,
2406                             struct rte_security_session_conf *conf,
2407                             void *sess)
2408 {
2409         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2410         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2411         struct rte_crypto_auth_xform *auth_xform = NULL;
2412         struct rte_crypto_aead_xform *aead_xform = NULL;
2413         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
2414         struct ctxt_priv *priv;
2415         struct ipsec_encap_pdb encap_pdb;
2416         struct ipsec_decap_pdb decap_pdb;
2417         struct alginfo authdata, cipherdata;
2418         int bufsize;
2419         struct sec_flow_context *flc;
2420         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2421         int ret = -1;
2422
2423         PMD_INIT_FUNC_TRACE();
2424
2425         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2426                                 sizeof(struct ctxt_priv) +
2427                                 sizeof(struct sec_flc_desc),
2428                                 RTE_CACHE_LINE_SIZE);
2429
2430         if (priv == NULL) {
2431                 DPAA2_SEC_ERR("No memory for priv CTXT");
2432                 return -ENOMEM;
2433         }
2434
2435         priv->fle_pool = dev_priv->fle_pool;
2436         flc = &priv->flc_desc[0].flc;
2437
2438         memset(session, 0, sizeof(dpaa2_sec_session));
2439
2440         if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2441                 cipher_xform = &conf->crypto_xform->cipher;
2442                 if (conf->crypto_xform->next)
2443                         auth_xform = &conf->crypto_xform->next->auth;
2444                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2445                                         session, &cipherdata, &authdata);
2446         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2447                 auth_xform = &conf->crypto_xform->auth;
2448                 if (conf->crypto_xform->next)
2449                         cipher_xform = &conf->crypto_xform->next->cipher;
2450                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
2451                                         session, &cipherdata, &authdata);
2452         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
2453                 aead_xform = &conf->crypto_xform->aead;
2454                 ret = dpaa2_sec_ipsec_aead_init(aead_xform,
2455                                         session, &cipherdata);
2456         } else {
2457                 DPAA2_SEC_ERR("XFORM not specified");
2458                 ret = -EINVAL;
2459                 goto out;
2460         }
2461         if (ret) {
2462                 DPAA2_SEC_ERR("Failed to process xform");
2463                 goto out;
2464         }
2465
2466         session->ctxt_type = DPAA2_SEC_IPSEC;
2467         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2468                 struct ip ip4_hdr;
2469
2470                 flc->dhr = SEC_FLC_DHR_OUTBOUND;
2471                 ip4_hdr.ip_v = IPVERSION;
2472                 ip4_hdr.ip_hl = 5;
2473                 ip4_hdr.ip_len = rte_cpu_to_be_16(sizeof(ip4_hdr));
2474                 ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2475                 ip4_hdr.ip_id = 0;
2476                 ip4_hdr.ip_off = 0;
2477                 ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2478                 ip4_hdr.ip_p = IPPROTO_ESP;
2479                 ip4_hdr.ip_sum = 0;
2480                 ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
2481                 ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
2482                 ip4_hdr.ip_sum = calc_chksum((uint16_t *)(void *)&ip4_hdr,
2483                         sizeof(struct ip));
2484
2485                 /* For Sec Proto only one descriptor is required. */
2486                 memset(&encap_pdb, 0, sizeof(struct ipsec_encap_pdb));
2487                 encap_pdb.options = (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2488                         PDBOPTS_ESP_OIHI_PDB_INL |
2489                         PDBOPTS_ESP_IVSRC |
2490                         PDBHMO_ESP_ENCAP_DTTL |
2491                         PDBHMO_ESP_SNR;
2492                 encap_pdb.spi = ipsec_xform->spi;
2493                 encap_pdb.ip_hdr_len = sizeof(struct ip);
2494
2495                 session->dir = DIR_ENC;
2496                 bufsize = cnstr_shdsc_ipsec_new_encap(priv->flc_desc[0].desc,
2497                                 1, 0, SHR_SERIAL, &encap_pdb,
2498                                 (uint8_t *)&ip4_hdr,
2499                                 &cipherdata, &authdata);
2500         } else if (ipsec_xform->direction ==
2501                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2502                 flc->dhr = SEC_FLC_DHR_INBOUND;
2503                 memset(&decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2504                 decap_pdb.options = sizeof(struct ip) << 16;
2505                 session->dir = DIR_DEC;
2506                 bufsize = cnstr_shdsc_ipsec_new_decap(priv->flc_desc[0].desc,
2507                                 1, 0, SHR_SERIAL,
2508                                 &decap_pdb, &cipherdata, &authdata);
2509         } else
2510                 goto out;
2511
2512         if (bufsize < 0) {
2513                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2514                 goto out;
2515         }
2516
2517         flc->word1_sdl = (uint8_t)bufsize;
2518
2519         /* Enable the stashing control bit */
2520         DPAA2_SET_FLC_RSC(flc);
2521         flc->word2_rflc_31_0 = lower_32_bits(
2522                         (size_t)&(((struct dpaa2_sec_qp *)
2523                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
2524         flc->word3_rflc_63_32 = upper_32_bits(
2525                         (size_t)&(((struct dpaa2_sec_qp *)
2526                         dev->data->queue_pairs[0])->rx_vq));
2527
2528         /* Set EWS bit i.e. enable write-safe */
2529         DPAA2_SET_FLC_EWS(flc);
2530         /* Set BS = 1 i.e reuse input buffers as output buffers */
2531         DPAA2_SET_FLC_REUSE_BS(flc);
2532         /* Set FF = 10; reuse input buffers if they provide sufficient space */
2533         DPAA2_SET_FLC_REUSE_FF(flc);
2534
2535         session->ctxt = priv;
2536
2537         return 0;
2538 out:
2539         rte_free(session->auth_key.data);
2540         rte_free(session->cipher_key.data);
2541         rte_free(priv);
2542         return ret;
2543 }
2544
2545 static int
2546 dpaa2_sec_security_session_create(void *dev,
2547                                   struct rte_security_session_conf *conf,
2548                                   struct rte_security_session *sess,
2549                                   struct rte_mempool *mempool)
2550 {
2551         void *sess_private_data;
2552         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2553         int ret;
2554
2555         if (rte_mempool_get(mempool, &sess_private_data)) {
2556                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
2557                 return -ENOMEM;
2558         }
2559
2560         switch (conf->protocol) {
2561         case RTE_SECURITY_PROTOCOL_IPSEC:
2562                 ret = dpaa2_sec_set_ipsec_session(cdev, conf,
2563                                 sess_private_data);
2564                 break;
2565         case RTE_SECURITY_PROTOCOL_MACSEC:
2566                 return -ENOTSUP;
2567         default:
2568                 return -EINVAL;
2569         }
2570         if (ret != 0) {
2571                 DPAA2_SEC_ERR("Failed to configure session parameters");
2572                 /* Return session to mempool */
2573                 rte_mempool_put(mempool, sess_private_data);
2574                 return ret;
2575         }
2576
2577         set_sec_session_private_data(sess, sess_private_data);
2578
2579         return ret;
2580 }
2581
2582 /** Clear the memory of session so it doesn't leave key material behind */
2583 static int
2584 dpaa2_sec_security_session_destroy(void *dev __rte_unused,
2585                 struct rte_security_session *sess)
2586 {
2587         PMD_INIT_FUNC_TRACE();
2588         void *sess_priv = get_sec_session_private_data(sess);
2589
2590         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
2591
2592         if (sess_priv) {
2593                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
2594
2595                 rte_free(s->ctxt);
2596                 rte_free(s->cipher_key.data);
2597                 rte_free(s->auth_key.data);
2598                 memset(sess, 0, sizeof(dpaa2_sec_session));
2599                 set_sec_session_private_data(sess, NULL);
2600                 rte_mempool_put(sess_mp, sess_priv);
2601         }
2602         return 0;
2603 }
2604
2605 static int
2606 dpaa2_sec_sym_session_configure(struct rte_cryptodev *dev,
2607                 struct rte_crypto_sym_xform *xform,
2608                 struct rte_cryptodev_sym_session *sess,
2609                 struct rte_mempool *mempool)
2610 {
2611         void *sess_private_data;
2612         int ret;
2613
2614         if (rte_mempool_get(mempool, &sess_private_data)) {
2615                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
2616                 return -ENOMEM;
2617         }
2618
2619         ret = dpaa2_sec_set_session_parameters(dev, xform, sess_private_data);
2620         if (ret != 0) {
2621                 DPAA2_SEC_ERR("Failed to configure session parameters");
2622                 /* Return session to mempool */
2623                 rte_mempool_put(mempool, sess_private_data);
2624                 return ret;
2625         }
2626
2627         set_sym_session_private_data(sess, dev->driver_id,
2628                 sess_private_data);
2629
2630         return 0;
2631 }
2632
2633 /** Clear the memory of session so it doesn't leave key material behind */
2634 static void
2635 dpaa2_sec_sym_session_clear(struct rte_cryptodev *dev,
2636                 struct rte_cryptodev_sym_session *sess)
2637 {
2638         PMD_INIT_FUNC_TRACE();
2639         uint8_t index = dev->driver_id;
2640         void *sess_priv = get_sym_session_private_data(sess, index);
2641         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
2642
2643         if (sess_priv) {
2644                 rte_free(s->ctxt);
2645                 rte_free(s->cipher_key.data);
2646                 rte_free(s->auth_key.data);
2647                 memset(sess, 0, sizeof(dpaa2_sec_session));
2648                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
2649                 set_sym_session_private_data(sess, index, NULL);
2650                 rte_mempool_put(sess_mp, sess_priv);
2651         }
2652 }
2653
2654 static int
2655 dpaa2_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2656                         struct rte_cryptodev_config *config __rte_unused)
2657 {
2658         PMD_INIT_FUNC_TRACE();
2659
2660         return 0;
2661 }
2662
2663 static int
2664 dpaa2_sec_dev_start(struct rte_cryptodev *dev)
2665 {
2666         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
2667         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
2668         struct dpseci_attr attr;
2669         struct dpaa2_queue *dpaa2_q;
2670         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
2671                                         dev->data->queue_pairs;
2672         struct dpseci_rx_queue_attr rx_attr;
2673         struct dpseci_tx_queue_attr tx_attr;
2674         int ret, i;
2675
2676         PMD_INIT_FUNC_TRACE();
2677
2678         memset(&attr, 0, sizeof(struct dpseci_attr));
2679
2680         ret = dpseci_enable(dpseci, CMD_PRI_LOW, priv->token);
2681         if (ret) {
2682                 DPAA2_SEC_ERR("DPSECI with HW_ID = %d ENABLE FAILED",
2683                               priv->hw_id);
2684                 goto get_attr_failure;
2685         }
2686         ret = dpseci_get_attributes(dpseci, CMD_PRI_LOW, priv->token, &attr);
2687         if (ret) {
2688                 DPAA2_SEC_ERR("DPSEC ATTRIBUTE READ FAILED, disabling DPSEC");
2689                 goto get_attr_failure;
2690         }
2691         for (i = 0; i < attr.num_rx_queues && qp[i]; i++) {
2692                 dpaa2_q = &qp[i]->rx_vq;
2693                 dpseci_get_rx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
2694                                     &rx_attr);
2695                 dpaa2_q->fqid = rx_attr.fqid;
2696                 DPAA2_SEC_DEBUG("rx_fqid: %d", dpaa2_q->fqid);
2697         }
2698         for (i = 0; i < attr.num_tx_queues && qp[i]; i++) {
2699                 dpaa2_q = &qp[i]->tx_vq;
2700                 dpseci_get_tx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
2701                                     &tx_attr);
2702                 dpaa2_q->fqid = tx_attr.fqid;
2703                 DPAA2_SEC_DEBUG("tx_fqid: %d", dpaa2_q->fqid);
2704         }
2705
2706         return 0;
2707 get_attr_failure:
2708         dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
2709         return -1;
2710 }
2711
2712 static void
2713 dpaa2_sec_dev_stop(struct rte_cryptodev *dev)
2714 {
2715         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
2716         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
2717         int ret;
2718
2719         PMD_INIT_FUNC_TRACE();
2720
2721         ret = dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
2722         if (ret) {
2723                 DPAA2_SEC_ERR("Failure in disabling dpseci %d device",
2724                              priv->hw_id);
2725                 return;
2726         }
2727
2728         ret = dpseci_reset(dpseci, CMD_PRI_LOW, priv->token);
2729         if (ret < 0) {
2730                 DPAA2_SEC_ERR("SEC Device cannot be reset:Error = %0x", ret);
2731                 return;
2732         }
2733 }
2734
2735 static int
2736 dpaa2_sec_dev_close(struct rte_cryptodev *dev)
2737 {
2738         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
2739         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
2740         int ret;
2741
2742         PMD_INIT_FUNC_TRACE();
2743
2744         /* Function is reverse of dpaa2_sec_dev_init.
2745          * It does the following:
2746          * 1. Detach a DPSECI from attached resources i.e. buffer pools, dpbp_id
2747          * 2. Close the DPSECI device
2748          * 3. Free the allocated resources.
2749          */
2750
2751         /*Close the device at underlying layer*/
2752         ret = dpseci_close(dpseci, CMD_PRI_LOW, priv->token);
2753         if (ret) {
2754                 DPAA2_SEC_ERR("Failure closing dpseci device: err(%d)", ret);
2755                 return -1;
2756         }
2757
2758         /*Free the allocated memory for ethernet private data and dpseci*/
2759         priv->hw = NULL;
2760         rte_free(dpseci);
2761
2762         return 0;
2763 }
2764
2765 static void
2766 dpaa2_sec_dev_infos_get(struct rte_cryptodev *dev,
2767                         struct rte_cryptodev_info *info)
2768 {
2769         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
2770
2771         PMD_INIT_FUNC_TRACE();
2772         if (info != NULL) {
2773                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2774                 info->feature_flags = dev->feature_flags;
2775                 info->capabilities = dpaa2_sec_capabilities;
2776                 /* No limit of number of sessions */
2777                 info->sym.max_nb_sessions = 0;
2778                 info->driver_id = cryptodev_driver_id;
2779         }
2780 }
2781
2782 static
2783 void dpaa2_sec_stats_get(struct rte_cryptodev *dev,
2784                          struct rte_cryptodev_stats *stats)
2785 {
2786         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
2787         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
2788         struct dpseci_sec_counters counters = {0};
2789         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
2790                                         dev->data->queue_pairs;
2791         int ret, i;
2792
2793         PMD_INIT_FUNC_TRACE();
2794         if (stats == NULL) {
2795                 DPAA2_SEC_ERR("Invalid stats ptr NULL");
2796                 return;
2797         }
2798         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
2799                 if (qp[i] == NULL) {
2800                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
2801                         continue;
2802                 }
2803
2804                 stats->enqueued_count += qp[i]->tx_vq.tx_pkts;
2805                 stats->dequeued_count += qp[i]->rx_vq.rx_pkts;
2806                 stats->enqueue_err_count += qp[i]->tx_vq.err_pkts;
2807                 stats->dequeue_err_count += qp[i]->rx_vq.err_pkts;
2808         }
2809
2810         ret = dpseci_get_sec_counters(dpseci, CMD_PRI_LOW, priv->token,
2811                                       &counters);
2812         if (ret) {
2813                 DPAA2_SEC_ERR("SEC counters failed");
2814         } else {
2815                 DPAA2_SEC_INFO("dpseci hardware stats:"
2816                             "\n\tNum of Requests Dequeued = %" PRIu64
2817                             "\n\tNum of Outbound Encrypt Requests = %" PRIu64
2818                             "\n\tNum of Inbound Decrypt Requests = %" PRIu64
2819                             "\n\tNum of Outbound Bytes Encrypted = %" PRIu64
2820                             "\n\tNum of Outbound Bytes Protected = %" PRIu64
2821                             "\n\tNum of Inbound Bytes Decrypted = %" PRIu64
2822                             "\n\tNum of Inbound Bytes Validated = %" PRIu64,
2823                             counters.dequeued_requests,
2824                             counters.ob_enc_requests,
2825                             counters.ib_dec_requests,
2826                             counters.ob_enc_bytes,
2827                             counters.ob_prot_bytes,
2828                             counters.ib_dec_bytes,
2829                             counters.ib_valid_bytes);
2830         }
2831 }
2832
2833 static
2834 void dpaa2_sec_stats_reset(struct rte_cryptodev *dev)
2835 {
2836         int i;
2837         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
2838                                    (dev->data->queue_pairs);
2839
2840         PMD_INIT_FUNC_TRACE();
2841
2842         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
2843                 if (qp[i] == NULL) {
2844                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
2845                         continue;
2846                 }
2847                 qp[i]->tx_vq.rx_pkts = 0;
2848                 qp[i]->tx_vq.tx_pkts = 0;
2849                 qp[i]->tx_vq.err_pkts = 0;
2850                 qp[i]->rx_vq.rx_pkts = 0;
2851                 qp[i]->rx_vq.tx_pkts = 0;
2852                 qp[i]->rx_vq.err_pkts = 0;
2853         }
2854 }
2855
2856 static struct rte_cryptodev_ops crypto_ops = {
2857         .dev_configure        = dpaa2_sec_dev_configure,
2858         .dev_start            = dpaa2_sec_dev_start,
2859         .dev_stop             = dpaa2_sec_dev_stop,
2860         .dev_close            = dpaa2_sec_dev_close,
2861         .dev_infos_get        = dpaa2_sec_dev_infos_get,
2862         .stats_get            = dpaa2_sec_stats_get,
2863         .stats_reset          = dpaa2_sec_stats_reset,
2864         .queue_pair_setup     = dpaa2_sec_queue_pair_setup,
2865         .queue_pair_release   = dpaa2_sec_queue_pair_release,
2866         .queue_pair_count     = dpaa2_sec_queue_pair_count,
2867         .sym_session_get_size     = dpaa2_sec_sym_session_get_size,
2868         .sym_session_configure    = dpaa2_sec_sym_session_configure,
2869         .sym_session_clear        = dpaa2_sec_sym_session_clear,
2870 };
2871
2872 static const struct rte_security_capability *
2873 dpaa2_sec_capabilities_get(void *device __rte_unused)
2874 {
2875         return dpaa2_sec_security_cap;
2876 }
2877
2878 struct rte_security_ops dpaa2_sec_security_ops = {
2879         .session_create = dpaa2_sec_security_session_create,
2880         .session_update = NULL,
2881         .session_stats_get = NULL,
2882         .session_destroy = dpaa2_sec_security_session_destroy,
2883         .set_pkt_metadata = NULL,
2884         .capabilities_get = dpaa2_sec_capabilities_get
2885 };
2886
2887 static int
2888 dpaa2_sec_uninit(const struct rte_cryptodev *dev)
2889 {
2890         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
2891
2892         rte_free(dev->security_ctx);
2893
2894         rte_mempool_free(internals->fle_pool);
2895
2896         DPAA2_SEC_INFO("Closing DPAA2_SEC device %s on numa socket %u",
2897                        dev->data->name, rte_socket_id());
2898
2899         return 0;
2900 }
2901
2902 static int
2903 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
2904 {
2905         struct dpaa2_sec_dev_private *internals;
2906         struct rte_device *dev = cryptodev->device;
2907         struct rte_dpaa2_device *dpaa2_dev;
2908         struct rte_security_ctx *security_instance;
2909         struct fsl_mc_io *dpseci;
2910         uint16_t token;
2911         struct dpseci_attr attr;
2912         int retcode, hw_id;
2913         char str[20];
2914
2915         PMD_INIT_FUNC_TRACE();
2916         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
2917         if (dpaa2_dev == NULL) {
2918                 DPAA2_SEC_ERR("DPAA2 SEC device not found");
2919                 return -1;
2920         }
2921         hw_id = dpaa2_dev->object_id;
2922
2923         cryptodev->driver_id = cryptodev_driver_id;
2924         cryptodev->dev_ops = &crypto_ops;
2925
2926         cryptodev->enqueue_burst = dpaa2_sec_enqueue_burst;
2927         cryptodev->dequeue_burst = dpaa2_sec_dequeue_burst;
2928         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2929                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
2930                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2931                         RTE_CRYPTODEV_FF_SECURITY |
2932                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2933                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2934                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2935                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2936                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2937
2938         internals = cryptodev->data->dev_private;
2939
2940         /*
2941          * For secondary processes, we don't initialise any further as primary
2942          * has already done this work. Only check we don't need a different
2943          * RX function
2944          */
2945         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2946                 DPAA2_SEC_DEBUG("Device already init by primary process");
2947                 return 0;
2948         }
2949
2950         /* Initialize security_ctx only for primary process*/
2951         security_instance = rte_malloc("rte_security_instances_ops",
2952                                 sizeof(struct rte_security_ctx), 0);
2953         if (security_instance == NULL)
2954                 return -ENOMEM;
2955         security_instance->device = (void *)cryptodev;
2956         security_instance->ops = &dpaa2_sec_security_ops;
2957         security_instance->sess_cnt = 0;
2958         cryptodev->security_ctx = security_instance;
2959
2960         /*Open the rte device via MC and save the handle for further use*/
2961         dpseci = (struct fsl_mc_io *)rte_calloc(NULL, 1,
2962                                 sizeof(struct fsl_mc_io), 0);
2963         if (!dpseci) {
2964                 DPAA2_SEC_ERR(
2965                         "Error in allocating the memory for dpsec object");
2966                 return -1;
2967         }
2968         dpseci->regs = rte_mcp_ptr_list[0];
2969
2970         retcode = dpseci_open(dpseci, CMD_PRI_LOW, hw_id, &token);
2971         if (retcode != 0) {
2972                 DPAA2_SEC_ERR("Cannot open the dpsec device: Error = %x",
2973                               retcode);
2974                 goto init_error;
2975         }
2976         retcode = dpseci_get_attributes(dpseci, CMD_PRI_LOW, token, &attr);
2977         if (retcode != 0) {
2978                 DPAA2_SEC_ERR(
2979                              "Cannot get dpsec device attributed: Error = %x",
2980                              retcode);
2981                 goto init_error;
2982         }
2983         sprintf(cryptodev->data->name, "dpsec-%u", hw_id);
2984
2985         internals->max_nb_queue_pairs = attr.num_tx_queues;
2986         cryptodev->data->nb_queue_pairs = internals->max_nb_queue_pairs;
2987         internals->hw = dpseci;
2988         internals->token = token;
2989
2990         sprintf(str, "fle_pool_%d", cryptodev->data->dev_id);
2991         internals->fle_pool = rte_mempool_create((const char *)str,
2992                         FLE_POOL_NUM_BUFS,
2993                         FLE_POOL_BUF_SIZE,
2994                         FLE_POOL_CACHE_SIZE, 0,
2995                         NULL, NULL, NULL, NULL,
2996                         SOCKET_ID_ANY, 0);
2997         if (!internals->fle_pool) {
2998                 DPAA2_SEC_ERR("Mempool (%s) creation failed", str);
2999                 goto init_error;
3000         }
3001
3002         DPAA2_SEC_INFO("driver %s: created", cryptodev->data->name);
3003         return 0;
3004
3005 init_error:
3006         DPAA2_SEC_ERR("driver %s: create failed", cryptodev->data->name);
3007
3008         /* dpaa2_sec_uninit(crypto_dev_name); */
3009         return -EFAULT;
3010 }
3011
3012 static int
3013 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
3014                           struct rte_dpaa2_device *dpaa2_dev)
3015 {
3016         struct rte_cryptodev *cryptodev;
3017         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3018
3019         int retval;
3020
3021         sprintf(cryptodev_name, "dpsec-%d", dpaa2_dev->object_id);
3022
3023         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3024         if (cryptodev == NULL)
3025                 return -ENOMEM;
3026
3027         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3028                 cryptodev->data->dev_private = rte_zmalloc_socket(
3029                                         "cryptodev private structure",
3030                                         sizeof(struct dpaa2_sec_dev_private),
3031                                         RTE_CACHE_LINE_SIZE,
3032                                         rte_socket_id());
3033
3034                 if (cryptodev->data->dev_private == NULL)
3035                         rte_panic("Cannot allocate memzone for private "
3036                                   "device data");
3037         }
3038
3039         dpaa2_dev->cryptodev = cryptodev;
3040         cryptodev->device = &dpaa2_dev->device;
3041
3042         /* init user callbacks */
3043         TAILQ_INIT(&(cryptodev->link_intr_cbs));
3044
3045         /* Invoke PMD device initialization function */
3046         retval = dpaa2_sec_dev_init(cryptodev);
3047         if (retval == 0)
3048                 return 0;
3049
3050         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3051                 rte_free(cryptodev->data->dev_private);
3052
3053         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
3054
3055         return -ENXIO;
3056 }
3057
3058 static int
3059 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
3060 {
3061         struct rte_cryptodev *cryptodev;
3062         int ret;
3063
3064         cryptodev = dpaa2_dev->cryptodev;
3065         if (cryptodev == NULL)
3066                 return -ENODEV;
3067
3068         ret = dpaa2_sec_uninit(cryptodev);
3069         if (ret)
3070                 return ret;
3071
3072         return rte_cryptodev_pmd_destroy(cryptodev);
3073 }
3074
3075 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
3076         .drv_flags = RTE_DPAA2_DRV_IOVA_AS_VA,
3077         .drv_type = DPAA2_CRYPTO,
3078         .driver = {
3079                 .name = "DPAA2 SEC PMD"
3080         },
3081         .probe = cryptodev_dpaa2_sec_probe,
3082         .remove = cryptodev_dpaa2_sec_remove,
3083 };
3084
3085 static struct cryptodev_driver dpaa2_sec_crypto_drv;
3086
3087 RTE_PMD_REGISTER_DPAA2(CRYPTODEV_NAME_DPAA2_SEC_PMD, rte_dpaa2_sec_driver);
3088 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa2_sec_crypto_drv,
3089                 rte_dpaa2_sec_driver.driver, cryptodev_driver_id);
3090
3091 RTE_INIT(dpaa2_sec_init_log)
3092 {
3093         /* Bus level logs */
3094         dpaa2_logtype_sec = rte_log_register("pmd.crypto.dpaa2");
3095         if (dpaa2_logtype_sec >= 0)
3096                 rte_log_set_level(dpaa2_logtype_sec, RTE_LOG_NOTICE);
3097 }