crypto/octeontx2: add asymmetric session
[dpdk.git] / drivers / crypto / octeontx2 / otx2_cryptodev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2019 Marvell International Ltd.
3  */
4
5 #include <unistd.h>
6
7 #include <rte_cryptodev_pmd.h>
8 #include <rte_errno.h>
9
10 #include "otx2_cryptodev.h"
11 #include "otx2_cryptodev_capabilities.h"
12 #include "otx2_cryptodev_hw_access.h"
13 #include "otx2_cryptodev_mbox.h"
14 #include "otx2_cryptodev_ops.h"
15 #include "otx2_mbox.h"
16
17 #include "cpt_hw_types.h"
18 #include "cpt_pmd_logs.h"
19 #include "cpt_pmd_ops_helper.h"
20 #include "cpt_ucode.h"
21 #include "cpt_ucode_asym.h"
22
23 #define METABUF_POOL_CACHE_SIZE 512
24
25 /* Forward declarations */
26
27 static int
28 otx2_cpt_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id);
29
30 static void
31 qp_memzone_name_get(char *name, int size, int dev_id, int qp_id)
32 {
33         snprintf(name, size, "otx2_cpt_lf_mem_%u:%u", dev_id, qp_id);
34 }
35
36 static int
37 otx2_cpt_metabuf_mempool_create(const struct rte_cryptodev *dev,
38                                 struct otx2_cpt_qp *qp, uint8_t qp_id,
39                                 int nb_elements)
40 {
41         char mempool_name[RTE_MEMPOOL_NAMESIZE];
42         struct cpt_qp_meta_info *meta_info;
43         struct rte_mempool *pool;
44         int ret, max_mlen;
45         int asym_mlen = 0;
46         int lb_mlen = 0;
47         int sg_mlen = 0;
48
49         if (dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
50
51                 /* Get meta len for scatter gather mode */
52                 sg_mlen = cpt_pmd_ops_helper_get_mlen_sg_mode();
53
54                 /* Extra 32B saved for future considerations */
55                 sg_mlen += 4 * sizeof(uint64_t);
56
57                 /* Get meta len for linear buffer (direct) mode */
58                 lb_mlen = cpt_pmd_ops_helper_get_mlen_direct_mode();
59
60                 /* Extra 32B saved for future considerations */
61                 lb_mlen += 4 * sizeof(uint64_t);
62         }
63
64         if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) {
65
66                 /* Get meta len required for asymmetric operations */
67                 asym_mlen = cpt_pmd_ops_helper_asym_get_mlen();
68         }
69
70         /*
71          * Check max requirement for meta buffer to
72          * support crypto op of any type (sym/asym).
73          */
74         max_mlen = RTE_MAX(RTE_MAX(lb_mlen, sg_mlen), asym_mlen);
75
76         /* Allocate mempool */
77
78         snprintf(mempool_name, RTE_MEMPOOL_NAMESIZE, "otx2_cpt_mb_%u:%u",
79                  dev->data->dev_id, qp_id);
80
81         pool = rte_mempool_create_empty(mempool_name, nb_elements, max_mlen,
82                                         METABUF_POOL_CACHE_SIZE, 0,
83                                         rte_socket_id(), 0);
84
85         if (pool == NULL) {
86                 CPT_LOG_ERR("Could not create mempool for metabuf");
87                 return rte_errno;
88         }
89
90         ret = rte_mempool_set_ops_byname(pool, RTE_MBUF_DEFAULT_MEMPOOL_OPS,
91                                          NULL);
92         if (ret) {
93                 CPT_LOG_ERR("Could not set mempool ops");
94                 goto mempool_free;
95         }
96
97         ret = rte_mempool_populate_default(pool);
98         if (ret <= 0) {
99                 CPT_LOG_ERR("Could not populate metabuf pool");
100                 goto mempool_free;
101         }
102
103         meta_info = &qp->meta_info;
104
105         meta_info->pool = pool;
106         meta_info->lb_mlen = lb_mlen;
107         meta_info->sg_mlen = sg_mlen;
108
109         return 0;
110
111 mempool_free:
112         rte_mempool_free(pool);
113         return ret;
114 }
115
116 static void
117 otx2_cpt_metabuf_mempool_destroy(struct otx2_cpt_qp *qp)
118 {
119         struct cpt_qp_meta_info *meta_info = &qp->meta_info;
120
121         rte_mempool_free(meta_info->pool);
122
123         meta_info->pool = NULL;
124         meta_info->lb_mlen = 0;
125         meta_info->sg_mlen = 0;
126 }
127
128 static struct otx2_cpt_qp *
129 otx2_cpt_qp_create(const struct rte_cryptodev *dev, uint16_t qp_id,
130                    uint8_t group)
131 {
132         struct otx2_cpt_vf *vf = dev->data->dev_private;
133         uint64_t pg_sz = sysconf(_SC_PAGESIZE);
134         const struct rte_memzone *lf_mem;
135         uint32_t len, iq_len, size_div40;
136         char name[RTE_MEMZONE_NAMESIZE];
137         uint64_t used_len, iova;
138         struct otx2_cpt_qp *qp;
139         uint64_t lmtline;
140         uint8_t *va;
141         int ret;
142
143         /* Allocate queue pair */
144         qp = rte_zmalloc_socket("OCTEON TX2 Crypto PMD Queue Pair", sizeof(*qp),
145                                 OTX2_ALIGN, 0);
146         if (qp == NULL) {
147                 CPT_LOG_ERR("Could not allocate queue pair");
148                 return NULL;
149         }
150
151         iq_len = OTX2_CPT_IQ_LEN;
152
153         /*
154          * Queue size must be a multiple of 40 and effective queue size to
155          * software is (size_div40 - 1) * 40
156          */
157         size_div40 = (iq_len + 40 - 1) / 40 + 1;
158
159         /* For pending queue */
160         len = iq_len * RTE_ALIGN(sizeof(struct rid), 8);
161
162         /* Space for instruction group memory */
163         len += size_div40 * 16;
164
165         /* So that instruction queues start as pg size aligned */
166         len = RTE_ALIGN(len, pg_sz);
167
168         /* For instruction queues */
169         len += OTX2_CPT_IQ_LEN * sizeof(union cpt_inst_s);
170
171         /* Wastage after instruction queues */
172         len = RTE_ALIGN(len, pg_sz);
173
174         qp_memzone_name_get(name, RTE_MEMZONE_NAMESIZE, dev->data->dev_id,
175                             qp_id);
176
177         lf_mem = rte_memzone_reserve_aligned(name, len, vf->otx2_dev.node,
178                         RTE_MEMZONE_SIZE_HINT_ONLY | RTE_MEMZONE_256MB,
179                         RTE_CACHE_LINE_SIZE);
180         if (lf_mem == NULL) {
181                 CPT_LOG_ERR("Could not allocate reserved memzone");
182                 goto qp_free;
183         }
184
185         va = lf_mem->addr;
186         iova = lf_mem->iova;
187
188         memset(va, 0, len);
189
190         ret = otx2_cpt_metabuf_mempool_create(dev, qp, qp_id, iq_len);
191         if (ret) {
192                 CPT_LOG_ERR("Could not create mempool for metabuf");
193                 goto lf_mem_free;
194         }
195
196         /* Initialize pending queue */
197         qp->pend_q.rid_queue = (struct rid *)va;
198         qp->pend_q.enq_tail = 0;
199         qp->pend_q.deq_head = 0;
200         qp->pend_q.pending_count = 0;
201
202         used_len = iq_len * RTE_ALIGN(sizeof(struct rid), 8);
203         used_len += size_div40 * 16;
204         used_len = RTE_ALIGN(used_len, pg_sz);
205         iova += used_len;
206
207         qp->iq_dma_addr = iova;
208         qp->id = qp_id;
209         qp->base = OTX2_CPT_LF_BAR2(vf, qp_id);
210
211         lmtline = vf->otx2_dev.bar2 +
212                   (RVU_BLOCK_ADDR_LMT << 20 | qp_id << 12) +
213                   OTX2_LMT_LF_LMTLINE(0);
214
215         qp->lmtline = (void *)lmtline;
216
217         qp->lf_nq_reg = qp->base + OTX2_CPT_LF_NQ(0);
218
219         otx2_cpt_iq_disable(qp);
220
221         ret = otx2_cpt_iq_enable(dev, qp, group, OTX2_CPT_QUEUE_HI_PRIO,
222                                  size_div40);
223         if (ret) {
224                 CPT_LOG_ERR("Could not enable instruction queue");
225                 goto mempool_destroy;
226         }
227
228         return qp;
229
230 mempool_destroy:
231         otx2_cpt_metabuf_mempool_destroy(qp);
232 lf_mem_free:
233         rte_memzone_free(lf_mem);
234 qp_free:
235         rte_free(qp);
236         return NULL;
237 }
238
239 static int
240 otx2_cpt_qp_destroy(const struct rte_cryptodev *dev, struct otx2_cpt_qp *qp)
241 {
242         const struct rte_memzone *lf_mem;
243         char name[RTE_MEMZONE_NAMESIZE];
244         int ret;
245
246         otx2_cpt_iq_disable(qp);
247
248         otx2_cpt_metabuf_mempool_destroy(qp);
249
250         qp_memzone_name_get(name, RTE_MEMZONE_NAMESIZE, dev->data->dev_id,
251                             qp->id);
252
253         lf_mem = rte_memzone_lookup(name);
254
255         ret = rte_memzone_free(lf_mem);
256         if (ret)
257                 return ret;
258
259         rte_free(qp);
260
261         return 0;
262 }
263
264 static int
265 sym_session_configure(int driver_id, struct rte_crypto_sym_xform *xform,
266                       struct rte_cryptodev_sym_session *sess,
267                       struct rte_mempool *pool)
268 {
269         struct cpt_sess_misc *misc;
270         void *priv;
271         int ret;
272
273         if (unlikely(cpt_is_algo_supported(xform))) {
274                 CPT_LOG_ERR("Crypto xform not supported");
275                 return -ENOTSUP;
276         }
277
278         if (unlikely(rte_mempool_get(pool, &priv))) {
279                 CPT_LOG_ERR("Could not allocate session private data");
280                 return -ENOMEM;
281         }
282
283         misc = priv;
284
285         for ( ; xform != NULL; xform = xform->next) {
286                 switch (xform->type) {
287                 case RTE_CRYPTO_SYM_XFORM_AEAD:
288                         ret = fill_sess_aead(xform, misc);
289                         break;
290                 case RTE_CRYPTO_SYM_XFORM_CIPHER:
291                         ret = fill_sess_cipher(xform, misc);
292                         break;
293                 case RTE_CRYPTO_SYM_XFORM_AUTH:
294                         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
295                                 ret = fill_sess_gmac(xform, misc);
296                         else
297                                 ret = fill_sess_auth(xform, misc);
298                         break;
299                 default:
300                         ret = -1;
301                 }
302
303                 if (ret)
304                         goto priv_put;
305         }
306
307         set_sym_session_private_data(sess, driver_id, misc);
308
309         misc->ctx_dma_addr = rte_mempool_virt2iova(misc) +
310                              sizeof(struct cpt_sess_misc);
311
312         /*
313          * IE engines support IPsec operations
314          * SE engines support IPsec operations and Air-Crypto operations
315          */
316         if (misc->zsk_flag)
317                 misc->egrp = OTX2_CPT_EGRP_SE;
318         else
319                 misc->egrp = OTX2_CPT_EGRP_SE_IE;
320
321         return 0;
322
323 priv_put:
324         rte_mempool_put(pool, priv);
325
326         CPT_LOG_ERR("Crypto xform not supported");
327         return -ENOTSUP;
328 }
329
330 static void
331 sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
332 {
333         void *priv = get_sym_session_private_data(sess, driver_id);
334         struct rte_mempool *pool;
335
336         if (priv == NULL)
337                 return;
338
339         memset(priv, 0, cpt_get_session_size());
340
341         pool = rte_mempool_from_obj(priv);
342
343         set_sym_session_private_data(sess, driver_id, NULL);
344
345         rte_mempool_put(pool, priv);
346 }
347
348 static __rte_always_inline int32_t __hot
349 otx2_cpt_enqueue_req(const struct otx2_cpt_qp *qp,
350                      struct pending_queue *pend_q,
351                      struct cpt_request_info *req)
352 {
353         void *lmtline = qp->lmtline;
354         union cpt_inst_s inst;
355         uint64_t lmt_status;
356
357         if (unlikely(pend_q->pending_count >= OTX2_CPT_DEFAULT_CMD_QLEN))
358                 return -EAGAIN;
359
360         inst.u[0] = 0;
361         inst.s9x.res_addr = req->comp_baddr;
362         inst.u[2] = 0;
363         inst.u[3] = 0;
364
365         inst.s9x.ei0 = req->ist.ei0;
366         inst.s9x.ei1 = req->ist.ei1;
367         inst.s9x.ei2 = req->ist.ei2;
368         inst.s9x.ei3 = req->ist.ei3;
369
370         req->time_out = rte_get_timer_cycles() +
371                         DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
372
373         do {
374                 /* Copy CPT command to LMTLINE */
375                 memcpy(lmtline, &inst, sizeof(inst));
376
377                 /*
378                  * Make sure compiler does not reorder memcpy and ldeor.
379                  * LMTST transactions are always flushed from the write
380                  * buffer immediately, a DMB is not required to push out
381                  * LMTSTs.
382                  */
383                 rte_cio_wmb();
384                 lmt_status = otx2_lmt_submit(qp->lf_nq_reg);
385         } while (lmt_status == 0);
386
387         pend_q->rid_queue[pend_q->enq_tail].rid = (uintptr_t)req;
388
389         /* We will use soft queue length here to limit requests */
390         MOD_INC(pend_q->enq_tail, OTX2_CPT_DEFAULT_CMD_QLEN);
391         pend_q->pending_count += 1;
392
393         return 0;
394 }
395
396 static __rte_always_inline int __hot
397 otx2_cpt_enqueue_sym(struct otx2_cpt_qp *qp, struct rte_crypto_op *op,
398                      struct pending_queue *pend_q)
399 {
400         struct rte_crypto_sym_op *sym_op = op->sym;
401         struct cpt_request_info *req;
402         struct cpt_sess_misc *sess;
403         vq_cmd_word3_t *w3;
404         uint64_t cpt_op;
405         void *mdata;
406         int ret;
407
408         sess = get_sym_session_private_data(sym_op->session,
409                                             otx2_cryptodev_driver_id);
410
411         cpt_op = sess->cpt_op;
412
413         if (cpt_op & CPT_OP_CIPHER_MASK)
414                 ret = fill_fc_params(op, sess, &qp->meta_info, &mdata,
415                                      (void **)&req);
416         else
417                 ret = fill_digest_params(op, sess, &qp->meta_info, &mdata,
418                                          (void **)&req);
419
420         if (unlikely(ret)) {
421                 CPT_LOG_DP_ERR("Crypto req : op %p, cpt_op 0x%x ret 0x%x",
422                                 op, (unsigned int)cpt_op, ret);
423                 return ret;
424         }
425
426         w3 = ((vq_cmd_word3_t *)(&req->ist.ei3));
427         w3->s.grp = sess->egrp;
428
429         ret = otx2_cpt_enqueue_req(qp, pend_q, req);
430
431         if (unlikely(ret)) {
432                 /* Free buffer allocated by fill params routines */
433                 free_op_meta(mdata, qp->meta_info.pool);
434         }
435
436         return ret;
437 }
438
439 static __rte_always_inline int __hot
440 otx2_cpt_enqueue_sym_sessless(struct otx2_cpt_qp *qp, struct rte_crypto_op *op,
441                               struct pending_queue *pend_q)
442 {
443         const int driver_id = otx2_cryptodev_driver_id;
444         struct rte_crypto_sym_op *sym_op = op->sym;
445         struct rte_cryptodev_sym_session *sess;
446         int ret;
447
448         /* Create temporary session */
449
450         if (rte_mempool_get(qp->sess_mp, (void **)&sess))
451                 return -ENOMEM;
452
453         ret = sym_session_configure(driver_id, sym_op->xform, sess,
454                                     qp->sess_mp_priv);
455         if (ret)
456                 goto sess_put;
457
458         sym_op->session = sess;
459
460         ret = otx2_cpt_enqueue_sym(qp, op, pend_q);
461
462         if (unlikely(ret))
463                 goto priv_put;
464
465         return 0;
466
467 priv_put:
468         sym_session_clear(driver_id, sess);
469 sess_put:
470         rte_mempool_put(qp->sess_mp, sess);
471         return ret;
472 }
473
474 static uint16_t
475 otx2_cpt_enqueue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
476 {
477         uint16_t nb_allowed, count = 0;
478         struct otx2_cpt_qp *qp = qptr;
479         struct pending_queue *pend_q;
480         struct rte_crypto_op *op;
481         int ret;
482
483         pend_q = &qp->pend_q;
484
485         nb_allowed = OTX2_CPT_DEFAULT_CMD_QLEN - pend_q->pending_count;
486         if (nb_ops > nb_allowed)
487                 nb_ops = nb_allowed;
488
489         for (count = 0; count < nb_ops; count++) {
490                 op = ops[count];
491                 if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
492                         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
493                                 ret = otx2_cpt_enqueue_sym(qp, op, pend_q);
494                         else
495                                 ret = otx2_cpt_enqueue_sym_sessless(qp, op,
496                                                                     pend_q);
497                 } else
498                         break;
499
500                 if (unlikely(ret))
501                         break;
502         }
503
504         return count;
505 }
506
507 static inline void
508 otx2_cpt_dequeue_post_process(struct otx2_cpt_qp *qp, struct rte_crypto_op *cop,
509                               uintptr_t *rsp, uint8_t cc)
510 {
511         if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
512                 if (likely(cc == NO_ERR)) {
513                         /* Verify authentication data if required */
514                         if (unlikely(rsp[2]))
515                                 compl_auth_verify(cop, (uint8_t *)rsp[2],
516                                                  rsp[3]);
517                         else
518                                 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
519                 } else {
520                         if (cc == ERR_GC_ICV_MISCOMPARE)
521                                 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
522                         else
523                                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
524                 }
525
526                 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
527                         sym_session_clear(otx2_cryptodev_driver_id,
528                                           cop->sym->session);
529                         rte_mempool_put(qp->sess_mp, cop->sym->session);
530                         cop->sym->session = NULL;
531                 }
532         }
533 }
534
535 static __rte_always_inline uint8_t
536 otx2_cpt_compcode_get(struct cpt_request_info *req)
537 {
538         volatile struct cpt_res_s_9s *res;
539         uint8_t ret;
540
541         res = (volatile struct cpt_res_s_9s *)req->completion_addr;
542
543         if (unlikely(res->compcode == CPT_9X_COMP_E_NOTDONE)) {
544                 if (rte_get_timer_cycles() < req->time_out)
545                         return ERR_REQ_PENDING;
546
547                 CPT_LOG_DP_ERR("Request timed out");
548                 return ERR_REQ_TIMEOUT;
549         }
550
551         if (likely(res->compcode == CPT_9X_COMP_E_GOOD)) {
552                 ret = NO_ERR;
553                 if (unlikely(res->uc_compcode)) {
554                         ret = res->uc_compcode;
555                         CPT_LOG_DP_DEBUG("Request failed with microcode error");
556                         CPT_LOG_DP_DEBUG("MC completion code 0x%x",
557                                          res->uc_compcode);
558                 }
559         } else {
560                 CPT_LOG_DP_DEBUG("HW completion code 0x%x", res->compcode);
561
562                 ret = res->compcode;
563                 switch (res->compcode) {
564                 case CPT_9X_COMP_E_INSTERR:
565                         CPT_LOG_DP_ERR("Request failed with instruction error");
566                         break;
567                 case CPT_9X_COMP_E_FAULT:
568                         CPT_LOG_DP_ERR("Request failed with DMA fault");
569                         break;
570                 case CPT_9X_COMP_E_HWERR:
571                         CPT_LOG_DP_ERR("Request failed with hardware error");
572                         break;
573                 default:
574                         CPT_LOG_DP_ERR("Request failed with unknown completion code");
575                 }
576         }
577
578         return ret;
579 }
580
581 static uint16_t
582 otx2_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
583 {
584         int i, nb_pending, nb_completed;
585         struct otx2_cpt_qp *qp = qptr;
586         struct pending_queue *pend_q;
587         struct cpt_request_info *req;
588         struct rte_crypto_op *cop;
589         uint8_t cc[nb_ops];
590         struct rid *rid;
591         uintptr_t *rsp;
592         void *metabuf;
593
594         pend_q = &qp->pend_q;
595
596         nb_pending = pend_q->pending_count;
597
598         if (nb_ops > nb_pending)
599                 nb_ops = nb_pending;
600
601         for (i = 0; i < nb_ops; i++) {
602                 rid = &pend_q->rid_queue[pend_q->deq_head];
603                 req = (struct cpt_request_info *)(rid->rid);
604
605                 cc[i] = otx2_cpt_compcode_get(req);
606
607                 if (unlikely(cc[i] == ERR_REQ_PENDING))
608                         break;
609
610                 ops[i] = req->op;
611
612                 MOD_INC(pend_q->deq_head, OTX2_CPT_DEFAULT_CMD_QLEN);
613                 pend_q->pending_count -= 1;
614         }
615
616         nb_completed = i;
617
618         for (i = 0; i < nb_completed; i++) {
619                 rsp = (void *)ops[i];
620
621                 metabuf = (void *)rsp[0];
622                 cop = (void *)rsp[1];
623
624                 ops[i] = cop;
625
626                 otx2_cpt_dequeue_post_process(qp, cop, rsp, cc[i]);
627
628                 free_op_meta(metabuf, qp->meta_info.pool);
629         }
630
631         return nb_completed;
632 }
633
634 /* PMD ops */
635
636 static int
637 otx2_cpt_dev_config(struct rte_cryptodev *dev,
638                     struct rte_cryptodev_config *conf)
639 {
640         struct otx2_cpt_vf *vf = dev->data->dev_private;
641         int ret;
642
643         if (conf->nb_queue_pairs > vf->max_queues) {
644                 CPT_LOG_ERR("Invalid number of queue pairs requested");
645                 return -EINVAL;
646         }
647
648         dev->feature_flags &= ~conf->ff_disable;
649
650         /* Unregister error interrupts */
651         if (vf->err_intr_registered)
652                 otx2_cpt_err_intr_unregister(dev);
653
654         /* Detach queues */
655         if (vf->nb_queues) {
656                 ret = otx2_cpt_queues_detach(dev);
657                 if (ret) {
658                         CPT_LOG_ERR("Could not detach CPT queues");
659                         return ret;
660                 }
661         }
662
663         /* Attach queues */
664         ret = otx2_cpt_queues_attach(dev, conf->nb_queue_pairs);
665         if (ret) {
666                 CPT_LOG_ERR("Could not attach CPT queues");
667                 return -ENODEV;
668         }
669
670         ret = otx2_cpt_msix_offsets_get(dev);
671         if (ret) {
672                 CPT_LOG_ERR("Could not get MSI-X offsets");
673                 goto queues_detach;
674         }
675
676         /* Register error interrupts */
677         ret = otx2_cpt_err_intr_register(dev);
678         if (ret) {
679                 CPT_LOG_ERR("Could not register error interrupts");
680                 goto queues_detach;
681         }
682
683         dev->enqueue_burst = otx2_cpt_enqueue_burst;
684         dev->dequeue_burst = otx2_cpt_dequeue_burst;
685
686         rte_mb();
687         return 0;
688
689 queues_detach:
690         otx2_cpt_queues_detach(dev);
691         return ret;
692 }
693
694 static int
695 otx2_cpt_dev_start(struct rte_cryptodev *dev)
696 {
697         RTE_SET_USED(dev);
698
699         CPT_PMD_INIT_FUNC_TRACE();
700
701         return 0;
702 }
703
704 static void
705 otx2_cpt_dev_stop(struct rte_cryptodev *dev)
706 {
707         RTE_SET_USED(dev);
708
709         CPT_PMD_INIT_FUNC_TRACE();
710 }
711
712 static int
713 otx2_cpt_dev_close(struct rte_cryptodev *dev)
714 {
715         struct otx2_cpt_vf *vf = dev->data->dev_private;
716         int i, ret = 0;
717
718         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
719                 ret = otx2_cpt_queue_pair_release(dev, i);
720                 if (ret)
721                         return ret;
722         }
723
724         /* Unregister error interrupts */
725         if (vf->err_intr_registered)
726                 otx2_cpt_err_intr_unregister(dev);
727
728         /* Detach queues */
729         if (vf->nb_queues) {
730                 ret = otx2_cpt_queues_detach(dev);
731                 if (ret)
732                         CPT_LOG_ERR("Could not detach CPT queues");
733         }
734
735         return ret;
736 }
737
738 static void
739 otx2_cpt_dev_info_get(struct rte_cryptodev *dev,
740                       struct rte_cryptodev_info *info)
741 {
742         struct otx2_cpt_vf *vf = dev->data->dev_private;
743
744         if (info != NULL) {
745                 info->max_nb_queue_pairs = vf->max_queues;
746                 info->feature_flags = dev->feature_flags;
747                 info->capabilities = otx2_cpt_capabilities_get();
748                 info->sym.max_nb_sessions = 0;
749                 info->driver_id = otx2_cryptodev_driver_id;
750                 info->min_mbuf_headroom_req = OTX2_CPT_MIN_HEADROOM_REQ;
751                 info->min_mbuf_tailroom_req = OTX2_CPT_MIN_TAILROOM_REQ;
752         }
753 }
754
755 static int
756 otx2_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
757                           const struct rte_cryptodev_qp_conf *conf,
758                           int socket_id __rte_unused)
759 {
760         uint8_t grp_mask = OTX2_CPT_ENG_GRPS_MASK;
761         struct rte_pci_device *pci_dev;
762         struct otx2_cpt_qp *qp;
763
764         CPT_PMD_INIT_FUNC_TRACE();
765
766         if (dev->data->queue_pairs[qp_id] != NULL)
767                 otx2_cpt_queue_pair_release(dev, qp_id);
768
769         if (conf->nb_descriptors > OTX2_CPT_DEFAULT_CMD_QLEN) {
770                 CPT_LOG_ERR("Could not setup queue pair for %u descriptors",
771                             conf->nb_descriptors);
772                 return -EINVAL;
773         }
774
775         pci_dev = RTE_DEV_TO_PCI(dev->device);
776
777         if (pci_dev->mem_resource[2].addr == NULL) {
778                 CPT_LOG_ERR("Invalid PCI mem address");
779                 return -EIO;
780         }
781
782         qp = otx2_cpt_qp_create(dev, qp_id, grp_mask);
783         if (qp == NULL) {
784                 CPT_LOG_ERR("Could not create queue pair %d", qp_id);
785                 return -ENOMEM;
786         }
787
788         qp->sess_mp = conf->mp_session;
789         qp->sess_mp_priv = conf->mp_session_private;
790         dev->data->queue_pairs[qp_id] = qp;
791
792         return 0;
793 }
794
795 static int
796 otx2_cpt_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id)
797 {
798         struct otx2_cpt_qp *qp = dev->data->queue_pairs[qp_id];
799         int ret;
800
801         CPT_PMD_INIT_FUNC_TRACE();
802
803         if (qp == NULL)
804                 return -EINVAL;
805
806         CPT_LOG_INFO("Releasing queue pair %d", qp_id);
807
808         ret = otx2_cpt_qp_destroy(dev, qp);
809         if (ret) {
810                 CPT_LOG_ERR("Could not destroy queue pair %d", qp_id);
811                 return ret;
812         }
813
814         dev->data->queue_pairs[qp_id] = NULL;
815
816         return 0;
817 }
818
819 static unsigned int
820 otx2_cpt_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
821 {
822         return cpt_get_session_size();
823 }
824
825 static int
826 otx2_cpt_sym_session_configure(struct rte_cryptodev *dev,
827                                struct rte_crypto_sym_xform *xform,
828                                struct rte_cryptodev_sym_session *sess,
829                                struct rte_mempool *pool)
830 {
831         CPT_PMD_INIT_FUNC_TRACE();
832
833         return sym_session_configure(dev->driver_id, xform, sess, pool);
834 }
835
836 static void
837 otx2_cpt_sym_session_clear(struct rte_cryptodev *dev,
838                            struct rte_cryptodev_sym_session *sess)
839 {
840         CPT_PMD_INIT_FUNC_TRACE();
841
842         return sym_session_clear(dev->driver_id, sess);
843 }
844
845 static unsigned int
846 otx2_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
847 {
848         return sizeof(struct cpt_asym_sess_misc);
849 }
850
851 static int
852 otx2_cpt_asym_session_cfg(struct rte_cryptodev *dev,
853                           struct rte_crypto_asym_xform *xform,
854                           struct rte_cryptodev_asym_session *sess,
855                           struct rte_mempool *pool)
856 {
857         struct cpt_asym_sess_misc *priv;
858         int ret;
859
860         CPT_PMD_INIT_FUNC_TRACE();
861
862         if (rte_mempool_get(pool, (void **)&priv)) {
863                 CPT_LOG_ERR("Could not allocate session_private_data");
864                 return -ENOMEM;
865         }
866
867         memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
868
869         ret = cpt_fill_asym_session_parameters(priv, xform);
870         if (ret) {
871                 CPT_LOG_ERR("Could not configure session parameters");
872
873                 /* Return session to mempool */
874                 rte_mempool_put(pool, priv);
875                 return ret;
876         }
877
878         set_asym_session_private_data(sess, dev->driver_id, priv);
879         return 0;
880 }
881
882 static void
883 otx2_cpt_asym_session_clear(struct rte_cryptodev *dev,
884                             struct rte_cryptodev_asym_session *sess)
885 {
886         struct cpt_asym_sess_misc *priv;
887         struct rte_mempool *sess_mp;
888
889         CPT_PMD_INIT_FUNC_TRACE();
890
891         priv = get_asym_session_private_data(sess, dev->driver_id);
892         if (priv == NULL)
893                 return;
894
895         /* Free resources allocated in session_cfg */
896         cpt_free_asym_session_parameters(priv);
897
898         /* Reset and free object back to pool */
899         memset(priv, 0, otx2_cpt_asym_session_size_get(dev));
900         sess_mp = rte_mempool_from_obj(priv);
901         set_asym_session_private_data(sess, dev->driver_id, NULL);
902         rte_mempool_put(sess_mp, priv);
903 }
904
905 struct rte_cryptodev_ops otx2_cpt_ops = {
906         /* Device control ops */
907         .dev_configure = otx2_cpt_dev_config,
908         .dev_start = otx2_cpt_dev_start,
909         .dev_stop = otx2_cpt_dev_stop,
910         .dev_close = otx2_cpt_dev_close,
911         .dev_infos_get = otx2_cpt_dev_info_get,
912
913         .stats_get = NULL,
914         .stats_reset = NULL,
915         .queue_pair_setup = otx2_cpt_queue_pair_setup,
916         .queue_pair_release = otx2_cpt_queue_pair_release,
917         .queue_pair_count = NULL,
918
919         /* Symmetric crypto ops */
920         .sym_session_get_size = otx2_cpt_sym_session_get_size,
921         .sym_session_configure = otx2_cpt_sym_session_configure,
922         .sym_session_clear = otx2_cpt_sym_session_clear,
923
924         /* Asymmetric crypto ops */
925         .asym_session_get_size = otx2_cpt_asym_session_size_get,
926         .asym_session_configure = otx2_cpt_asym_session_cfg,
927         .asym_session_clear = otx2_cpt_asym_session_clear,
928
929 };