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