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