crypto/dpaa2_sec: fix buffer pool ID check
[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         if (unlikely(DPAA2_GET_FD_IVP(fd))) {
1570                 DPAA2_SEC_ERR("error: non inline buffer");
1571                 return NULL;
1572         }
1573         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
1574                 DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd)),
1575                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
1576
1577         diff = len - mbuf->pkt_len;
1578         mbuf->pkt_len += diff;
1579         mbuf->data_len += diff;
1580         op = (struct rte_crypto_op *)(size_t)mbuf->buf_iova;
1581         mbuf->buf_iova = op->sym->aead.digest.phys_addr;
1582         op->sym->aead.digest.phys_addr = 0L;
1583
1584         sess_priv = (dpaa2_sec_session *)get_sec_session_private_data(
1585                                 op->sym->sec_session);
1586         if (sess_priv->dir == DIR_ENC)
1587                 mbuf->data_off += SEC_FLC_DHR_OUTBOUND;
1588         else
1589                 mbuf->data_off += SEC_FLC_DHR_INBOUND;
1590
1591         return op;
1592 }
1593 #endif
1594
1595 static inline struct rte_crypto_op *
1596 sec_fd_to_mbuf(const struct qbman_fd *fd)
1597 {
1598         struct qbman_fle *fle;
1599         struct rte_crypto_op *op;
1600         struct ctxt_priv *priv;
1601         struct rte_mbuf *dst, *src;
1602
1603 #ifdef RTE_LIB_SECURITY
1604         if (DPAA2_FD_GET_FORMAT(fd) == qbman_fd_single)
1605                 return sec_simple_fd_to_mbuf(fd);
1606 #endif
1607         fle = (struct qbman_fle *)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
1608
1609         DPAA2_SEC_DP_DEBUG("FLE addr = %x - %x, offset = %x\n",
1610                            fle->addr_hi, fle->addr_lo, fle->fin_bpid_offset);
1611
1612         /* we are using the first FLE entry to store Mbuf.
1613          * Currently we donot know which FLE has the mbuf stored.
1614          * So while retreiving we can go back 1 FLE from the FD -ADDR
1615          * to get the MBUF Addr from the previous FLE.
1616          * We can have a better approach to use the inline Mbuf
1617          */
1618
1619         op = (struct rte_crypto_op *)DPAA2_GET_FLE_ADDR((fle - 1));
1620
1621         /* Prefeth op */
1622         src = op->sym->m_src;
1623         rte_prefetch0(src);
1624
1625         if (op->sym->m_dst) {
1626                 dst = op->sym->m_dst;
1627                 rte_prefetch0(dst);
1628         } else
1629                 dst = src;
1630
1631 #ifdef RTE_LIB_SECURITY
1632         if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1633                 uint16_t len = DPAA2_GET_FD_LEN(fd);
1634                 dst->pkt_len = len;
1635                 while (dst->next != NULL) {
1636                         len -= dst->data_len;
1637                         dst = dst->next;
1638                 }
1639                 dst->data_len = len;
1640         }
1641 #endif
1642         DPAA2_SEC_DP_DEBUG("mbuf %p BMAN buf addr %p,"
1643                 " fdaddr =%" PRIx64 " bpid =%d meta =%d off =%d, len =%d\n",
1644                 (void *)dst,
1645                 dst->buf_addr,
1646                 DPAA2_GET_FD_ADDR(fd),
1647                 DPAA2_GET_FD_BPID(fd),
1648                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
1649                 DPAA2_GET_FD_OFFSET(fd),
1650                 DPAA2_GET_FD_LEN(fd));
1651
1652         /* free the fle memory */
1653         if (likely(rte_pktmbuf_is_contiguous(src))) {
1654                 priv = (struct ctxt_priv *)(size_t)DPAA2_GET_FLE_CTXT(fle - 1);
1655                 rte_mempool_put(priv->fle_pool, (void *)(fle-1));
1656         } else
1657                 rte_free((void *)(fle-1));
1658
1659         return op;
1660 }
1661
1662 static void
1663 dpaa2_sec_dump(struct rte_crypto_op *op)
1664 {
1665         int i;
1666         dpaa2_sec_session *sess = NULL;
1667         struct ctxt_priv *priv;
1668         uint8_t bufsize;
1669         struct rte_crypto_sym_op *sym_op;
1670
1671         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
1672                 sess = (dpaa2_sec_session *)get_sym_session_private_data(
1673                         op->sym->session, cryptodev_driver_id);
1674 #ifdef RTE_LIBRTE_SECURITY
1675         else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1676                 sess = (dpaa2_sec_session *)get_sec_session_private_data(
1677                         op->sym->sec_session);
1678 #endif
1679
1680         if (sess == NULL)
1681                 goto mbuf_dump;
1682
1683         priv = (struct ctxt_priv *)sess->ctxt;
1684         printf("\n****************************************\n"
1685                 "session params:\n\tContext type:\t%d\n\tDirection:\t%s\n"
1686                 "\tCipher alg:\t%d\n\tAuth alg:\t%d\n\tAead alg:\t%d\n"
1687                 "\tCipher key len:\t%zd\n", sess->ctxt_type,
1688                 (sess->dir == DIR_ENC) ? "DIR_ENC" : "DIR_DEC",
1689                 sess->cipher_alg, sess->auth_alg, sess->aead_alg,
1690                 sess->cipher_key.length);
1691                 rte_hexdump(stdout, "cipher key", sess->cipher_key.data,
1692                                 sess->cipher_key.length);
1693                 rte_hexdump(stdout, "auth key", sess->auth_key.data,
1694                                 sess->auth_key.length);
1695         printf("\tAuth key len:\t%zd\n\tIV len:\t\t%d\n\tIV offset:\t%d\n"
1696                 "\tdigest length:\t%d\n\tstatus:\t\t%d\n\taead auth only"
1697                 " len:\t%d\n\taead cipher text:\t%d\n",
1698                 sess->auth_key.length, sess->iv.length, sess->iv.offset,
1699                 sess->digest_length, sess->status,
1700                 sess->ext_params.aead_ctxt.auth_only_len,
1701                 sess->ext_params.aead_ctxt.auth_cipher_text);
1702 #ifdef RTE_LIBRTE_SECURITY
1703         printf("PDCP session params:\n"
1704                 "\tDomain:\t\t%d\n\tBearer:\t\t%d\n\tpkt_dir:\t%d\n\thfn_ovd:"
1705                 "\t%d\n\tsn_size:\t%d\n\thfn_ovd_offset:\t%d\n\thfn:\t\t%d\n"
1706                 "\thfn_threshold:\t0x%x\n", sess->pdcp.domain,
1707                 sess->pdcp.bearer, sess->pdcp.pkt_dir, sess->pdcp.hfn_ovd,
1708                 sess->pdcp.sn_size, sess->pdcp.hfn_ovd_offset, sess->pdcp.hfn,
1709                 sess->pdcp.hfn_threshold);
1710
1711 #endif
1712         bufsize = (uint8_t)priv->flc_desc[0].flc.word1_sdl;
1713         printf("Descriptor Dump:\n");
1714         for (i = 0; i < bufsize; i++)
1715                 printf("\tDESC[%d]:0x%x\n", i, priv->flc_desc[0].desc[i]);
1716
1717         printf("\n");
1718 mbuf_dump:
1719         sym_op = op->sym;
1720         if (sym_op->m_src) {
1721                 printf("Source mbuf:\n");
1722                 rte_pktmbuf_dump(stdout, sym_op->m_src, sym_op->m_src->data_len);
1723         }
1724         if (sym_op->m_dst) {
1725                 printf("Destination mbuf:\n");
1726                 rte_pktmbuf_dump(stdout, sym_op->m_dst, sym_op->m_dst->data_len);
1727         }
1728
1729         printf("Session address = %p\ncipher offset: %d, length: %d\n"
1730                 "auth offset: %d, length:  %d\n aead offset: %d, length: %d\n"
1731                 , sym_op->session,
1732                 sym_op->cipher.data.offset, sym_op->cipher.data.length,
1733                 sym_op->auth.data.offset, sym_op->auth.data.length,
1734                 sym_op->aead.data.offset, sym_op->aead.data.length);
1735         printf("\n");
1736
1737 }
1738
1739 static void
1740 dpaa2_sec_free_eqresp_buf(uint16_t eqresp_ci)
1741 {
1742         struct dpaa2_dpio_dev *dpio_dev = DPAA2_PER_LCORE_DPIO;
1743         struct rte_crypto_op *op;
1744         struct qbman_fd *fd;
1745
1746         fd = qbman_result_eqresp_fd(&dpio_dev->eqresp[eqresp_ci]);
1747         op = sec_fd_to_mbuf(fd);
1748         /* Instead of freeing, enqueue it to the sec tx queue (sec->core)
1749          * after setting an error in FD. But this will have performance impact.
1750          */
1751         rte_pktmbuf_free(op->sym->m_src);
1752 }
1753
1754 static void
1755 dpaa2_sec_set_enqueue_descriptor(struct dpaa2_queue *dpaa2_q,
1756                              struct rte_mbuf *m,
1757                              struct qbman_eq_desc *eqdesc)
1758 {
1759         struct dpaa2_dpio_dev *dpio_dev = DPAA2_PER_LCORE_DPIO;
1760         struct eqresp_metadata *eqresp_meta;
1761         struct dpaa2_sec_dev_private *priv = dpaa2_q->crypto_data->dev_private;
1762         uint16_t orpid, seqnum;
1763         uint8_t dq_idx;
1764
1765         if (*dpaa2_seqn(m) & DPAA2_ENQUEUE_FLAG_ORP) {
1766                 orpid = (*dpaa2_seqn(m) & DPAA2_EQCR_OPRID_MASK) >>
1767                         DPAA2_EQCR_OPRID_SHIFT;
1768                 seqnum = (*dpaa2_seqn(m) & DPAA2_EQCR_SEQNUM_MASK) >>
1769                         DPAA2_EQCR_SEQNUM_SHIFT;
1770
1771
1772                 if (!priv->en_loose_ordered) {
1773                         qbman_eq_desc_set_orp(eqdesc, 1, orpid, seqnum, 0);
1774                         qbman_eq_desc_set_response(eqdesc, (uint64_t)
1775                                 DPAA2_VADDR_TO_IOVA(&dpio_dev->eqresp[
1776                                 dpio_dev->eqresp_pi]), 1);
1777                         qbman_eq_desc_set_token(eqdesc, 1);
1778
1779                         eqresp_meta = &dpio_dev->eqresp_meta[dpio_dev->eqresp_pi];
1780                         eqresp_meta->dpaa2_q = dpaa2_q;
1781                         eqresp_meta->mp = m->pool;
1782
1783                         dpio_dev->eqresp_pi + 1 < MAX_EQ_RESP_ENTRIES ?
1784                                 dpio_dev->eqresp_pi++ : (dpio_dev->eqresp_pi = 0);
1785                 } else {
1786                         qbman_eq_desc_set_orp(eqdesc, 0, orpid, seqnum, 0);
1787                 }
1788         } else {
1789                 dq_idx = *dpaa2_seqn(m) - 1;
1790                 qbman_eq_desc_set_dca(eqdesc, 1, dq_idx, 0);
1791                 DPAA2_PER_LCORE_DQRR_SIZE--;
1792                 DPAA2_PER_LCORE_DQRR_HELD &= ~(1 << dq_idx);
1793         }
1794         *dpaa2_seqn(m) = DPAA2_INVALID_MBUF_SEQN;
1795 }
1796
1797
1798 static uint16_t
1799 dpaa2_sec_enqueue_burst_ordered(void *qp, struct rte_crypto_op **ops,
1800                         uint16_t nb_ops)
1801 {
1802         /* Function to transmit the frames to given device and VQ*/
1803         uint32_t loop;
1804         int32_t ret;
1805         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
1806         uint32_t frames_to_send, num_free_eq_desc, retry_count;
1807         struct qbman_eq_desc eqdesc[MAX_TX_RING_SLOTS];
1808         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1809         struct qbman_swp *swp;
1810         uint16_t num_tx = 0;
1811         uint16_t bpid;
1812         struct rte_mempool *mb_pool;
1813         struct dpaa2_sec_dev_private *priv =
1814                                 dpaa2_qp->tx_vq.crypto_data->dev_private;
1815
1816         if (unlikely(nb_ops == 0))
1817                 return 0;
1818
1819         if (ops[0]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1820                 DPAA2_SEC_ERR("sessionless crypto op not supported");
1821                 return 0;
1822         }
1823
1824         if (!DPAA2_PER_LCORE_DPIO) {
1825                 ret = dpaa2_affine_qbman_swp();
1826                 if (ret) {
1827                         DPAA2_SEC_ERR("Failure in affining portal");
1828                         return 0;
1829                 }
1830         }
1831         swp = DPAA2_PER_LCORE_PORTAL;
1832
1833         while (nb_ops) {
1834                 frames_to_send = (nb_ops > dpaa2_eqcr_size) ?
1835                         dpaa2_eqcr_size : nb_ops;
1836
1837                 if (!priv->en_loose_ordered) {
1838                         if (*dpaa2_seqn((*ops)->sym->m_src)) {
1839                                 num_free_eq_desc = dpaa2_free_eq_descriptors();
1840                                 if (num_free_eq_desc < frames_to_send)
1841                                         frames_to_send = num_free_eq_desc;
1842                         }
1843                 }
1844
1845                 for (loop = 0; loop < frames_to_send; loop++) {
1846                         /*Prepare enqueue descriptor*/
1847                         qbman_eq_desc_clear(&eqdesc[loop]);
1848                         qbman_eq_desc_set_fq(&eqdesc[loop], dpaa2_qp->tx_vq.fqid);
1849
1850                         if (*dpaa2_seqn((*ops)->sym->m_src))
1851                                 dpaa2_sec_set_enqueue_descriptor(
1852                                                 &dpaa2_qp->tx_vq,
1853                                                 (*ops)->sym->m_src,
1854                                                 &eqdesc[loop]);
1855                         else
1856                                 qbman_eq_desc_set_no_orp(&eqdesc[loop],
1857                                                          DPAA2_EQ_RESP_ERR_FQ);
1858
1859                         /*Clear the unused FD fields before sending*/
1860                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
1861                         mb_pool = (*ops)->sym->m_src->pool;
1862                         bpid = mempool_to_bpid(mb_pool);
1863                         ret = build_sec_fd(*ops, &fd_arr[loop], bpid);
1864                         if (ret) {
1865                                 DPAA2_SEC_ERR("error: Improper packet contents"
1866                                               " for crypto operation");
1867                                 goto skip_tx;
1868                         }
1869                         ops++;
1870                 }
1871
1872                 loop = 0;
1873                 retry_count = 0;
1874                 while (loop < frames_to_send) {
1875                         ret = qbman_swp_enqueue_multiple_desc(swp,
1876                                         &eqdesc[loop], &fd_arr[loop],
1877                                         frames_to_send - loop);
1878                         if (unlikely(ret < 0)) {
1879                                 retry_count++;
1880                                 if (retry_count > DPAA2_MAX_TX_RETRY_COUNT) {
1881                                         num_tx += loop;
1882                                         nb_ops -= loop;
1883                                         DPAA2_SEC_DP_DEBUG("Enqueue fail\n");
1884                                         /* freeing the fle buffers */
1885                                         while (loop < frames_to_send) {
1886                                                 free_fle(&fd_arr[loop]);
1887                                                 loop++;
1888                                         }
1889                                         goto skip_tx;
1890                                 }
1891                         } else {
1892                                 loop += ret;
1893                                 retry_count = 0;
1894                         }
1895                 }
1896
1897                 num_tx += loop;
1898                 nb_ops -= loop;
1899         }
1900
1901 skip_tx:
1902         dpaa2_qp->tx_vq.tx_pkts += num_tx;
1903         dpaa2_qp->tx_vq.err_pkts += nb_ops;
1904         return num_tx;
1905 }
1906
1907 static uint16_t
1908 dpaa2_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1909                         uint16_t nb_ops)
1910 {
1911         /* Function is responsible to receive frames for a given device and VQ*/
1912         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
1913         struct qbman_result *dq_storage;
1914         uint32_t fqid = dpaa2_qp->rx_vq.fqid;
1915         int ret, num_rx = 0;
1916         uint8_t is_last = 0, status;
1917         struct qbman_swp *swp;
1918         const struct qbman_fd *fd;
1919         struct qbman_pull_desc pulldesc;
1920
1921         if (!DPAA2_PER_LCORE_DPIO) {
1922                 ret = dpaa2_affine_qbman_swp();
1923                 if (ret) {
1924                         DPAA2_SEC_ERR(
1925                                 "Failed to allocate IO portal, tid: %d\n",
1926                                 rte_gettid());
1927                         return 0;
1928                 }
1929         }
1930         swp = DPAA2_PER_LCORE_PORTAL;
1931         dq_storage = dpaa2_qp->rx_vq.q_storage->dq_storage[0];
1932
1933         qbman_pull_desc_clear(&pulldesc);
1934         qbman_pull_desc_set_numframes(&pulldesc,
1935                                       (nb_ops > dpaa2_dqrr_size) ?
1936                                       dpaa2_dqrr_size : nb_ops);
1937         qbman_pull_desc_set_fq(&pulldesc, fqid);
1938         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
1939                                     (dma_addr_t)DPAA2_VADDR_TO_IOVA(dq_storage),
1940                                     1);
1941
1942         /*Issue a volatile dequeue command. */
1943         while (1) {
1944                 if (qbman_swp_pull(swp, &pulldesc)) {
1945                         DPAA2_SEC_WARN(
1946                                 "SEC VDQ command is not issued : QBMAN busy");
1947                         /* Portal was busy, try again */
1948                         continue;
1949                 }
1950                 break;
1951         };
1952
1953         /* Receive the packets till Last Dequeue entry is found with
1954          * respect to the above issues PULL command.
1955          */
1956         while (!is_last) {
1957                 /* Check if the previous issued command is completed.
1958                  * Also seems like the SWP is shared between the Ethernet Driver
1959                  * and the SEC driver.
1960                  */
1961                 while (!qbman_check_command_complete(dq_storage))
1962                         ;
1963
1964                 /* Loop until the dq_storage is updated with
1965                  * new token by QBMAN
1966                  */
1967                 while (!qbman_check_new_result(dq_storage))
1968                         ;
1969                 /* Check whether Last Pull command is Expired and
1970                  * setting Condition for Loop termination
1971                  */
1972                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
1973                         is_last = 1;
1974                         /* Check for valid frame. */
1975                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
1976                         if (unlikely(
1977                                 (status & QBMAN_DQ_STAT_VALIDFRAME) == 0)) {
1978                                 DPAA2_SEC_DP_DEBUG("No frame is delivered\n");
1979                                 continue;
1980                         }
1981                 }
1982
1983                 fd = qbman_result_DQ_fd(dq_storage);
1984                 ops[num_rx] = sec_fd_to_mbuf(fd);
1985
1986                 if (unlikely(fd->simple.frc)) {
1987                         /* TODO Parse SEC errors */
1988                         if (dpaa2_sec_dp_dump > DPAA2_SEC_DP_NO_DUMP) {
1989                                 DPAA2_SEC_DP_ERR("SEC returned Error - %x\n",
1990                                                  fd->simple.frc);
1991                                 if (dpaa2_sec_dp_dump > DPAA2_SEC_DP_ERR_DUMP)
1992                                         dpaa2_sec_dump(ops[num_rx]);
1993                         }
1994
1995                         dpaa2_qp->rx_vq.err_pkts += 1;
1996                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_ERROR;
1997                 } else {
1998                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1999                 }
2000
2001                 num_rx++;
2002                 dq_storage++;
2003         } /* End of Packet Rx loop */
2004
2005         dpaa2_qp->rx_vq.rx_pkts += num_rx;
2006
2007         DPAA2_SEC_DP_DEBUG("SEC RX pkts %d err pkts %" PRIu64 "\n", num_rx,
2008                                 dpaa2_qp->rx_vq.err_pkts);
2009         /*Return the total number of packets received to DPAA2 app*/
2010         return num_rx;
2011 }
2012
2013 /** Release queue pair */
2014 static int
2015 dpaa2_sec_queue_pair_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
2016 {
2017         struct dpaa2_sec_qp *qp =
2018                 (struct dpaa2_sec_qp *)dev->data->queue_pairs[queue_pair_id];
2019
2020         PMD_INIT_FUNC_TRACE();
2021
2022         if (qp->rx_vq.q_storage) {
2023                 dpaa2_free_dq_storage(qp->rx_vq.q_storage);
2024                 rte_free(qp->rx_vq.q_storage);
2025         }
2026         rte_free(qp);
2027
2028         dev->data->queue_pairs[queue_pair_id] = NULL;
2029
2030         return 0;
2031 }
2032
2033 /** Setup a queue pair */
2034 static int
2035 dpaa2_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
2036                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
2037                 __rte_unused int socket_id)
2038 {
2039         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
2040         struct dpaa2_sec_qp *qp;
2041         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
2042         struct dpseci_rx_queue_cfg cfg;
2043         int32_t retcode;
2044
2045         PMD_INIT_FUNC_TRACE();
2046
2047         /* If qp is already in use free ring memory and qp metadata. */
2048         if (dev->data->queue_pairs[qp_id] != NULL) {
2049                 DPAA2_SEC_INFO("QP already setup");
2050                 return 0;
2051         }
2052
2053         DPAA2_SEC_DEBUG("dev =%p, queue =%d, conf =%p",
2054                     dev, qp_id, qp_conf);
2055
2056         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
2057
2058         qp = rte_malloc(NULL, sizeof(struct dpaa2_sec_qp),
2059                         RTE_CACHE_LINE_SIZE);
2060         if (!qp) {
2061                 DPAA2_SEC_ERR("malloc failed for rx/tx queues");
2062                 return -ENOMEM;
2063         }
2064
2065         qp->rx_vq.crypto_data = dev->data;
2066         qp->tx_vq.crypto_data = dev->data;
2067         qp->rx_vq.q_storage = rte_malloc("sec dq storage",
2068                 sizeof(struct queue_storage_info_t),
2069                 RTE_CACHE_LINE_SIZE);
2070         if (!qp->rx_vq.q_storage) {
2071                 DPAA2_SEC_ERR("malloc failed for q_storage");
2072                 return -ENOMEM;
2073         }
2074         memset(qp->rx_vq.q_storage, 0, sizeof(struct queue_storage_info_t));
2075
2076         if (dpaa2_alloc_dq_storage(qp->rx_vq.q_storage)) {
2077                 DPAA2_SEC_ERR("Unable to allocate dequeue storage");
2078                 return -ENOMEM;
2079         }
2080
2081         dev->data->queue_pairs[qp_id] = qp;
2082
2083         cfg.options = cfg.options | DPSECI_QUEUE_OPT_USER_CTX;
2084         cfg.user_ctx = (size_t)(&qp->rx_vq);
2085         retcode = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
2086                                       qp_id, &cfg);
2087         return retcode;
2088 }
2089
2090 /** Returns the size of the aesni gcm session structure */
2091 static unsigned int
2092 dpaa2_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2093 {
2094         PMD_INIT_FUNC_TRACE();
2095
2096         return sizeof(dpaa2_sec_session);
2097 }
2098
2099 static int
2100 dpaa2_sec_cipher_init(struct rte_cryptodev *dev,
2101                       struct rte_crypto_sym_xform *xform,
2102                       dpaa2_sec_session *session)
2103 {
2104         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2105         struct alginfo cipherdata;
2106         int bufsize, ret = 0;
2107         struct ctxt_priv *priv;
2108         struct sec_flow_context *flc;
2109
2110         PMD_INIT_FUNC_TRACE();
2111
2112         /* For SEC CIPHER only one descriptor is required. */
2113         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2114                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2115                         RTE_CACHE_LINE_SIZE);
2116         if (priv == NULL) {
2117                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2118                 return -ENOMEM;
2119         }
2120
2121         priv->fle_pool = dev_priv->fle_pool;
2122
2123         flc = &priv->flc_desc[0].flc;
2124
2125         session->ctxt_type = DPAA2_SEC_CIPHER;
2126         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2127                         RTE_CACHE_LINE_SIZE);
2128         if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2129                 DPAA2_SEC_ERR("No Memory for cipher key");
2130                 rte_free(priv);
2131                 return -ENOMEM;
2132         }
2133         session->cipher_key.length = xform->cipher.key.length;
2134
2135         memcpy(session->cipher_key.data, xform->cipher.key.data,
2136                xform->cipher.key.length);
2137         cipherdata.key = (size_t)session->cipher_key.data;
2138         cipherdata.keylen = session->cipher_key.length;
2139         cipherdata.key_enc_flags = 0;
2140         cipherdata.key_type = RTA_DATA_IMM;
2141
2142         /* Set IV parameters */
2143         session->iv.offset = xform->cipher.iv.offset;
2144         session->iv.length = xform->cipher.iv.length;
2145         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2146                                 DIR_ENC : DIR_DEC;
2147
2148         switch (xform->cipher.algo) {
2149         case RTE_CRYPTO_CIPHER_AES_CBC:
2150                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2151                 cipherdata.algmode = OP_ALG_AAI_CBC;
2152                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
2153                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
2154                                                 SHR_NEVER, &cipherdata,
2155                                                 session->iv.length,
2156                                                 session->dir);
2157                 break;
2158         case RTE_CRYPTO_CIPHER_3DES_CBC:
2159                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
2160                 cipherdata.algmode = OP_ALG_AAI_CBC;
2161                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
2162                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
2163                                                 SHR_NEVER, &cipherdata,
2164                                                 session->iv.length,
2165                                                 session->dir);
2166                 break;
2167         case RTE_CRYPTO_CIPHER_DES_CBC:
2168                 cipherdata.algtype = OP_ALG_ALGSEL_DES;
2169                 cipherdata.algmode = OP_ALG_AAI_CBC;
2170                 session->cipher_alg = RTE_CRYPTO_CIPHER_DES_CBC;
2171                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
2172                                                 SHR_NEVER, &cipherdata,
2173                                                 session->iv.length,
2174                                                 session->dir);
2175                 break;
2176         case RTE_CRYPTO_CIPHER_AES_CTR:
2177                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2178                 cipherdata.algmode = OP_ALG_AAI_CTR;
2179                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
2180                 bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
2181                                                 SHR_NEVER, &cipherdata,
2182                                                 session->iv.length,
2183                                                 session->dir);
2184                 break;
2185         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2186                 cipherdata.algtype = OP_ALG_ALGSEL_SNOW_F8;
2187                 session->cipher_alg = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
2188                 bufsize = cnstr_shdsc_snow_f8(priv->flc_desc[0].desc, 1, 0,
2189                                               &cipherdata,
2190                                               session->dir);
2191                 break;
2192         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2193                 cipherdata.algtype = OP_ALG_ALGSEL_ZUCE;
2194                 session->cipher_alg = RTE_CRYPTO_CIPHER_ZUC_EEA3;
2195                 bufsize = cnstr_shdsc_zuce(priv->flc_desc[0].desc, 1, 0,
2196                                               &cipherdata,
2197                                               session->dir);
2198                 break;
2199         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2200         case RTE_CRYPTO_CIPHER_AES_F8:
2201         case RTE_CRYPTO_CIPHER_AES_ECB:
2202         case RTE_CRYPTO_CIPHER_3DES_ECB:
2203         case RTE_CRYPTO_CIPHER_3DES_CTR:
2204         case RTE_CRYPTO_CIPHER_AES_XTS:
2205         case RTE_CRYPTO_CIPHER_ARC4:
2206         case RTE_CRYPTO_CIPHER_NULL:
2207                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2208                         xform->cipher.algo);
2209                 ret = -ENOTSUP;
2210                 goto error_out;
2211         default:
2212                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2213                         xform->cipher.algo);
2214                 ret = -ENOTSUP;
2215                 goto error_out;
2216         }
2217
2218         if (bufsize < 0) {
2219                 DPAA2_SEC_ERR("Crypto: Descriptor build failed");
2220                 ret = -EINVAL;
2221                 goto error_out;
2222         }
2223
2224         flc->word1_sdl = (uint8_t)bufsize;
2225         session->ctxt = priv;
2226
2227 #ifdef CAAM_DESC_DEBUG
2228         int i;
2229         for (i = 0; i < bufsize; i++)
2230                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x", i, priv->flc_desc[0].desc[i]);
2231 #endif
2232         return ret;
2233
2234 error_out:
2235         rte_free(session->cipher_key.data);
2236         rte_free(priv);
2237         return ret;
2238 }
2239
2240 static int
2241 dpaa2_sec_auth_init(struct rte_cryptodev *dev,
2242                     struct rte_crypto_sym_xform *xform,
2243                     dpaa2_sec_session *session)
2244 {
2245         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2246         struct alginfo authdata;
2247         int bufsize, ret = 0;
2248         struct ctxt_priv *priv;
2249         struct sec_flow_context *flc;
2250
2251         PMD_INIT_FUNC_TRACE();
2252
2253         /* For SEC AUTH three descriptors are required for various stages */
2254         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2255                         sizeof(struct ctxt_priv) + 3 *
2256                         sizeof(struct sec_flc_desc),
2257                         RTE_CACHE_LINE_SIZE);
2258         if (priv == NULL) {
2259                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2260                 return -ENOMEM;
2261         }
2262
2263         priv->fle_pool = dev_priv->fle_pool;
2264         flc = &priv->flc_desc[DESC_INITFINAL].flc;
2265
2266         session->ctxt_type = DPAA2_SEC_AUTH;
2267         session->auth_key.length = xform->auth.key.length;
2268         if (xform->auth.key.length) {
2269                 session->auth_key.data = rte_zmalloc(NULL,
2270                         xform->auth.key.length,
2271                         RTE_CACHE_LINE_SIZE);
2272                 if (session->auth_key.data == NULL) {
2273                         DPAA2_SEC_ERR("Unable to allocate memory for auth key");
2274                         rte_free(priv);
2275                         return -ENOMEM;
2276                 }
2277                 memcpy(session->auth_key.data, xform->auth.key.data,
2278                        xform->auth.key.length);
2279                 authdata.key = (size_t)session->auth_key.data;
2280                 authdata.key_enc_flags = 0;
2281                 authdata.key_type = RTA_DATA_IMM;
2282         }
2283         authdata.keylen = session->auth_key.length;
2284
2285         session->digest_length = xform->auth.digest_length;
2286         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2287                                 DIR_ENC : DIR_DEC;
2288
2289         switch (xform->auth.algo) {
2290         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2291                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2292                 authdata.algmode = OP_ALG_AAI_HMAC;
2293                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2294                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2295                                            1, 0, SHR_NEVER, &authdata,
2296                                            !session->dir,
2297                                            session->digest_length);
2298                 break;
2299         case RTE_CRYPTO_AUTH_MD5_HMAC:
2300                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2301                 authdata.algmode = OP_ALG_AAI_HMAC;
2302                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2303                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2304                                            1, 0, SHR_NEVER, &authdata,
2305                                            !session->dir,
2306                                            session->digest_length);
2307                 break;
2308         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2309                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2310                 authdata.algmode = OP_ALG_AAI_HMAC;
2311                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2312                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2313                                            1, 0, SHR_NEVER, &authdata,
2314                                            !session->dir,
2315                                            session->digest_length);
2316                 break;
2317         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2318                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2319                 authdata.algmode = OP_ALG_AAI_HMAC;
2320                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2321                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2322                                            1, 0, SHR_NEVER, &authdata,
2323                                            !session->dir,
2324                                            session->digest_length);
2325                 break;
2326         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2327                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2328                 authdata.algmode = OP_ALG_AAI_HMAC;
2329                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2330                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2331                                            1, 0, SHR_NEVER, &authdata,
2332                                            !session->dir,
2333                                            session->digest_length);
2334                 break;
2335         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2336                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2337                 authdata.algmode = OP_ALG_AAI_HMAC;
2338                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2339                 bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
2340                                            1, 0, SHR_NEVER, &authdata,
2341                                            !session->dir,
2342                                            session->digest_length);
2343                 break;
2344         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2345                 authdata.algtype = OP_ALG_ALGSEL_SNOW_F9;
2346                 authdata.algmode = OP_ALG_AAI_F9;
2347                 session->auth_alg = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
2348                 session->iv.offset = xform->auth.iv.offset;
2349                 session->iv.length = xform->auth.iv.length;
2350                 bufsize = cnstr_shdsc_snow_f9(priv->flc_desc[DESC_INITFINAL].desc,
2351                                               1, 0, &authdata,
2352                                               !session->dir,
2353                                               session->digest_length);
2354                 break;
2355         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2356                 authdata.algtype = OP_ALG_ALGSEL_ZUCA;
2357                 authdata.algmode = OP_ALG_AAI_F9;
2358                 session->auth_alg = RTE_CRYPTO_AUTH_ZUC_EIA3;
2359                 session->iv.offset = xform->auth.iv.offset;
2360                 session->iv.length = xform->auth.iv.length;
2361                 bufsize = cnstr_shdsc_zuca(priv->flc_desc[DESC_INITFINAL].desc,
2362                                            1, 0, &authdata,
2363                                            !session->dir,
2364                                            session->digest_length);
2365                 break;
2366         case RTE_CRYPTO_AUTH_SHA1:
2367                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2368                 authdata.algmode = OP_ALG_AAI_HASH;
2369                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1;
2370                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2371                                            1, 0, SHR_NEVER, &authdata,
2372                                            !session->dir,
2373                                            session->digest_length);
2374                 break;
2375         case RTE_CRYPTO_AUTH_MD5:
2376                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2377                 authdata.algmode = OP_ALG_AAI_HASH;
2378                 session->auth_alg = RTE_CRYPTO_AUTH_MD5;
2379                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2380                                            1, 0, SHR_NEVER, &authdata,
2381                                            !session->dir,
2382                                            session->digest_length);
2383                 break;
2384         case RTE_CRYPTO_AUTH_SHA256:
2385                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2386                 authdata.algmode = OP_ALG_AAI_HASH;
2387                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256;
2388                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2389                                            1, 0, SHR_NEVER, &authdata,
2390                                            !session->dir,
2391                                            session->digest_length);
2392                 break;
2393         case RTE_CRYPTO_AUTH_SHA384:
2394                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2395                 authdata.algmode = OP_ALG_AAI_HASH;
2396                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384;
2397                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2398                                            1, 0, SHR_NEVER, &authdata,
2399                                            !session->dir,
2400                                            session->digest_length);
2401                 break;
2402         case RTE_CRYPTO_AUTH_SHA512:
2403                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2404                 authdata.algmode = OP_ALG_AAI_HASH;
2405                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512;
2406                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2407                                            1, 0, SHR_NEVER, &authdata,
2408                                            !session->dir,
2409                                            session->digest_length);
2410                 break;
2411         case RTE_CRYPTO_AUTH_SHA224:
2412                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2413                 authdata.algmode = OP_ALG_AAI_HASH;
2414                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224;
2415                 bufsize = cnstr_shdsc_hash(priv->flc_desc[DESC_INITFINAL].desc,
2416                                            1, 0, SHR_NEVER, &authdata,
2417                                            !session->dir,
2418                                            session->digest_length);
2419                 break;
2420         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2421                 authdata.algtype = OP_ALG_ALGSEL_AES;
2422                 authdata.algmode = OP_ALG_AAI_XCBC_MAC;
2423                 session->auth_alg = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
2424                 bufsize = cnstr_shdsc_aes_mac(
2425                                         priv->flc_desc[DESC_INITFINAL].desc,
2426                                         1, 0, SHR_NEVER, &authdata,
2427                                         !session->dir,
2428                                         session->digest_length);
2429                 break;
2430         case RTE_CRYPTO_AUTH_AES_CMAC:
2431                 authdata.algtype = OP_ALG_ALGSEL_AES;
2432                 authdata.algmode = OP_ALG_AAI_CMAC;
2433                 session->auth_alg = RTE_CRYPTO_AUTH_AES_CMAC;
2434                 bufsize = cnstr_shdsc_aes_mac(
2435                                            priv->flc_desc[DESC_INITFINAL].desc,
2436                                            1, 0, SHR_NEVER, &authdata,
2437                                            !session->dir,
2438                                            session->digest_length);
2439                 break;
2440         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2441         case RTE_CRYPTO_AUTH_AES_GMAC:
2442         case RTE_CRYPTO_AUTH_KASUMI_F9:
2443         case RTE_CRYPTO_AUTH_NULL:
2444                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %un",
2445                               xform->auth.algo);
2446                 ret = -ENOTSUP;
2447                 goto error_out;
2448         default:
2449                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2450                               xform->auth.algo);
2451                 ret = -ENOTSUP;
2452                 goto error_out;
2453         }
2454
2455         if (bufsize < 0) {
2456                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2457                 ret = -EINVAL;
2458                 goto error_out;
2459         }
2460
2461         flc->word1_sdl = (uint8_t)bufsize;
2462         session->ctxt = priv;
2463 #ifdef CAAM_DESC_DEBUG
2464         int i;
2465         for (i = 0; i < bufsize; i++)
2466                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2467                                 i, priv->flc_desc[DESC_INITFINAL].desc[i]);
2468 #endif
2469
2470         return ret;
2471
2472 error_out:
2473         rte_free(session->auth_key.data);
2474         rte_free(priv);
2475         return ret;
2476 }
2477
2478 static int
2479 dpaa2_sec_aead_init(struct rte_cryptodev *dev,
2480                     struct rte_crypto_sym_xform *xform,
2481                     dpaa2_sec_session *session)
2482 {
2483         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
2484         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2485         struct alginfo aeaddata;
2486         int bufsize;
2487         struct ctxt_priv *priv;
2488         struct sec_flow_context *flc;
2489         struct rte_crypto_aead_xform *aead_xform = &xform->aead;
2490         int err, ret = 0;
2491
2492         PMD_INIT_FUNC_TRACE();
2493
2494         /* Set IV parameters */
2495         session->iv.offset = aead_xform->iv.offset;
2496         session->iv.length = aead_xform->iv.length;
2497         session->ctxt_type = DPAA2_SEC_AEAD;
2498
2499         /* For SEC AEAD only one descriptor is required */
2500         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2501                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2502                         RTE_CACHE_LINE_SIZE);
2503         if (priv == NULL) {
2504                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2505                 return -ENOMEM;
2506         }
2507
2508         priv->fle_pool = dev_priv->fle_pool;
2509         flc = &priv->flc_desc[0].flc;
2510
2511         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2512                                                RTE_CACHE_LINE_SIZE);
2513         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2514                 DPAA2_SEC_ERR("No Memory for aead key");
2515                 rte_free(priv);
2516                 return -ENOMEM;
2517         }
2518         memcpy(session->aead_key.data, aead_xform->key.data,
2519                aead_xform->key.length);
2520
2521         session->digest_length = aead_xform->digest_length;
2522         session->aead_key.length = aead_xform->key.length;
2523         ctxt->auth_only_len = aead_xform->aad_length;
2524
2525         aeaddata.key = (size_t)session->aead_key.data;
2526         aeaddata.keylen = session->aead_key.length;
2527         aeaddata.key_enc_flags = 0;
2528         aeaddata.key_type = RTA_DATA_IMM;
2529
2530         switch (aead_xform->algo) {
2531         case RTE_CRYPTO_AEAD_AES_GCM:
2532                 aeaddata.algtype = OP_ALG_ALGSEL_AES;
2533                 aeaddata.algmode = OP_ALG_AAI_GCM;
2534                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2535                 break;
2536         case RTE_CRYPTO_AEAD_AES_CCM:
2537                 DPAA2_SEC_ERR("Crypto: Unsupported AEAD alg %u",
2538                               aead_xform->algo);
2539                 ret = -ENOTSUP;
2540                 goto error_out;
2541         default:
2542                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2543                               aead_xform->algo);
2544                 ret = -ENOTSUP;
2545                 goto error_out;
2546         }
2547         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2548                                 DIR_ENC : DIR_DEC;
2549
2550         priv->flc_desc[0].desc[0] = aeaddata.keylen;
2551         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2552                                DESC_JOB_IO_LEN,
2553                                (unsigned int *)priv->flc_desc[0].desc,
2554                                &priv->flc_desc[0].desc[1], 1);
2555
2556         if (err < 0) {
2557                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2558                 ret = -EINVAL;
2559                 goto error_out;
2560         }
2561         if (priv->flc_desc[0].desc[1] & 1) {
2562                 aeaddata.key_type = RTA_DATA_IMM;
2563         } else {
2564                 aeaddata.key = DPAA2_VADDR_TO_IOVA(aeaddata.key);
2565                 aeaddata.key_type = RTA_DATA_PTR;
2566         }
2567         priv->flc_desc[0].desc[0] = 0;
2568         priv->flc_desc[0].desc[1] = 0;
2569
2570         if (session->dir == DIR_ENC)
2571                 bufsize = cnstr_shdsc_gcm_encap(
2572                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2573                                 &aeaddata, session->iv.length,
2574                                 session->digest_length);
2575         else
2576                 bufsize = cnstr_shdsc_gcm_decap(
2577                                 priv->flc_desc[0].desc, 1, 0, SHR_NEVER,
2578                                 &aeaddata, session->iv.length,
2579                                 session->digest_length);
2580         if (bufsize < 0) {
2581                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2582                 ret = -EINVAL;
2583                 goto error_out;
2584         }
2585
2586         flc->word1_sdl = (uint8_t)bufsize;
2587         session->ctxt = priv;
2588 #ifdef CAAM_DESC_DEBUG
2589         int i;
2590         for (i = 0; i < bufsize; i++)
2591                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x\n",
2592                             i, priv->flc_desc[0].desc[i]);
2593 #endif
2594         return ret;
2595
2596 error_out:
2597         rte_free(session->aead_key.data);
2598         rte_free(priv);
2599         return ret;
2600 }
2601
2602
2603 static int
2604 dpaa2_sec_aead_chain_init(struct rte_cryptodev *dev,
2605                     struct rte_crypto_sym_xform *xform,
2606                     dpaa2_sec_session *session)
2607 {
2608         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
2609         struct alginfo authdata, cipherdata;
2610         int bufsize;
2611         struct ctxt_priv *priv;
2612         struct sec_flow_context *flc;
2613         struct rte_crypto_cipher_xform *cipher_xform;
2614         struct rte_crypto_auth_xform *auth_xform;
2615         int err, ret = 0;
2616
2617         PMD_INIT_FUNC_TRACE();
2618
2619         if (session->ext_params.aead_ctxt.auth_cipher_text) {
2620                 cipher_xform = &xform->cipher;
2621                 auth_xform = &xform->next->auth;
2622                 session->ctxt_type =
2623                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2624                         DPAA2_SEC_CIPHER_HASH : DPAA2_SEC_HASH_CIPHER;
2625         } else {
2626                 cipher_xform = &xform->next->cipher;
2627                 auth_xform = &xform->auth;
2628                 session->ctxt_type =
2629                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2630                         DPAA2_SEC_HASH_CIPHER : DPAA2_SEC_CIPHER_HASH;
2631         }
2632
2633         /* Set IV parameters */
2634         session->iv.offset = cipher_xform->iv.offset;
2635         session->iv.length = cipher_xform->iv.length;
2636
2637         /* For SEC AEAD only one descriptor is required */
2638         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
2639                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
2640                         RTE_CACHE_LINE_SIZE);
2641         if (priv == NULL) {
2642                 DPAA2_SEC_ERR("No Memory for priv CTXT");
2643                 return -ENOMEM;
2644         }
2645
2646         priv->fle_pool = dev_priv->fle_pool;
2647         flc = &priv->flc_desc[0].flc;
2648
2649         session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2650                                                RTE_CACHE_LINE_SIZE);
2651         if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2652                 DPAA2_SEC_ERR("No Memory for cipher key");
2653                 rte_free(priv);
2654                 return -ENOMEM;
2655         }
2656         session->cipher_key.length = cipher_xform->key.length;
2657         session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2658                                              RTE_CACHE_LINE_SIZE);
2659         if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2660                 DPAA2_SEC_ERR("No Memory for auth key");
2661                 rte_free(session->cipher_key.data);
2662                 rte_free(priv);
2663                 return -ENOMEM;
2664         }
2665         session->auth_key.length = auth_xform->key.length;
2666         memcpy(session->cipher_key.data, cipher_xform->key.data,
2667                cipher_xform->key.length);
2668         memcpy(session->auth_key.data, auth_xform->key.data,
2669                auth_xform->key.length);
2670
2671         authdata.key = (size_t)session->auth_key.data;
2672         authdata.keylen = session->auth_key.length;
2673         authdata.key_enc_flags = 0;
2674         authdata.key_type = RTA_DATA_IMM;
2675
2676         session->digest_length = auth_xform->digest_length;
2677
2678         switch (auth_xform->algo) {
2679         case RTE_CRYPTO_AUTH_SHA1_HMAC:
2680                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
2681                 authdata.algmode = OP_ALG_AAI_HMAC;
2682                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
2683                 break;
2684         case RTE_CRYPTO_AUTH_MD5_HMAC:
2685                 authdata.algtype = OP_ALG_ALGSEL_MD5;
2686                 authdata.algmode = OP_ALG_AAI_HMAC;
2687                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
2688                 break;
2689         case RTE_CRYPTO_AUTH_SHA224_HMAC:
2690                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
2691                 authdata.algmode = OP_ALG_AAI_HMAC;
2692                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
2693                 break;
2694         case RTE_CRYPTO_AUTH_SHA256_HMAC:
2695                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
2696                 authdata.algmode = OP_ALG_AAI_HMAC;
2697                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
2698                 break;
2699         case RTE_CRYPTO_AUTH_SHA384_HMAC:
2700                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
2701                 authdata.algmode = OP_ALG_AAI_HMAC;
2702                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
2703                 break;
2704         case RTE_CRYPTO_AUTH_SHA512_HMAC:
2705                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
2706                 authdata.algmode = OP_ALG_AAI_HMAC;
2707                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
2708                 break;
2709         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2710                 authdata.algtype = OP_ALG_ALGSEL_AES;
2711                 authdata.algmode = OP_ALG_AAI_XCBC_MAC;
2712                 session->auth_alg = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
2713                 break;
2714         case RTE_CRYPTO_AUTH_AES_CMAC:
2715                 authdata.algtype = OP_ALG_ALGSEL_AES;
2716                 authdata.algmode = OP_ALG_AAI_CMAC;
2717                 session->auth_alg = RTE_CRYPTO_AUTH_AES_CMAC;
2718                 break;
2719         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2720         case RTE_CRYPTO_AUTH_AES_GMAC:
2721         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2722         case RTE_CRYPTO_AUTH_NULL:
2723         case RTE_CRYPTO_AUTH_SHA1:
2724         case RTE_CRYPTO_AUTH_SHA256:
2725         case RTE_CRYPTO_AUTH_SHA512:
2726         case RTE_CRYPTO_AUTH_SHA224:
2727         case RTE_CRYPTO_AUTH_SHA384:
2728         case RTE_CRYPTO_AUTH_MD5:
2729         case RTE_CRYPTO_AUTH_KASUMI_F9:
2730         case RTE_CRYPTO_AUTH_ZUC_EIA3:
2731                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
2732                               auth_xform->algo);
2733                 ret = -ENOTSUP;
2734                 goto error_out;
2735         default:
2736                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
2737                               auth_xform->algo);
2738                 ret = -ENOTSUP;
2739                 goto error_out;
2740         }
2741         cipherdata.key = (size_t)session->cipher_key.data;
2742         cipherdata.keylen = session->cipher_key.length;
2743         cipherdata.key_enc_flags = 0;
2744         cipherdata.key_type = RTA_DATA_IMM;
2745
2746         switch (cipher_xform->algo) {
2747         case RTE_CRYPTO_CIPHER_AES_CBC:
2748                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2749                 cipherdata.algmode = OP_ALG_AAI_CBC;
2750                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
2751                 break;
2752         case RTE_CRYPTO_CIPHER_3DES_CBC:
2753                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
2754                 cipherdata.algmode = OP_ALG_AAI_CBC;
2755                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
2756                 break;
2757         case RTE_CRYPTO_CIPHER_DES_CBC:
2758                 cipherdata.algtype = OP_ALG_ALGSEL_DES;
2759                 cipherdata.algmode = OP_ALG_AAI_CBC;
2760                 session->cipher_alg = RTE_CRYPTO_CIPHER_DES_CBC;
2761                 break;
2762         case RTE_CRYPTO_CIPHER_AES_CTR:
2763                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
2764                 cipherdata.algmode = OP_ALG_AAI_CTR;
2765                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
2766                 break;
2767         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2768         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2769         case RTE_CRYPTO_CIPHER_NULL:
2770         case RTE_CRYPTO_CIPHER_3DES_ECB:
2771         case RTE_CRYPTO_CIPHER_3DES_CTR:
2772         case RTE_CRYPTO_CIPHER_AES_ECB:
2773         case RTE_CRYPTO_CIPHER_KASUMI_F8:
2774                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2775                               cipher_xform->algo);
2776                 ret = -ENOTSUP;
2777                 goto error_out;
2778         default:
2779                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
2780                               cipher_xform->algo);
2781                 ret = -ENOTSUP;
2782                 goto error_out;
2783         }
2784         session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2785                                 DIR_ENC : DIR_DEC;
2786
2787         priv->flc_desc[0].desc[0] = cipherdata.keylen;
2788         priv->flc_desc[0].desc[1] = authdata.keylen;
2789         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
2790                                DESC_JOB_IO_LEN,
2791                                (unsigned int *)priv->flc_desc[0].desc,
2792                                &priv->flc_desc[0].desc[2], 2);
2793
2794         if (err < 0) {
2795                 DPAA2_SEC_ERR("Crypto: Incorrect key lengths");
2796                 ret = -EINVAL;
2797                 goto error_out;
2798         }
2799         if (priv->flc_desc[0].desc[2] & 1) {
2800                 cipherdata.key_type = RTA_DATA_IMM;
2801         } else {
2802                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
2803                 cipherdata.key_type = RTA_DATA_PTR;
2804         }
2805         if (priv->flc_desc[0].desc[2] & (1 << 1)) {
2806                 authdata.key_type = RTA_DATA_IMM;
2807         } else {
2808                 authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
2809                 authdata.key_type = RTA_DATA_PTR;
2810         }
2811         priv->flc_desc[0].desc[0] = 0;
2812         priv->flc_desc[0].desc[1] = 0;
2813         priv->flc_desc[0].desc[2] = 0;
2814
2815         if (session->ctxt_type == DPAA2_SEC_CIPHER_HASH) {
2816                 bufsize = cnstr_shdsc_authenc(priv->flc_desc[0].desc, 1,
2817                                               0, SHR_SERIAL,
2818                                               &cipherdata, &authdata,
2819                                               session->iv.length,
2820                                               session->digest_length,
2821                                               session->dir);
2822                 if (bufsize < 0) {
2823                         DPAA2_SEC_ERR("Crypto: Invalid buffer length");
2824                         ret = -EINVAL;
2825                         goto error_out;
2826                 }
2827         } else {
2828                 DPAA2_SEC_ERR("Hash before cipher not supported");
2829                 ret = -ENOTSUP;
2830                 goto error_out;
2831         }
2832
2833         flc->word1_sdl = (uint8_t)bufsize;
2834         session->ctxt = priv;
2835 #ifdef CAAM_DESC_DEBUG
2836         int i;
2837         for (i = 0; i < bufsize; i++)
2838                 DPAA2_SEC_DEBUG("DESC[%d]:0x%x",
2839                             i, priv->flc_desc[0].desc[i]);
2840 #endif
2841
2842         return ret;
2843
2844 error_out:
2845         rte_free(session->cipher_key.data);
2846         rte_free(session->auth_key.data);
2847         rte_free(priv);
2848         return ret;
2849 }
2850
2851 static int
2852 dpaa2_sec_set_session_parameters(struct rte_cryptodev *dev,
2853                             struct rte_crypto_sym_xform *xform, void *sess)
2854 {
2855         dpaa2_sec_session *session = sess;
2856         int ret;
2857
2858         PMD_INIT_FUNC_TRACE();
2859
2860         if (unlikely(sess == NULL)) {
2861                 DPAA2_SEC_ERR("Invalid session struct");
2862                 return -EINVAL;
2863         }
2864
2865         memset(session, 0, sizeof(dpaa2_sec_session));
2866         /* Default IV length = 0 */
2867         session->iv.length = 0;
2868
2869         /* Cipher Only */
2870         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2871                 ret = dpaa2_sec_cipher_init(dev, xform, session);
2872
2873         /* Authentication Only */
2874         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2875                    xform->next == NULL) {
2876                 ret = dpaa2_sec_auth_init(dev, xform, session);
2877
2878         /* Cipher then Authenticate */
2879         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2880                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2881                 session->ext_params.aead_ctxt.auth_cipher_text = true;
2882                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2883                         ret = dpaa2_sec_auth_init(dev, xform, session);
2884                 else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2885                         ret = dpaa2_sec_cipher_init(dev, xform, session);
2886                 else
2887                         ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2888         /* Authenticate then Cipher */
2889         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2890                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2891                 session->ext_params.aead_ctxt.auth_cipher_text = false;
2892                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2893                         ret = dpaa2_sec_cipher_init(dev, xform, session);
2894                 else if (xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2895                         ret = dpaa2_sec_auth_init(dev, xform, session);
2896                 else
2897                         ret = dpaa2_sec_aead_chain_init(dev, xform, session);
2898         /* AEAD operation for AES-GCM kind of Algorithms */
2899         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2900                    xform->next == NULL) {
2901                 ret = dpaa2_sec_aead_init(dev, xform, session);
2902
2903         } else {
2904                 DPAA2_SEC_ERR("Invalid crypto type");
2905                 return -EINVAL;
2906         }
2907
2908         return ret;
2909 }
2910
2911 #ifdef RTE_LIB_SECURITY
2912 static int
2913 dpaa2_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2914                         dpaa2_sec_session *session,
2915                         struct alginfo *aeaddata)
2916 {
2917         PMD_INIT_FUNC_TRACE();
2918
2919         session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2920                                                RTE_CACHE_LINE_SIZE);
2921         if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2922                 DPAA2_SEC_ERR("No Memory for aead key");
2923                 return -ENOMEM;
2924         }
2925         memcpy(session->aead_key.data, aead_xform->key.data,
2926                aead_xform->key.length);
2927
2928         session->digest_length = aead_xform->digest_length;
2929         session->aead_key.length = aead_xform->key.length;
2930
2931         aeaddata->key = (size_t)session->aead_key.data;
2932         aeaddata->keylen = session->aead_key.length;
2933         aeaddata->key_enc_flags = 0;
2934         aeaddata->key_type = RTA_DATA_IMM;
2935
2936         switch (aead_xform->algo) {
2937         case RTE_CRYPTO_AEAD_AES_GCM:
2938                 switch (session->digest_length) {
2939                 case 8:
2940                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM8;
2941                         break;
2942                 case 12:
2943                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM12;
2944                         break;
2945                 case 16:
2946                         aeaddata->algtype = OP_PCL_IPSEC_AES_GCM16;
2947                         break;
2948                 default:
2949                         DPAA2_SEC_ERR("Crypto: Undefined GCM digest %d",
2950                                       session->digest_length);
2951                         return -EINVAL;
2952                 }
2953                 aeaddata->algmode = OP_ALG_AAI_GCM;
2954                 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2955                 break;
2956         case RTE_CRYPTO_AEAD_AES_CCM:
2957                 switch (session->digest_length) {
2958                 case 8:
2959                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM8;
2960                         break;
2961                 case 12:
2962                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM12;
2963                         break;
2964                 case 16:
2965                         aeaddata->algtype = OP_PCL_IPSEC_AES_CCM16;
2966                         break;
2967                 default:
2968                         DPAA2_SEC_ERR("Crypto: Undefined CCM digest %d",
2969                                       session->digest_length);
2970                         return -EINVAL;
2971                 }
2972                 aeaddata->algmode = OP_ALG_AAI_CCM;
2973                 session->aead_alg = RTE_CRYPTO_AEAD_AES_CCM;
2974                 break;
2975         default:
2976                 DPAA2_SEC_ERR("Crypto: Undefined AEAD specified %u",
2977                               aead_xform->algo);
2978                 return -ENOTSUP;
2979         }
2980         session->dir = (aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2981                                 DIR_ENC : DIR_DEC;
2982
2983         return 0;
2984 }
2985
2986 static int
2987 dpaa2_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2988         struct rte_crypto_auth_xform *auth_xform,
2989         dpaa2_sec_session *session,
2990         struct alginfo *cipherdata,
2991         struct alginfo *authdata)
2992 {
2993         if (cipher_xform) {
2994                 session->cipher_key.data = rte_zmalloc(NULL,
2995                                                        cipher_xform->key.length,
2996                                                        RTE_CACHE_LINE_SIZE);
2997                 if (session->cipher_key.data == NULL &&
2998                                 cipher_xform->key.length > 0) {
2999                         DPAA2_SEC_ERR("No Memory for cipher key");
3000                         return -ENOMEM;
3001                 }
3002
3003                 session->cipher_key.length = cipher_xform->key.length;
3004                 memcpy(session->cipher_key.data, cipher_xform->key.data,
3005                                 cipher_xform->key.length);
3006                 session->cipher_alg = cipher_xform->algo;
3007         } else {
3008                 session->cipher_key.data = NULL;
3009                 session->cipher_key.length = 0;
3010                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3011         }
3012
3013         if (auth_xform) {
3014                 session->auth_key.data = rte_zmalloc(NULL,
3015                                                 auth_xform->key.length,
3016                                                 RTE_CACHE_LINE_SIZE);
3017                 if (session->auth_key.data == NULL &&
3018                                 auth_xform->key.length > 0) {
3019                         DPAA2_SEC_ERR("No Memory for auth key");
3020                         return -ENOMEM;
3021                 }
3022                 session->auth_key.length = auth_xform->key.length;
3023                 memcpy(session->auth_key.data, auth_xform->key.data,
3024                                 auth_xform->key.length);
3025                 session->auth_alg = auth_xform->algo;
3026                 session->digest_length = auth_xform->digest_length;
3027         } else {
3028                 session->auth_key.data = NULL;
3029                 session->auth_key.length = 0;
3030                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
3031         }
3032
3033         authdata->key = (size_t)session->auth_key.data;
3034         authdata->keylen = session->auth_key.length;
3035         authdata->key_enc_flags = 0;
3036         authdata->key_type = RTA_DATA_IMM;
3037         switch (session->auth_alg) {
3038         case RTE_CRYPTO_AUTH_SHA1_HMAC:
3039                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA1_96;
3040                 authdata->algmode = OP_ALG_AAI_HMAC;
3041                 break;
3042         case RTE_CRYPTO_AUTH_MD5_HMAC:
3043                 authdata->algtype = OP_PCL_IPSEC_HMAC_MD5_96;
3044                 authdata->algmode = OP_ALG_AAI_HMAC;
3045                 break;
3046         case RTE_CRYPTO_AUTH_SHA256_HMAC:
3047                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_256_128;
3048                 authdata->algmode = OP_ALG_AAI_HMAC;
3049                 if (session->digest_length != 16)
3050                         DPAA2_SEC_WARN(
3051                         "+++Using sha256-hmac truncated len is non-standard,"
3052                         "it will not work with lookaside proto");
3053                 break;
3054         case RTE_CRYPTO_AUTH_SHA384_HMAC:
3055                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_384_192;
3056                 authdata->algmode = OP_ALG_AAI_HMAC;
3057                 break;
3058         case RTE_CRYPTO_AUTH_SHA512_HMAC:
3059                 authdata->algtype = OP_PCL_IPSEC_HMAC_SHA2_512_256;
3060                 authdata->algmode = OP_ALG_AAI_HMAC;
3061                 break;
3062         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
3063                 authdata->algtype = OP_PCL_IPSEC_AES_XCBC_MAC_96;
3064                 authdata->algmode = OP_ALG_AAI_XCBC_MAC;
3065                 break;
3066         case RTE_CRYPTO_AUTH_AES_CMAC:
3067                 authdata->algtype = OP_PCL_IPSEC_AES_CMAC_96;
3068                 authdata->algmode = OP_ALG_AAI_CMAC;
3069                 break;
3070         case RTE_CRYPTO_AUTH_NULL:
3071                 authdata->algtype = OP_PCL_IPSEC_HMAC_NULL;
3072                 break;
3073         case RTE_CRYPTO_AUTH_SHA224_HMAC:
3074         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
3075         case RTE_CRYPTO_AUTH_SHA1:
3076         case RTE_CRYPTO_AUTH_SHA256:
3077         case RTE_CRYPTO_AUTH_SHA512:
3078         case RTE_CRYPTO_AUTH_SHA224:
3079         case RTE_CRYPTO_AUTH_SHA384:
3080         case RTE_CRYPTO_AUTH_MD5:
3081         case RTE_CRYPTO_AUTH_AES_GMAC:
3082         case RTE_CRYPTO_AUTH_KASUMI_F9:
3083         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
3084         case RTE_CRYPTO_AUTH_ZUC_EIA3:
3085                 DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
3086                               session->auth_alg);
3087                 return -ENOTSUP;
3088         default:
3089                 DPAA2_SEC_ERR("Crypto: Undefined Auth specified %u",
3090                               session->auth_alg);
3091                 return -ENOTSUP;
3092         }
3093         cipherdata->key = (size_t)session->cipher_key.data;
3094         cipherdata->keylen = session->cipher_key.length;
3095         cipherdata->key_enc_flags = 0;
3096         cipherdata->key_type = RTA_DATA_IMM;
3097
3098         switch (session->cipher_alg) {
3099         case RTE_CRYPTO_CIPHER_AES_CBC:
3100                 cipherdata->algtype = OP_PCL_IPSEC_AES_CBC;
3101                 cipherdata->algmode = OP_ALG_AAI_CBC;
3102                 break;
3103         case RTE_CRYPTO_CIPHER_3DES_CBC:
3104                 cipherdata->algtype = OP_PCL_IPSEC_3DES;
3105                 cipherdata->algmode = OP_ALG_AAI_CBC;
3106                 break;
3107         case RTE_CRYPTO_CIPHER_DES_CBC:
3108                 cipherdata->algtype = OP_PCL_IPSEC_DES;
3109                 cipherdata->algmode = OP_ALG_AAI_CBC;
3110                 break;
3111         case RTE_CRYPTO_CIPHER_AES_CTR:
3112                 cipherdata->algtype = OP_PCL_IPSEC_AES_CTR;
3113                 cipherdata->algmode = OP_ALG_AAI_CTR;
3114                 break;
3115         case RTE_CRYPTO_CIPHER_NULL:
3116                 cipherdata->algtype = OP_PCL_IPSEC_NULL;
3117                 break;
3118         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
3119         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
3120         case RTE_CRYPTO_CIPHER_3DES_ECB:
3121         case RTE_CRYPTO_CIPHER_3DES_CTR:
3122         case RTE_CRYPTO_CIPHER_AES_ECB:
3123         case RTE_CRYPTO_CIPHER_KASUMI_F8:
3124                 DPAA2_SEC_ERR("Crypto: Unsupported Cipher alg %u",
3125                               session->cipher_alg);
3126                 return -ENOTSUP;
3127         default:
3128                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
3129                               session->cipher_alg);
3130                 return -ENOTSUP;
3131         }
3132
3133         return 0;
3134 }
3135
3136 static int
3137 dpaa2_sec_set_ipsec_session(struct rte_cryptodev *dev,
3138                             struct rte_security_session_conf *conf,
3139                             void *sess)
3140 {
3141         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
3142         struct rte_crypto_cipher_xform *cipher_xform = NULL;
3143         struct rte_crypto_auth_xform *auth_xform = NULL;
3144         struct rte_crypto_aead_xform *aead_xform = NULL;
3145         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
3146         struct ctxt_priv *priv;
3147         struct alginfo authdata, cipherdata;
3148         int bufsize;
3149         struct sec_flow_context *flc;
3150         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
3151         int ret = -1;
3152
3153         PMD_INIT_FUNC_TRACE();
3154
3155         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
3156                                 sizeof(struct ctxt_priv) +
3157                                 sizeof(struct sec_flc_desc),
3158                                 RTE_CACHE_LINE_SIZE);
3159
3160         if (priv == NULL) {
3161                 DPAA2_SEC_ERR("No memory for priv CTXT");
3162                 return -ENOMEM;
3163         }
3164
3165         priv->fle_pool = dev_priv->fle_pool;
3166         flc = &priv->flc_desc[0].flc;
3167
3168         if (ipsec_xform->life.bytes_hard_limit != 0 ||
3169             ipsec_xform->life.bytes_soft_limit != 0 ||
3170             ipsec_xform->life.packets_hard_limit != 0 ||
3171             ipsec_xform->life.packets_soft_limit != 0)
3172                 return -ENOTSUP;
3173
3174         memset(session, 0, sizeof(dpaa2_sec_session));
3175
3176         if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3177                 cipher_xform = &conf->crypto_xform->cipher;
3178                 if (conf->crypto_xform->next)
3179                         auth_xform = &conf->crypto_xform->next->auth;
3180                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
3181                                         session, &cipherdata, &authdata);
3182         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3183                 auth_xform = &conf->crypto_xform->auth;
3184                 if (conf->crypto_xform->next)
3185                         cipher_xform = &conf->crypto_xform->next->cipher;
3186                 ret = dpaa2_sec_ipsec_proto_init(cipher_xform, auth_xform,
3187                                         session, &cipherdata, &authdata);
3188         } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
3189                 aead_xform = &conf->crypto_xform->aead;
3190                 ret = dpaa2_sec_ipsec_aead_init(aead_xform,
3191                                         session, &cipherdata);
3192                 authdata.keylen = 0;
3193                 authdata.algtype = 0;
3194         } else {
3195                 DPAA2_SEC_ERR("XFORM not specified");
3196                 ret = -EINVAL;
3197                 goto out;
3198         }
3199         if (ret) {
3200                 DPAA2_SEC_ERR("Failed to process xform");
3201                 goto out;
3202         }
3203
3204         session->ctxt_type = DPAA2_SEC_IPSEC;
3205         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
3206                 uint8_t *hdr = NULL;
3207                 struct ip ip4_hdr;
3208                 struct rte_ipv6_hdr ip6_hdr;
3209                 struct ipsec_encap_pdb encap_pdb;
3210
3211                 flc->dhr = SEC_FLC_DHR_OUTBOUND;
3212                 /* For Sec Proto only one descriptor is required. */
3213                 memset(&encap_pdb, 0, sizeof(struct ipsec_encap_pdb));
3214
3215                 /* copy algo specific data to PDB */
3216                 switch (cipherdata.algtype) {
3217                 case OP_PCL_IPSEC_AES_CTR:
3218                         encap_pdb.ctr.ctr_initial = 0x00000001;
3219                         encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
3220                         break;
3221                 case OP_PCL_IPSEC_AES_GCM8:
3222                 case OP_PCL_IPSEC_AES_GCM12:
3223                 case OP_PCL_IPSEC_AES_GCM16:
3224                         memcpy(encap_pdb.gcm.salt,
3225                                 (uint8_t *)&(ipsec_xform->salt), 4);
3226                         break;
3227                 }
3228
3229                 encap_pdb.options = (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
3230                         PDBOPTS_ESP_OIHI_PDB_INL |
3231                         PDBOPTS_ESP_IVSRC |
3232                         PDBHMO_ESP_SNR;
3233                 if (ipsec_xform->options.dec_ttl)
3234                         encap_pdb.options |= PDBHMO_ESP_ENCAP_DTTL;
3235                 if (ipsec_xform->options.esn)
3236                         encap_pdb.options |= PDBOPTS_ESP_ESN;
3237                 encap_pdb.spi = ipsec_xform->spi;
3238                 session->dir = DIR_ENC;
3239                 if (ipsec_xform->tunnel.type ==
3240                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
3241                         encap_pdb.ip_hdr_len = sizeof(struct ip);
3242                         ip4_hdr.ip_v = IPVERSION;
3243                         ip4_hdr.ip_hl = 5;
3244                         ip4_hdr.ip_len = rte_cpu_to_be_16(sizeof(ip4_hdr));
3245                         ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
3246                         ip4_hdr.ip_id = 0;
3247                         ip4_hdr.ip_off = 0;
3248                         ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
3249                         ip4_hdr.ip_p = IPPROTO_ESP;
3250                         ip4_hdr.ip_sum = 0;
3251                         ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
3252                         ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
3253                         ip4_hdr.ip_sum = calc_chksum((uint16_t *)(void *)
3254                                         &ip4_hdr, sizeof(struct ip));
3255                         hdr = (uint8_t *)&ip4_hdr;
3256                 } else if (ipsec_xform->tunnel.type ==
3257                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
3258                         ip6_hdr.vtc_flow = rte_cpu_to_be_32(
3259                                 DPAA2_IPv6_DEFAULT_VTC_FLOW |
3260                                 ((ipsec_xform->tunnel.ipv6.dscp <<
3261                                         RTE_IPV6_HDR_TC_SHIFT) &
3262                                         RTE_IPV6_HDR_TC_MASK) |
3263                                 ((ipsec_xform->tunnel.ipv6.flabel <<
3264                                         RTE_IPV6_HDR_FL_SHIFT) &
3265                                         RTE_IPV6_HDR_FL_MASK));
3266                         /* Payload length will be updated by HW */
3267                         ip6_hdr.payload_len = 0;
3268                         ip6_hdr.hop_limits =
3269                                         ipsec_xform->tunnel.ipv6.hlimit;
3270                         ip6_hdr.proto = (ipsec_xform->proto ==
3271                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
3272                                         IPPROTO_ESP : IPPROTO_AH;
3273                         memcpy(&ip6_hdr.src_addr,
3274                                 &ipsec_xform->tunnel.ipv6.src_addr, 16);
3275                         memcpy(&ip6_hdr.dst_addr,
3276                                 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
3277                         encap_pdb.ip_hdr_len = sizeof(struct rte_ipv6_hdr);
3278                         hdr = (uint8_t *)&ip6_hdr;
3279                 }
3280
3281                 bufsize = cnstr_shdsc_ipsec_new_encap(priv->flc_desc[0].desc,
3282                                 1, 0, (rta_sec_era >= RTA_SEC_ERA_10) ?
3283                                 SHR_WAIT : SHR_SERIAL, &encap_pdb,
3284                                 hdr, &cipherdata, &authdata);
3285         } else if (ipsec_xform->direction ==
3286                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
3287                 struct ipsec_decap_pdb decap_pdb;
3288
3289                 flc->dhr = SEC_FLC_DHR_INBOUND;
3290                 memset(&decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
3291                 /* copy algo specific data to PDB */
3292                 switch (cipherdata.algtype) {
3293                 case OP_PCL_IPSEC_AES_CTR:
3294                         decap_pdb.ctr.ctr_initial = 0x00000001;
3295                         decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
3296                         break;
3297                 case OP_PCL_IPSEC_AES_GCM8:
3298                 case OP_PCL_IPSEC_AES_GCM12:
3299                 case OP_PCL_IPSEC_AES_GCM16:
3300                         memcpy(decap_pdb.gcm.salt,
3301                                 (uint8_t *)&(ipsec_xform->salt), 4);
3302                         break;
3303                 }
3304
3305                 decap_pdb.options = (ipsec_xform->tunnel.type ==
3306                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) ?
3307                                 sizeof(struct ip) << 16 :
3308                                 sizeof(struct rte_ipv6_hdr) << 16;
3309                 if (ipsec_xform->options.esn)
3310                         decap_pdb.options |= PDBOPTS_ESP_ESN;
3311
3312                 if (ipsec_xform->replay_win_sz) {
3313                         uint32_t win_sz;
3314                         win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
3315
3316                         if (rta_sec_era < RTA_SEC_ERA_10 && win_sz > 128) {
3317                                 DPAA2_SEC_INFO("Max Anti replay Win sz = 128");
3318                                 win_sz = 128;
3319                         }
3320                         switch (win_sz) {
3321                         case 1:
3322                         case 2:
3323                         case 4:
3324                         case 8:
3325                         case 16:
3326                         case 32:
3327                                 decap_pdb.options |= PDBOPTS_ESP_ARS32;
3328                                 break;
3329                         case 64:
3330                                 decap_pdb.options |= PDBOPTS_ESP_ARS64;
3331                                 break;
3332                         case 256:
3333                                 decap_pdb.options |= PDBOPTS_ESP_ARS256;
3334                                 break;
3335                         case 512:
3336                                 decap_pdb.options |= PDBOPTS_ESP_ARS512;
3337                                 break;
3338                         case 1024:
3339                                 decap_pdb.options |= PDBOPTS_ESP_ARS1024;
3340                                 break;
3341                         case 128:
3342                         default:
3343                                 decap_pdb.options |= PDBOPTS_ESP_ARS128;
3344                         }
3345                 }
3346                 session->dir = DIR_DEC;
3347                 bufsize = cnstr_shdsc_ipsec_new_decap(priv->flc_desc[0].desc,
3348                                 1, 0, (rta_sec_era >= RTA_SEC_ERA_10) ?
3349                                 SHR_WAIT : SHR_SERIAL,
3350                                 &decap_pdb, &cipherdata, &authdata);
3351         } else
3352                 goto out;
3353
3354         if (bufsize < 0) {
3355                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
3356                 goto out;
3357         }
3358
3359         flc->word1_sdl = (uint8_t)bufsize;
3360
3361         /* Enable the stashing control bit */
3362         DPAA2_SET_FLC_RSC(flc);
3363         flc->word2_rflc_31_0 = lower_32_bits(
3364                         (size_t)&(((struct dpaa2_sec_qp *)
3365                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
3366         flc->word3_rflc_63_32 = upper_32_bits(
3367                         (size_t)&(((struct dpaa2_sec_qp *)
3368                         dev->data->queue_pairs[0])->rx_vq));
3369
3370         /* Set EWS bit i.e. enable write-safe */
3371         DPAA2_SET_FLC_EWS(flc);
3372         /* Set BS = 1 i.e reuse input buffers as output buffers */
3373         DPAA2_SET_FLC_REUSE_BS(flc);
3374         /* Set FF = 10; reuse input buffers if they provide sufficient space */
3375         DPAA2_SET_FLC_REUSE_FF(flc);
3376
3377         session->ctxt = priv;
3378
3379         return 0;
3380 out:
3381         rte_free(session->auth_key.data);
3382         rte_free(session->cipher_key.data);
3383         rte_free(priv);
3384         return ret;
3385 }
3386
3387 static int
3388 dpaa2_sec_set_pdcp_session(struct rte_cryptodev *dev,
3389                            struct rte_security_session_conf *conf,
3390                            void *sess)
3391 {
3392         struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
3393         struct rte_crypto_sym_xform *xform = conf->crypto_xform;
3394         struct rte_crypto_auth_xform *auth_xform = NULL;
3395         struct rte_crypto_cipher_xform *cipher_xform = NULL;
3396         dpaa2_sec_session *session = (dpaa2_sec_session *)sess;
3397         struct ctxt_priv *priv;
3398         struct dpaa2_sec_dev_private *dev_priv = dev->data->dev_private;
3399         struct alginfo authdata, cipherdata;
3400         struct alginfo *p_authdata = NULL;
3401         int bufsize = -1;
3402         struct sec_flow_context *flc;
3403 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
3404         int swap = true;
3405 #else
3406         int swap = false;
3407 #endif
3408
3409         PMD_INIT_FUNC_TRACE();
3410
3411         memset(session, 0, sizeof(dpaa2_sec_session));
3412
3413         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
3414                                 sizeof(struct ctxt_priv) +
3415                                 sizeof(struct sec_flc_desc),
3416                                 RTE_CACHE_LINE_SIZE);
3417
3418         if (priv == NULL) {
3419                 DPAA2_SEC_ERR("No memory for priv CTXT");
3420                 return -ENOMEM;
3421         }
3422
3423         priv->fle_pool = dev_priv->fle_pool;
3424         flc = &priv->flc_desc[0].flc;
3425
3426         /* find xfrm types */
3427         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3428                 cipher_xform = &xform->cipher;
3429                 if (xform->next != NULL &&
3430                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3431                         session->ext_params.aead_ctxt.auth_cipher_text = true;
3432                         auth_xform = &xform->next->auth;
3433                 }
3434         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3435                 auth_xform = &xform->auth;
3436                 if (xform->next != NULL &&
3437                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3438                         session->ext_params.aead_ctxt.auth_cipher_text = false;
3439                         cipher_xform = &xform->next->cipher;
3440                 }
3441         } else {
3442                 DPAA2_SEC_ERR("Invalid crypto type");
3443                 return -EINVAL;
3444         }
3445
3446         session->ctxt_type = DPAA2_SEC_PDCP;
3447         if (cipher_xform) {
3448                 session->cipher_key.data = rte_zmalloc(NULL,
3449                                                cipher_xform->key.length,
3450                                                RTE_CACHE_LINE_SIZE);
3451                 if (session->cipher_key.data == NULL &&
3452                                 cipher_xform->key.length > 0) {
3453                         DPAA2_SEC_ERR("No Memory for cipher key");
3454                         rte_free(priv);
3455                         return -ENOMEM;
3456                 }
3457                 session->cipher_key.length = cipher_xform->key.length;
3458                 memcpy(session->cipher_key.data, cipher_xform->key.data,
3459                         cipher_xform->key.length);
3460                 session->dir =
3461                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3462                                         DIR_ENC : DIR_DEC;
3463                 session->cipher_alg = cipher_xform->algo;
3464         } else {
3465                 session->cipher_key.data = NULL;
3466                 session->cipher_key.length = 0;
3467                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3468                 session->dir = DIR_ENC;
3469         }
3470
3471         session->pdcp.domain = pdcp_xform->domain;
3472         session->pdcp.bearer = pdcp_xform->bearer;
3473         session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3474         session->pdcp.sn_size = pdcp_xform->sn_size;
3475         session->pdcp.hfn = pdcp_xform->hfn;
3476         session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
3477         session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
3478         /* hfv ovd offset location is stored in iv.offset value*/
3479         if (cipher_xform)
3480                 session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3481
3482         cipherdata.key = (size_t)session->cipher_key.data;
3483         cipherdata.keylen = session->cipher_key.length;
3484         cipherdata.key_enc_flags = 0;
3485         cipherdata.key_type = RTA_DATA_IMM;
3486
3487         switch (session->cipher_alg) {
3488         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
3489                 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
3490                 break;
3491         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
3492                 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
3493                 break;
3494         case RTE_CRYPTO_CIPHER_AES_CTR:
3495                 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
3496                 break;
3497         case RTE_CRYPTO_CIPHER_NULL:
3498                 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
3499                 break;
3500         default:
3501                 DPAA2_SEC_ERR("Crypto: Undefined Cipher specified %u",
3502                               session->cipher_alg);
3503                 goto out;
3504         }
3505
3506         if (auth_xform) {
3507                 session->auth_key.data = rte_zmalloc(NULL,
3508                                                      auth_xform->key.length,
3509                                                      RTE_CACHE_LINE_SIZE);
3510                 if (!session->auth_key.data &&
3511                     auth_xform->key.length > 0) {
3512                         DPAA2_SEC_ERR("No Memory for auth key");
3513                         rte_free(session->cipher_key.data);
3514                         rte_free(priv);
3515                         return -ENOMEM;
3516                 }
3517                 session->auth_key.length = auth_xform->key.length;
3518                 memcpy(session->auth_key.data, auth_xform->key.data,
3519                        auth_xform->key.length);
3520                 session->auth_alg = auth_xform->algo;
3521         } else {
3522                 session->auth_key.data = NULL;
3523                 session->auth_key.length = 0;
3524                 session->auth_alg = 0;
3525         }
3526         authdata.key = (size_t)session->auth_key.data;
3527         authdata.keylen = session->auth_key.length;
3528         authdata.key_enc_flags = 0;
3529         authdata.key_type = RTA_DATA_IMM;
3530
3531         if (session->auth_alg) {
3532                 switch (session->auth_alg) {
3533                 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
3534                         authdata.algtype = PDCP_AUTH_TYPE_SNOW;
3535                         break;
3536                 case RTE_CRYPTO_AUTH_ZUC_EIA3:
3537                         authdata.algtype = PDCP_AUTH_TYPE_ZUC;
3538                         break;
3539                 case RTE_CRYPTO_AUTH_AES_CMAC:
3540                         authdata.algtype = PDCP_AUTH_TYPE_AES;
3541                         break;
3542                 case RTE_CRYPTO_AUTH_NULL:
3543                         authdata.algtype = PDCP_AUTH_TYPE_NULL;
3544                         break;
3545                 default:
3546                         DPAA2_SEC_ERR("Crypto: Unsupported auth alg %u",
3547                                       session->auth_alg);
3548                         goto out;
3549                 }
3550
3551                 p_authdata = &authdata;
3552         } else if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3553                 DPAA2_SEC_ERR("Crypto: Integrity must for c-plane");
3554                 goto out;
3555         }
3556
3557         if (pdcp_xform->sdap_enabled) {
3558                 int nb_keys_to_inline =
3559                         rta_inline_pdcp_sdap_query(authdata.algtype,
3560                                         cipherdata.algtype,
3561                                         session->pdcp.sn_size,
3562                                         session->pdcp.hfn_ovd);
3563                 if (nb_keys_to_inline >= 1) {
3564                         cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
3565                         cipherdata.key_type = RTA_DATA_PTR;
3566                 }
3567                 if (nb_keys_to_inline >= 2) {
3568                         authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
3569                         authdata.key_type = RTA_DATA_PTR;
3570                 }
3571         } else {
3572                 if (rta_inline_pdcp_query(authdata.algtype,
3573                                         cipherdata.algtype,
3574                                         session->pdcp.sn_size,
3575                                         session->pdcp.hfn_ovd)) {
3576                         cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
3577                         cipherdata.key_type = RTA_DATA_PTR;
3578                 }
3579         }
3580
3581         if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3582                 if (session->dir == DIR_ENC)
3583                         bufsize = cnstr_shdsc_pdcp_c_plane_encap(
3584                                         priv->flc_desc[0].desc, 1, swap,
3585                                         pdcp_xform->hfn,
3586                                         session->pdcp.sn_size,
3587                                         pdcp_xform->bearer,
3588                                         pdcp_xform->pkt_dir,
3589                                         pdcp_xform->hfn_threshold,
3590                                         &cipherdata, &authdata);
3591                 else if (session->dir == DIR_DEC)
3592                         bufsize = cnstr_shdsc_pdcp_c_plane_decap(
3593                                         priv->flc_desc[0].desc, 1, swap,
3594                                         pdcp_xform->hfn,
3595                                         session->pdcp.sn_size,
3596                                         pdcp_xform->bearer,
3597                                         pdcp_xform->pkt_dir,
3598                                         pdcp_xform->hfn_threshold,
3599                                         &cipherdata, &authdata);
3600
3601         } else if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_SHORT_MAC) {
3602                 bufsize = cnstr_shdsc_pdcp_short_mac(priv->flc_desc[0].desc,
3603                                                      1, swap, &authdata);
3604         } else {
3605                 if (session->dir == DIR_ENC) {
3606                         if (pdcp_xform->sdap_enabled)
3607                                 bufsize = cnstr_shdsc_pdcp_sdap_u_plane_encap(
3608                                         priv->flc_desc[0].desc, 1, swap,
3609                                         session->pdcp.sn_size,
3610                                         pdcp_xform->hfn,
3611                                         pdcp_xform->bearer,
3612                                         pdcp_xform->pkt_dir,
3613                                         pdcp_xform->hfn_threshold,
3614                                         &cipherdata, p_authdata);
3615                         else
3616                                 bufsize = cnstr_shdsc_pdcp_u_plane_encap(
3617                                         priv->flc_desc[0].desc, 1, swap,
3618                                         session->pdcp.sn_size,
3619                                         pdcp_xform->hfn,
3620                                         pdcp_xform->bearer,
3621                                         pdcp_xform->pkt_dir,
3622                                         pdcp_xform->hfn_threshold,
3623                                         &cipherdata, p_authdata);
3624                 } else if (session->dir == DIR_DEC) {
3625                         if (pdcp_xform->sdap_enabled)
3626                                 bufsize = cnstr_shdsc_pdcp_sdap_u_plane_decap(
3627                                         priv->flc_desc[0].desc, 1, swap,
3628                                         session->pdcp.sn_size,
3629                                         pdcp_xform->hfn,
3630                                         pdcp_xform->bearer,
3631                                         pdcp_xform->pkt_dir,
3632                                         pdcp_xform->hfn_threshold,
3633                                         &cipherdata, p_authdata);
3634                         else
3635                                 bufsize = cnstr_shdsc_pdcp_u_plane_decap(
3636                                         priv->flc_desc[0].desc, 1, swap,
3637                                         session->pdcp.sn_size,
3638                                         pdcp_xform->hfn,
3639                                         pdcp_xform->bearer,
3640                                         pdcp_xform->pkt_dir,
3641                                         pdcp_xform->hfn_threshold,
3642                                         &cipherdata, p_authdata);
3643                 }
3644         }
3645
3646         if (bufsize < 0) {
3647                 DPAA2_SEC_ERR("Crypto: Invalid buffer length");
3648                 goto out;
3649         }
3650
3651         /* Enable the stashing control bit */
3652         DPAA2_SET_FLC_RSC(flc);
3653         flc->word2_rflc_31_0 = lower_32_bits(
3654                         (size_t)&(((struct dpaa2_sec_qp *)
3655                         dev->data->queue_pairs[0])->rx_vq) | 0x14);
3656         flc->word3_rflc_63_32 = upper_32_bits(
3657                         (size_t)&(((struct dpaa2_sec_qp *)
3658                         dev->data->queue_pairs[0])->rx_vq));
3659
3660         flc->word1_sdl = (uint8_t)bufsize;
3661
3662         /* TODO - check the perf impact or
3663          * align as per descriptor type
3664          * Set EWS bit i.e. enable write-safe
3665          * DPAA2_SET_FLC_EWS(flc);
3666          */
3667
3668         /* Set BS = 1 i.e reuse input buffers as output buffers */
3669         DPAA2_SET_FLC_REUSE_BS(flc);
3670         /* Set FF = 10; reuse input buffers if they provide sufficient space */
3671         DPAA2_SET_FLC_REUSE_FF(flc);
3672
3673         session->ctxt = priv;
3674
3675         return 0;
3676 out:
3677         rte_free(session->auth_key.data);
3678         rte_free(session->cipher_key.data);
3679         rte_free(priv);
3680         return -EINVAL;
3681 }
3682
3683 static int
3684 dpaa2_sec_security_session_create(void *dev,
3685                                   struct rte_security_session_conf *conf,
3686                                   struct rte_security_session *sess,
3687                                   struct rte_mempool *mempool)
3688 {
3689         void *sess_private_data;
3690         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
3691         int ret;
3692
3693         if (rte_mempool_get(mempool, &sess_private_data)) {
3694                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
3695                 return -ENOMEM;
3696         }
3697
3698         switch (conf->protocol) {
3699         case RTE_SECURITY_PROTOCOL_IPSEC:
3700                 ret = dpaa2_sec_set_ipsec_session(cdev, conf,
3701                                 sess_private_data);
3702                 break;
3703         case RTE_SECURITY_PROTOCOL_MACSEC:
3704                 return -ENOTSUP;
3705         case RTE_SECURITY_PROTOCOL_PDCP:
3706                 ret = dpaa2_sec_set_pdcp_session(cdev, conf,
3707                                 sess_private_data);
3708                 break;
3709         default:
3710                 return -EINVAL;
3711         }
3712         if (ret != 0) {
3713                 DPAA2_SEC_ERR("Failed to configure session parameters");
3714                 /* Return session to mempool */
3715                 rte_mempool_put(mempool, sess_private_data);
3716                 return ret;
3717         }
3718
3719         set_sec_session_private_data(sess, sess_private_data);
3720
3721         return ret;
3722 }
3723
3724 /** Clear the memory of session so it doesn't leave key material behind */
3725 static int
3726 dpaa2_sec_security_session_destroy(void *dev __rte_unused,
3727                 struct rte_security_session *sess)
3728 {
3729         PMD_INIT_FUNC_TRACE();
3730         void *sess_priv = get_sec_session_private_data(sess);
3731
3732         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3733
3734         if (sess_priv) {
3735                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3736
3737                 rte_free(s->ctxt);
3738                 rte_free(s->cipher_key.data);
3739                 rte_free(s->auth_key.data);
3740                 memset(s, 0, sizeof(dpaa2_sec_session));
3741                 set_sec_session_private_data(sess, NULL);
3742                 rte_mempool_put(sess_mp, sess_priv);
3743         }
3744         return 0;
3745 }
3746 #endif
3747 static int
3748 dpaa2_sec_sym_session_configure(struct rte_cryptodev *dev,
3749                 struct rte_crypto_sym_xform *xform,
3750                 struct rte_cryptodev_sym_session *sess,
3751                 struct rte_mempool *mempool)
3752 {
3753         void *sess_private_data;
3754         int ret;
3755
3756         if (rte_mempool_get(mempool, &sess_private_data)) {
3757                 DPAA2_SEC_ERR("Couldn't get object from session mempool");
3758                 return -ENOMEM;
3759         }
3760
3761         ret = dpaa2_sec_set_session_parameters(dev, xform, sess_private_data);
3762         if (ret != 0) {
3763                 DPAA2_SEC_ERR("Failed to configure session parameters");
3764                 /* Return session to mempool */
3765                 rte_mempool_put(mempool, sess_private_data);
3766                 return ret;
3767         }
3768
3769         set_sym_session_private_data(sess, dev->driver_id,
3770                 sess_private_data);
3771
3772         return 0;
3773 }
3774
3775 /** Clear the memory of session so it doesn't leave key material behind */
3776 static void
3777 dpaa2_sec_sym_session_clear(struct rte_cryptodev *dev,
3778                 struct rte_cryptodev_sym_session *sess)
3779 {
3780         PMD_INIT_FUNC_TRACE();
3781         uint8_t index = dev->driver_id;
3782         void *sess_priv = get_sym_session_private_data(sess, index);
3783         dpaa2_sec_session *s = (dpaa2_sec_session *)sess_priv;
3784
3785         if (sess_priv) {
3786                 rte_free(s->ctxt);
3787                 rte_free(s->cipher_key.data);
3788                 rte_free(s->auth_key.data);
3789                 memset(s, 0, sizeof(dpaa2_sec_session));
3790                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
3791                 set_sym_session_private_data(sess, index, NULL);
3792                 rte_mempool_put(sess_mp, sess_priv);
3793         }
3794 }
3795
3796 static int
3797 dpaa2_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3798                         struct rte_cryptodev_config *config __rte_unused)
3799 {
3800         PMD_INIT_FUNC_TRACE();
3801
3802         return 0;
3803 }
3804
3805 static int
3806 dpaa2_sec_dev_start(struct rte_cryptodev *dev)
3807 {
3808         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3809         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3810         struct dpseci_attr attr;
3811         struct dpaa2_queue *dpaa2_q;
3812         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3813                                         dev->data->queue_pairs;
3814         struct dpseci_rx_queue_attr rx_attr;
3815         struct dpseci_tx_queue_attr tx_attr;
3816         int ret, i;
3817
3818         PMD_INIT_FUNC_TRACE();
3819
3820         /* Change the tx burst function if ordered queues are used */
3821         if (priv->en_ordered)
3822                 dev->enqueue_burst = dpaa2_sec_enqueue_burst_ordered;
3823
3824         memset(&attr, 0, sizeof(struct dpseci_attr));
3825
3826         ret = dpseci_enable(dpseci, CMD_PRI_LOW, priv->token);
3827         if (ret) {
3828                 DPAA2_SEC_ERR("DPSECI with HW_ID = %d ENABLE FAILED",
3829                               priv->hw_id);
3830                 goto get_attr_failure;
3831         }
3832         ret = dpseci_get_attributes(dpseci, CMD_PRI_LOW, priv->token, &attr);
3833         if (ret) {
3834                 DPAA2_SEC_ERR("DPSEC ATTRIBUTE READ FAILED, disabling DPSEC");
3835                 goto get_attr_failure;
3836         }
3837         for (i = 0; i < attr.num_rx_queues && qp[i]; i++) {
3838                 dpaa2_q = &qp[i]->rx_vq;
3839                 dpseci_get_rx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3840                                     &rx_attr);
3841                 dpaa2_q->fqid = rx_attr.fqid;
3842                 DPAA2_SEC_DEBUG("rx_fqid: %d", dpaa2_q->fqid);
3843         }
3844         for (i = 0; i < attr.num_tx_queues && qp[i]; i++) {
3845                 dpaa2_q = &qp[i]->tx_vq;
3846                 dpseci_get_tx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
3847                                     &tx_attr);
3848                 dpaa2_q->fqid = tx_attr.fqid;
3849                 DPAA2_SEC_DEBUG("tx_fqid: %d", dpaa2_q->fqid);
3850         }
3851
3852         return 0;
3853 get_attr_failure:
3854         dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3855         return -1;
3856 }
3857
3858 static void
3859 dpaa2_sec_dev_stop(struct rte_cryptodev *dev)
3860 {
3861         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3862         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
3863         int ret;
3864
3865         PMD_INIT_FUNC_TRACE();
3866
3867         ret = dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
3868         if (ret) {
3869                 DPAA2_SEC_ERR("Failure in disabling dpseci %d device",
3870                              priv->hw_id);
3871                 return;
3872         }
3873
3874         ret = dpseci_reset(dpseci, CMD_PRI_LOW, priv->token);
3875         if (ret < 0) {
3876                 DPAA2_SEC_ERR("SEC Device cannot be reset:Error = %0x", ret);
3877                 return;
3878         }
3879 }
3880
3881 static int
3882 dpaa2_sec_dev_close(struct rte_cryptodev *dev __rte_unused)
3883 {
3884         PMD_INIT_FUNC_TRACE();
3885
3886         return 0;
3887 }
3888
3889 static void
3890 dpaa2_sec_dev_infos_get(struct rte_cryptodev *dev,
3891                         struct rte_cryptodev_info *info)
3892 {
3893         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
3894
3895         PMD_INIT_FUNC_TRACE();
3896         if (info != NULL) {
3897                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3898                 info->feature_flags = dev->feature_flags;
3899                 info->capabilities = dpaa2_sec_capabilities;
3900                 /* No limit of number of sessions */
3901                 info->sym.max_nb_sessions = 0;
3902                 info->driver_id = cryptodev_driver_id;
3903         }
3904 }
3905
3906 static
3907 void dpaa2_sec_stats_get(struct rte_cryptodev *dev,
3908                          struct rte_cryptodev_stats *stats)
3909 {
3910         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
3911         struct fsl_mc_io dpseci;
3912         struct dpseci_sec_counters counters = {0};
3913         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3914                                         dev->data->queue_pairs;
3915         int ret, i;
3916
3917         PMD_INIT_FUNC_TRACE();
3918         if (stats == NULL) {
3919                 DPAA2_SEC_ERR("Invalid stats ptr NULL");
3920                 return;
3921         }
3922         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3923                 if (qp == NULL || qp[i] == NULL) {
3924                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3925                         continue;
3926                 }
3927
3928                 stats->enqueued_count += qp[i]->tx_vq.tx_pkts;
3929                 stats->dequeued_count += qp[i]->rx_vq.rx_pkts;
3930                 stats->enqueue_err_count += qp[i]->tx_vq.err_pkts;
3931                 stats->dequeue_err_count += qp[i]->rx_vq.err_pkts;
3932         }
3933
3934         /* In case as secondary process access stats, MCP portal in priv-hw
3935          * may have primary process address. Need the secondary process
3936          * based MCP portal address for this object.
3937          */
3938         dpseci.regs = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
3939         ret = dpseci_get_sec_counters(&dpseci, CMD_PRI_LOW, priv->token,
3940                                       &counters);
3941         if (ret) {
3942                 DPAA2_SEC_ERR("SEC counters failed");
3943         } else {
3944                 DPAA2_SEC_INFO("dpseci hardware stats:"
3945                             "\n\tNum of Requests Dequeued = %" PRIu64
3946                             "\n\tNum of Outbound Encrypt Requests = %" PRIu64
3947                             "\n\tNum of Inbound Decrypt Requests = %" PRIu64
3948                             "\n\tNum of Outbound Bytes Encrypted = %" PRIu64
3949                             "\n\tNum of Outbound Bytes Protected = %" PRIu64
3950                             "\n\tNum of Inbound Bytes Decrypted = %" PRIu64
3951                             "\n\tNum of Inbound Bytes Validated = %" PRIu64,
3952                             counters.dequeued_requests,
3953                             counters.ob_enc_requests,
3954                             counters.ib_dec_requests,
3955                             counters.ob_enc_bytes,
3956                             counters.ob_prot_bytes,
3957                             counters.ib_dec_bytes,
3958                             counters.ib_valid_bytes);
3959         }
3960 }
3961
3962 static
3963 void dpaa2_sec_stats_reset(struct rte_cryptodev *dev)
3964 {
3965         int i;
3966         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
3967                                    (dev->data->queue_pairs);
3968
3969         PMD_INIT_FUNC_TRACE();
3970
3971         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
3972                 if (qp[i] == NULL) {
3973                         DPAA2_SEC_DEBUG("Uninitialised queue pair");
3974                         continue;
3975                 }
3976                 qp[i]->tx_vq.rx_pkts = 0;
3977                 qp[i]->tx_vq.tx_pkts = 0;
3978                 qp[i]->tx_vq.err_pkts = 0;
3979                 qp[i]->rx_vq.rx_pkts = 0;
3980                 qp[i]->rx_vq.tx_pkts = 0;
3981                 qp[i]->rx_vq.err_pkts = 0;
3982         }
3983 }
3984
3985 static void __rte_hot
3986 dpaa2_sec_process_parallel_event(struct qbman_swp *swp,
3987                                  const struct qbman_fd *fd,
3988                                  const struct qbman_result *dq,
3989                                  struct dpaa2_queue *rxq,
3990                                  struct rte_event *ev)
3991 {
3992         /* Prefetching mbuf */
3993         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
3994                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
3995
3996         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
3997         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
3998
3999         ev->flow_id = rxq->ev.flow_id;
4000         ev->sub_event_type = rxq->ev.sub_event_type;
4001         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
4002         ev->op = RTE_EVENT_OP_NEW;
4003         ev->sched_type = rxq->ev.sched_type;
4004         ev->queue_id = rxq->ev.queue_id;
4005         ev->priority = rxq->ev.priority;
4006         ev->event_ptr = sec_fd_to_mbuf(fd);
4007
4008         qbman_swp_dqrr_consume(swp, dq);
4009 }
4010 static void
4011 dpaa2_sec_process_atomic_event(struct qbman_swp *swp __rte_unused,
4012                                  const struct qbman_fd *fd,
4013                                  const struct qbman_result *dq,
4014                                  struct dpaa2_queue *rxq,
4015                                  struct rte_event *ev)
4016 {
4017         uint8_t dqrr_index;
4018         struct rte_crypto_op *crypto_op = (struct rte_crypto_op *)ev->event_ptr;
4019         /* Prefetching mbuf */
4020         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
4021                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
4022
4023         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
4024         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
4025
4026         ev->flow_id = rxq->ev.flow_id;
4027         ev->sub_event_type = rxq->ev.sub_event_type;
4028         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
4029         ev->op = RTE_EVENT_OP_NEW;
4030         ev->sched_type = rxq->ev.sched_type;
4031         ev->queue_id = rxq->ev.queue_id;
4032         ev->priority = rxq->ev.priority;
4033
4034         ev->event_ptr = sec_fd_to_mbuf(fd);
4035         dqrr_index = qbman_get_dqrr_idx(dq);
4036         *dpaa2_seqn(crypto_op->sym->m_src) = QBMAN_ENQUEUE_FLAG_DCA | dqrr_index;
4037         DPAA2_PER_LCORE_DQRR_SIZE++;
4038         DPAA2_PER_LCORE_DQRR_HELD |= 1 << dqrr_index;
4039         DPAA2_PER_LCORE_DQRR_MBUF(dqrr_index) = crypto_op->sym->m_src;
4040 }
4041
4042 static void __rte_hot
4043 dpaa2_sec_process_ordered_event(struct qbman_swp *swp,
4044                                 const struct qbman_fd *fd,
4045                                 const struct qbman_result *dq,
4046                                 struct dpaa2_queue *rxq,
4047                                 struct rte_event *ev)
4048 {
4049         struct rte_crypto_op *crypto_op = (struct rte_crypto_op *)ev->event_ptr;
4050
4051         /* Prefetching mbuf */
4052         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-
4053                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size));
4054
4055         /* Prefetching ipsec crypto_op stored in priv data of mbuf */
4056         rte_prefetch0((void *)(size_t)(DPAA2_GET_FD_ADDR(fd)-64));
4057
4058         ev->flow_id = rxq->ev.flow_id;
4059         ev->sub_event_type = rxq->ev.sub_event_type;
4060         ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
4061         ev->op = RTE_EVENT_OP_NEW;
4062         ev->sched_type = rxq->ev.sched_type;
4063         ev->queue_id = rxq->ev.queue_id;
4064         ev->priority = rxq->ev.priority;
4065         ev->event_ptr = sec_fd_to_mbuf(fd);
4066
4067         *dpaa2_seqn(crypto_op->sym->m_src) = DPAA2_ENQUEUE_FLAG_ORP;
4068         *dpaa2_seqn(crypto_op->sym->m_src) |= qbman_result_DQ_odpid(dq) <<
4069                 DPAA2_EQCR_OPRID_SHIFT;
4070         *dpaa2_seqn(crypto_op->sym->m_src) |= qbman_result_DQ_seqnum(dq) <<
4071                 DPAA2_EQCR_SEQNUM_SHIFT;
4072
4073         qbman_swp_dqrr_consume(swp, dq);
4074 }
4075
4076 int
4077 dpaa2_sec_eventq_attach(const struct rte_cryptodev *dev,
4078                 int qp_id,
4079                 struct dpaa2_dpcon_dev *dpcon,
4080                 const struct rte_event *event)
4081 {
4082         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
4083         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
4084         struct dpaa2_sec_qp *qp = dev->data->queue_pairs[qp_id];
4085         struct dpseci_rx_queue_cfg cfg;
4086         uint8_t priority;
4087         int ret;
4088
4089         if (event->sched_type == RTE_SCHED_TYPE_PARALLEL)
4090                 qp->rx_vq.cb = dpaa2_sec_process_parallel_event;
4091         else if (event->sched_type == RTE_SCHED_TYPE_ATOMIC)
4092                 qp->rx_vq.cb = dpaa2_sec_process_atomic_event;
4093         else if (event->sched_type == RTE_SCHED_TYPE_ORDERED)
4094                 qp->rx_vq.cb = dpaa2_sec_process_ordered_event;
4095         else
4096                 return -EINVAL;
4097
4098         priority = (RTE_EVENT_DEV_PRIORITY_LOWEST / event->priority) *
4099                    (dpcon->num_priorities - 1);
4100
4101         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
4102         cfg.options = DPSECI_QUEUE_OPT_DEST;
4103         cfg.dest_cfg.dest_type = DPSECI_DEST_DPCON;
4104         cfg.dest_cfg.dest_id = dpcon->dpcon_id;
4105         cfg.dest_cfg.priority = priority;
4106
4107         cfg.options |= DPSECI_QUEUE_OPT_USER_CTX;
4108         cfg.user_ctx = (size_t)(qp);
4109         if (event->sched_type == RTE_SCHED_TYPE_ATOMIC) {
4110                 cfg.options |= DPSECI_QUEUE_OPT_ORDER_PRESERVATION;
4111                 cfg.order_preservation_en = 1;
4112         }
4113
4114         if (event->sched_type == RTE_SCHED_TYPE_ORDERED) {
4115                 struct opr_cfg ocfg;
4116
4117                 /* Restoration window size = 256 frames */
4118                 ocfg.oprrws = 3;
4119                 /* Restoration window size = 512 frames for LX2 */
4120                 if (dpaa2_svr_family == SVR_LX2160A)
4121                         ocfg.oprrws = 4;
4122                 /* Auto advance NESN window enabled */
4123                 ocfg.oa = 1;
4124                 /* Late arrival window size disabled */
4125                 ocfg.olws = 0;
4126                 /* ORL resource exhaustaion advance NESN disabled */
4127                 ocfg.oeane = 0;
4128
4129                 if (priv->en_loose_ordered)
4130                         ocfg.oloe = 1;
4131                 else
4132                         ocfg.oloe = 0;
4133
4134                 ret = dpseci_set_opr(dpseci, CMD_PRI_LOW, priv->token,
4135                                    qp_id, OPR_OPT_CREATE, &ocfg);
4136                 if (ret) {
4137                         RTE_LOG(ERR, PMD, "Error setting opr: ret: %d\n", ret);
4138                         return ret;
4139                 }
4140                 qp->tx_vq.cb_eqresp_free = dpaa2_sec_free_eqresp_buf;
4141                 priv->en_ordered = 1;
4142         }
4143
4144         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
4145                                   qp_id, &cfg);
4146         if (ret) {
4147                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
4148                 return ret;
4149         }
4150
4151         memcpy(&qp->rx_vq.ev, event, sizeof(struct rte_event));
4152
4153         return 0;
4154 }
4155
4156 int
4157 dpaa2_sec_eventq_detach(const struct rte_cryptodev *dev,
4158                         int qp_id)
4159 {
4160         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
4161         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
4162         struct dpseci_rx_queue_cfg cfg;
4163         int ret;
4164
4165         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
4166         cfg.options = DPSECI_QUEUE_OPT_DEST;
4167         cfg.dest_cfg.dest_type = DPSECI_DEST_NONE;
4168
4169         ret = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
4170                                   qp_id, &cfg);
4171         if (ret)
4172                 RTE_LOG(ERR, PMD, "Error in dpseci_set_queue: ret: %d\n", ret);
4173
4174         return ret;
4175 }
4176
4177 static struct rte_cryptodev_ops crypto_ops = {
4178         .dev_configure        = dpaa2_sec_dev_configure,
4179         .dev_start            = dpaa2_sec_dev_start,
4180         .dev_stop             = dpaa2_sec_dev_stop,
4181         .dev_close            = dpaa2_sec_dev_close,
4182         .dev_infos_get        = dpaa2_sec_dev_infos_get,
4183         .stats_get            = dpaa2_sec_stats_get,
4184         .stats_reset          = dpaa2_sec_stats_reset,
4185         .queue_pair_setup     = dpaa2_sec_queue_pair_setup,
4186         .queue_pair_release   = dpaa2_sec_queue_pair_release,
4187         .sym_session_get_size     = dpaa2_sec_sym_session_get_size,
4188         .sym_session_configure    = dpaa2_sec_sym_session_configure,
4189         .sym_session_clear        = dpaa2_sec_sym_session_clear,
4190         /* Raw data-path API related operations */
4191         .sym_get_raw_dp_ctx_size = dpaa2_sec_get_dp_ctx_size,
4192         .sym_configure_raw_dp_ctx = dpaa2_sec_configure_raw_dp_ctx,
4193 };
4194
4195 #ifdef RTE_LIB_SECURITY
4196 static const struct rte_security_capability *
4197 dpaa2_sec_capabilities_get(void *device __rte_unused)
4198 {
4199         return dpaa2_sec_security_cap;
4200 }
4201
4202 static const struct rte_security_ops dpaa2_sec_security_ops = {
4203         .session_create = dpaa2_sec_security_session_create,
4204         .session_update = NULL,
4205         .session_stats_get = NULL,
4206         .session_destroy = dpaa2_sec_security_session_destroy,
4207         .set_pkt_metadata = NULL,
4208         .capabilities_get = dpaa2_sec_capabilities_get
4209 };
4210 #endif
4211
4212 static int
4213 dpaa2_sec_uninit(const struct rte_cryptodev *dev)
4214 {
4215         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
4216         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
4217         int ret;
4218
4219         PMD_INIT_FUNC_TRACE();
4220
4221         /* Function is reverse of dpaa2_sec_dev_init.
4222          * It does the following:
4223          * 1. Detach a DPSECI from attached resources i.e. buffer pools, dpbp_id
4224          * 2. Close the DPSECI device
4225          * 3. Free the allocated resources.
4226          */
4227
4228         /*Close the device at underlying layer*/
4229         ret = dpseci_close(dpseci, CMD_PRI_LOW, priv->token);
4230         if (ret) {
4231                 DPAA2_SEC_ERR("Failure closing dpseci device: err(%d)", ret);
4232                 return -1;
4233         }
4234
4235         /*Free the allocated memory for ethernet private data and dpseci*/
4236         priv->hw = NULL;
4237         rte_free(dpseci);
4238         rte_free(dev->security_ctx);
4239         rte_mempool_free(priv->fle_pool);
4240
4241         DPAA2_SEC_INFO("Closing DPAA2_SEC device %s on numa socket %u",
4242                        dev->data->name, rte_socket_id());
4243
4244         return 0;
4245 }
4246
4247 static int
4248 check_devargs_handler(const char *key, const char *value,
4249                       void *opaque)
4250 {
4251         struct rte_cryptodev *dev = (struct rte_cryptodev *)opaque;
4252         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
4253
4254         if (!strcmp(key, "drv_strict_order")) {
4255                 priv->en_loose_ordered = false;
4256         } else if (!strcmp(key, "drv_dump_mode")) {
4257                 dpaa2_sec_dp_dump = atoi(value);
4258                 if (dpaa2_sec_dp_dump > DPAA2_SEC_DP_FULL_DUMP) {
4259                         DPAA2_SEC_WARN("WARN: DPAA2_SEC_DP_DUMP_LEVEL is not "
4260                                       "supported, changing to FULL error"
4261                                       " prints\n");
4262                         dpaa2_sec_dp_dump = DPAA2_SEC_DP_FULL_DUMP;
4263                 }
4264         } else
4265                 return -1;
4266
4267         return 0;
4268 }
4269
4270 static void
4271 dpaa2_sec_get_devargs(struct rte_cryptodev *cryptodev, const char *key)
4272 {
4273         struct rte_kvargs *kvlist;
4274         struct rte_devargs *devargs;
4275
4276         devargs = cryptodev->device->devargs;
4277         if (!devargs)
4278                 return;
4279
4280         kvlist = rte_kvargs_parse(devargs->args, NULL);
4281         if (!kvlist)
4282                 return;
4283
4284         if (!rte_kvargs_count(kvlist, key)) {
4285                 rte_kvargs_free(kvlist);
4286                 return;
4287         }
4288
4289         rte_kvargs_process(kvlist, key,
4290                         check_devargs_handler, (void *)cryptodev);
4291         rte_kvargs_free(kvlist);
4292 }
4293
4294 static int
4295 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
4296 {
4297         struct dpaa2_sec_dev_private *internals;
4298         struct rte_device *dev = cryptodev->device;
4299         struct rte_dpaa2_device *dpaa2_dev;
4300 #ifdef RTE_LIB_SECURITY
4301         struct rte_security_ctx *security_instance;
4302 #endif
4303         struct fsl_mc_io *dpseci;
4304         uint16_t token;
4305         struct dpseci_attr attr;
4306         int retcode, hw_id;
4307         char str[30];
4308
4309         PMD_INIT_FUNC_TRACE();
4310         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
4311         hw_id = dpaa2_dev->object_id;
4312
4313         cryptodev->driver_id = cryptodev_driver_id;
4314         cryptodev->dev_ops = &crypto_ops;
4315
4316         cryptodev->enqueue_burst = dpaa2_sec_enqueue_burst;
4317         cryptodev->dequeue_burst = dpaa2_sec_dequeue_burst;
4318         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
4319                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
4320                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
4321                         RTE_CRYPTODEV_FF_SECURITY |
4322                         RTE_CRYPTODEV_FF_SYM_RAW_DP |
4323                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
4324                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
4325                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
4326                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
4327                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
4328
4329         internals = cryptodev->data->dev_private;
4330
4331         /*
4332          * For secondary processes, we don't initialise any further as primary
4333          * has already done this work. Only check we don't need a different
4334          * RX function
4335          */
4336         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
4337                 DPAA2_SEC_DEBUG("Device already init by primary process");
4338                 return 0;
4339         }
4340 #ifdef RTE_LIB_SECURITY
4341         /* Initialize security_ctx only for primary process*/
4342         security_instance = rte_malloc("rte_security_instances_ops",
4343                                 sizeof(struct rte_security_ctx), 0);
4344         if (security_instance == NULL)
4345                 return -ENOMEM;
4346         security_instance->device = (void *)cryptodev;
4347         security_instance->ops = &dpaa2_sec_security_ops;
4348         security_instance->sess_cnt = 0;
4349         cryptodev->security_ctx = security_instance;
4350 #endif
4351         /*Open the rte device via MC and save the handle for further use*/
4352         dpseci = (struct fsl_mc_io *)rte_calloc(NULL, 1,
4353                                 sizeof(struct fsl_mc_io), 0);
4354         if (!dpseci) {
4355                 DPAA2_SEC_ERR(
4356                         "Error in allocating the memory for dpsec object");
4357                 return -ENOMEM;
4358         }
4359         dpseci->regs = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
4360
4361         retcode = dpseci_open(dpseci, CMD_PRI_LOW, hw_id, &token);
4362         if (retcode != 0) {
4363                 DPAA2_SEC_ERR("Cannot open the dpsec device: Error = %x",
4364                               retcode);
4365                 goto init_error;
4366         }
4367         retcode = dpseci_get_attributes(dpseci, CMD_PRI_LOW, token, &attr);
4368         if (retcode != 0) {
4369                 DPAA2_SEC_ERR(
4370                              "Cannot get dpsec device attributed: Error = %x",
4371                              retcode);
4372                 goto init_error;
4373         }
4374         snprintf(cryptodev->data->name, sizeof(cryptodev->data->name),
4375                         "dpsec-%u", hw_id);
4376
4377         internals->max_nb_queue_pairs = attr.num_tx_queues;
4378         cryptodev->data->nb_queue_pairs = internals->max_nb_queue_pairs;
4379         internals->hw = dpseci;
4380         internals->token = token;
4381         internals->en_loose_ordered = true;
4382
4383         snprintf(str, sizeof(str), "sec_fle_pool_p%d_%d",
4384                         getpid(), cryptodev->data->dev_id);
4385         internals->fle_pool = rte_mempool_create((const char *)str,
4386                         FLE_POOL_NUM_BUFS,
4387                         FLE_POOL_BUF_SIZE,
4388                         FLE_POOL_CACHE_SIZE, 0,
4389                         NULL, NULL, NULL, NULL,
4390                         SOCKET_ID_ANY, 0);
4391         if (!internals->fle_pool) {
4392                 DPAA2_SEC_ERR("Mempool (%s) creation failed", str);
4393                 goto init_error;
4394         }
4395
4396         dpaa2_sec_get_devargs(cryptodev, DRIVER_DUMP_MODE);
4397         dpaa2_sec_get_devargs(cryptodev, DRIVER_STRICT_ORDER);
4398         DPAA2_SEC_INFO("driver %s: created", cryptodev->data->name);
4399         return 0;
4400
4401 init_error:
4402         DPAA2_SEC_ERR("driver %s: create failed", cryptodev->data->name);
4403
4404         /* dpaa2_sec_uninit(crypto_dev_name); */
4405         return -EFAULT;
4406 }
4407
4408 static int
4409 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
4410                           struct rte_dpaa2_device *dpaa2_dev)
4411 {
4412         struct rte_cryptodev *cryptodev;
4413         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
4414
4415         int retval;
4416
4417         snprintf(cryptodev_name, sizeof(cryptodev_name), "dpsec-%d",
4418                         dpaa2_dev->object_id);
4419
4420         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
4421         if (cryptodev == NULL)
4422                 return -ENOMEM;
4423
4424         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
4425                 cryptodev->data->dev_private = rte_zmalloc_socket(
4426                                         "cryptodev private structure",
4427                                         sizeof(struct dpaa2_sec_dev_private),
4428                                         RTE_CACHE_LINE_SIZE,
4429                                         rte_socket_id());
4430
4431                 if (cryptodev->data->dev_private == NULL)
4432                         rte_panic("Cannot allocate memzone for private "
4433                                   "device data");
4434         }
4435
4436         dpaa2_dev->cryptodev = cryptodev;
4437         cryptodev->device = &dpaa2_dev->device;
4438
4439         /* init user callbacks */
4440         TAILQ_INIT(&(cryptodev->link_intr_cbs));
4441
4442         if (dpaa2_svr_family == SVR_LX2160A)
4443                 rta_set_sec_era(RTA_SEC_ERA_10);
4444         else
4445                 rta_set_sec_era(RTA_SEC_ERA_8);
4446
4447         DPAA2_SEC_INFO("2-SEC ERA is %d", rta_get_sec_era());
4448
4449         /* Invoke PMD device initialization function */
4450         retval = dpaa2_sec_dev_init(cryptodev);
4451         if (retval == 0) {
4452                 rte_cryptodev_pmd_probing_finish(cryptodev);
4453                 return 0;
4454         }
4455
4456         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
4457                 rte_free(cryptodev->data->dev_private);
4458
4459         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
4460
4461         return -ENXIO;
4462 }
4463
4464 static int
4465 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
4466 {
4467         struct rte_cryptodev *cryptodev;
4468         int ret;
4469
4470         cryptodev = dpaa2_dev->cryptodev;
4471         if (cryptodev == NULL)
4472                 return -ENODEV;
4473
4474         ret = dpaa2_sec_uninit(cryptodev);
4475         if (ret)
4476                 return ret;
4477
4478         return rte_cryptodev_pmd_destroy(cryptodev);
4479 }
4480
4481 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
4482         .drv_flags = RTE_DPAA2_DRV_IOVA_AS_VA,
4483         .drv_type = DPAA2_CRYPTO,
4484         .driver = {
4485                 .name = "DPAA2 SEC PMD"
4486         },
4487         .probe = cryptodev_dpaa2_sec_probe,
4488         .remove = cryptodev_dpaa2_sec_remove,
4489 };
4490
4491 static struct cryptodev_driver dpaa2_sec_crypto_drv;
4492
4493 RTE_PMD_REGISTER_DPAA2(CRYPTODEV_NAME_DPAA2_SEC_PMD, rte_dpaa2_sec_driver);
4494 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa2_sec_crypto_drv,
4495                 rte_dpaa2_sec_driver.driver, cryptodev_driver_id);
4496 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_DPAA2_SEC_PMD,
4497                 DRIVER_STRICT_ORDER "=<int>"
4498                 DRIVER_DUMP_MODE "=<int>");
4499 RTE_LOG_REGISTER(dpaa2_logtype_sec, pmd.crypto.dpaa2, NOTICE);