059d440999e8034ddd4f6e263677ce984eb918b2
[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                 }
1758 send_pkts:
1759                 loop = 0;
1760                 while (loop < frames_to_send) {
1761                         loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1762                                         frames_to_send - loop);
1763                 }
1764                 nb_ops -= frames_to_send;
1765                 num_tx += frames_to_send;
1766         }
1767
1768         dpaa_qp->tx_pkts += num_tx;
1769         dpaa_qp->tx_errs += nb_ops - num_tx;
1770
1771         return num_tx;
1772 }
1773
1774 static uint16_t
1775 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1776                        uint16_t nb_ops)
1777 {
1778         uint16_t num_rx;
1779         struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1780
1781         num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1782
1783         dpaa_qp->rx_pkts += num_rx;
1784         dpaa_qp->rx_errs += nb_ops - num_rx;
1785
1786         DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1787
1788         return num_rx;
1789 }
1790
1791 /** Release queue pair */
1792 static int
1793 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1794                             uint16_t qp_id)
1795 {
1796         struct dpaa_sec_dev_private *internals;
1797         struct dpaa_sec_qp *qp = NULL;
1798
1799         PMD_INIT_FUNC_TRACE();
1800
1801         DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1802
1803         internals = dev->data->dev_private;
1804         if (qp_id >= internals->max_nb_queue_pairs) {
1805                 DPAA_SEC_ERR("Max supported qpid %d",
1806                              internals->max_nb_queue_pairs);
1807                 return -EINVAL;
1808         }
1809
1810         qp = &internals->qps[qp_id];
1811         qp->internals = NULL;
1812         dev->data->queue_pairs[qp_id] = NULL;
1813
1814         return 0;
1815 }
1816
1817 /** Setup a queue pair */
1818 static int
1819 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1820                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1821                 __rte_unused int socket_id)
1822 {
1823         struct dpaa_sec_dev_private *internals;
1824         struct dpaa_sec_qp *qp = NULL;
1825
1826         DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1827
1828         internals = dev->data->dev_private;
1829         if (qp_id >= internals->max_nb_queue_pairs) {
1830                 DPAA_SEC_ERR("Max supported qpid %d",
1831                              internals->max_nb_queue_pairs);
1832                 return -EINVAL;
1833         }
1834
1835         qp = &internals->qps[qp_id];
1836         qp->internals = internals;
1837         dev->data->queue_pairs[qp_id] = qp;
1838
1839         return 0;
1840 }
1841
1842 /** Return the number of allocated queue pairs */
1843 static uint32_t
1844 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1845 {
1846         PMD_INIT_FUNC_TRACE();
1847
1848         return dev->data->nb_queue_pairs;
1849 }
1850
1851 /** Returns the size of session structure */
1852 static unsigned int
1853 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1854 {
1855         PMD_INIT_FUNC_TRACE();
1856
1857         return sizeof(dpaa_sec_session);
1858 }
1859
1860 static int
1861 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1862                      struct rte_crypto_sym_xform *xform,
1863                      dpaa_sec_session *session)
1864 {
1865         session->cipher_alg = xform->cipher.algo;
1866         session->iv.length = xform->cipher.iv.length;
1867         session->iv.offset = xform->cipher.iv.offset;
1868         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1869                                                RTE_CACHE_LINE_SIZE);
1870         if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1871                 DPAA_SEC_ERR("No Memory for cipher key");
1872                 return -ENOMEM;
1873         }
1874         session->cipher_key.length = xform->cipher.key.length;
1875
1876         memcpy(session->cipher_key.data, xform->cipher.key.data,
1877                xform->cipher.key.length);
1878         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1879                         DIR_ENC : DIR_DEC;
1880
1881         return 0;
1882 }
1883
1884 static int
1885 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1886                    struct rte_crypto_sym_xform *xform,
1887                    dpaa_sec_session *session)
1888 {
1889         session->auth_alg = xform->auth.algo;
1890         session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1891                                              RTE_CACHE_LINE_SIZE);
1892         if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1893                 DPAA_SEC_ERR("No Memory for auth key");
1894                 return -ENOMEM;
1895         }
1896         session->auth_key.length = xform->auth.key.length;
1897         session->digest_length = xform->auth.digest_length;
1898
1899         memcpy(session->auth_key.data, xform->auth.key.data,
1900                xform->auth.key.length);
1901         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1902                         DIR_ENC : DIR_DEC;
1903
1904         return 0;
1905 }
1906
1907 static int
1908 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1909                    struct rte_crypto_sym_xform *xform,
1910                    dpaa_sec_session *session)
1911 {
1912         session->aead_alg = xform->aead.algo;
1913         session->iv.length = xform->aead.iv.length;
1914         session->iv.offset = xform->aead.iv.offset;
1915         session->auth_only_len = xform->aead.aad_length;
1916         session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1917                                              RTE_CACHE_LINE_SIZE);
1918         if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1919                 DPAA_SEC_ERR("No Memory for aead key\n");
1920                 return -ENOMEM;
1921         }
1922         session->aead_key.length = xform->aead.key.length;
1923         session->digest_length = xform->aead.digest_length;
1924
1925         memcpy(session->aead_key.data, xform->aead.key.data,
1926                xform->aead.key.length);
1927         session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1928                         DIR_ENC : DIR_DEC;
1929
1930         return 0;
1931 }
1932
1933 static struct qman_fq *
1934 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1935 {
1936         unsigned int i;
1937
1938         for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
1939                 if (qi->inq_attach[i] == 0) {
1940                         qi->inq_attach[i] = 1;
1941                         return &qi->inq[i];
1942                 }
1943         }
1944         DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
1945
1946         return NULL;
1947 }
1948
1949 static int
1950 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1951 {
1952         unsigned int i;
1953
1954         for (i = 0; i < qi->max_nb_sessions; i++) {
1955                 if (&qi->inq[i] == fq) {
1956                         qman_retire_fq(fq, NULL);
1957                         qman_oos_fq(fq);
1958                         qi->inq_attach[i] = 0;
1959                         return 0;
1960                 }
1961         }
1962         return -1;
1963 }
1964
1965 static int
1966 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1967 {
1968         int ret;
1969
1970         sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
1971         ret = dpaa_sec_prep_cdb(sess);
1972         if (ret) {
1973                 DPAA_SEC_ERR("Unable to prepare sec cdb");
1974                 return -1;
1975         }
1976         if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
1977                 ret = rte_dpaa_portal_init((void *)0);
1978                 if (ret) {
1979                         DPAA_SEC_ERR("Failure in affining portal");
1980                         return ret;
1981                 }
1982         }
1983         ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
1984                                dpaa_mem_vtop(&sess->cdb),
1985                                qman_fq_fqid(&qp->outq));
1986         if (ret)
1987                 DPAA_SEC_ERR("Unable to init sec queue");
1988
1989         return ret;
1990 }
1991
1992 static int
1993 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1994                             struct rte_crypto_sym_xform *xform, void *sess)
1995 {
1996         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1997         dpaa_sec_session *session = sess;
1998         uint32_t i;
1999
2000         PMD_INIT_FUNC_TRACE();
2001
2002         if (unlikely(sess == NULL)) {
2003                 DPAA_SEC_ERR("invalid session struct");
2004                 return -EINVAL;
2005         }
2006         memset(session, 0, sizeof(dpaa_sec_session));
2007
2008         /* Default IV length = 0 */
2009         session->iv.length = 0;
2010
2011         /* Cipher Only */
2012         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2013                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2014                 dpaa_sec_cipher_init(dev, xform, session);
2015
2016         /* Authentication Only */
2017         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2018                    xform->next == NULL) {
2019                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2020                 dpaa_sec_auth_init(dev, xform, session);
2021
2022         /* Cipher then Authenticate */
2023         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2024                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2025                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2026                         dpaa_sec_cipher_init(dev, xform, session);
2027                         dpaa_sec_auth_init(dev, xform->next, session);
2028                 } else {
2029                         DPAA_SEC_ERR("Not supported: Auth then Cipher");
2030                         return -EINVAL;
2031                 }
2032
2033         /* Authenticate then Cipher */
2034         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2035                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2036                 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2037                         dpaa_sec_auth_init(dev, xform, session);
2038                         dpaa_sec_cipher_init(dev, xform->next, session);
2039                 } else {
2040                         DPAA_SEC_ERR("Not supported: Auth then Cipher");
2041                         return -EINVAL;
2042                 }
2043
2044         /* AEAD operation for AES-GCM kind of Algorithms */
2045         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2046                    xform->next == NULL) {
2047                 dpaa_sec_aead_init(dev, xform, session);
2048
2049         } else {
2050                 DPAA_SEC_ERR("Invalid crypto type");
2051                 return -EINVAL;
2052         }
2053         session->ctx_pool = internals->ctx_pool;
2054         rte_spinlock_lock(&internals->lock);
2055         for (i = 0; i < MAX_DPAA_CORES; i++) {
2056                 session->inq[i] = dpaa_sec_attach_rxq(internals);
2057                 if (session->inq[i] == NULL) {
2058                         DPAA_SEC_ERR("unable to attach sec queue");
2059                         rte_spinlock_unlock(&internals->lock);
2060                         goto err1;
2061                 }
2062         }
2063         rte_spinlock_unlock(&internals->lock);
2064
2065         return 0;
2066
2067 err1:
2068         rte_free(session->cipher_key.data);
2069         rte_free(session->auth_key.data);
2070         memset(session, 0, sizeof(dpaa_sec_session));
2071
2072         return -EINVAL;
2073 }
2074
2075 static int
2076 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2077                 struct rte_crypto_sym_xform *xform,
2078                 struct rte_cryptodev_sym_session *sess,
2079                 struct rte_mempool *mempool)
2080 {
2081         void *sess_private_data;
2082         int ret;
2083
2084         PMD_INIT_FUNC_TRACE();
2085
2086         if (rte_mempool_get(mempool, &sess_private_data)) {
2087                 DPAA_SEC_ERR("Couldn't get object from session mempool");
2088                 return -ENOMEM;
2089         }
2090
2091         ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2092         if (ret != 0) {
2093                 DPAA_SEC_ERR("failed to configure session parameters");
2094
2095                 /* Return session to mempool */
2096                 rte_mempool_put(mempool, sess_private_data);
2097                 return ret;
2098         }
2099
2100         set_sym_session_private_data(sess, dev->driver_id,
2101                         sess_private_data);
2102
2103
2104         return 0;
2105 }
2106
2107 static inline void
2108 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2109 {
2110         struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2111         struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2112         uint8_t i;
2113
2114         for (i = 0; i < MAX_DPAA_CORES; i++) {
2115                 if (s->inq[i])
2116                         dpaa_sec_detach_rxq(qi, s->inq[i]);
2117                 s->inq[i] = NULL;
2118                 s->qp[i] = NULL;
2119         }
2120         rte_free(s->cipher_key.data);
2121         rte_free(s->auth_key.data);
2122         memset(s, 0, sizeof(dpaa_sec_session));
2123         rte_mempool_put(sess_mp, (void *)s);
2124 }
2125
2126 /** Clear the memory of session so it doesn't leave key material behind */
2127 static void
2128 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2129                 struct rte_cryptodev_sym_session *sess)
2130 {
2131         PMD_INIT_FUNC_TRACE();
2132         uint8_t index = dev->driver_id;
2133         void *sess_priv = get_sym_session_private_data(sess, index);
2134         dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2135
2136         if (sess_priv) {
2137                 free_session_memory(dev, s);
2138                 set_sym_session_private_data(sess, index, NULL);
2139         }
2140 }
2141
2142 static int
2143 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2144                            struct rte_security_session_conf *conf,
2145                            void *sess)
2146 {
2147         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2148         struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2149         struct rte_crypto_auth_xform *auth_xform = NULL;
2150         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2151         dpaa_sec_session *session = (dpaa_sec_session *)sess;
2152         uint32_t i;
2153
2154         PMD_INIT_FUNC_TRACE();
2155
2156         memset(session, 0, sizeof(dpaa_sec_session));
2157         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2158                 cipher_xform = &conf->crypto_xform->cipher;
2159                 if (conf->crypto_xform->next)
2160                         auth_xform = &conf->crypto_xform->next->auth;
2161         } else {
2162                 auth_xform = &conf->crypto_xform->auth;
2163                 if (conf->crypto_xform->next)
2164                         cipher_xform = &conf->crypto_xform->next->cipher;
2165         }
2166         session->proto_alg = conf->protocol;
2167
2168         if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2169                 session->cipher_key.data = rte_zmalloc(NULL,
2170                                                        cipher_xform->key.length,
2171                                                        RTE_CACHE_LINE_SIZE);
2172                 if (session->cipher_key.data == NULL &&
2173                                 cipher_xform->key.length > 0) {
2174                         DPAA_SEC_ERR("No Memory for cipher key");
2175                         return -ENOMEM;
2176                 }
2177                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2178                                 cipher_xform->key.length);
2179                 session->cipher_key.length = cipher_xform->key.length;
2180
2181                 switch (cipher_xform->algo) {
2182                 case RTE_CRYPTO_CIPHER_AES_CBC:
2183                 case RTE_CRYPTO_CIPHER_3DES_CBC:
2184                 case RTE_CRYPTO_CIPHER_AES_CTR:
2185                         break;
2186                 default:
2187                         DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2188                                 cipher_xform->algo);
2189                         goto out;
2190                 }
2191                 session->cipher_alg = cipher_xform->algo;
2192         } else {
2193                 session->cipher_key.data = NULL;
2194                 session->cipher_key.length = 0;
2195                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2196         }
2197
2198         if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2199                 session->auth_key.data = rte_zmalloc(NULL,
2200                                                 auth_xform->key.length,
2201                                                 RTE_CACHE_LINE_SIZE);
2202                 if (session->auth_key.data == NULL &&
2203                                 auth_xform->key.length > 0) {
2204                         DPAA_SEC_ERR("No Memory for auth key");
2205                         rte_free(session->cipher_key.data);
2206                         return -ENOMEM;
2207                 }
2208                 memcpy(session->auth_key.data, auth_xform->key.data,
2209                                 auth_xform->key.length);
2210                 session->auth_key.length = auth_xform->key.length;
2211
2212                 switch (auth_xform->algo) {
2213                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2214                 case RTE_CRYPTO_AUTH_MD5_HMAC:
2215                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2216                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2217                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2218                 case RTE_CRYPTO_AUTH_AES_CMAC:
2219                         break;
2220                 default:
2221                         DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2222                                 auth_xform->algo);
2223                         goto out;
2224                 }
2225                 session->auth_alg = auth_xform->algo;
2226         } else {
2227                 session->auth_key.data = NULL;
2228                 session->auth_key.length = 0;
2229                 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2230         }
2231
2232         if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2233                 if (ipsec_xform->tunnel.type ==
2234                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2235                         memset(&session->encap_pdb, 0,
2236                                 sizeof(struct ipsec_encap_pdb) +
2237                                 sizeof(session->ip4_hdr));
2238                         session->ip4_hdr.ip_v = IPVERSION;
2239                         session->ip4_hdr.ip_hl = 5;
2240                         session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2241                                                 sizeof(session->ip4_hdr));
2242                         session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2243                         session->ip4_hdr.ip_id = 0;
2244                         session->ip4_hdr.ip_off = 0;
2245                         session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2246                         session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2247                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2248                                         IPPROTO_ESP : IPPROTO_AH;
2249                         session->ip4_hdr.ip_sum = 0;
2250                         session->ip4_hdr.ip_src =
2251                                         ipsec_xform->tunnel.ipv4.src_ip;
2252                         session->ip4_hdr.ip_dst =
2253                                         ipsec_xform->tunnel.ipv4.dst_ip;
2254                         session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2255                                                 (void *)&session->ip4_hdr,
2256                                                 sizeof(struct ip));
2257                         session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2258                 } else if (ipsec_xform->tunnel.type ==
2259                                 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2260                         memset(&session->encap_pdb, 0,
2261                                 sizeof(struct ipsec_encap_pdb) +
2262                                 sizeof(session->ip6_hdr));
2263                         session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2264                                 DPAA_IPv6_DEFAULT_VTC_FLOW |
2265                                 ((ipsec_xform->tunnel.ipv6.dscp <<
2266                                         RTE_IPV6_HDR_TC_SHIFT) &
2267                                         RTE_IPV6_HDR_TC_MASK) |
2268                                 ((ipsec_xform->tunnel.ipv6.flabel <<
2269                                         RTE_IPV6_HDR_FL_SHIFT) &
2270                                         RTE_IPV6_HDR_FL_MASK));
2271                         /* Payload length will be updated by HW */
2272                         session->ip6_hdr.payload_len = 0;
2273                         session->ip6_hdr.hop_limits =
2274                                         ipsec_xform->tunnel.ipv6.hlimit;
2275                         session->ip6_hdr.proto = (ipsec_xform->proto ==
2276                                         RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2277                                         IPPROTO_ESP : IPPROTO_AH;
2278                         memcpy(&session->ip6_hdr.src_addr,
2279                                         &ipsec_xform->tunnel.ipv6.src_addr, 16);
2280                         memcpy(&session->ip6_hdr.dst_addr,
2281                                         &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2282                         session->encap_pdb.ip_hdr_len =
2283                                                 sizeof(struct rte_ipv6_hdr);
2284                 }
2285                 session->encap_pdb.options =
2286                         (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2287                         PDBOPTS_ESP_OIHI_PDB_INL |
2288                         PDBOPTS_ESP_IVSRC |
2289                         PDBHMO_ESP_ENCAP_DTTL |
2290                         PDBHMO_ESP_SNR;
2291                 if (ipsec_xform->options.esn)
2292                         session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2293                 session->encap_pdb.spi = ipsec_xform->spi;
2294                 session->dir = DIR_ENC;
2295         } else if (ipsec_xform->direction ==
2296                         RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2297                 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2298                 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2299                         session->decap_pdb.options = sizeof(struct ip) << 16;
2300                 else
2301                         session->decap_pdb.options =
2302                                         sizeof(struct rte_ipv6_hdr) << 16;
2303                 if (ipsec_xform->options.esn)
2304                         session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2305                 session->dir = DIR_DEC;
2306         } else
2307                 goto out;
2308         session->ctx_pool = internals->ctx_pool;
2309         rte_spinlock_lock(&internals->lock);
2310         for (i = 0; i < MAX_DPAA_CORES; i++) {
2311                 session->inq[i] = dpaa_sec_attach_rxq(internals);
2312                 if (session->inq[i] == NULL) {
2313                         DPAA_SEC_ERR("unable to attach sec queue");
2314                         rte_spinlock_unlock(&internals->lock);
2315                         goto out;
2316                 }
2317         }
2318         rte_spinlock_unlock(&internals->lock);
2319
2320         return 0;
2321 out:
2322         rte_free(session->auth_key.data);
2323         rte_free(session->cipher_key.data);
2324         memset(session, 0, sizeof(dpaa_sec_session));
2325         return -1;
2326 }
2327
2328 static int
2329 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2330                           struct rte_security_session_conf *conf,
2331                           void *sess)
2332 {
2333         struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2334         struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2335         struct rte_crypto_auth_xform *auth_xform = NULL;
2336         struct rte_crypto_cipher_xform *cipher_xform = NULL;
2337         dpaa_sec_session *session = (dpaa_sec_session *)sess;
2338         struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2339         uint32_t i;
2340
2341         PMD_INIT_FUNC_TRACE();
2342
2343         memset(session, 0, sizeof(dpaa_sec_session));
2344
2345         /* find xfrm types */
2346         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2347                 cipher_xform = &xform->cipher;
2348                 if (xform->next != NULL)
2349                         auth_xform = &xform->next->auth;
2350         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2351                 auth_xform = &xform->auth;
2352                 if (xform->next != NULL)
2353                         cipher_xform = &xform->next->cipher;
2354         } else {
2355                 DPAA_SEC_ERR("Invalid crypto type");
2356                 return -EINVAL;
2357         }
2358
2359         session->proto_alg = conf->protocol;
2360         if (cipher_xform) {
2361                 session->cipher_key.data = rte_zmalloc(NULL,
2362                                                cipher_xform->key.length,
2363                                                RTE_CACHE_LINE_SIZE);
2364                 if (session->cipher_key.data == NULL &&
2365                                 cipher_xform->key.length > 0) {
2366                         DPAA_SEC_ERR("No Memory for cipher key");
2367                         return -ENOMEM;
2368                 }
2369                 session->cipher_key.length = cipher_xform->key.length;
2370                 memcpy(session->cipher_key.data, cipher_xform->key.data,
2371                         cipher_xform->key.length);
2372                 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2373                                         DIR_ENC : DIR_DEC;
2374                 session->cipher_alg = cipher_xform->algo;
2375         } else {
2376                 session->cipher_key.data = NULL;
2377                 session->cipher_key.length = 0;
2378                 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2379                 session->dir = DIR_ENC;
2380         }
2381
2382         if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2383                 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2384                     pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2385                         DPAA_SEC_ERR(
2386                                 "PDCP Seq Num size should be 5/12 bits for cmode");
2387                         goto out;
2388                 }
2389         }
2390
2391         if (auth_xform) {
2392                 session->auth_key.data = rte_zmalloc(NULL,
2393                                                      auth_xform->key.length,
2394                                                      RTE_CACHE_LINE_SIZE);
2395                 if (!session->auth_key.data &&
2396                     auth_xform->key.length > 0) {
2397                         DPAA_SEC_ERR("No Memory for auth key");
2398                         rte_free(session->cipher_key.data);
2399                         return -ENOMEM;
2400                 }
2401                 session->auth_key.length = auth_xform->key.length;
2402                 memcpy(session->auth_key.data, auth_xform->key.data,
2403                        auth_xform->key.length);
2404                 session->auth_alg = auth_xform->algo;
2405         } else {
2406                 session->auth_key.data = NULL;
2407                 session->auth_key.length = 0;
2408                 session->auth_alg = 0;
2409         }
2410         session->pdcp.domain = pdcp_xform->domain;
2411         session->pdcp.bearer = pdcp_xform->bearer;
2412         session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2413         session->pdcp.sn_size = pdcp_xform->sn_size;
2414 #ifdef ENABLE_HFN_OVERRIDE
2415         session->pdcp.hfn_ovd = pdcp_xform->hfn_ovd;
2416 #endif
2417         session->pdcp.hfn = pdcp_xform->hfn;
2418         session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2419
2420         session->ctx_pool = dev_priv->ctx_pool;
2421         rte_spinlock_lock(&dev_priv->lock);
2422         for (i = 0; i < MAX_DPAA_CORES; i++) {
2423                 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2424                 if (session->inq[i] == NULL) {
2425                         DPAA_SEC_ERR("unable to attach sec queue");
2426                         rte_spinlock_unlock(&dev_priv->lock);
2427                         goto out;
2428                 }
2429         }
2430         rte_spinlock_unlock(&dev_priv->lock);
2431         return 0;
2432 out:
2433         rte_free(session->auth_key.data);
2434         rte_free(session->cipher_key.data);
2435         memset(session, 0, sizeof(dpaa_sec_session));
2436         return -1;
2437 }
2438
2439 static int
2440 dpaa_sec_security_session_create(void *dev,
2441                                  struct rte_security_session_conf *conf,
2442                                  struct rte_security_session *sess,
2443                                  struct rte_mempool *mempool)
2444 {
2445         void *sess_private_data;
2446         struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2447         int ret;
2448
2449         if (rte_mempool_get(mempool, &sess_private_data)) {
2450                 DPAA_SEC_ERR("Couldn't get object from session mempool");
2451                 return -ENOMEM;
2452         }
2453
2454         switch (conf->protocol) {
2455         case RTE_SECURITY_PROTOCOL_IPSEC:
2456                 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2457                                 sess_private_data);
2458                 break;
2459         case RTE_SECURITY_PROTOCOL_PDCP:
2460                 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2461                                 sess_private_data);
2462                 break;
2463         case RTE_SECURITY_PROTOCOL_MACSEC:
2464                 return -ENOTSUP;
2465         default:
2466                 return -EINVAL;
2467         }
2468         if (ret != 0) {
2469                 DPAA_SEC_ERR("failed to configure session parameters");
2470                 /* Return session to mempool */
2471                 rte_mempool_put(mempool, sess_private_data);
2472                 return ret;
2473         }
2474
2475         set_sec_session_private_data(sess, sess_private_data);
2476
2477         return ret;
2478 }
2479
2480 /** Clear the memory of session so it doesn't leave key material behind */
2481 static int
2482 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2483                 struct rte_security_session *sess)
2484 {
2485         PMD_INIT_FUNC_TRACE();
2486         void *sess_priv = get_sec_session_private_data(sess);
2487         dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2488
2489         if (sess_priv) {
2490                 free_session_memory((struct rte_cryptodev *)dev, s);
2491                 set_sec_session_private_data(sess, NULL);
2492         }
2493         return 0;
2494 }
2495
2496 static int
2497 dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2498                        struct rte_cryptodev_config *config __rte_unused)
2499 {
2500
2501         char str[20];
2502         struct dpaa_sec_dev_private *internals;
2503
2504         PMD_INIT_FUNC_TRACE();
2505
2506         internals = dev->data->dev_private;
2507         snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
2508         if (!internals->ctx_pool) {
2509                 internals->ctx_pool = rte_mempool_create((const char *)str,
2510                                                         CTX_POOL_NUM_BUFS,
2511                                                         CTX_POOL_BUF_SIZE,
2512                                                         CTX_POOL_CACHE_SIZE, 0,
2513                                                         NULL, NULL, NULL, NULL,
2514                                                         SOCKET_ID_ANY, 0);
2515                 if (!internals->ctx_pool) {
2516                         DPAA_SEC_ERR("%s create failed\n", str);
2517                         return -ENOMEM;
2518                 }
2519         } else
2520                 DPAA_SEC_INFO("mempool already created for dev_id : %d",
2521                                 dev->data->dev_id);
2522
2523         return 0;
2524 }
2525
2526 static int
2527 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2528 {
2529         PMD_INIT_FUNC_TRACE();
2530         return 0;
2531 }
2532
2533 static void
2534 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2535 {
2536         PMD_INIT_FUNC_TRACE();
2537 }
2538
2539 static int
2540 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2541 {
2542         struct dpaa_sec_dev_private *internals;
2543
2544         PMD_INIT_FUNC_TRACE();
2545
2546         if (dev == NULL)
2547                 return -ENOMEM;
2548
2549         internals = dev->data->dev_private;
2550         rte_mempool_free(internals->ctx_pool);
2551         internals->ctx_pool = NULL;
2552
2553         return 0;
2554 }
2555
2556 static void
2557 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2558                        struct rte_cryptodev_info *info)
2559 {
2560         struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2561
2562         PMD_INIT_FUNC_TRACE();
2563         if (info != NULL) {
2564                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2565                 info->feature_flags = dev->feature_flags;
2566                 info->capabilities = dpaa_sec_capabilities;
2567                 info->sym.max_nb_sessions = internals->max_nb_sessions;
2568                 info->driver_id = cryptodev_driver_id;
2569         }
2570 }
2571
2572 static struct rte_cryptodev_ops crypto_ops = {
2573         .dev_configure        = dpaa_sec_dev_configure,
2574         .dev_start            = dpaa_sec_dev_start,
2575         .dev_stop             = dpaa_sec_dev_stop,
2576         .dev_close            = dpaa_sec_dev_close,
2577         .dev_infos_get        = dpaa_sec_dev_infos_get,
2578         .queue_pair_setup     = dpaa_sec_queue_pair_setup,
2579         .queue_pair_release   = dpaa_sec_queue_pair_release,
2580         .queue_pair_count     = dpaa_sec_queue_pair_count,
2581         .sym_session_get_size     = dpaa_sec_sym_session_get_size,
2582         .sym_session_configure    = dpaa_sec_sym_session_configure,
2583         .sym_session_clear        = dpaa_sec_sym_session_clear
2584 };
2585
2586 static const struct rte_security_capability *
2587 dpaa_sec_capabilities_get(void *device __rte_unused)
2588 {
2589         return dpaa_sec_security_cap;
2590 }
2591
2592 static const struct rte_security_ops dpaa_sec_security_ops = {
2593         .session_create = dpaa_sec_security_session_create,
2594         .session_update = NULL,
2595         .session_stats_get = NULL,
2596         .session_destroy = dpaa_sec_security_session_destroy,
2597         .set_pkt_metadata = NULL,
2598         .capabilities_get = dpaa_sec_capabilities_get
2599 };
2600
2601 static int
2602 dpaa_sec_uninit(struct rte_cryptodev *dev)
2603 {
2604         struct dpaa_sec_dev_private *internals;
2605
2606         if (dev == NULL)
2607                 return -ENODEV;
2608
2609         internals = dev->data->dev_private;
2610         rte_free(dev->security_ctx);
2611
2612         /* In case close has been called, internals->ctx_pool would be NULL */
2613         rte_mempool_free(internals->ctx_pool);
2614         rte_free(internals);
2615
2616         DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2617                       dev->data->name, rte_socket_id());
2618
2619         return 0;
2620 }
2621
2622 static int
2623 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2624 {
2625         struct dpaa_sec_dev_private *internals;
2626         struct rte_security_ctx *security_instance;
2627         struct dpaa_sec_qp *qp;
2628         uint32_t i, flags;
2629         int ret;
2630
2631         PMD_INIT_FUNC_TRACE();
2632
2633         cryptodev->driver_id = cryptodev_driver_id;
2634         cryptodev->dev_ops = &crypto_ops;
2635
2636         cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2637         cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2638         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2639                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
2640                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2641                         RTE_CRYPTODEV_FF_SECURITY |
2642                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2643                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2644                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2645                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2646                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2647
2648         internals = cryptodev->data->dev_private;
2649         internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2650         internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2651
2652         /*
2653          * For secondary processes, we don't initialise any further as primary
2654          * has already done this work. Only check we don't need a different
2655          * RX function
2656          */
2657         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2658                 DPAA_SEC_WARN("Device already init by primary process");
2659                 return 0;
2660         }
2661
2662         /* Initialize security_ctx only for primary process*/
2663         security_instance = rte_malloc("rte_security_instances_ops",
2664                                 sizeof(struct rte_security_ctx), 0);
2665         if (security_instance == NULL)
2666                 return -ENOMEM;
2667         security_instance->device = (void *)cryptodev;
2668         security_instance->ops = &dpaa_sec_security_ops;
2669         security_instance->sess_cnt = 0;
2670         cryptodev->security_ctx = security_instance;
2671
2672         rte_spinlock_init(&internals->lock);
2673         for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2674                 /* init qman fq for queue pair */
2675                 qp = &internals->qps[i];
2676                 ret = dpaa_sec_init_tx(&qp->outq);
2677                 if (ret) {
2678                         DPAA_SEC_ERR("config tx of queue pair  %d", i);
2679                         goto init_error;
2680                 }
2681         }
2682
2683         flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2684                 QMAN_FQ_FLAG_TO_DCPORTAL;
2685         for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2686                 /* create rx qman fq for sessions*/
2687                 ret = qman_create_fq(0, flags, &internals->inq[i]);
2688                 if (unlikely(ret != 0)) {
2689                         DPAA_SEC_ERR("sec qman_create_fq failed");
2690                         goto init_error;
2691                 }
2692         }
2693
2694         RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2695         return 0;
2696
2697 init_error:
2698         DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2699
2700         dpaa_sec_uninit(cryptodev);
2701         return -EFAULT;
2702 }
2703
2704 static int
2705 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2706                                 struct rte_dpaa_device *dpaa_dev)
2707 {
2708         struct rte_cryptodev *cryptodev;
2709         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2710
2711         int retval;
2712
2713         snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2714                         dpaa_dev->id.dev_id);
2715
2716         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2717         if (cryptodev == NULL)
2718                 return -ENOMEM;
2719
2720         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2721                 cryptodev->data->dev_private = rte_zmalloc_socket(
2722                                         "cryptodev private structure",
2723                                         sizeof(struct dpaa_sec_dev_private),
2724                                         RTE_CACHE_LINE_SIZE,
2725                                         rte_socket_id());
2726
2727                 if (cryptodev->data->dev_private == NULL)
2728                         rte_panic("Cannot allocate memzone for private "
2729                                         "device data");
2730         }
2731
2732         dpaa_dev->crypto_dev = cryptodev;
2733         cryptodev->device = &dpaa_dev->device;
2734
2735         /* init user callbacks */
2736         TAILQ_INIT(&(cryptodev->link_intr_cbs));
2737
2738         /* if sec device version is not configured */
2739         if (!rta_get_sec_era()) {
2740                 const struct device_node *caam_node;
2741
2742                 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2743                         const uint32_t *prop = of_get_property(caam_node,
2744                                         "fsl,sec-era",
2745                                         NULL);
2746                         if (prop) {
2747                                 rta_set_sec_era(
2748                                         INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2749                                 break;
2750                         }
2751                 }
2752         }
2753
2754         /* Invoke PMD device initialization function */
2755         retval = dpaa_sec_dev_init(cryptodev);
2756         if (retval == 0)
2757                 return 0;
2758
2759         /* In case of error, cleanup is done */
2760         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2761                 rte_free(cryptodev->data->dev_private);
2762
2763         rte_cryptodev_pmd_release_device(cryptodev);
2764
2765         return -ENXIO;
2766 }
2767
2768 static int
2769 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2770 {
2771         struct rte_cryptodev *cryptodev;
2772         int ret;
2773
2774         cryptodev = dpaa_dev->crypto_dev;
2775         if (cryptodev == NULL)
2776                 return -ENODEV;
2777
2778         ret = dpaa_sec_uninit(cryptodev);
2779         if (ret)
2780                 return ret;
2781
2782         return rte_cryptodev_pmd_destroy(cryptodev);
2783 }
2784
2785 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2786         .drv_type = FSL_DPAA_CRYPTO,
2787         .driver = {
2788                 .name = "DPAA SEC PMD"
2789         },
2790         .probe = cryptodev_dpaa_sec_probe,
2791         .remove = cryptodev_dpaa_sec_remove,
2792 };
2793
2794 static struct cryptodev_driver dpaa_sec_crypto_drv;
2795
2796 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2797 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2798                 cryptodev_driver_id);
2799
2800 RTE_INIT(dpaa_sec_init_log)
2801 {
2802         dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2803         if (dpaa_logtype_sec >= 0)
2804                 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2805 }