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