crypto/octeontx: add asymmetric enqueue/dequeue ops
[dpdk.git] / drivers / crypto / octeontx / otx_cryptodev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4
5 #include <rte_alarm.h>
6 #include <rte_bus_pci.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_errno.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
12
13 #include "otx_cryptodev.h"
14 #include "otx_cryptodev_capabilities.h"
15 #include "otx_cryptodev_hw_access.h"
16 #include "otx_cryptodev_mbox.h"
17 #include "otx_cryptodev_ops.h"
18
19 #include "cpt_pmd_logs.h"
20 #include "cpt_ucode.h"
21 #include "cpt_ucode_asym.h"
22
23 /* Forward declarations */
24
25 static int
26 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
27
28 /* Alarm routines */
29
30 static void
31 otx_cpt_alarm_cb(void *arg)
32 {
33         struct cpt_vf *cptvf = arg;
34         otx_cpt_poll_misc(cptvf);
35         rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
36                           otx_cpt_alarm_cb, cptvf);
37 }
38
39 static int
40 otx_cpt_periodic_alarm_start(void *arg)
41 {
42         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
43                                  otx_cpt_alarm_cb, arg);
44 }
45
46 static int
47 otx_cpt_periodic_alarm_stop(void *arg)
48 {
49         return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
50 }
51
52 /* PMD ops */
53
54 static int
55 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
56                    struct rte_cryptodev_config *config __rte_unused)
57 {
58         CPT_PMD_INIT_FUNC_TRACE();
59         return 0;
60 }
61
62 static int
63 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
64 {
65         void *cptvf = c_dev->data->dev_private;
66
67         CPT_PMD_INIT_FUNC_TRACE();
68
69         return otx_cpt_start_device(cptvf);
70 }
71
72 static void
73 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
74 {
75         void *cptvf = c_dev->data->dev_private;
76
77         CPT_PMD_INIT_FUNC_TRACE();
78
79         otx_cpt_stop_device(cptvf);
80 }
81
82 static int
83 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
84 {
85         void *cptvf = c_dev->data->dev_private;
86         int i, ret;
87
88         CPT_PMD_INIT_FUNC_TRACE();
89
90         for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
91                 ret = otx_cpt_que_pair_release(c_dev, i);
92                 if (ret)
93                         return ret;
94         }
95
96         otx_cpt_periodic_alarm_stop(cptvf);
97         otx_cpt_deinit_device(cptvf);
98
99         return 0;
100 }
101
102 static void
103 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
104 {
105         CPT_PMD_INIT_FUNC_TRACE();
106         if (info != NULL) {
107                 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
108                 info->feature_flags = dev->feature_flags;
109                 info->capabilities = otx_get_capabilities(info->feature_flags);
110                 info->sym.max_nb_sessions = 0;
111                 info->driver_id = otx_cryptodev_driver_id;
112                 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
113                 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
114         }
115 }
116
117 static void
118 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
119                   struct rte_cryptodev_stats *stats __rte_unused)
120 {
121         CPT_PMD_INIT_FUNC_TRACE();
122 }
123
124 static void
125 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
126 {
127         CPT_PMD_INIT_FUNC_TRACE();
128 }
129
130 static int
131 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
132                        uint16_t que_pair_id,
133                        const struct rte_cryptodev_qp_conf *qp_conf,
134                        int socket_id __rte_unused)
135 {
136         struct cpt_instance *instance = NULL;
137         struct rte_pci_device *pci_dev;
138         int ret = -1;
139
140         CPT_PMD_INIT_FUNC_TRACE();
141
142         if (dev->data->queue_pairs[que_pair_id] != NULL) {
143                 ret = otx_cpt_que_pair_release(dev, que_pair_id);
144                 if (ret)
145                         return ret;
146         }
147
148         if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
149                 CPT_LOG_INFO("Number of descriptors too big %d, using default "
150                              "queue length of %d", qp_conf->nb_descriptors,
151                              DEFAULT_CMD_QLEN);
152         }
153
154         pci_dev = RTE_DEV_TO_PCI(dev->device);
155
156         if (pci_dev->mem_resource[0].addr == NULL) {
157                 CPT_LOG_ERR("PCI mem address null");
158                 return -EIO;
159         }
160
161         ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
162         if (ret != 0 || instance == NULL) {
163                 CPT_LOG_ERR("Error getting instance handle from device %s : "
164                             "ret = %d", dev->data->name, ret);
165                 return ret;
166         }
167
168         instance->queue_id = que_pair_id;
169         instance->sess_mp = qp_conf->mp_session;
170         instance->sess_mp_priv = qp_conf->mp_session_private;
171         dev->data->queue_pairs[que_pair_id] = instance;
172
173         return 0;
174 }
175
176 static int
177 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
178 {
179         struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
180         int ret;
181
182         CPT_PMD_INIT_FUNC_TRACE();
183
184         ret = otx_cpt_put_resource(instance);
185         if (ret != 0) {
186                 CPT_LOG_ERR("Error putting instance handle of device %s : "
187                             "ret = %d", dev->data->name, ret);
188                 return ret;
189         }
190
191         dev->data->queue_pairs[que_pair_id] = NULL;
192
193         return 0;
194 }
195
196 static unsigned int
197 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
198 {
199         return cpt_get_session_size();
200 }
201
202 static void
203 otx_cpt_session_init(void *sym_sess, uint8_t driver_id)
204 {
205         struct rte_cryptodev_sym_session *sess = sym_sess;
206         struct cpt_sess_misc *cpt_sess =
207          (struct cpt_sess_misc *) get_sym_session_private_data(sess, driver_id);
208
209         CPT_PMD_INIT_FUNC_TRACE();
210         cpt_sess->ctx_dma_addr = rte_mempool_virt2iova(cpt_sess) +
211                         sizeof(struct cpt_sess_misc);
212 }
213
214 static int
215 otx_cpt_session_cfg(struct rte_cryptodev *dev,
216                     struct rte_crypto_sym_xform *xform,
217                     struct rte_cryptodev_sym_session *sess,
218                     struct rte_mempool *mempool)
219 {
220         struct rte_crypto_sym_xform *chain;
221         void *sess_private_data = NULL;
222
223         CPT_PMD_INIT_FUNC_TRACE();
224
225         if (cpt_is_algo_supported(xform))
226                 goto err;
227
228         if (unlikely(sess == NULL)) {
229                 CPT_LOG_ERR("invalid session struct");
230                 return -EINVAL;
231         }
232
233         if (rte_mempool_get(mempool, &sess_private_data)) {
234                 CPT_LOG_ERR("Could not allocate sess_private_data");
235                 return -ENOMEM;
236         }
237
238         chain = xform;
239         while (chain) {
240                 switch (chain->type) {
241                 case RTE_CRYPTO_SYM_XFORM_AEAD:
242                         if (fill_sess_aead(chain, sess_private_data))
243                                 goto err;
244                         break;
245                 case RTE_CRYPTO_SYM_XFORM_CIPHER:
246                         if (fill_sess_cipher(chain, sess_private_data))
247                                 goto err;
248                         break;
249                 case RTE_CRYPTO_SYM_XFORM_AUTH:
250                         if (chain->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
251                                 if (fill_sess_gmac(chain, sess_private_data))
252                                         goto err;
253                         } else {
254                                 if (fill_sess_auth(chain, sess_private_data))
255                                         goto err;
256                         }
257                         break;
258                 default:
259                         CPT_LOG_ERR("Invalid crypto xform type");
260                         break;
261                 }
262                 chain = chain->next;
263         }
264         set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
265         otx_cpt_session_init(sess, dev->driver_id);
266         return 0;
267
268 err:
269         if (sess_private_data)
270                 rte_mempool_put(mempool, sess_private_data);
271         return -EPERM;
272 }
273
274 static void
275 otx_cpt_session_clear(struct rte_cryptodev *dev,
276                   struct rte_cryptodev_sym_session *sess)
277 {
278         void *sess_priv = get_sym_session_private_data(sess, dev->driver_id);
279
280         CPT_PMD_INIT_FUNC_TRACE();
281         if (sess_priv) {
282                 memset(sess_priv, 0, otx_cpt_get_session_size(dev));
283                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
284                 set_sym_session_private_data(sess, dev->driver_id, NULL);
285                 rte_mempool_put(sess_mp, sess_priv);
286         }
287 }
288
289 static unsigned int
290 otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
291 {
292         return sizeof(struct cpt_asym_sess_misc);
293 }
294
295 static int
296 otx_cpt_asym_session_cfg(struct rte_cryptodev *dev,
297                          struct rte_crypto_asym_xform *xform __rte_unused,
298                          struct rte_cryptodev_asym_session *sess,
299                          struct rte_mempool *pool)
300 {
301         struct cpt_asym_sess_misc *priv;
302         int ret;
303
304         CPT_PMD_INIT_FUNC_TRACE();
305
306         if (rte_mempool_get(pool, (void **)&priv)) {
307                 CPT_LOG_ERR("Could not allocate session private data");
308                 return -ENOMEM;
309         }
310
311         memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
312
313         ret = cpt_fill_asym_session_parameters(priv, xform);
314         if (ret) {
315                 CPT_LOG_ERR("Could not configure session parameters");
316
317                 /* Return session to mempool */
318                 rte_mempool_put(pool, priv);
319                 return ret;
320         }
321
322         set_asym_session_private_data(sess, dev->driver_id, priv);
323         return 0;
324 }
325
326 static void
327 otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
328                            struct rte_cryptodev_asym_session *sess)
329 {
330         struct cpt_asym_sess_misc *priv;
331         struct rte_mempool *sess_mp;
332
333         CPT_PMD_INIT_FUNC_TRACE();
334
335         priv = get_asym_session_private_data(sess, dev->driver_id);
336
337         if (priv == NULL)
338                 return;
339
340         /* Free resources allocated during session configure */
341         cpt_free_asym_session_parameters(priv);
342         memset(priv, 0, otx_cpt_asym_session_size_get(dev));
343         sess_mp = rte_mempool_from_obj(priv);
344         set_asym_session_private_data(sess, dev->driver_id, NULL);
345         rte_mempool_put(sess_mp, priv);
346 }
347
348 static __rte_always_inline int32_t __hot
349 otx_cpt_request_enqueue(struct cpt_instance *instance,
350                         struct pending_queue *pqueue,
351                         void *req)
352 {
353         struct cpt_request_info *user_req = (struct cpt_request_info *)req;
354
355         if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN))
356                 return -EAGAIN;
357
358         fill_cpt_inst(instance, req);
359
360         CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
361
362         /* Fill time_out cycles */
363         user_req->time_out = rte_get_timer_cycles() +
364                         DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
365         user_req->extra_time = 0;
366
367         /* Default mode of software queue */
368         mark_cpt_inst(instance);
369
370         pqueue->rid_queue[pqueue->enq_tail].rid = (uintptr_t)user_req;
371
372         /* We will use soft queue length here to limit requests */
373         MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
374         pqueue->pending_count += 1;
375
376         CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
377                          "op: %p", user_req, user_req->op);
378         return 0;
379 }
380
381 static __rte_always_inline int __hot
382 otx_cpt_enq_single_asym(struct cpt_instance *instance,
383                         struct rte_crypto_op *op,
384                         struct pending_queue *pqueue)
385 {
386         struct cpt_qp_meta_info *minfo = &instance->meta_info;
387         struct rte_crypto_asym_op *asym_op = op->asym;
388         struct asym_op_params params = {0};
389         struct cpt_asym_sess_misc *sess;
390         uintptr_t *cop;
391         void *mdata;
392         int ret;
393
394         if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
395                 CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
396                 return -ENOMEM;
397         }
398
399         sess = get_asym_session_private_data(asym_op->session,
400                                              otx_cryptodev_driver_id);
401
402         /* Store phys_addr of the mdata to meta_buf */
403         params.meta_buf = rte_mempool_virt2iova(mdata);
404
405         cop = mdata;
406         cop[0] = (uintptr_t)mdata;
407         cop[1] = (uintptr_t)op;
408         cop[2] = cop[3] = 0ULL;
409
410         params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
411         params.req->op = cop;
412
413         /* Adjust meta_buf by crypto_op data  and request_info struct */
414         params.meta_buf += (4 * sizeof(uintptr_t)) +
415                            sizeof(struct cpt_request_info);
416
417         switch (sess->xfrm_type) {
418         case RTE_CRYPTO_ASYM_XFORM_MODEX:
419                 ret = cpt_modex_prep(&params, &sess->mod_ctx);
420                 if (unlikely(ret))
421                         goto req_fail;
422                 break;
423         case RTE_CRYPTO_ASYM_XFORM_RSA:
424                 ret = cpt_enqueue_rsa_op(op, &params, sess);
425                 if (unlikely(ret))
426                         goto req_fail;
427                 break;
428         default:
429                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
430                 ret = -EINVAL;
431                 goto req_fail;
432         }
433
434         ret = otx_cpt_request_enqueue(instance, pqueue, params.req);
435
436         if (unlikely(ret)) {
437                 CPT_LOG_DP_ERR("Could not enqueue crypto req");
438                 goto req_fail;
439         }
440
441         return 0;
442
443 req_fail:
444         free_op_meta(mdata, minfo->pool);
445
446         return ret;
447 }
448
449 static __rte_always_inline int __hot
450 otx_cpt_enq_single_sym(struct cpt_instance *instance,
451                        struct rte_crypto_op *op,
452                        struct pending_queue *pqueue)
453 {
454         struct cpt_sess_misc *sess;
455         struct rte_crypto_sym_op *sym_op = op->sym;
456         void *prep_req, *mdata = NULL;
457         int ret = 0;
458         uint64_t cpt_op;
459
460         sess = (struct cpt_sess_misc *)
461                         get_sym_session_private_data(sym_op->session,
462                                                      otx_cryptodev_driver_id);
463
464         cpt_op = sess->cpt_op;
465
466         if (likely(cpt_op & CPT_OP_CIPHER_MASK))
467                 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
468                                      &prep_req);
469         else
470                 ret = fill_digest_params(op, sess, &instance->meta_info,
471                                          &mdata, &prep_req);
472
473         if (unlikely(ret)) {
474                 CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
475                                "ret 0x%x", op, (unsigned int)cpt_op, ret);
476                 return ret;
477         }
478
479         /* Enqueue prepared instruction to h/w */
480         ret = otx_cpt_request_enqueue(instance, pqueue, prep_req);
481
482         if (unlikely(ret)) {
483                 /* Buffer allocated for request preparation need to be freed */
484                 free_op_meta(mdata, instance->meta_info.pool);
485                 return ret;
486         }
487
488         return 0;
489 }
490
491 static __rte_always_inline int __hot
492 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
493                                 struct rte_crypto_op *op,
494                                 struct pending_queue *pqueue)
495 {
496         struct cpt_sess_misc *sess;
497         struct rte_crypto_sym_op *sym_op = op->sym;
498         int ret;
499         void *sess_t = NULL;
500         void *sess_private_data_t = NULL;
501
502         /* Create tmp session */
503
504         if (rte_mempool_get(instance->sess_mp, (void **)&sess_t)) {
505                 ret = -ENOMEM;
506                 goto exit;
507         }
508
509         if (rte_mempool_get(instance->sess_mp_priv,
510                         (void **)&sess_private_data_t)) {
511                 ret = -ENOMEM;
512                 goto free_sess;
513         }
514
515         sess = (struct cpt_sess_misc *)sess_private_data_t;
516
517         sess->ctx_dma_addr = rte_mempool_virt2iova(sess) +
518                         sizeof(struct cpt_sess_misc);
519
520         ret = instance_session_cfg(sym_op->xform, (void *)sess);
521         if (unlikely(ret)) {
522                 ret = -EINVAL;
523                 goto free_sess_priv;
524         }
525
526         /* Save tmp session in op */
527
528         sym_op->session = (struct rte_cryptodev_sym_session *)sess_t;
529         set_sym_session_private_data(sym_op->session, otx_cryptodev_driver_id,
530                                      sess_private_data_t);
531
532         /* Enqueue op with the tmp session set */
533         ret = otx_cpt_enq_single_sym(instance, op, pqueue);
534
535         if (unlikely(ret))
536                 goto free_sess_priv;
537
538         return 0;
539
540 free_sess_priv:
541         rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
542 free_sess:
543         rte_mempool_put(instance->sess_mp, sess_t);
544 exit:
545         return ret;
546 }
547
548 #define OP_TYPE_SYM             0
549 #define OP_TYPE_ASYM            1
550
551 static __rte_always_inline int __hot
552 otx_cpt_enq_single(struct cpt_instance *inst,
553                    struct rte_crypto_op *op,
554                    struct pending_queue *pqueue,
555                    const uint8_t op_type)
556 {
557         /* Check for the type */
558
559         if (op_type == OP_TYPE_SYM) {
560                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
561                         return otx_cpt_enq_single_sym(inst, op, pqueue);
562                 else
563                         return otx_cpt_enq_single_sym_sessless(inst, op,
564                                                                pqueue);
565         }
566
567         if (op_type == OP_TYPE_ASYM) {
568                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
569                         return otx_cpt_enq_single_asym(inst, op, pqueue);
570         }
571
572         /* Should not reach here */
573         return -ENOTSUP;
574 }
575
576 static  __rte_always_inline uint16_t __hot
577 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
578                     const uint8_t op_type)
579 {
580         struct cpt_instance *instance = (struct cpt_instance *)qptr;
581         uint16_t count;
582         int ret;
583         struct cpt_vf *cptvf = (struct cpt_vf *)instance;
584         struct pending_queue *pqueue = &cptvf->pqueue;
585
586         count = DEFAULT_CMD_QLEN - pqueue->pending_count;
587         if (nb_ops > count)
588                 nb_ops = count;
589
590         count = 0;
591         while (likely(count < nb_ops)) {
592
593                 /* Enqueue single op */
594                 ret = otx_cpt_enq_single(instance, ops[count], pqueue, op_type);
595
596                 if (unlikely(ret))
597                         break;
598                 count++;
599         }
600         otx_cpt_ring_dbell(instance, count);
601         return count;
602 }
603
604 static uint16_t
605 otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
606 {
607         return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
608 }
609
610 static uint16_t
611 otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
612 {
613         return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
614 }
615
616 static inline void
617 otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
618                     struct rte_crypto_rsa_xform *rsa_ctx)
619
620 {
621         struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
622
623         switch (rsa->op_type) {
624         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
625                 rsa->cipher.length = rsa_ctx->n.length;
626                 memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
627                 break;
628         case RTE_CRYPTO_ASYM_OP_DECRYPT:
629                 if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
630                         rsa->message.length = rsa_ctx->n.length;
631                 else {
632                         /* Get length of decrypted output */
633                         rsa->message.length = rte_cpu_to_be_16
634                                         (*((uint16_t *)req->rptr));
635
636                         /* Offset data pointer by length fields */
637                         req->rptr += 2;
638                 }
639                 memcpy(rsa->message.data, req->rptr, rsa->message.length);
640                 break;
641         case RTE_CRYPTO_ASYM_OP_SIGN:
642                 rsa->sign.length = rsa_ctx->n.length;
643                 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
644                 break;
645         case RTE_CRYPTO_ASYM_OP_VERIFY:
646                 if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
647                         rsa->sign.length = rsa_ctx->n.length;
648                 else {
649                         /* Get length of decrypted output */
650                         rsa->sign.length = rte_cpu_to_be_16
651                                         (*((uint16_t *)req->rptr));
652
653                         /* Offset data pointer by length fields */
654                         req->rptr += 2;
655                 }
656                 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
657
658                 if (memcmp(rsa->sign.data, rsa->message.data,
659                            rsa->message.length)) {
660                         CPT_LOG_DP_ERR("RSA verification failed");
661                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
662                 }
663                 break;
664         default:
665                 CPT_LOG_DP_DEBUG("Invalid RSA operation type");
666                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
667                 break;
668         }
669 }
670
671 static __rte_always_inline void __hot
672 otx_cpt_asym_post_process(struct rte_crypto_op *cop,
673                           struct cpt_request_info *req)
674 {
675         struct rte_crypto_asym_op *op = cop->asym;
676         struct cpt_asym_sess_misc *sess;
677
678         sess = get_asym_session_private_data(op->session,
679                                              otx_cryptodev_driver_id);
680
681         switch (sess->xfrm_type) {
682         case RTE_CRYPTO_ASYM_XFORM_RSA:
683                 otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
684                 break;
685         case RTE_CRYPTO_ASYM_XFORM_MODEX:
686                 op->modex.result.length = sess->mod_ctx.modulus.length;
687                 memcpy(op->modex.result.data, req->rptr,
688                        op->modex.result.length);
689                 break;
690         default:
691                 CPT_LOG_DP_DEBUG("Invalid crypto xform type");
692                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
693                 break;
694         }
695 }
696
697 static __rte_always_inline void __hot
698 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
699                              const uint8_t op_type)
700 {
701         /* H/w has returned success */
702         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
703
704         /* Perform further post processing */
705
706         if ((op_type == OP_TYPE_SYM) &&
707             (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
708                 /* Check if auth verify need to be completed */
709                 if (unlikely(rsp[2]))
710                         compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
711                 return;
712         }
713
714         if ((op_type == OP_TYPE_ASYM) &&
715             (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
716                 rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
717                 otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
718         }
719
720         return;
721 }
722
723 static __rte_always_inline uint16_t __hot
724 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
725                     const uint8_t op_type)
726 {
727         struct cpt_instance *instance = (struct cpt_instance *)qptr;
728         struct cpt_request_info *user_req;
729         struct cpt_vf *cptvf = (struct cpt_vf *)instance;
730         struct rid *rid_e;
731         uint8_t cc[nb_ops];
732         int i, count, pcount;
733         uint8_t ret;
734         int nb_completed;
735         struct pending_queue *pqueue = &cptvf->pqueue;
736         struct rte_crypto_op *cop;
737         void *metabuf;
738         uintptr_t *rsp;
739
740         pcount = pqueue->pending_count;
741         count = (nb_ops > pcount) ? pcount : nb_ops;
742
743         for (i = 0; i < count; i++) {
744                 rid_e = &pqueue->rid_queue[pqueue->deq_head];
745                 user_req = (struct cpt_request_info *)(rid_e->rid);
746
747                 if (likely((i+1) < count))
748                         rte_prefetch_non_temporal((void *)rid_e[1].rid);
749
750                 ret = check_nb_command_id(user_req, instance);
751
752                 if (unlikely(ret == ERR_REQ_PENDING)) {
753                         /* Stop checking for completions */
754                         break;
755                 }
756
757                 /* Return completion code and op handle */
758                 cc[i] = ret;
759                 ops[i] = user_req->op;
760
761                 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
762                                  user_req, user_req->op, ret);
763
764                 MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
765                 pqueue->pending_count -= 1;
766         }
767
768         nb_completed = i;
769
770         for (i = 0; i < nb_completed; i++) {
771
772                 rsp = (void *)ops[i];
773
774                 if (likely((i + 1) < nb_completed))
775                         rte_prefetch0(ops[i+1]);
776
777                 metabuf = (void *)rsp[0];
778                 cop = (void *)rsp[1];
779
780                 ops[i] = cop;
781
782                 /* Check completion code */
783
784                 if (likely(cc[i] == 0)) {
785                         /* H/w success pkt. Post process */
786                         otx_cpt_dequeue_post_process(cop, rsp, op_type);
787                 } else if (cc[i] == ERR_GC_ICV_MISCOMPARE) {
788                         /* auth data mismatch */
789                         cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
790                 } else {
791                         /* Error */
792                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
793                 }
794
795                 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
796                         void *sess_private_data_t =
797                                 get_sym_session_private_data(cop->sym->session,
798                                                 otx_cryptodev_driver_id);
799                         memset(sess_private_data_t, 0,
800                                         cpt_get_session_size());
801                         memset(cop->sym->session, 0,
802                         rte_cryptodev_sym_get_existing_header_session_size(
803                                         cop->sym->session));
804                         rte_mempool_put(instance->sess_mp_priv,
805                                         sess_private_data_t);
806                         rte_mempool_put(instance->sess_mp, cop->sym->session);
807                         cop->sym->session = NULL;
808                 }
809                 free_op_meta(metabuf, instance->meta_info.pool);
810         }
811
812         return nb_completed;
813 }
814
815 static uint16_t
816 otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
817 {
818         return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
819 }
820
821 static uint16_t
822 otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
823 {
824         return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
825 }
826
827 static struct rte_cryptodev_ops cptvf_ops = {
828         /* Device related operations */
829         .dev_configure = otx_cpt_dev_config,
830         .dev_start = otx_cpt_dev_start,
831         .dev_stop = otx_cpt_dev_stop,
832         .dev_close = otx_cpt_dev_close,
833         .dev_infos_get = otx_cpt_dev_info_get,
834
835         .stats_get = otx_cpt_stats_get,
836         .stats_reset = otx_cpt_stats_reset,
837         .queue_pair_setup = otx_cpt_que_pair_setup,
838         .queue_pair_release = otx_cpt_que_pair_release,
839         .queue_pair_count = NULL,
840
841         /* Crypto related operations */
842         .sym_session_get_size = otx_cpt_get_session_size,
843         .sym_session_configure = otx_cpt_session_cfg,
844         .sym_session_clear = otx_cpt_session_clear,
845
846         .asym_session_get_size = otx_cpt_asym_session_size_get,
847         .asym_session_configure = otx_cpt_asym_session_cfg,
848         .asym_session_clear = otx_cpt_asym_session_clear,
849 };
850
851 int
852 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
853 {
854         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
855         struct cpt_vf *cptvf = NULL;
856         void *reg_base;
857         char dev_name[32];
858         int ret;
859
860         if (pdev->mem_resource[0].phys_addr == 0ULL)
861                 return -EIO;
862
863         /* for secondary processes, we don't initialise any further as primary
864          * has already done this work.
865          */
866         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
867                 return 0;
868
869         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
870                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
871                         rte_socket_id());
872
873         if (cptvf == NULL) {
874                 CPT_LOG_ERR("Cannot allocate memory for device private data");
875                 return -ENOMEM;
876         }
877
878         snprintf(dev_name, 32, "%02x:%02x.%x",
879                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
880
881         reg_base = pdev->mem_resource[0].addr;
882         if (!reg_base) {
883                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
884                 ret = -ENODEV;
885                 goto fail;
886         }
887
888         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
889         if (ret) {
890                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
891                 ret = -EIO;
892                 goto fail;
893         }
894
895         switch (cptvf->vftype) {
896         case OTX_CPT_VF_TYPE_AE:
897                 /* Set asymmetric cpt feature flags */
898                 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
899                                 RTE_CRYPTODEV_FF_HW_ACCELERATED |
900                                 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
901                 break;
902         case OTX_CPT_VF_TYPE_SE:
903                 /* Set symmetric cpt feature flags */
904                 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
905                                 RTE_CRYPTODEV_FF_HW_ACCELERATED |
906                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
907                                 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
908                                 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
909                                 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
910                 break;
911         default:
912                 /* Feature not supported. Abort */
913                 CPT_LOG_ERR("VF type not supported by %s", dev_name);
914                 ret = -EIO;
915                 goto deinit_dev;
916         }
917
918         /* Start off timer for mailbox interrupts */
919         otx_cpt_periodic_alarm_start(cptvf);
920
921         c_dev->dev_ops = &cptvf_ops;
922
923         if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
924                 c_dev->enqueue_burst = otx_cpt_enqueue_sym;
925                 c_dev->dequeue_burst = otx_cpt_dequeue_sym;
926         } else {
927                 c_dev->enqueue_burst = otx_cpt_enqueue_asym;
928                 c_dev->dequeue_burst = otx_cpt_dequeue_asym;
929         }
930
931         /* Save dev private data */
932         c_dev->data->dev_private = cptvf;
933
934         return 0;
935
936 deinit_dev:
937         otx_cpt_deinit_device(cptvf);
938
939 fail:
940         if (cptvf) {
941                 /* Free private data allocated */
942                 rte_free(cptvf);
943         }
944
945         return ret;
946 }