crypto/dpaax_sec: support PDCP 12-bit C-Plane
[dpdk.git] / drivers / crypto / dpaa_sec / dpaa_sec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2017-2019 NXP
5  *
6  */
7
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <sched.h>
11 #include <net/if.h>
12
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15 #include <rte_cryptodev_pmd.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #include <rte_security_driver.h>
19 #include <rte_cycles.h>
20 #include <rte_dev.h>
21 #include <rte_kvargs.h>
22 #include <rte_malloc.h>
23 #include <rte_mbuf.h>
24 #include <rte_memcpy.h>
25 #include <rte_string_fns.h>
26 #include <rte_spinlock.h>
27
28 #include <fsl_usd.h>
29 #include <fsl_qman.h>
30 #include <of.h>
31
32 /* RTA header files */
33 #include <hw/desc/common.h>
34 #include <hw/desc/algo.h>
35 #include <hw/desc/ipsec.h>
36 #include <hw/desc/pdcp.h>
37
38 #include <rte_dpaa_bus.h>
39 #include <dpaa_sec.h>
40 #include <dpaa_sec_log.h>
41 #include <dpaax_iova_table.h>
42
43 enum rta_sec_era rta_sec_era;
44
45 int dpaa_logtype_sec;
46
47 static uint8_t cryptodev_driver_id;
48
49 static __thread struct rte_crypto_op **dpaa_sec_ops;
50 static __thread int dpaa_sec_op_nb;
51
52 static int
53 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
54
55 static inline void
56 dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
57 {
58         if (!ctx->fd_status) {
59                 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
60         } else {
61                 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
62                 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
63         }
64
65         /* report op status to sym->op and then free the ctx memory  */
66         rte_mempool_put(ctx->ctx_pool, (void *)ctx);
67 }
68
69 static inline struct dpaa_sec_op_ctx *
70 dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
71 {
72         struct dpaa_sec_op_ctx *ctx;
73         int retval;
74
75         retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
76         if (!ctx || retval) {
77                 DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
78                 return NULL;
79         }
80         /*
81          * Clear SG memory. There are 16 SG entries of 16 Bytes each.
82          * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
83          * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
84          * each packet, memset is costlier than dcbz_64().
85          */
86         dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
87         dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
88         dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
89         dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
90
91         ctx->ctx_pool = ses->ctx_pool;
92         ctx->vtop_offset = (size_t) ctx
93                                 - rte_mempool_virt2iova(ctx);
94
95         return ctx;
96 }
97
98 static inline rte_iova_t
99 dpaa_mem_vtop(void *vaddr)
100 {
101         const struct rte_memseg *ms;
102
103         ms = rte_mem_virt2memseg(vaddr, NULL);
104         if (ms) {
105                 dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
106                 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
107         }
108         return (size_t)NULL;
109 }
110
111 static inline void *
112 dpaa_mem_ptov(rte_iova_t paddr)
113 {
114         void *va;
115
116         va = (void *)dpaax_iova_table_get_va(paddr);
117         if (likely(va))
118                 return va;
119
120         return rte_mem_iova2virt(paddr);
121 }
122
123 static void
124 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
125                    struct qman_fq *fq,
126                    const struct qm_mr_entry *msg)
127 {
128         DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
129                         fq->fqid, msg->ern.rc, msg->ern.seqnum);
130 }
131
132 /* initialize the queue with dest chan as caam chan so that
133  * all the packets in this queue could be dispatched into caam
134  */
135 static int
136 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
137                  uint32_t fqid_out)
138 {
139         struct qm_mcc_initfq fq_opts;
140         uint32_t flags;
141         int ret = -1;
142
143         /* Clear FQ options */
144         memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
145
146         flags = QMAN_INITFQ_FLAG_SCHED;
147         fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
148                           QM_INITFQ_WE_CONTEXTB;
149
150         qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
151         fq_opts.fqd.context_b = fqid_out;
152         fq_opts.fqd.dest.channel = qm_channel_caam;
153         fq_opts.fqd.dest.wq = 0;
154
155         fq_in->cb.ern  = ern_sec_fq_handler;
156
157         DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
158
159         ret = qman_init_fq(fq_in, flags, &fq_opts);
160         if (unlikely(ret != 0))
161                 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
162
163         return ret;
164 }
165
166 /* something is put into in_fq and caam put the crypto result into out_fq */
167 static enum qman_cb_dqrr_result
168 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
169                   struct qman_fq *fq __always_unused,
170                   const struct qm_dqrr_entry *dqrr)
171 {
172         const struct qm_fd *fd;
173         struct dpaa_sec_job *job;
174         struct dpaa_sec_op_ctx *ctx;
175
176         if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
177                 return qman_cb_dqrr_defer;
178
179         if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
180                 return qman_cb_dqrr_consume;
181
182         fd = &dqrr->fd;
183         /* sg is embedded in an op ctx,
184          * sg[0] is for output
185          * sg[1] for input
186          */
187         job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
188
189         ctx = container_of(job, struct dpaa_sec_op_ctx, job);
190         ctx->fd_status = fd->status;
191         if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
192                 struct qm_sg_entry *sg_out;
193                 uint32_t len;
194
195                 sg_out = &job->sg[0];
196                 hw_sg_to_cpu(sg_out);
197                 len = sg_out->length;
198                 ctx->op->sym->m_src->pkt_len = len;
199                 ctx->op->sym->m_src->data_len = len;
200         }
201         dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
202         dpaa_sec_op_ending(ctx);
203
204         return qman_cb_dqrr_consume;
205 }
206
207 /* caam result is put into this queue */
208 static int
209 dpaa_sec_init_tx(struct qman_fq *fq)
210 {
211         int ret;
212         struct qm_mcc_initfq opts;
213         uint32_t flags;
214
215         flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
216                 QMAN_FQ_FLAG_DYNAMIC_FQID;
217
218         ret = qman_create_fq(0, flags, fq);
219         if (unlikely(ret)) {
220                 DPAA_SEC_ERR("qman_create_fq failed");
221                 return ret;
222         }
223
224         memset(&opts, 0, sizeof(opts));
225         opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
226                        QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
227
228         /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
229
230         fq->cb.dqrr = dqrr_out_fq_cb_rx;
231         fq->cb.ern  = ern_sec_fq_handler;
232
233         ret = qman_init_fq(fq, 0, &opts);
234         if (unlikely(ret)) {
235                 DPAA_SEC_ERR("unable to init caam source fq!");
236                 return ret;
237         }
238
239         return ret;
240 }
241
242 static inline int is_cipher_only(dpaa_sec_session *ses)
243 {
244         return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
245                 (ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
246 }
247
248 static inline int is_auth_only(dpaa_sec_session *ses)
249 {
250         return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
251                 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
252 }
253
254 static inline int is_aead(dpaa_sec_session *ses)
255 {
256         return ((ses->cipher_alg == 0) &&
257                 (ses->auth_alg == 0) &&
258                 (ses->aead_alg != 0));
259 }
260
261 static inline int is_auth_cipher(dpaa_sec_session *ses)
262 {
263         return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
264                 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
265                 (ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
266 }
267
268 static inline int is_proto_ipsec(dpaa_sec_session *ses)
269 {
270         return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
271 }
272
273 static inline int is_proto_pdcp(dpaa_sec_session *ses)
274 {
275         return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
276 }
277
278 static inline int is_encode(dpaa_sec_session *ses)
279 {
280         return ses->dir == DIR_ENC;
281 }
282
283 static inline int is_decode(dpaa_sec_session *ses)
284 {
285         return ses->dir == DIR_DEC;
286 }
287
288 static inline void
289 caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
290 {
291         switch (ses->auth_alg) {
292         case RTE_CRYPTO_AUTH_NULL:
293                 alginfo_a->algtype =
294                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
295                         OP_PCL_IPSEC_HMAC_NULL : 0;
296                 ses->digest_length = 0;
297                 break;
298         case RTE_CRYPTO_AUTH_MD5_HMAC:
299                 alginfo_a->algtype =
300                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
301                         OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
302                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
303                 break;
304         case RTE_CRYPTO_AUTH_SHA1_HMAC:
305                 alginfo_a->algtype =
306                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
307                         OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
308                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
309                 break;
310         case RTE_CRYPTO_AUTH_SHA224_HMAC:
311                 alginfo_a->algtype =
312                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
313                         OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
314                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
315                 break;
316         case RTE_CRYPTO_AUTH_SHA256_HMAC:
317                 alginfo_a->algtype =
318                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
319                         OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
320                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
321                 break;
322         case RTE_CRYPTO_AUTH_SHA384_HMAC:
323                 alginfo_a->algtype =
324                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
325                         OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
326                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
327                 break;
328         case RTE_CRYPTO_AUTH_SHA512_HMAC:
329                 alginfo_a->algtype =
330                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
331                         OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
332                 alginfo_a->algmode = OP_ALG_AAI_HMAC;
333                 break;
334         default:
335                 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
336         }
337 }
338
339 static inline void
340 caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
341 {
342         switch (ses->cipher_alg) {
343         case RTE_CRYPTO_CIPHER_NULL:
344                 alginfo_c->algtype =
345                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
346                         OP_PCL_IPSEC_NULL : 0;
347                 break;
348         case RTE_CRYPTO_CIPHER_AES_CBC:
349                 alginfo_c->algtype =
350                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
351                         OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
352                 alginfo_c->algmode = OP_ALG_AAI_CBC;
353                 break;
354         case RTE_CRYPTO_CIPHER_3DES_CBC:
355                 alginfo_c->algtype =
356                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
357                         OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
358                 alginfo_c->algmode = OP_ALG_AAI_CBC;
359                 break;
360         case RTE_CRYPTO_CIPHER_AES_CTR:
361                 alginfo_c->algtype =
362                         (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
363                         OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
364                 alginfo_c->algmode = OP_ALG_AAI_CTR;
365                 break;
366         default:
367                 DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
368         }
369 }
370
371 static inline void
372 caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
373 {
374         switch (ses->aead_alg) {
375         case RTE_CRYPTO_AEAD_AES_GCM:
376                 alginfo->algtype = OP_ALG_ALGSEL_AES;
377                 alginfo->algmode = OP_ALG_AAI_GCM;
378                 break;
379         default:
380                 DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
381         }
382 }
383
384 static int
385 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
386 {
387         struct alginfo authdata = {0}, cipherdata = {0};
388         struct sec_cdb *cdb = &ses->cdb;
389         int32_t shared_desc_len = 0;
390         int err;
391 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
392         int swap = false;
393 #else
394         int swap = true;
395 #endif
396
397         switch (ses->cipher_alg) {
398         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
399                 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
400                 break;
401         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
402                 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
403                 break;
404         case RTE_CRYPTO_CIPHER_AES_CTR:
405                 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
406                 break;
407         case RTE_CRYPTO_CIPHER_NULL:
408                 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
409                 break;
410         default:
411                 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
412                               ses->cipher_alg);
413                 return -1;
414         }
415
416         cipherdata.key = (size_t)ses->cipher_key.data;
417         cipherdata.keylen = ses->cipher_key.length;
418         cipherdata.key_enc_flags = 0;
419         cipherdata.key_type = RTA_DATA_IMM;
420
421         if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
422                 switch (ses->auth_alg) {
423                 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
424                         authdata.algtype = PDCP_AUTH_TYPE_SNOW;
425                         break;
426                 case RTE_CRYPTO_AUTH_ZUC_EIA3:
427                         authdata.algtype = PDCP_AUTH_TYPE_ZUC;
428                         break;
429                 case RTE_CRYPTO_AUTH_AES_CMAC:
430                         authdata.algtype = PDCP_AUTH_TYPE_AES;
431                         break;
432                 case RTE_CRYPTO_AUTH_NULL:
433                         authdata.algtype = PDCP_AUTH_TYPE_NULL;
434                         break;
435                 default:
436                         DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
437                                       ses->auth_alg);
438                         return -1;
439                 }
440
441                 authdata.key = (size_t)ses->auth_key.data;
442                 authdata.keylen = ses->auth_key.length;
443                 authdata.key_enc_flags = 0;
444                 authdata.key_type = RTA_DATA_IMM;
445
446                 cdb->sh_desc[0] = cipherdata.keylen;
447                 cdb->sh_desc[1] = authdata.keylen;
448                 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
449                                        MIN_JOB_DESC_SIZE,
450                                        (unsigned int *)cdb->sh_desc,
451                                        &cdb->sh_desc[2], 2);
452
453                 if (err < 0) {
454                         DPAA_SEC_ERR("Crypto: Incorrect key lengths");
455                         return err;
456                 }
457                 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
458                         cipherdata.key = (size_t)dpaa_mem_vtop(
459                                                 (void *)(size_t)cipherdata.key);
460                         cipherdata.key_type = RTA_DATA_PTR;
461                 }
462                 if (!(cdb->sh_desc[2] & (1<<1)) &&  authdata.keylen) {
463                         authdata.key = (size_t)dpaa_mem_vtop(
464                                                 (void *)(size_t)authdata.key);
465                         authdata.key_type = RTA_DATA_PTR;
466                 }
467
468                 cdb->sh_desc[0] = 0;
469                 cdb->sh_desc[1] = 0;
470                 cdb->sh_desc[2] = 0;
471
472                 if (ses->dir == DIR_ENC)
473                         shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
474                                         cdb->sh_desc, 1, swap,
475                                         ses->pdcp.hfn,
476                                         ses->pdcp.sn_size,
477                                         ses->pdcp.bearer,
478                                         ses->pdcp.pkt_dir,
479                                         ses->pdcp.hfn_threshold,
480                                         &cipherdata, &authdata,
481                                         0);
482                 else if (ses->dir == DIR_DEC)
483                         shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
484                                         cdb->sh_desc, 1, swap,
485                                         ses->pdcp.hfn,
486                                         ses->pdcp.sn_size,
487                                         ses->pdcp.bearer,
488                                         ses->pdcp.pkt_dir,
489                                         ses->pdcp.hfn_threshold,
490                                         &cipherdata, &authdata,
491                                         0);
492         } else {
493                 cdb->sh_desc[0] = cipherdata.keylen;
494                 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
495                                        MIN_JOB_DESC_SIZE,
496                                        (unsigned int *)cdb->sh_desc,
497                                        &cdb->sh_desc[2], 1);
498
499                 if (err < 0) {
500                         DPAA_SEC_ERR("Crypto: Incorrect key lengths");
501                         return err;
502                 }
503                 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
504                         cipherdata.key = (size_t)dpaa_mem_vtop(
505                                                 (void *)(size_t)cipherdata.key);
506                         cipherdata.key_type = RTA_DATA_PTR;
507                 }
508                 cdb->sh_desc[0] = 0;
509                 cdb->sh_desc[1] = 0;
510                 cdb->sh_desc[2] = 0;
511
512                 if (ses->dir == DIR_ENC)
513                         shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
514                                         cdb->sh_desc, 1, swap,
515                                         ses->pdcp.sn_size,
516                                         ses->pdcp.hfn,
517                                         ses->pdcp.bearer,
518                                         ses->pdcp.pkt_dir,
519                                         ses->pdcp.hfn_threshold,
520                                         &cipherdata, 0);
521                 else if (ses->dir == DIR_DEC)
522                         shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
523                                         cdb->sh_desc, 1, swap,
524                                         ses->pdcp.sn_size,
525                                         ses->pdcp.hfn,
526                                         ses->pdcp.bearer,
527                                         ses->pdcp.pkt_dir,
528                                         ses->pdcp.hfn_threshold,
529                                         &cipherdata, 0);
530         }
531
532         return shared_desc_len;
533 }
534
535 /* prepare ipsec proto command block of the session */
536 static int
537 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
538 {
539         struct alginfo cipherdata = {0}, authdata = {0};
540         struct sec_cdb *cdb = &ses->cdb;
541         int32_t shared_desc_len = 0;
542         int err;
543 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
544         int swap = false;
545 #else
546         int swap = true;
547 #endif
548
549         caam_cipher_alg(ses, &cipherdata);
550         if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
551                 DPAA_SEC_ERR("not supported cipher alg");
552                 return -ENOTSUP;
553         }
554
555         cipherdata.key = (size_t)ses->cipher_key.data;
556         cipherdata.keylen = ses->cipher_key.length;
557         cipherdata.key_enc_flags = 0;
558         cipherdata.key_type = RTA_DATA_IMM;
559
560         caam_auth_alg(ses, &authdata);
561         if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
562                 DPAA_SEC_ERR("not supported auth alg");
563                 return -ENOTSUP;
564         }
565
566         authdata.key = (size_t)ses->auth_key.data;
567         authdata.keylen = ses->auth_key.length;
568         authdata.key_enc_flags = 0;
569         authdata.key_type = RTA_DATA_IMM;
570
571         cdb->sh_desc[0] = cipherdata.keylen;
572         cdb->sh_desc[1] = authdata.keylen;
573         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
574                                MIN_JOB_DESC_SIZE,
575                                (unsigned int *)cdb->sh_desc,
576                                &cdb->sh_desc[2], 2);
577
578         if (err < 0) {
579                 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
580                 return err;
581         }
582         if (cdb->sh_desc[2] & 1)
583                 cipherdata.key_type = RTA_DATA_IMM;
584         else {
585                 cipherdata.key = (size_t)dpaa_mem_vtop(
586                                         (void *)(size_t)cipherdata.key);
587                 cipherdata.key_type = RTA_DATA_PTR;
588         }
589         if (cdb->sh_desc[2] & (1<<1))
590                 authdata.key_type = RTA_DATA_IMM;
591         else {
592                 authdata.key = (size_t)dpaa_mem_vtop(
593                                         (void *)(size_t)authdata.key);
594                 authdata.key_type = RTA_DATA_PTR;
595         }
596
597         cdb->sh_desc[0] = 0;
598         cdb->sh_desc[1] = 0;
599         cdb->sh_desc[2] = 0;
600         if (ses->dir == DIR_ENC) {
601                 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
602                                 cdb->sh_desc,
603                                 true, swap, SHR_SERIAL,
604                                 &ses->encap_pdb,
605                                 (uint8_t *)&ses->ip4_hdr,
606                                 &cipherdata, &authdata);
607         } else if (ses->dir == DIR_DEC) {
608                 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
609                                 cdb->sh_desc,
610                                 true, swap, SHR_SERIAL,
611                                 &ses->decap_pdb,
612                                 &cipherdata, &authdata);
613         }
614         return shared_desc_len;
615 }
616
617 /* prepare command block of the session */
618 static int
619 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
620 {
621         struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
622         int32_t shared_desc_len = 0;
623         struct sec_cdb *cdb = &ses->cdb;
624         int err;
625 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
626         int swap = false;
627 #else
628         int swap = true;
629 #endif
630
631         memset(cdb, 0, sizeof(struct sec_cdb));
632
633         if (is_proto_ipsec(ses)) {
634                 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
635         } else if (is_proto_pdcp(ses)) {
636                 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
637         } else if (is_cipher_only(ses)) {
638                 caam_cipher_alg(ses, &alginfo_c);
639                 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
640                         DPAA_SEC_ERR("not supported cipher alg");
641                         return -ENOTSUP;
642                 }
643
644                 alginfo_c.key = (size_t)ses->cipher_key.data;
645                 alginfo_c.keylen = ses->cipher_key.length;
646                 alginfo_c.key_enc_flags = 0;
647                 alginfo_c.key_type = RTA_DATA_IMM;
648
649                 shared_desc_len = cnstr_shdsc_blkcipher(
650                                                 cdb->sh_desc, true,
651                                                 swap, SHR_NEVER, &alginfo_c,
652                                                 NULL,
653                                                 ses->iv.length,
654                                                 ses->dir);
655         } else if (is_auth_only(ses)) {
656                 caam_auth_alg(ses, &alginfo_a);
657                 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
658                         DPAA_SEC_ERR("not supported auth alg");
659                         return -ENOTSUP;
660                 }
661
662                 alginfo_a.key = (size_t)ses->auth_key.data;
663                 alginfo_a.keylen = ses->auth_key.length;
664                 alginfo_a.key_enc_flags = 0;
665                 alginfo_a.key_type = RTA_DATA_IMM;
666
667                 shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
668                                                    swap, SHR_NEVER, &alginfo_a,
669                                                    !ses->dir,
670                                                    ses->digest_length);
671         } else if (is_aead(ses)) {
672                 caam_aead_alg(ses, &alginfo);
673                 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
674                         DPAA_SEC_ERR("not supported aead alg");
675                         return -ENOTSUP;
676                 }
677                 alginfo.key = (size_t)ses->aead_key.data;
678                 alginfo.keylen = ses->aead_key.length;
679                 alginfo.key_enc_flags = 0;
680                 alginfo.key_type = RTA_DATA_IMM;
681
682                 if (ses->dir == DIR_ENC)
683                         shared_desc_len = cnstr_shdsc_gcm_encap(
684                                         cdb->sh_desc, true, swap, SHR_NEVER,
685                                         &alginfo,
686                                         ses->iv.length,
687                                         ses->digest_length);
688                 else
689                         shared_desc_len = cnstr_shdsc_gcm_decap(
690                                         cdb->sh_desc, true, swap, SHR_NEVER,
691                                         &alginfo,
692                                         ses->iv.length,
693                                         ses->digest_length);
694         } else {
695                 caam_cipher_alg(ses, &alginfo_c);
696                 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
697                         DPAA_SEC_ERR("not supported cipher alg");
698                         return -ENOTSUP;
699                 }
700
701                 alginfo_c.key = (size_t)ses->cipher_key.data;
702                 alginfo_c.keylen = ses->cipher_key.length;
703                 alginfo_c.key_enc_flags = 0;
704                 alginfo_c.key_type = RTA_DATA_IMM;
705
706                 caam_auth_alg(ses, &alginfo_a);
707                 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
708                         DPAA_SEC_ERR("not supported auth alg");
709                         return -ENOTSUP;
710                 }
711
712                 alginfo_a.key = (size_t)ses->auth_key.data;
713                 alginfo_a.keylen = ses->auth_key.length;
714                 alginfo_a.key_enc_flags = 0;
715                 alginfo_a.key_type = RTA_DATA_IMM;
716
717                 cdb->sh_desc[0] = alginfo_c.keylen;
718                 cdb->sh_desc[1] = alginfo_a.keylen;
719                 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
720                                        MIN_JOB_DESC_SIZE,
721                                        (unsigned int *)cdb->sh_desc,
722                                        &cdb->sh_desc[2], 2);
723
724                 if (err < 0) {
725                         DPAA_SEC_ERR("Crypto: Incorrect key lengths");
726                         return err;
727                 }
728                 if (cdb->sh_desc[2] & 1)
729                         alginfo_c.key_type = RTA_DATA_IMM;
730                 else {
731                         alginfo_c.key = (size_t)dpaa_mem_vtop(
732                                                 (void *)(size_t)alginfo_c.key);
733                         alginfo_c.key_type = RTA_DATA_PTR;
734                 }
735                 if (cdb->sh_desc[2] & (1<<1))
736                         alginfo_a.key_type = RTA_DATA_IMM;
737                 else {
738                         alginfo_a.key = (size_t)dpaa_mem_vtop(
739                                                 (void *)(size_t)alginfo_a.key);
740                         alginfo_a.key_type = RTA_DATA_PTR;
741                 }
742                 cdb->sh_desc[0] = 0;
743                 cdb->sh_desc[1] = 0;
744                 cdb->sh_desc[2] = 0;
745                 /* Auth_only_len is set as 0 here and it will be
746                  * overwritten in fd for each packet.
747                  */
748                 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
749                                 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
750                                 ses->iv.length, 0,
751                                 ses->digest_length, ses->dir);
752         }
753
754         if (shared_desc_len < 0) {
755                 DPAA_SEC_ERR("error in preparing command block");
756                 return shared_desc_len;
757         }
758
759         cdb->sh_hdr.hi.field.idlen = shared_desc_len;
760         cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
761         cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
762
763         return 0;
764 }
765
766 /* qp is lockless, should be accessed by only one thread */
767 static int
768 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
769 {
770         struct qman_fq *fq;
771         unsigned int pkts = 0;
772         int num_rx_bufs, ret;
773         struct qm_dqrr_entry *dq;
774         uint32_t vdqcr_flags = 0;
775
776         fq = &qp->outq;
777         /*
778          * Until request for four buffers, we provide exact number of buffers.
779          * Otherwise we do not set the QM_VDQCR_EXACT flag.
780          * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
781          * requested, so we request two less in this case.
782          */
783         if (nb_ops < 4) {
784                 vdqcr_flags = QM_VDQCR_EXACT;
785                 num_rx_bufs = nb_ops;
786         } else {
787                 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
788                         (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
789         }
790         ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
791         if (ret)
792                 return 0;
793
794         do {
795                 const struct qm_fd *fd;
796                 struct dpaa_sec_job *job;
797                 struct dpaa_sec_op_ctx *ctx;
798                 struct rte_crypto_op *op;
799
800                 dq = qman_dequeue(fq);
801                 if (!dq)
802                         continue;
803
804                 fd = &dq->fd;
805                 /* sg is embedded in an op ctx,
806                  * sg[0] is for output
807                  * sg[1] for input
808                  */
809                 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
810
811                 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
812                 ctx->fd_status = fd->status;
813                 op = ctx->op;
814                 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
815                         struct qm_sg_entry *sg_out;
816                         uint32_t len;
817
818                         sg_out = &job->sg[0];
819                         hw_sg_to_cpu(sg_out);
820                         len = sg_out->length;
821                         op->sym->m_src->pkt_len = len;
822                         op->sym->m_src->data_len = len;
823                 }
824                 if (!ctx->fd_status) {
825                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
826                 } else {
827                         DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
828                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
829                 }
830                 ops[pkts++] = op;
831
832                 /* report op status to sym->op and then free the ctx memeory */
833                 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
834
835                 qman_dqrr_consume(fq, dq);
836         } while (fq->flags & QMAN_FQ_STATE_VDQCR);
837
838         return pkts;
839 }
840
841 static inline struct dpaa_sec_job *
842 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
843 {
844         struct rte_crypto_sym_op *sym = op->sym;
845         struct rte_mbuf *mbuf = sym->m_src;
846         struct dpaa_sec_job *cf;
847         struct dpaa_sec_op_ctx *ctx;
848         struct qm_sg_entry *sg, *out_sg, *in_sg;
849         phys_addr_t start_addr;
850         uint8_t *old_digest, extra_segs;
851
852         if (is_decode(ses))
853                 extra_segs = 3;
854         else
855                 extra_segs = 2;
856
857         if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
858                 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
859                                 MAX_SG_ENTRIES);
860                 return NULL;
861         }
862         ctx = dpaa_sec_alloc_ctx(ses);
863         if (!ctx)
864                 return NULL;
865
866         cf = &ctx->job;
867         ctx->op = op;
868         old_digest = ctx->digest;
869
870         /* output */
871         out_sg = &cf->sg[0];
872         qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
873         out_sg->length = ses->digest_length;
874         cpu_to_hw_sg(out_sg);
875
876         /* input */
877         in_sg = &cf->sg[1];
878         /* need to extend the input to a compound frame */
879         in_sg->extension = 1;
880         in_sg->final = 1;
881         in_sg->length = sym->auth.data.length;
882         qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
883
884         /* 1st seg */
885         sg = in_sg + 1;
886         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
887         sg->length = mbuf->data_len - sym->auth.data.offset;
888         sg->offset = sym->auth.data.offset;
889
890         /* Successive segs */
891         mbuf = mbuf->next;
892         while (mbuf) {
893                 cpu_to_hw_sg(sg);
894                 sg++;
895                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
896                 sg->length = mbuf->data_len;
897                 mbuf = mbuf->next;
898         }
899
900         if (is_decode(ses)) {
901                 /* Digest verification case */
902                 cpu_to_hw_sg(sg);
903                 sg++;
904                 rte_memcpy(old_digest, sym->auth.digest.data,
905                                 ses->digest_length);
906                 start_addr = dpaa_mem_vtop(old_digest);
907                 qm_sg_entry_set64(sg, start_addr);
908                 sg->length = ses->digest_length;
909                 in_sg->length += ses->digest_length;
910         } else {
911                 /* Digest calculation case */
912                 sg->length -= ses->digest_length;
913         }
914         sg->final = 1;
915         cpu_to_hw_sg(sg);
916         cpu_to_hw_sg(in_sg);
917
918         return cf;
919 }
920
921 /**
922  * packet looks like:
923  *              |<----data_len------->|
924  *    |ip_header|ah_header|icv|payload|
925  *              ^
926  *              |
927  *         mbuf->pkt.data
928  */
929 static inline struct dpaa_sec_job *
930 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
931 {
932         struct rte_crypto_sym_op *sym = op->sym;
933         struct rte_mbuf *mbuf = sym->m_src;
934         struct dpaa_sec_job *cf;
935         struct dpaa_sec_op_ctx *ctx;
936         struct qm_sg_entry *sg;
937         rte_iova_t start_addr;
938         uint8_t *old_digest;
939
940         ctx = dpaa_sec_alloc_ctx(ses);
941         if (!ctx)
942                 return NULL;
943
944         cf = &ctx->job;
945         ctx->op = op;
946         old_digest = ctx->digest;
947
948         start_addr = rte_pktmbuf_iova(mbuf);
949         /* output */
950         sg = &cf->sg[0];
951         qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
952         sg->length = ses->digest_length;
953         cpu_to_hw_sg(sg);
954
955         /* input */
956         sg = &cf->sg[1];
957         if (is_decode(ses)) {
958                 /* need to extend the input to a compound frame */
959                 sg->extension = 1;
960                 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
961                 sg->length = sym->auth.data.length + ses->digest_length;
962                 sg->final = 1;
963                 cpu_to_hw_sg(sg);
964
965                 sg = &cf->sg[2];
966                 /* hash result or digest, save digest first */
967                 rte_memcpy(old_digest, sym->auth.digest.data,
968                            ses->digest_length);
969                 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
970                 sg->length = sym->auth.data.length;
971                 cpu_to_hw_sg(sg);
972
973                 /* let's check digest by hw */
974                 start_addr = dpaa_mem_vtop(old_digest);
975                 sg++;
976                 qm_sg_entry_set64(sg, start_addr);
977                 sg->length = ses->digest_length;
978                 sg->final = 1;
979                 cpu_to_hw_sg(sg);
980         } else {
981                 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
982                 sg->length = sym->auth.data.length;
983                 sg->final = 1;
984                 cpu_to_hw_sg(sg);
985         }
986
987         return cf;
988 }
989
990 static inline struct dpaa_sec_job *
991 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
992 {
993         struct rte_crypto_sym_op *sym = op->sym;
994         struct dpaa_sec_job *cf;
995         struct dpaa_sec_op_ctx *ctx;
996         struct qm_sg_entry *sg, *out_sg, *in_sg;
997         struct rte_mbuf *mbuf;
998         uint8_t req_segs;
999         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1000                         ses->iv.offset);
1001
1002         if (sym->m_dst) {
1003                 mbuf = sym->m_dst;
1004                 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1005         } else {
1006                 mbuf = sym->m_src;
1007                 req_segs = mbuf->nb_segs * 2 + 3;
1008         }
1009
1010         if (req_segs > MAX_SG_ENTRIES) {
1011                 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1012                                 MAX_SG_ENTRIES);
1013                 return NULL;
1014         }
1015
1016         ctx = dpaa_sec_alloc_ctx(ses);
1017         if (!ctx)
1018                 return NULL;
1019
1020         cf = &ctx->job;
1021         ctx->op = op;
1022
1023         /* output */
1024         out_sg = &cf->sg[0];
1025         out_sg->extension = 1;
1026         out_sg->length = sym->cipher.data.length;
1027         qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1028         cpu_to_hw_sg(out_sg);
1029
1030         /* 1st seg */
1031         sg = &cf->sg[2];
1032         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1033         sg->length = mbuf->data_len - sym->cipher.data.offset;
1034         sg->offset = sym->cipher.data.offset;
1035
1036         /* Successive segs */
1037         mbuf = mbuf->next;
1038         while (mbuf) {
1039                 cpu_to_hw_sg(sg);
1040                 sg++;
1041                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1042                 sg->length = mbuf->data_len;
1043                 mbuf = mbuf->next;
1044         }
1045         sg->final = 1;
1046         cpu_to_hw_sg(sg);
1047
1048         /* input */
1049         mbuf = sym->m_src;
1050         in_sg = &cf->sg[1];
1051         in_sg->extension = 1;
1052         in_sg->final = 1;
1053         in_sg->length = sym->cipher.data.length + ses->iv.length;
1054
1055         sg++;
1056         qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1057         cpu_to_hw_sg(in_sg);
1058
1059         /* IV */
1060         qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1061         sg->length = ses->iv.length;
1062         cpu_to_hw_sg(sg);
1063
1064         /* 1st seg */
1065         sg++;
1066         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1067         sg->length = mbuf->data_len - sym->cipher.data.offset;
1068         sg->offset = sym->cipher.data.offset;
1069
1070         /* Successive segs */
1071         mbuf = mbuf->next;
1072         while (mbuf) {
1073                 cpu_to_hw_sg(sg);
1074                 sg++;
1075                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1076                 sg->length = mbuf->data_len;
1077                 mbuf = mbuf->next;
1078         }
1079         sg->final = 1;
1080         cpu_to_hw_sg(sg);
1081
1082         return cf;
1083 }
1084
1085 static inline struct dpaa_sec_job *
1086 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1087 {
1088         struct rte_crypto_sym_op *sym = op->sym;
1089         struct dpaa_sec_job *cf;
1090         struct dpaa_sec_op_ctx *ctx;
1091         struct qm_sg_entry *sg;
1092         rte_iova_t src_start_addr, dst_start_addr;
1093         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1094                         ses->iv.offset);
1095
1096         ctx = dpaa_sec_alloc_ctx(ses);
1097         if (!ctx)
1098                 return NULL;
1099
1100         cf = &ctx->job;
1101         ctx->op = op;
1102
1103         src_start_addr = rte_pktmbuf_iova(sym->m_src);
1104
1105         if (sym->m_dst)
1106                 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1107         else
1108                 dst_start_addr = src_start_addr;
1109
1110         /* output */
1111         sg = &cf->sg[0];
1112         qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1113         sg->length = sym->cipher.data.length + ses->iv.length;
1114         cpu_to_hw_sg(sg);
1115
1116         /* input */
1117         sg = &cf->sg[1];
1118
1119         /* need to extend the input to a compound frame */
1120         sg->extension = 1;
1121         sg->final = 1;
1122         sg->length = sym->cipher.data.length + ses->iv.length;
1123         qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1124         cpu_to_hw_sg(sg);
1125
1126         sg = &cf->sg[2];
1127         qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1128         sg->length = ses->iv.length;
1129         cpu_to_hw_sg(sg);
1130
1131         sg++;
1132         qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1133         sg->length = sym->cipher.data.length;
1134         sg->final = 1;
1135         cpu_to_hw_sg(sg);
1136
1137         return cf;
1138 }
1139
1140 static inline struct dpaa_sec_job *
1141 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1142 {
1143         struct rte_crypto_sym_op *sym = op->sym;
1144         struct dpaa_sec_job *cf;
1145         struct dpaa_sec_op_ctx *ctx;
1146         struct qm_sg_entry *sg, *out_sg, *in_sg;
1147         struct rte_mbuf *mbuf;
1148         uint8_t req_segs;
1149         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1150                         ses->iv.offset);
1151
1152         if (sym->m_dst) {
1153                 mbuf = sym->m_dst;
1154                 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1155         } else {
1156                 mbuf = sym->m_src;
1157                 req_segs = mbuf->nb_segs * 2 + 4;
1158         }
1159
1160         if (ses->auth_only_len)
1161                 req_segs++;
1162
1163         if (req_segs > MAX_SG_ENTRIES) {
1164                 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1165                                 MAX_SG_ENTRIES);
1166                 return NULL;
1167         }
1168
1169         ctx = dpaa_sec_alloc_ctx(ses);
1170         if (!ctx)
1171                 return NULL;
1172
1173         cf = &ctx->job;
1174         ctx->op = op;
1175
1176         rte_prefetch0(cf->sg);
1177
1178         /* output */
1179         out_sg = &cf->sg[0];
1180         out_sg->extension = 1;
1181         if (is_encode(ses))
1182                 out_sg->length = sym->aead.data.length + ses->auth_only_len
1183                                                 + ses->digest_length;
1184         else
1185                 out_sg->length = sym->aead.data.length + ses->auth_only_len;
1186
1187         /* output sg entries */
1188         sg = &cf->sg[2];
1189         qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1190         cpu_to_hw_sg(out_sg);
1191
1192         /* 1st seg */
1193         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1194         sg->length = mbuf->data_len - sym->aead.data.offset +
1195                                         ses->auth_only_len;
1196         sg->offset = sym->aead.data.offset - ses->auth_only_len;
1197
1198         /* Successive segs */
1199         mbuf = mbuf->next;
1200         while (mbuf) {
1201                 cpu_to_hw_sg(sg);
1202                 sg++;
1203                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1204                 sg->length = mbuf->data_len;
1205                 mbuf = mbuf->next;
1206         }
1207         sg->length -= ses->digest_length;
1208
1209         if (is_encode(ses)) {
1210                 cpu_to_hw_sg(sg);
1211                 /* set auth output */
1212                 sg++;
1213                 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1214                 sg->length = ses->digest_length;
1215         }
1216         sg->final = 1;
1217         cpu_to_hw_sg(sg);
1218
1219         /* input */
1220         mbuf = sym->m_src;
1221         in_sg = &cf->sg[1];
1222         in_sg->extension = 1;
1223         in_sg->final = 1;
1224         if (is_encode(ses))
1225                 in_sg->length = ses->iv.length + sym->aead.data.length
1226                                                         + ses->auth_only_len;
1227         else
1228                 in_sg->length = ses->iv.length + sym->aead.data.length
1229                                 + ses->auth_only_len + ses->digest_length;
1230
1231         /* input sg entries */
1232         sg++;
1233         qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1234         cpu_to_hw_sg(in_sg);
1235
1236         /* 1st seg IV */
1237         qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1238         sg->length = ses->iv.length;
1239         cpu_to_hw_sg(sg);
1240
1241         /* 2nd seg auth only */
1242         if (ses->auth_only_len) {
1243                 sg++;
1244                 qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1245                 sg->length = ses->auth_only_len;
1246                 cpu_to_hw_sg(sg);
1247         }
1248
1249         /* 3rd seg */
1250         sg++;
1251         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1252         sg->length = mbuf->data_len - sym->aead.data.offset;
1253         sg->offset = sym->aead.data.offset;
1254
1255         /* Successive segs */
1256         mbuf = mbuf->next;
1257         while (mbuf) {
1258                 cpu_to_hw_sg(sg);
1259                 sg++;
1260                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1261                 sg->length = mbuf->data_len;
1262                 mbuf = mbuf->next;
1263         }
1264
1265         if (is_decode(ses)) {
1266                 cpu_to_hw_sg(sg);
1267                 sg++;
1268                 memcpy(ctx->digest, sym->aead.digest.data,
1269                         ses->digest_length);
1270                 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1271                 sg->length = ses->digest_length;
1272         }
1273         sg->final = 1;
1274         cpu_to_hw_sg(sg);
1275
1276         return cf;
1277 }
1278
1279 static inline struct dpaa_sec_job *
1280 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1281 {
1282         struct rte_crypto_sym_op *sym = op->sym;
1283         struct dpaa_sec_job *cf;
1284         struct dpaa_sec_op_ctx *ctx;
1285         struct qm_sg_entry *sg;
1286         uint32_t length = 0;
1287         rte_iova_t src_start_addr, dst_start_addr;
1288         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1289                         ses->iv.offset);
1290
1291         src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1292
1293         if (sym->m_dst)
1294                 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1295         else
1296                 dst_start_addr = src_start_addr;
1297
1298         ctx = dpaa_sec_alloc_ctx(ses);
1299         if (!ctx)
1300                 return NULL;
1301
1302         cf = &ctx->job;
1303         ctx->op = op;
1304
1305         /* input */
1306         rte_prefetch0(cf->sg);
1307         sg = &cf->sg[2];
1308         qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1309         if (is_encode(ses)) {
1310                 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1311                 sg->length = ses->iv.length;
1312                 length += sg->length;
1313                 cpu_to_hw_sg(sg);
1314
1315                 sg++;
1316                 if (ses->auth_only_len) {
1317                         qm_sg_entry_set64(sg,
1318                                           dpaa_mem_vtop(sym->aead.aad.data));
1319                         sg->length = ses->auth_only_len;
1320                         length += sg->length;
1321                         cpu_to_hw_sg(sg);
1322                         sg++;
1323                 }
1324                 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1325                 sg->length = sym->aead.data.length;
1326                 length += sg->length;
1327                 sg->final = 1;
1328                 cpu_to_hw_sg(sg);
1329         } else {
1330                 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1331                 sg->length = ses->iv.length;
1332                 length += sg->length;
1333                 cpu_to_hw_sg(sg);
1334
1335                 sg++;
1336                 if (ses->auth_only_len) {
1337                         qm_sg_entry_set64(sg,
1338                                           dpaa_mem_vtop(sym->aead.aad.data));
1339                         sg->length = ses->auth_only_len;
1340                         length += sg->length;
1341                         cpu_to_hw_sg(sg);
1342                         sg++;
1343                 }
1344                 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1345                 sg->length = sym->aead.data.length;
1346                 length += sg->length;
1347                 cpu_to_hw_sg(sg);
1348
1349                 memcpy(ctx->digest, sym->aead.digest.data,
1350                        ses->digest_length);
1351                 sg++;
1352
1353                 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1354                 sg->length = ses->digest_length;
1355                 length += sg->length;
1356                 sg->final = 1;
1357                 cpu_to_hw_sg(sg);
1358         }
1359         /* input compound frame */
1360         cf->sg[1].length = length;
1361         cf->sg[1].extension = 1;
1362         cf->sg[1].final = 1;
1363         cpu_to_hw_sg(&cf->sg[1]);
1364
1365         /* output */
1366         sg++;
1367         qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1368         qm_sg_entry_set64(sg,
1369                 dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1370         sg->length = sym->aead.data.length + ses->auth_only_len;
1371         length = sg->length;
1372         if (is_encode(ses)) {
1373                 cpu_to_hw_sg(sg);
1374                 /* set auth output */
1375                 sg++;
1376                 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1377                 sg->length = ses->digest_length;
1378                 length += sg->length;
1379         }
1380         sg->final = 1;
1381         cpu_to_hw_sg(sg);
1382
1383         /* output compound frame */
1384         cf->sg[0].length = length;
1385         cf->sg[0].extension = 1;
1386         cpu_to_hw_sg(&cf->sg[0]);
1387
1388         return cf;
1389 }
1390
1391 static inline struct dpaa_sec_job *
1392 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1393 {
1394         struct rte_crypto_sym_op *sym = op->sym;
1395         struct dpaa_sec_job *cf;
1396         struct dpaa_sec_op_ctx *ctx;
1397         struct qm_sg_entry *sg, *out_sg, *in_sg;
1398         struct rte_mbuf *mbuf;
1399         uint8_t req_segs;
1400         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1401                         ses->iv.offset);
1402
1403         if (sym->m_dst) {
1404                 mbuf = sym->m_dst;
1405                 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1406         } else {
1407                 mbuf = sym->m_src;
1408                 req_segs = mbuf->nb_segs * 2 + 4;
1409         }
1410
1411         if (req_segs > MAX_SG_ENTRIES) {
1412                 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1413                                 MAX_SG_ENTRIES);
1414                 return NULL;
1415         }
1416
1417         ctx = dpaa_sec_alloc_ctx(ses);
1418         if (!ctx)
1419                 return NULL;
1420
1421         cf = &ctx->job;
1422         ctx->op = op;
1423
1424         rte_prefetch0(cf->sg);
1425
1426         /* output */
1427         out_sg = &cf->sg[0];
1428         out_sg->extension = 1;
1429         if (is_encode(ses))
1430                 out_sg->length = sym->auth.data.length + ses->digest_length;
1431         else
1432                 out_sg->length = sym->auth.data.length;
1433
1434         /* output sg entries */
1435         sg = &cf->sg[2];
1436         qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1437         cpu_to_hw_sg(out_sg);
1438
1439         /* 1st seg */
1440         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1441         sg->length = mbuf->data_len - sym->auth.data.offset;
1442         sg->offset = sym->auth.data.offset;
1443
1444         /* Successive segs */
1445         mbuf = mbuf->next;
1446         while (mbuf) {
1447                 cpu_to_hw_sg(sg);
1448                 sg++;
1449                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1450                 sg->length = mbuf->data_len;
1451                 mbuf = mbuf->next;
1452         }
1453         sg->length -= ses->digest_length;
1454
1455         if (is_encode(ses)) {
1456                 cpu_to_hw_sg(sg);
1457                 /* set auth output */
1458                 sg++;
1459                 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1460                 sg->length = ses->digest_length;
1461         }
1462         sg->final = 1;
1463         cpu_to_hw_sg(sg);
1464
1465         /* input */
1466         mbuf = sym->m_src;
1467         in_sg = &cf->sg[1];
1468         in_sg->extension = 1;
1469         in_sg->final = 1;
1470         if (is_encode(ses))
1471                 in_sg->length = ses->iv.length + sym->auth.data.length;
1472         else
1473                 in_sg->length = ses->iv.length + sym->auth.data.length
1474                                                 + ses->digest_length;
1475
1476         /* input sg entries */
1477         sg++;
1478         qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1479         cpu_to_hw_sg(in_sg);
1480
1481         /* 1st seg IV */
1482         qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1483         sg->length = ses->iv.length;
1484         cpu_to_hw_sg(sg);
1485
1486         /* 2nd seg */
1487         sg++;
1488         qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1489         sg->length = mbuf->data_len - sym->auth.data.offset;
1490         sg->offset = sym->auth.data.offset;
1491
1492         /* Successive segs */
1493         mbuf = mbuf->next;
1494         while (mbuf) {
1495                 cpu_to_hw_sg(sg);
1496                 sg++;
1497                 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1498                 sg->length = mbuf->data_len;
1499                 mbuf = mbuf->next;
1500         }
1501
1502         sg->length -= ses->digest_length;
1503         if (is_decode(ses)) {
1504                 cpu_to_hw_sg(sg);
1505                 sg++;
1506                 memcpy(ctx->digest, sym->auth.digest.data,
1507                         ses->digest_length);
1508                 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1509                 sg->length = ses->digest_length;
1510         }
1511         sg->final = 1;
1512         cpu_to_hw_sg(sg);
1513
1514         return cf;
1515 }
1516
1517 static inline struct dpaa_sec_job *
1518 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1519 {
1520         struct rte_crypto_sym_op *sym = op->sym;
1521         struct dpaa_sec_job *cf;
1522         struct dpaa_sec_op_ctx *ctx;
1523         struct qm_sg_entry *sg;
1524         rte_iova_t src_start_addr, dst_start_addr;
1525         uint32_t length = 0;
1526         uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1527                         ses->iv.offset);
1528
1529         src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1530         if (sym->m_dst)
1531                 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1532         else
1533                 dst_start_addr = src_start_addr;
1534
1535         ctx = dpaa_sec_alloc_ctx(ses);
1536         if (!ctx)
1537                 return NULL;
1538
1539         cf = &ctx->job;
1540         ctx->op = op;
1541
1542         /* input */
1543         rte_prefetch0(cf->sg);
1544         sg = &cf->sg[2];
1545         qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1546         if (is_encode(ses)) {
1547                 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1548                 sg->length = ses->iv.length;
1549                 length += sg->length;
1550                 cpu_to_hw_sg(sg);
1551
1552                 sg++;
1553                 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1554                 sg->length = sym->auth.data.length;
1555                 length += sg->length;
1556                 sg->final = 1;
1557                 cpu_to_hw_sg(sg);
1558         } else {
1559                 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1560                 sg->length = ses->iv.length;
1561                 length += sg->length;
1562                 cpu_to_hw_sg(sg);
1563
1564                 sg++;
1565
1566                 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1567                 sg->length = sym->auth.data.length;
1568                 length += sg->length;
1569                 cpu_to_hw_sg(sg);
1570
1571                 memcpy(ctx->digest, sym->auth.digest.data,
1572                        ses->digest_length);
1573                 sg++;
1574
1575                 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1576                 sg->length = ses->digest_length;
1577                 length += sg->length;
1578                 sg->final = 1;
1579                 cpu_to_hw_sg(sg);
1580         }
1581         /* input compound frame */
1582         cf->sg[1].length = length;
1583         cf->sg[1].extension = 1;
1584         cf->sg[1].final = 1;
1585         cpu_to_hw_sg(&cf->sg[1]);
1586
1587         /* output */
1588         sg++;
1589         qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1590         qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1591         sg->length = sym->cipher.data.length;
1592         length = sg->length;
1593         if (is_encode(ses)) {
1594                 cpu_to_hw_sg(sg);
1595                 /* set auth output */
1596                 sg++;
1597                 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1598                 sg->length = ses->digest_length;
1599                 length += sg->length;
1600         }
1601         sg->final = 1;
1602         cpu_to_hw_sg(sg);
1603
1604         /* output compound frame */
1605         cf->sg[0].length = length;
1606         cf->sg[0].extension = 1;
1607         cpu_to_hw_sg(&cf->sg[0]);
1608
1609         return cf;
1610 }
1611
1612 static inline struct dpaa_sec_job *
1613 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1614 {
1615         struct rte_crypto_sym_op *sym = op->sym;
1616         struct dpaa_sec_job *cf;
1617         struct dpaa_sec_op_ctx *ctx;
1618         struct qm_sg_entry *sg;
1619         phys_addr_t src_start_addr, dst_start_addr;
1620
1621         ctx = dpaa_sec_alloc_ctx(ses);
1622         if (!ctx)
1623                 return NULL;
1624         cf = &ctx->job;
1625         ctx->op = op;
1626
1627         src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1628
1629         if (sym->m_dst)
1630                 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1631         else
1632                 dst_start_addr = src_start_addr;
1633
1634         /* input */
1635         sg = &cf->sg[1];
1636         qm_sg_entry_set64(sg, src_start_addr);
1637         sg->length = sym->m_src->pkt_len;
1638         sg->final = 1;
1639         cpu_to_hw_sg(sg);
1640
1641         sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1642         /* output */
1643         sg = &cf->sg[0];
1644         qm_sg_entry_set64(sg, dst_start_addr);
1645         sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1646         cpu_to_hw_sg(sg);
1647
1648         return cf;
1649 }
1650
1651 static uint16_t
1652 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1653                        uint16_t nb_ops)
1654 {
1655         /* Function to transmit the frames to given device and queuepair */
1656         uint32_t loop;
1657         struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1658         uint16_t num_tx = 0;
1659         struct qm_fd fds[DPAA_SEC_BURST], *fd;
1660         uint32_t frames_to_send;
1661         struct rte_crypto_op *op;
1662         struct dpaa_sec_job *cf;
1663         dpaa_sec_session *ses;
1664         uint32_t auth_only_len;
1665         struct qman_fq *inq[DPAA_SEC_BURST];
1666
1667         while (nb_ops) {
1668                 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1669                                 DPAA_SEC_BURST : nb_ops;
1670                 for (loop = 0; loop < frames_to_send; loop++) {
1671                         op = *(ops++);
1672                         switch (op->sess_type) {
1673                         case RTE_CRYPTO_OP_WITH_SESSION:
1674                                 ses = (dpaa_sec_session *)
1675                                         get_sym_session_private_data(
1676                                                         op->sym->session,
1677                                                         cryptodev_driver_id);
1678                                 break;
1679                         case RTE_CRYPTO_OP_SECURITY_SESSION:
1680                                 ses = (dpaa_sec_session *)
1681                                         get_sec_session_private_data(
1682                                                         op->sym->sec_session);
1683                                 break;
1684                         default:
1685                                 DPAA_SEC_DP_ERR(
1686                                         "sessionless crypto op not supported");
1687                                 frames_to_send = loop;
1688                                 nb_ops = loop;
1689                                 goto send_pkts;
1690                         }
1691                         if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1692                                 if (dpaa_sec_attach_sess_q(qp, ses)) {
1693                                         frames_to_send = loop;
1694                                         nb_ops = loop;
1695                                         goto send_pkts;
1696                                 }
1697                         } else if (unlikely(ses->qp[rte_lcore_id() %
1698                                                 MAX_DPAA_CORES] != qp)) {
1699                                 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1700                                         " New qp = %p\n",
1701                                         ses->qp[rte_lcore_id() %
1702                                         MAX_DPAA_CORES], qp);
1703                                 frames_to_send = loop;
1704                                 nb_ops = loop;
1705                                 goto send_pkts;
1706                         }
1707
1708                         auth_only_len = op->sym->auth.data.length -
1709                                                 op->sym->cipher.data.length;
1710                         if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1711                                 if (is_proto_ipsec(ses)) {
1712                                         cf = build_proto(op, ses);
1713                                 } else if (is_proto_pdcp(ses)) {
1714                                         cf = build_proto(op, ses);
1715                                 } else if (is_auth_only(ses)) {
1716                                         cf = build_auth_only(op, ses);
1717                                 } else if (is_cipher_only(ses)) {
1718                                         cf = build_cipher_only(op, ses);
1719                                 } else if (is_aead(ses)) {
1720                                         cf = build_cipher_auth_gcm(op, ses);
1721                                         auth_only_len = ses->auth_only_len;
1722                                 } else if (is_auth_cipher(ses)) {
1723                                         cf = build_cipher_auth(op, ses);
1724                                 } else {
1725                                         DPAA_SEC_DP_ERR("not supported ops");
1726                                         frames_to_send = loop;
1727                                         nb_ops = loop;
1728                                         goto send_pkts;
1729                                 }
1730                         } else {
1731                                 if (is_auth_only(ses)) {
1732                                         cf = build_auth_only_sg(op, ses);
1733                                 } else if (is_cipher_only(ses)) {
1734                                         cf = build_cipher_only_sg(op, ses);
1735                                 } else if (is_aead(ses)) {
1736                                         cf = build_cipher_auth_gcm_sg(op, ses);
1737                                         auth_only_len = ses->auth_only_len;
1738                                 } else if (is_auth_cipher(ses)) {
1739                                         cf = build_cipher_auth_sg(op, ses);
1740                                 } else {
1741                                         DPAA_SEC_DP_ERR("not supported ops");
1742                                         frames_to_send = loop;
1743                                         nb_ops = loop;
1744                                         goto send_pkts;
1745                                 }
1746                         }
1747                         if (unlikely(!cf)) {
1748                                 frames_to_send = loop;
1749                                 nb_ops = loop;
1750                                 goto send_pkts;
1751                         }
1752
1753                         fd = &fds[loop];
1754                         inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1755                         fd->opaque_addr = 0;
1756                         fd->cmd = 0;
1757                         qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
1758                         fd->_format1 = qm_fd_compound;
1759                         fd->length29 = 2 * sizeof(struct qm_sg_entry);
1760                         /* Auth_only_len is set as 0 in descriptor and it is
1761                          * overwritten here in the fd.cmd which will update
1762                          * the DPOVRD reg.
1763                          */
1764                         if (auth_only_len)
1765                                 fd->cmd = 0x80000000 | auth_only_len;
1766
1767                 }
1768 send_pkts:
1769                 loop = 0;
1770                 while (loop < frames_to_send) {
1771                         loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1772                                         frames_to_send - loop);
1773                 }
1774                 nb_ops -= frames_to_send;
1775                 num_tx += frames_to_send;
1776         }
1777
1778         dpaa_qp->tx_pkts += num_tx;
1779         dpaa_qp->tx_errs += nb_ops - num_tx;
1780
1781         return num_tx;
1782 }
1783
1784 static uint16_t
1785 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1786                        uint16_t nb_ops)
1787 {
1788         uint16_t num_rx;
1789         struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1790
1791         num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1792
1793         dpaa_qp->rx_pkts += num_rx;
1794         dpaa_qp->rx_errs += nb_ops - num_rx;
1795
1796         DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1797
1798         return num_rx;
1799 }
1800
1801 /** Release queue pair */
1802 static int
1803 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1804                             uint16_t qp_id)
1805 {
1806         struct dpaa_sec_dev_private *internals;
1807         struct dpaa_sec_qp *qp = NULL;
1808
1809         PMD_INIT_FUNC_TRACE();
1810
1811         DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1812
1813         internals = dev->data->dev_private;
1814         if (qp_id >= internals->max_nb_queue_pairs) {
1815                 DPAA_SEC_ERR("Max supported qpid %d",
1816                              internals->max_nb_queue_pairs);
1817                 return -EINVAL;
1818         }
1819
1820         qp = &internals->qps[qp_id];
1821         qp->internals = NULL;
1822         dev->data->queue_pairs[qp_id] = NULL;
1823
1824         return 0;
1825 }
1826
1827 /** Setup a queue pair */
1828 static int
1829 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1830                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1831                 __rte_unused int socket_id)
1832 {
1833         struct dpaa_sec_dev_private *internals;
1834         struct dpaa_sec_qp *qp = NULL;
1835
1836         DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1837
1838         internals = dev->data->dev_private;
1839         if (qp_id >= internals->max_nb_queue_pairs) {
1840                 DPAA_SEC_ERR("Max supported qpid %d",
1841                              internals->max_nb_queue_pairs);
1842                 return -EINVAL;
1843         }
1844
1845         qp = &internals->qps[qp_id];
1846         qp->internals = internals;
1847         dev->data->queue_pairs[qp_id] = qp;
1848
1849         return 0;
1850 }
1851
1852 /** Return the number of allocated queue pairs */
1853 static uint32_t
1854 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1855 {
1856         PMD_INIT_FUNC_TRACE();
1857
1858         return dev->data->nb_queue_pairs;
1859 }
1860
1861 /** Returns the size of session structure */
1862 static unsigned int
1863 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1864 {
1865         PMD_INIT_FUNC_TRACE();
1866
1867         return sizeof(dpaa_sec_session);
1868 }
1869
1870 static int
1871 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1872                      struct rte_crypto_sym_xform *xform,
1873                      dpaa_sec_session *session)
1874 {
1875         session->cipher_alg = xform->cipher.algo;
1876         session->iv.length = xform->cipher.iv.length;
1877         session->iv.offset = xform->cipher.iv.offset;
1878         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1879                                                RTE_CACHE_LINE_SIZE);
1880         if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1881                 DPAA_SEC_ERR("No Memory for cipher key");
1882                 return -ENOMEM;
1883         }
1884         session->cipher_key.length = xform->cipher.key.length;
1885
1886         memcpy(session->cipher_key.data, xform->cipher.key.data,
1887                xform->cipher.key.length);
1888         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1889                         DIR_ENC : DIR_DEC;
1890
1891         return 0;
1892 }
1893
1894 static int
1895 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1896                    struct rte_crypto_sym_xform *xform,
1897                    dpaa_sec_session *session)
1898 {
1899         session->auth_alg = xform->auth.algo;
1900         session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1901                                              RTE_CACHE_LINE_SIZE);
1902         if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1903                 DPAA_SEC_ERR("No Memory for auth key");
1904                 return -ENOMEM;
1905         }
1906         session->auth_key.length = xform->auth.key.length;
1907         session->digest_length = xform->auth.digest_length;
1908
1909         memcpy(session->auth_key.data, xform->auth.key.data,
1910                xform->auth.key.length);
1911         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1912                         DIR_ENC : DIR_DEC;
1913
1914         return 0;
1915 }
1916
1917 static int
1918 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1919                    struct rte_crypto_sym_xform *xform,
1920                    dpaa_sec_session *session)
1921 {
1922         session->aead_alg = xform->aead.algo;
1923         session->iv.length = xform->aead.iv.length;
1924         session->iv.offset = xform->aead.iv.offset;
1925         session->auth_only_len = xform->aead.aad_length;
1926         session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1927                                              RTE_CACHE_LINE_SIZE);
1928         if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1929                 DPAA_SEC_ERR("No Memory for aead key\n");
1930                 return -ENOMEM;
1931         }
1932         session->aead_key.length = xform->aead.key.length;
1933         session->digest_length = xform->aead.digest_length;
1934
1935         memcpy(session->aead_key.data, xform->aead.key.data,
1936                xform->aead.key.length);
1937         session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1938                         DIR_ENC : DIR_DEC;
1939
1940         return 0;
1941 }
1942
1943 static struct qman_fq *
1944 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1945 {
1946         unsigned int i;
1947
1948         for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
1949                 if (qi->inq_attach[i] == 0) {
1950                         qi->inq_attach[i] = 1;
1951                         return &qi->inq[i];
1952                 }
1953         }
1954         DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
1955
1956         return NULL;
1957 }
1958
1959 static int
1960 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1961 {
1962         unsigned int i;
1963
1964         for (i = 0; i < qi->max_nb_sessions; i++) {
1965                 if (&qi->inq[i] == fq) {
1966                         qman_retire_fq(fq, NULL);
1967                         qman_oos_fq(fq);
1968                         qi->inq_attach[i] = 0;
1969                         return 0;
1970                 }
1971         }
1972         return -1;
1973 }
1974
1975 static int
1976 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1977 {
1978         int ret;
1979
1980         sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
1981         ret = dpaa_sec_prep_cdb(sess);
1982         if (ret) {
1983                 DPAA_SEC_ERR("Unable to prepare sec cdb");
1984                 return -1;
1985         }
1986         if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
1987                 ret = rte_dpaa_portal_init((void *)0);
1988                 if (ret) {
1989                         DPAA_SEC_ERR("Failure in affining portal");
1990                         return ret;
1991                 }
1992         }
1993         ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
1994                                dpaa_mem_vtop(&sess->cdb),
1995                                qman_fq_fqid(&qp->outq));
1996         if (ret)
1997                 DPAA_SEC_ERR("Unable to init sec queue");
1998
1999         return ret;
2000 }
2001
2002 static int
2003 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2004                             struct rte_crypto_sym_xform *xform, void *sess)
2005 {
2006         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2007         dpaa_sec_session *session = sess;
2008         uint32_t i;
2009
2010         PMD_INIT_FUNC_TRACE();
2011
2012         if (unlikely(sess == NULL)) {
2013                 DPAA_SEC_ERR("invalid session struct");
2014                 return -EINVAL;
2015         }
2016         memset(session, 0, sizeof(dpaa_sec_session));
2017
2018         /* Default IV length = 0 */
2019         session->iv.length = 0;
2020
2021         /* Cipher Only */
2022         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2023                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2024                 dpaa_sec_cipher_init(dev, xform, session);
2025
2026         /* Authentication Only */
2027         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2028                    xform->next == NULL) {
2029                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2030                 dpaa_sec_auth_init(dev, xform, session);
2031
2032         /* Cipher then Authenticate */
2033         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2034                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2035                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2036                         dpaa_sec_cipher_init(dev, xform, session);
2037                         dpaa_sec_auth_init(dev, xform->next, session);
2038                 } else {
2039                         DPAA_SEC_ERR("Not supported: Auth then Cipher");
2040                         return -EINVAL;
2041                 }
2042
2043         /* Authenticate then Cipher */
2044         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2045                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2046                 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2047                         dpaa_sec_auth_init(dev, xform, session);
2048                         dpaa_sec_cipher_init(dev, xform->next, session);
2049                 } else {
2050                         DPAA_SEC_ERR("Not supported: Auth then Cipher");
2051                         return -EINVAL;
2052                 }
2053
2054         /* AEAD operation for AES-GCM kind of Algorithms */
2055         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2056                    xform->next == NULL) {
2057                 dpaa_sec_aead_init(dev, xform, session);
2058
2059         } else {
2060                 DPAA_SEC_ERR("Invalid crypto type");
2061                 return -EINVAL;
2062         }
2063         session->ctx_pool = internals->ctx_pool;
2064         rte_spinlock_lock(&internals->lock);
2065         for (i = 0; i < MAX_DPAA_CORES; i++) {
2066                 session->inq[i] = dpaa_sec_attach_rxq(internals);
2067                 if (session->inq[i] == NULL) {
2068                         DPAA_SEC_ERR("unable to attach sec queue");
2069                         rte_spinlock_unlock(&internals->lock);
2070                         goto err1;
2071                 }
2072         }
2073         rte_spinlock_unlock(&internals->lock);
2074
2075         return 0;
2076
2077 err1:
2078         rte_free(session->cipher_key.data);
2079         rte_free(session->auth_key.data);
2080         memset(session, 0, sizeof(dpaa_sec_session));
2081
2082         return -EINVAL;
2083 }
2084
2085 static int
2086 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2087                 struct rte_crypto_sym_xform *xform,
2088                 struct rte_cryptodev_sym_session *sess,
2089                 struct rte_mempool *mempool)
2090 {
2091         void *sess_private_data;
2092         int ret;
2093
2094         PMD_INIT_FUNC_TRACE();
2095
2096         if (rte_mempool_get(mempool, &sess_private_data)) {
2097                 DPAA_SEC_ERR("Couldn't get object from session mempool");
2098                 return -ENOMEM;
2099         }
2100
2101         ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2102         if (ret != 0) {
2103                 DPAA_SEC_ERR("failed to configure session parameters");
2104
2105                 /* Return session to mempool */
2106                 rte_mempool_put(mempool, sess_private_data);
2107                 return ret;
2108         }
2109
2110         set_sym_session_private_data(sess, dev->driver_id,
2111                         sess_private_data);
2112
2113
2114         return 0;
2115 }
2116
2117 static inline void
2118 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2119 {
2120         struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2121         struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2122         uint8_t i;
2123
2124         for (i = 0; i < MAX_DPAA_CORES; i++) {
2125                 if (s->inq[i])
2126                         dpaa_sec_detach_rxq(qi, s->inq[i]);
2127                 s->inq[i] = NULL;
2128                 s->qp[i] = NULL;
2129         }
2130         rte_free(s->cipher_key.data);
2131         rte_free(s->auth_key.data);
2132         memset(s, 0, sizeof(dpaa_sec_session));
2133         rte_mempool_put(sess_mp, (void *)s);
2134 }
2135
2136 /** Clear the memory of session so it doesn't leave key material behind */
2137 static void
2138 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2139                 struct rte_cryptodev_sym_session *sess)
2140 {
2141         PMD_INIT_FUNC_TRACE();
2142         uint8_t index = dev->driver_id;
2143         void *sess_priv = get_sym_session_private_data(sess, index);
2144         dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2145
2146         if (sess_priv) {
2147                 free_session_memory(dev, s);
2148                 set_sym_session_private_data(sess, index, NULL);
2149         }
2150 }
2151
2152 static int
2153 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2154                            struct rte_security_session_conf *conf,
2155                            void *sess)
2156 {
2157         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2158         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2159         struct rte_crypto_auth_xform *auth_xform = NULL;
2160         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2161         dpaa_sec_session *session = (dpaa_sec_session *)sess;
2162         uint32_t i;
2163
2164         PMD_INIT_FUNC_TRACE();
2165
2166         memset(session, 0, sizeof(dpaa_sec_session));
2167         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2168                 cipher_xform = &conf->crypto_xform->cipher;
2169                 if (conf->crypto_xform->next)
2170                         auth_xform = &conf->crypto_xform->next->auth;
2171         } else {
2172                 auth_xform = &conf->crypto_xform->auth;
2173                 if (conf->crypto_xform->next)
2174                         cipher_xform = &conf->crypto_xform->next->cipher;
2175         }
2176         session->proto_alg = conf->protocol;
2177
2178         if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2179                 session->cipher_key.data = rte_zmalloc(NULL,
2180                                                        cipher_xform->key.length,
2181                                                        RTE_CACHE_LINE_SIZE);
2182                 if (session->cipher_key.data == NULL &&
2183                                 cipher_xform->key.length > 0) {
2184                         DPAA_SEC_ERR("No Memory for cipher key");
2185                         return -ENOMEM;
2186                 }
2187                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2188                                 cipher_xform->key.length);
2189                 session->cipher_key.length = cipher_xform->key.length;
2190
2191                 switch (cipher_xform->algo) {
2192                 case RTE_CRYPTO_CIPHER_AES_CBC:
2193                 case RTE_CRYPTO_CIPHER_3DES_CBC:
2194                 case RTE_CRYPTO_CIPHER_AES_CTR:
2195                         break;
2196                 default:
2197                         DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2198                                 cipher_xform->algo);
2199                         goto out;
2200                 }
2201                 session->cipher_alg = cipher_xform->algo;
2202         } else {
2203                 session->cipher_key.data = NULL;
2204                 session->cipher_key.length = 0;
2205                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2206         }
2207
2208         if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2209                 session->auth_key.data = rte_zmalloc(NULL,
2210                                                 auth_xform->key.length,
2211                                                 RTE_CACHE_LINE_SIZE);
2212                 if (session->auth_key.data == NULL &&
2213                                 auth_xform->key.length > 0) {
2214                         DPAA_SEC_ERR("No Memory for auth key");
2215                         rte_free(session->cipher_key.data);
2216                         return -ENOMEM;
2217                 }
2218                 memcpy(session->auth_key.data, auth_xform->key.data,
2219                                 auth_xform->key.length);
2220                 session->auth_key.length = auth_xform->key.length;
2221
2222                 switch (auth_xform->algo) {
2223                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2224                 case RTE_CRYPTO_AUTH_MD5_HMAC:
2225                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2226                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2227                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2228                 case RTE_CRYPTO_AUTH_AES_CMAC:
2229                         break;
2230                 default:
2231                         DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2232                                 auth_xform->algo);
2233                         goto out;
2234                 }
2235                 session->auth_alg = auth_xform->algo;
2236         } else {
2237                 session->auth_key.data = NULL;
2238                 session->auth_key.length = 0;
2239                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2240         }
2241
2242         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2243                 if (ipsec_xform->tunnel.type ==
2244                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2245                         memset(&session->encap_pdb, 0,
2246                                 sizeof(struct ipsec_encap_pdb) +
2247                                 sizeof(session->ip4_hdr));
2248                         session->ip4_hdr.ip_v = IPVERSION;
2249                         session->ip4_hdr.ip_hl = 5;
2250                         session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2251                                                 sizeof(session->ip4_hdr));
2252                         session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2253                         session->ip4_hdr.ip_id = 0;
2254                         session->ip4_hdr.ip_off = 0;
2255                         session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2256                         session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2257                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2258                                         IPPROTO_ESP : IPPROTO_AH;
2259                         session->ip4_hdr.ip_sum = 0;
2260                         session->ip4_hdr.ip_src =
2261                                         ipsec_xform->tunnel.ipv4.src_ip;
2262                         session->ip4_hdr.ip_dst =
2263                                         ipsec_xform->tunnel.ipv4.dst_ip;
2264                         session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2265                                                 (void *)&session->ip4_hdr,
2266                                                 sizeof(struct ip));
2267                         session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2268                 } else if (ipsec_xform->tunnel.type ==
2269                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2270                         memset(&session->encap_pdb, 0,
2271                                 sizeof(struct ipsec_encap_pdb) +
2272                                 sizeof(session->ip6_hdr));
2273                         session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2274                                 DPAA_IPv6_DEFAULT_VTC_FLOW |
2275                                 ((ipsec_xform->tunnel.ipv6.dscp <<
2276                                         RTE_IPV6_HDR_TC_SHIFT) &
2277                                         RTE_IPV6_HDR_TC_MASK) |
2278                                 ((ipsec_xform->tunnel.ipv6.flabel <<
2279                                         RTE_IPV6_HDR_FL_SHIFT) &
2280                                         RTE_IPV6_HDR_FL_MASK));
2281                         /* Payload length will be updated by HW */
2282                         session->ip6_hdr.payload_len = 0;
2283                         session->ip6_hdr.hop_limits =
2284                                         ipsec_xform->tunnel.ipv6.hlimit;
2285                         session->ip6_hdr.proto = (ipsec_xform->proto ==
2286                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2287                                         IPPROTO_ESP : IPPROTO_AH;
2288                         memcpy(&session->ip6_hdr.src_addr,
2289                                         &ipsec_xform->tunnel.ipv6.src_addr, 16);
2290                         memcpy(&session->ip6_hdr.dst_addr,
2291                                         &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2292                         session->encap_pdb.ip_hdr_len =
2293                                                 sizeof(struct rte_ipv6_hdr);
2294                 }
2295                 session->encap_pdb.options =
2296                         (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2297                         PDBOPTS_ESP_OIHI_PDB_INL |
2298                         PDBOPTS_ESP_IVSRC |
2299                         PDBHMO_ESP_ENCAP_DTTL |
2300                         PDBHMO_ESP_SNR;
2301                 if (ipsec_xform->options.esn)
2302                         session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2303                 session->encap_pdb.spi = ipsec_xform->spi;
2304                 session->dir = DIR_ENC;
2305         } else if (ipsec_xform->direction ==
2306                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2307                 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2308                 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2309                         session->decap_pdb.options = sizeof(struct ip) << 16;
2310                 else
2311                         session->decap_pdb.options =
2312                                         sizeof(struct rte_ipv6_hdr) << 16;
2313                 if (ipsec_xform->options.esn)
2314                         session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2315                 session->dir = DIR_DEC;
2316         } else
2317                 goto out;
2318         session->ctx_pool = internals->ctx_pool;
2319         rte_spinlock_lock(&internals->lock);
2320         for (i = 0; i < MAX_DPAA_CORES; i++) {
2321                 session->inq[i] = dpaa_sec_attach_rxq(internals);
2322                 if (session->inq[i] == NULL) {
2323                         DPAA_SEC_ERR("unable to attach sec queue");
2324                         rte_spinlock_unlock(&internals->lock);
2325                         goto out;
2326                 }
2327         }
2328         rte_spinlock_unlock(&internals->lock);
2329
2330         return 0;
2331 out:
2332         rte_free(session->auth_key.data);
2333         rte_free(session->cipher_key.data);
2334         memset(session, 0, sizeof(dpaa_sec_session));
2335         return -1;
2336 }
2337
2338 static int
2339 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2340                           struct rte_security_session_conf *conf,
2341                           void *sess)
2342 {
2343         struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2344         struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2345         struct rte_crypto_auth_xform *auth_xform = NULL;
2346         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2347         dpaa_sec_session *session = (dpaa_sec_session *)sess;
2348         struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2349         uint32_t i;
2350
2351         PMD_INIT_FUNC_TRACE();
2352
2353         memset(session, 0, sizeof(dpaa_sec_session));
2354
2355         /* find xfrm types */
2356         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2357                 cipher_xform = &xform->cipher;
2358                 if (xform->next != NULL)
2359                         auth_xform = &xform->next->auth;
2360         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2361                 auth_xform = &xform->auth;
2362                 if (xform->next != NULL)
2363                         cipher_xform = &xform->next->cipher;
2364         } else {
2365                 DPAA_SEC_ERR("Invalid crypto type");
2366                 return -EINVAL;
2367         }
2368
2369         session->proto_alg = conf->protocol;
2370         if (cipher_xform) {
2371                 session->cipher_key.data = rte_zmalloc(NULL,
2372                                                cipher_xform->key.length,
2373                                                RTE_CACHE_LINE_SIZE);
2374                 if (session->cipher_key.data == NULL &&
2375                                 cipher_xform->key.length > 0) {
2376                         DPAA_SEC_ERR("No Memory for cipher key");
2377                         return -ENOMEM;
2378                 }
2379                 session->cipher_key.length = cipher_xform->key.length;
2380                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2381                         cipher_xform->key.length);
2382                 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2383                                         DIR_ENC : DIR_DEC;
2384                 session->cipher_alg = cipher_xform->algo;
2385         } else {
2386                 session->cipher_key.data = NULL;
2387                 session->cipher_key.length = 0;
2388                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2389                 session->dir = DIR_ENC;
2390         }
2391
2392         /* Auth is only applicable for control mode operation. */
2393         if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2394                 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2395                     pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2396                         DPAA_SEC_ERR(
2397                                 "PDCP Seq Num size should be 5/12 bits for cmode");
2398                         goto out;
2399                 }
2400                 if (auth_xform) {
2401                         session->auth_key.data = rte_zmalloc(NULL,
2402                                                         auth_xform->key.length,
2403                                                         RTE_CACHE_LINE_SIZE);
2404                         if (session->auth_key.data == NULL &&
2405                                         auth_xform->key.length > 0) {
2406                                 DPAA_SEC_ERR("No Memory for auth key");
2407                                 rte_free(session->cipher_key.data);
2408                                 return -ENOMEM;
2409                         }
2410                         session->auth_key.length = auth_xform->key.length;
2411                         memcpy(session->auth_key.data, auth_xform->key.data,
2412                                         auth_xform->key.length);
2413                         session->auth_alg = auth_xform->algo;
2414                 } else {
2415                         session->auth_key.data = NULL;
2416                         session->auth_key.length = 0;
2417                         session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2418                 }
2419         }
2420         session->pdcp.domain = pdcp_xform->domain;
2421         session->pdcp.bearer = pdcp_xform->bearer;
2422         session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2423         session->pdcp.sn_size = pdcp_xform->sn_size;
2424 #ifdef ENABLE_HFN_OVERRIDE
2425         session->pdcp.hfn_ovd = pdcp_xform->hfn_ovd;
2426 #endif
2427         session->pdcp.hfn = pdcp_xform->hfn;
2428         session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2429
2430         session->ctx_pool = dev_priv->ctx_pool;
2431         rte_spinlock_lock(&dev_priv->lock);
2432         for (i = 0; i < MAX_DPAA_CORES; i++) {
2433                 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2434                 if (session->inq[i] == NULL) {
2435                         DPAA_SEC_ERR("unable to attach sec queue");
2436                         rte_spinlock_unlock(&dev_priv->lock);
2437                         goto out;
2438                 }
2439         }
2440         rte_spinlock_unlock(&dev_priv->lock);
2441         return 0;
2442 out:
2443         rte_free(session->auth_key.data);
2444         rte_free(session->cipher_key.data);
2445         memset(session, 0, sizeof(dpaa_sec_session));
2446         return -1;
2447 }
2448
2449 static int
2450 dpaa_sec_security_session_create(void *dev,
2451                                  struct rte_security_session_conf *conf,
2452                                  struct rte_security_session *sess,
2453                                  struct rte_mempool *mempool)
2454 {
2455         void *sess_private_data;
2456         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2457         int ret;
2458
2459         if (rte_mempool_get(mempool, &sess_private_data)) {
2460                 DPAA_SEC_ERR("Couldn't get object from session mempool");
2461                 return -ENOMEM;
2462         }
2463
2464         switch (conf->protocol) {
2465         case RTE_SECURITY_PROTOCOL_IPSEC:
2466                 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2467                                 sess_private_data);
2468                 break;
2469         case RTE_SECURITY_PROTOCOL_PDCP:
2470                 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2471                                 sess_private_data);
2472                 break;
2473         case RTE_SECURITY_PROTOCOL_MACSEC:
2474                 return -ENOTSUP;
2475         default:
2476                 return -EINVAL;
2477         }
2478         if (ret != 0) {
2479                 DPAA_SEC_ERR("failed to configure session parameters");
2480                 /* Return session to mempool */
2481                 rte_mempool_put(mempool, sess_private_data);
2482                 return ret;
2483         }
2484
2485         set_sec_session_private_data(sess, sess_private_data);
2486
2487         return ret;
2488 }
2489
2490 /** Clear the memory of session so it doesn't leave key material behind */
2491 static int
2492 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2493                 struct rte_security_session *sess)
2494 {
2495         PMD_INIT_FUNC_TRACE();
2496         void *sess_priv = get_sec_session_private_data(sess);
2497         dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2498
2499         if (sess_priv) {
2500                 free_session_memory((struct rte_cryptodev *)dev, s);
2501                 set_sec_session_private_data(sess, NULL);
2502         }
2503         return 0;
2504 }
2505
2506 static int
2507 dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2508                        struct rte_cryptodev_config *config __rte_unused)
2509 {
2510
2511         char str[20];
2512         struct dpaa_sec_dev_private *internals;
2513
2514         PMD_INIT_FUNC_TRACE();
2515
2516         internals = dev->data->dev_private;
2517         snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
2518         if (!internals->ctx_pool) {
2519                 internals->ctx_pool = rte_mempool_create((const char *)str,
2520                                                         CTX_POOL_NUM_BUFS,
2521                                                         CTX_POOL_BUF_SIZE,
2522                                                         CTX_POOL_CACHE_SIZE, 0,
2523                                                         NULL, NULL, NULL, NULL,
2524                                                         SOCKET_ID_ANY, 0);
2525                 if (!internals->ctx_pool) {
2526                         DPAA_SEC_ERR("%s create failed\n", str);
2527                         return -ENOMEM;
2528                 }
2529         } else
2530                 DPAA_SEC_INFO("mempool already created for dev_id : %d",
2531                                 dev->data->dev_id);
2532
2533         return 0;
2534 }
2535
2536 static int
2537 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2538 {
2539         PMD_INIT_FUNC_TRACE();
2540         return 0;
2541 }
2542
2543 static void
2544 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2545 {
2546         PMD_INIT_FUNC_TRACE();
2547 }
2548
2549 static int
2550 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2551 {
2552         struct dpaa_sec_dev_private *internals;
2553
2554         PMD_INIT_FUNC_TRACE();
2555
2556         if (dev == NULL)
2557                 return -ENOMEM;
2558
2559         internals = dev->data->dev_private;
2560         rte_mempool_free(internals->ctx_pool);
2561         internals->ctx_pool = NULL;
2562
2563         return 0;
2564 }
2565
2566 static void
2567 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2568                        struct rte_cryptodev_info *info)
2569 {
2570         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2571
2572         PMD_INIT_FUNC_TRACE();
2573         if (info != NULL) {
2574                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2575                 info->feature_flags = dev->feature_flags;
2576                 info->capabilities = dpaa_sec_capabilities;
2577                 info->sym.max_nb_sessions = internals->max_nb_sessions;
2578                 info->driver_id = cryptodev_driver_id;
2579         }
2580 }
2581
2582 static struct rte_cryptodev_ops crypto_ops = {
2583         .dev_configure        = dpaa_sec_dev_configure,
2584         .dev_start            = dpaa_sec_dev_start,
2585         .dev_stop             = dpaa_sec_dev_stop,
2586         .dev_close            = dpaa_sec_dev_close,
2587         .dev_infos_get        = dpaa_sec_dev_infos_get,
2588         .queue_pair_setup     = dpaa_sec_queue_pair_setup,
2589         .queue_pair_release   = dpaa_sec_queue_pair_release,
2590         .queue_pair_count     = dpaa_sec_queue_pair_count,
2591         .sym_session_get_size     = dpaa_sec_sym_session_get_size,
2592         .sym_session_configure    = dpaa_sec_sym_session_configure,
2593         .sym_session_clear        = dpaa_sec_sym_session_clear
2594 };
2595
2596 static const struct rte_security_capability *
2597 dpaa_sec_capabilities_get(void *device __rte_unused)
2598 {
2599         return dpaa_sec_security_cap;
2600 }
2601
2602 static const struct rte_security_ops dpaa_sec_security_ops = {
2603         .session_create = dpaa_sec_security_session_create,
2604         .session_update = NULL,
2605         .session_stats_get = NULL,
2606         .session_destroy = dpaa_sec_security_session_destroy,
2607         .set_pkt_metadata = NULL,
2608         .capabilities_get = dpaa_sec_capabilities_get
2609 };
2610
2611 static int
2612 dpaa_sec_uninit(struct rte_cryptodev *dev)
2613 {
2614         struct dpaa_sec_dev_private *internals;
2615
2616         if (dev == NULL)
2617                 return -ENODEV;
2618
2619         internals = dev->data->dev_private;
2620         rte_free(dev->security_ctx);
2621
2622         /* In case close has been called, internals->ctx_pool would be NULL */
2623         rte_mempool_free(internals->ctx_pool);
2624         rte_free(internals);
2625
2626         DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2627                       dev->data->name, rte_socket_id());
2628
2629         return 0;
2630 }
2631
2632 static int
2633 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2634 {
2635         struct dpaa_sec_dev_private *internals;
2636         struct rte_security_ctx *security_instance;
2637         struct dpaa_sec_qp *qp;
2638         uint32_t i, flags;
2639         int ret;
2640
2641         PMD_INIT_FUNC_TRACE();
2642
2643         cryptodev->driver_id = cryptodev_driver_id;
2644         cryptodev->dev_ops = &crypto_ops;
2645
2646         cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2647         cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2648         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2649                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
2650                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2651                         RTE_CRYPTODEV_FF_SECURITY |
2652                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2653                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2654                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2655                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2656                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2657
2658         internals = cryptodev->data->dev_private;
2659         internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2660         internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2661
2662         /*
2663          * For secondary processes, we don't initialise any further as primary
2664          * has already done this work. Only check we don't need a different
2665          * RX function
2666          */
2667         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2668                 DPAA_SEC_WARN("Device already init by primary process");
2669                 return 0;
2670         }
2671
2672         /* Initialize security_ctx only for primary process*/
2673         security_instance = rte_malloc("rte_security_instances_ops",
2674                                 sizeof(struct rte_security_ctx), 0);
2675         if (security_instance == NULL)
2676                 return -ENOMEM;
2677         security_instance->device = (void *)cryptodev;
2678         security_instance->ops = &dpaa_sec_security_ops;
2679         security_instance->sess_cnt = 0;
2680         cryptodev->security_ctx = security_instance;
2681
2682         rte_spinlock_init(&internals->lock);
2683         for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2684                 /* init qman fq for queue pair */
2685                 qp = &internals->qps[i];
2686                 ret = dpaa_sec_init_tx(&qp->outq);
2687                 if (ret) {
2688                         DPAA_SEC_ERR("config tx of queue pair  %d", i);
2689                         goto init_error;
2690                 }
2691         }
2692
2693         flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2694                 QMAN_FQ_FLAG_TO_DCPORTAL;
2695         for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2696                 /* create rx qman fq for sessions*/
2697                 ret = qman_create_fq(0, flags, &internals->inq[i]);
2698                 if (unlikely(ret != 0)) {
2699                         DPAA_SEC_ERR("sec qman_create_fq failed");
2700                         goto init_error;
2701                 }
2702         }
2703
2704         RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2705         return 0;
2706
2707 init_error:
2708         DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2709
2710         dpaa_sec_uninit(cryptodev);
2711         return -EFAULT;
2712 }
2713
2714 static int
2715 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2716                                 struct rte_dpaa_device *dpaa_dev)
2717 {
2718         struct rte_cryptodev *cryptodev;
2719         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2720
2721         int retval;
2722
2723         snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2724                         dpaa_dev->id.dev_id);
2725
2726         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2727         if (cryptodev == NULL)
2728                 return -ENOMEM;
2729
2730         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2731                 cryptodev->data->dev_private = rte_zmalloc_socket(
2732                                         "cryptodev private structure",
2733                                         sizeof(struct dpaa_sec_dev_private),
2734                                         RTE_CACHE_LINE_SIZE,
2735                                         rte_socket_id());
2736
2737                 if (cryptodev->data->dev_private == NULL)
2738                         rte_panic("Cannot allocate memzone for private "
2739                                         "device data");
2740         }
2741
2742         dpaa_dev->crypto_dev = cryptodev;
2743         cryptodev->device = &dpaa_dev->device;
2744
2745         /* init user callbacks */
2746         TAILQ_INIT(&(cryptodev->link_intr_cbs));
2747
2748         /* if sec device version is not configured */
2749         if (!rta_get_sec_era()) {
2750                 const struct device_node *caam_node;
2751
2752                 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2753                         const uint32_t *prop = of_get_property(caam_node,
2754                                         "fsl,sec-era",
2755                                         NULL);
2756                         if (prop) {
2757                                 rta_set_sec_era(
2758                                         INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2759                                 break;
2760                         }
2761                 }
2762         }
2763
2764         /* Invoke PMD device initialization function */
2765         retval = dpaa_sec_dev_init(cryptodev);
2766         if (retval == 0)
2767                 return 0;
2768
2769         /* In case of error, cleanup is done */
2770         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2771                 rte_free(cryptodev->data->dev_private);
2772
2773         rte_cryptodev_pmd_release_device(cryptodev);
2774
2775         return -ENXIO;
2776 }
2777
2778 static int
2779 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2780 {
2781         struct rte_cryptodev *cryptodev;
2782         int ret;
2783
2784         cryptodev = dpaa_dev->crypto_dev;
2785         if (cryptodev == NULL)
2786                 return -ENODEV;
2787
2788         ret = dpaa_sec_uninit(cryptodev);
2789         if (ret)
2790                 return ret;
2791
2792         return rte_cryptodev_pmd_destroy(cryptodev);
2793 }
2794
2795 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2796         .drv_type = FSL_DPAA_CRYPTO,
2797         .driver = {
2798                 .name = "DPAA SEC PMD"
2799         },
2800         .probe = cryptodev_dpaa_sec_probe,
2801         .remove = cryptodev_dpaa_sec_remove,
2802 };
2803
2804 static struct cryptodev_driver dpaa_sec_crypto_drv;
2805
2806 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2807 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2808                 cryptodev_driver_id);
2809
2810 RTE_INIT(dpaa_sec_init_log)
2811 {
2812         dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2813         if (dpaa_logtype_sec >= 0)
2814                 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2815 }