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