net/i40e: fix QinQ enablement
[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 <cryptodev_pmd.h>
9 #include <rte_eventdev.h>
10 #include <rte_event_crypto_adapter.h>
11 #include <rte_errno.h>
12 #include <rte_malloc.h>
13 #include <rte_mempool.h>
14
15 #include "otx_cryptodev.h"
16 #include "otx_cryptodev_capabilities.h"
17 #include "otx_cryptodev_hw_access.h"
18 #include "otx_cryptodev_mbox.h"
19 #include "otx_cryptodev_ops.h"
20
21 #include "cpt_pmd_logs.h"
22 #include "cpt_pmd_ops_helper.h"
23 #include "cpt_ucode.h"
24 #include "cpt_ucode_asym.h"
25
26 #include "ssovf_worker.h"
27
28 static uint64_t otx_fpm_iova[CPT_EC_ID_PMAX];
29
30 /* Forward declarations */
31
32 static int
33 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
34
35 /* Alarm routines */
36
37 static void
38 otx_cpt_alarm_cb(void *arg)
39 {
40         struct cpt_vf *cptvf = arg;
41         otx_cpt_poll_misc(cptvf);
42         rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
43                           otx_cpt_alarm_cb, cptvf);
44 }
45
46 static int
47 otx_cpt_periodic_alarm_start(void *arg)
48 {
49         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
50                                  otx_cpt_alarm_cb, arg);
51 }
52
53 static int
54 otx_cpt_periodic_alarm_stop(void *arg)
55 {
56         return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
57 }
58
59 /* PMD ops */
60
61 static int
62 otx_cpt_dev_config(struct rte_cryptodev *dev,
63                    struct rte_cryptodev_config *config __rte_unused)
64 {
65         int ret = 0;
66
67         CPT_PMD_INIT_FUNC_TRACE();
68
69         if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
70                 /* Initialize shared FPM table */
71                 ret = cpt_fpm_init(otx_fpm_iova);
72
73         return ret;
74 }
75
76 static int
77 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
78 {
79         void *cptvf = c_dev->data->dev_private;
80
81         CPT_PMD_INIT_FUNC_TRACE();
82
83         return otx_cpt_start_device(cptvf);
84 }
85
86 static void
87 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
88 {
89         void *cptvf = c_dev->data->dev_private;
90
91         CPT_PMD_INIT_FUNC_TRACE();
92
93         if (c_dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
94                 cpt_fpm_clear();
95
96         otx_cpt_stop_device(cptvf);
97 }
98
99 static int
100 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
101 {
102         void *cptvf = c_dev->data->dev_private;
103         int i, ret;
104
105         CPT_PMD_INIT_FUNC_TRACE();
106
107         for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
108                 ret = otx_cpt_que_pair_release(c_dev, i);
109                 if (ret)
110                         return ret;
111         }
112
113         otx_cpt_periodic_alarm_stop(cptvf);
114         otx_cpt_deinit_device(cptvf);
115
116         return 0;
117 }
118
119 static void
120 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
121 {
122         CPT_PMD_INIT_FUNC_TRACE();
123         if (info != NULL) {
124                 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
125                 info->feature_flags = dev->feature_flags;
126                 info->capabilities = otx_get_capabilities(info->feature_flags);
127                 info->sym.max_nb_sessions = 0;
128                 info->driver_id = otx_cryptodev_driver_id;
129                 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
130                 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
131         }
132 }
133
134 static int
135 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
136                        uint16_t que_pair_id,
137                        const struct rte_cryptodev_qp_conf *qp_conf,
138                        int socket_id __rte_unused)
139 {
140         struct cpt_instance *instance = NULL;
141         struct rte_pci_device *pci_dev;
142         int ret = -1;
143
144         CPT_PMD_INIT_FUNC_TRACE();
145
146         if (dev->data->queue_pairs[que_pair_id] != NULL) {
147                 ret = otx_cpt_que_pair_release(dev, que_pair_id);
148                 if (ret)
149                         return ret;
150         }
151
152         if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
153                 CPT_LOG_INFO("Number of descriptors too big %d, using default "
154                              "queue length of %d", qp_conf->nb_descriptors,
155                              DEFAULT_CMD_QLEN);
156         }
157
158         pci_dev = RTE_DEV_TO_PCI(dev->device);
159
160         if (pci_dev->mem_resource[0].addr == NULL) {
161                 CPT_LOG_ERR("PCI mem address null");
162                 return -EIO;
163         }
164
165         ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
166         if (ret != 0 || instance == NULL) {
167                 CPT_LOG_ERR("Error getting instance handle from device %s : "
168                             "ret = %d", dev->data->name, ret);
169                 return ret;
170         }
171
172         instance->queue_id = que_pair_id;
173         instance->sess_mp = qp_conf->mp_session;
174         instance->sess_mp_priv = qp_conf->mp_session_private;
175         dev->data->queue_pairs[que_pair_id] = instance;
176
177         return 0;
178 }
179
180 static int
181 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
182 {
183         struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
184         int ret;
185
186         CPT_PMD_INIT_FUNC_TRACE();
187
188         ret = otx_cpt_put_resource(instance);
189         if (ret != 0) {
190                 CPT_LOG_ERR("Error putting instance handle of device %s : "
191                             "ret = %d", dev->data->name, ret);
192                 return ret;
193         }
194
195         dev->data->queue_pairs[que_pair_id] = NULL;
196
197         return 0;
198 }
199
200 static unsigned int
201 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
202 {
203         return cpt_get_session_size();
204 }
205
206 static int
207 sym_xform_verify(struct rte_crypto_sym_xform *xform)
208 {
209         if (xform->next) {
210                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
211                     xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
212                     xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
213                     (xform->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC ||
214                      xform->next->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC))
215                         return -ENOTSUP;
216
217                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
218                     xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
219                     xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
220                     (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC ||
221                      xform->next->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC))
222                         return -ENOTSUP;
223
224                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
225                     xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC &&
226                     xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
227                     xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1)
228                         return -ENOTSUP;
229
230                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
231                     xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 &&
232                     xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
233                     xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC)
234                         return -ENOTSUP;
235
236         } else {
237                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
238                     xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
239                     xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
240                         return -ENOTSUP;
241         }
242         return 0;
243 }
244
245 static int
246 sym_session_configure(int driver_id, struct rte_crypto_sym_xform *xform,
247                       struct rte_cryptodev_sym_session *sess,
248                       struct rte_mempool *pool)
249 {
250         struct rte_crypto_sym_xform *temp_xform = xform;
251         struct cpt_sess_misc *misc;
252         vq_cmd_word3_t vq_cmd_w3;
253         void *priv;
254         int ret;
255
256         ret = sym_xform_verify(xform);
257         if (unlikely(ret))
258                 return ret;
259
260         if (unlikely(rte_mempool_get(pool, &priv))) {
261                 CPT_LOG_ERR("Could not allocate session private data");
262                 return -ENOMEM;
263         }
264
265         memset(priv, 0, sizeof(struct cpt_sess_misc) +
266                         offsetof(struct cpt_ctx, mc_ctx));
267
268         misc = priv;
269
270         for ( ; xform != NULL; xform = xform->next) {
271                 switch (xform->type) {
272                 case RTE_CRYPTO_SYM_XFORM_AEAD:
273                         ret = fill_sess_aead(xform, misc);
274                         break;
275                 case RTE_CRYPTO_SYM_XFORM_CIPHER:
276                         ret = fill_sess_cipher(xform, misc);
277                         break;
278                 case RTE_CRYPTO_SYM_XFORM_AUTH:
279                         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
280                                 ret = fill_sess_gmac(xform, misc);
281                         else
282                                 ret = fill_sess_auth(xform, misc);
283                         break;
284                 default:
285                         ret = -1;
286                 }
287
288                 if (ret)
289                         goto priv_put;
290         }
291
292         if ((GET_SESS_FC_TYPE(misc) == HASH_HMAC) &&
293                         cpt_mac_len_verify(&temp_xform->auth)) {
294                 CPT_LOG_ERR("MAC length is not supported");
295                 struct cpt_ctx *ctx = SESS_PRIV(misc);
296                 if (ctx->auth_key != NULL) {
297                         rte_free(ctx->auth_key);
298                         ctx->auth_key = NULL;
299                 }
300                 ret = -ENOTSUP;
301                 goto priv_put;
302         }
303
304         set_sym_session_private_data(sess, driver_id, priv);
305
306         misc->ctx_dma_addr = rte_mempool_virt2iova(misc) +
307                              sizeof(struct cpt_sess_misc);
308
309         vq_cmd_w3.u64 = 0;
310         vq_cmd_w3.s.grp = 0;
311         vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx,
312                                                          mc_ctx);
313
314         misc->cpt_inst_w7 = vq_cmd_w3.u64;
315
316         return 0;
317
318 priv_put:
319         if (priv)
320                 rte_mempool_put(pool, priv);
321         return -ENOTSUP;
322 }
323
324 static void
325 sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
326 {
327         void *priv = get_sym_session_private_data(sess, driver_id);
328         struct cpt_sess_misc *misc;
329         struct rte_mempool *pool;
330         struct cpt_ctx *ctx;
331
332         if (priv == NULL)
333                 return;
334
335         misc = priv;
336         ctx = SESS_PRIV(misc);
337
338         rte_free(ctx->auth_key);
339
340         memset(priv, 0, cpt_get_session_size());
341
342         pool = rte_mempool_from_obj(priv);
343
344         set_sym_session_private_data(sess, driver_id, NULL);
345
346         rte_mempool_put(pool, priv);
347 }
348
349 static int
350 otx_cpt_session_cfg(struct rte_cryptodev *dev,
351                     struct rte_crypto_sym_xform *xform,
352                     struct rte_cryptodev_sym_session *sess,
353                     struct rte_mempool *pool)
354 {
355         CPT_PMD_INIT_FUNC_TRACE();
356
357         return sym_session_configure(dev->driver_id, xform, sess, pool);
358 }
359
360
361 static void
362 otx_cpt_session_clear(struct rte_cryptodev *dev,
363                   struct rte_cryptodev_sym_session *sess)
364 {
365         CPT_PMD_INIT_FUNC_TRACE();
366
367         return sym_session_clear(dev->driver_id, sess);
368 }
369
370 static unsigned int
371 otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
372 {
373         return sizeof(struct cpt_asym_sess_misc);
374 }
375
376 static int
377 otx_cpt_asym_session_cfg(struct rte_cryptodev *dev __rte_unused,
378                          struct rte_crypto_asym_xform *xform __rte_unused,
379                          struct rte_cryptodev_asym_session *sess)
380 {
381         struct cpt_asym_sess_misc *priv = (struct cpt_asym_sess_misc *)
382                         sess->sess_private_data;
383         int ret;
384
385         CPT_PMD_INIT_FUNC_TRACE();
386
387         ret = cpt_fill_asym_session_parameters(priv, xform);
388         if (ret) {
389                 CPT_LOG_ERR("Could not configure session parameters");
390                 return ret;
391         }
392
393         priv->cpt_inst_w7 = 0;
394
395         return 0;
396 }
397
398 static void
399 otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
400                            struct rte_cryptodev_asym_session *sess)
401 {
402         struct cpt_asym_sess_misc *priv;
403
404         CPT_PMD_INIT_FUNC_TRACE();
405
406         priv = (struct cpt_asym_sess_misc *) sess->sess_private_data;
407
408         if (priv == NULL)
409                 return;
410
411         /* Free resources allocated during session configure */
412         cpt_free_asym_session_parameters(priv);
413         memset(priv, 0, otx_cpt_asym_session_size_get(dev));
414 }
415
416 static __rte_always_inline void * __rte_hot
417 otx_cpt_request_enqueue(struct cpt_instance *instance,
418                         void *req, uint64_t cpt_inst_w7)
419 {
420         struct cpt_request_info *user_req = (struct cpt_request_info *)req;
421
422         fill_cpt_inst(instance, req, cpt_inst_w7);
423
424         CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
425
426         /* Fill time_out cycles */
427         user_req->time_out = rte_get_timer_cycles() +
428                         DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
429         user_req->extra_time = 0;
430
431         /* Default mode of software queue */
432         mark_cpt_inst(instance);
433
434         CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
435                          "op: %p", user_req, user_req->op);
436         return req;
437 }
438
439 static __rte_always_inline void * __rte_hot
440 otx_cpt_enq_single_asym(struct cpt_instance *instance,
441                         struct rte_crypto_op *op)
442 {
443         struct cpt_qp_meta_info *minfo = &instance->meta_info;
444         struct rte_crypto_asym_op *asym_op = op->asym;
445         struct asym_op_params params = {0};
446         struct cpt_asym_sess_misc *sess;
447         uintptr_t *cop;
448         void *mdata;
449         void *req;
450         int ret;
451
452         if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
453                 CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
454                 rte_errno = ENOMEM;
455                 return NULL;
456         }
457
458         sess = (struct cpt_asym_sess_misc *)
459                         asym_op->session->sess_private_data;
460
461         /* Store phys_addr of the mdata to meta_buf */
462         params.meta_buf = rte_mempool_virt2iova(mdata);
463
464         cop = mdata;
465         cop[0] = (uintptr_t)mdata;
466         cop[1] = (uintptr_t)op;
467         cop[2] = cop[3] = 0ULL;
468
469         params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
470         params.req->op = cop;
471
472         /* Adjust meta_buf by crypto_op data  and request_info struct */
473         params.meta_buf += (4 * sizeof(uintptr_t)) +
474                            sizeof(struct cpt_request_info);
475
476         switch (sess->xfrm_type) {
477         case RTE_CRYPTO_ASYM_XFORM_MODEX:
478                 ret = cpt_modex_prep(&params, &sess->mod_ctx);
479                 if (unlikely(ret))
480                         goto req_fail;
481                 break;
482         case RTE_CRYPTO_ASYM_XFORM_RSA:
483                 ret = cpt_enqueue_rsa_op(op, &params, sess);
484                 if (unlikely(ret))
485                         goto req_fail;
486                 break;
487         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
488                 ret = cpt_enqueue_ecdsa_op(op, &params, sess, otx_fpm_iova);
489                 if (unlikely(ret))
490                         goto req_fail;
491                 break;
492         case RTE_CRYPTO_ASYM_XFORM_ECPM:
493                 ret = cpt_ecpm_prep(&asym_op->ecpm, &params,
494                                     sess->ec_ctx.curveid);
495                 if (unlikely(ret))
496                         goto req_fail;
497                 break;
498
499         default:
500                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
501                 rte_errno = EINVAL;
502                 goto req_fail;
503         }
504
505         req = otx_cpt_request_enqueue(instance, params.req, sess->cpt_inst_w7);
506         if (unlikely(req == NULL)) {
507                 CPT_LOG_DP_ERR("Could not enqueue crypto req");
508                 goto req_fail;
509         }
510
511         return req;
512
513 req_fail:
514         free_op_meta(mdata, minfo->pool);
515
516         return NULL;
517 }
518
519 static __rte_always_inline void * __rte_hot
520 otx_cpt_enq_single_sym(struct cpt_instance *instance,
521                        struct rte_crypto_op *op)
522 {
523         struct cpt_sess_misc *sess;
524         struct rte_crypto_sym_op *sym_op = op->sym;
525         struct cpt_request_info *prep_req;
526         void *mdata = NULL;
527         int ret = 0;
528         void *req;
529         uint64_t cpt_op;
530
531         sess = (struct cpt_sess_misc *)
532                         get_sym_session_private_data(sym_op->session,
533                                                      otx_cryptodev_driver_id);
534
535         cpt_op = sess->cpt_op;
536
537         if (likely(cpt_op & CPT_OP_CIPHER_MASK))
538                 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
539                                      (void **)&prep_req);
540         else
541                 ret = fill_digest_params(op, sess, &instance->meta_info,
542                                          &mdata, (void **)&prep_req);
543
544         if (unlikely(ret)) {
545                 CPT_LOG_DP_ERR("prep crypto req : op %p, cpt_op 0x%x "
546                                "ret 0x%x", op, (unsigned int)cpt_op, ret);
547                 return NULL;
548         }
549
550         /* Enqueue prepared instruction to h/w */
551         req = otx_cpt_request_enqueue(instance, prep_req, sess->cpt_inst_w7);
552         if (unlikely(req == NULL))
553                 /* Buffer allocated for request preparation need to be freed */
554                 free_op_meta(mdata, instance->meta_info.pool);
555
556         return req;
557 }
558
559 static __rte_always_inline void * __rte_hot
560 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
561                                 struct rte_crypto_op *op)
562 {
563         const int driver_id = otx_cryptodev_driver_id;
564         struct rte_crypto_sym_op *sym_op = op->sym;
565         struct rte_cryptodev_sym_session *sess;
566         void *req;
567         int ret;
568
569         /* Create temporary session */
570         sess = rte_cryptodev_sym_session_create(instance->sess_mp);
571         if (sess == NULL) {
572                 rte_errno = ENOMEM;
573                 return NULL;
574         }
575
576         ret = sym_session_configure(driver_id, sym_op->xform, sess,
577                                     instance->sess_mp_priv);
578         if (ret)
579                 goto sess_put;
580
581         sym_op->session = sess;
582
583         /* Enqueue op with the tmp session set */
584         req = otx_cpt_enq_single_sym(instance, op);
585         if (unlikely(req == NULL))
586                 goto priv_put;
587
588         return req;
589
590 priv_put:
591         sym_session_clear(driver_id, sess);
592 sess_put:
593         rte_mempool_put(instance->sess_mp, sess);
594         return NULL;
595 }
596
597 #define OP_TYPE_SYM             0
598 #define OP_TYPE_ASYM            1
599
600 static __rte_always_inline void *__rte_hot
601 otx_cpt_enq_single(struct cpt_instance *inst,
602                    struct rte_crypto_op *op,
603                    const uint8_t op_type)
604 {
605         /* Check for the type */
606
607         if (op_type == OP_TYPE_SYM) {
608                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
609                         return otx_cpt_enq_single_sym(inst, op);
610                 else
611                         return otx_cpt_enq_single_sym_sessless(inst, op);
612         }
613
614         if (op_type == OP_TYPE_ASYM) {
615                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
616                         return otx_cpt_enq_single_asym(inst, op);
617         }
618
619         /* Should not reach here */
620         rte_errno = ENOTSUP;
621         return NULL;
622 }
623
624 static  __rte_always_inline uint16_t __rte_hot
625 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
626                     const uint8_t op_type)
627 {
628         struct cpt_instance *instance = (struct cpt_instance *)qptr;
629         uint16_t count, free_slots;
630         void *req;
631         struct cpt_vf *cptvf = (struct cpt_vf *)instance;
632         struct pending_queue *pqueue = &cptvf->pqueue;
633
634         free_slots = pending_queue_free_slots(pqueue, DEFAULT_CMD_QLEN,
635                                 DEFAULT_CMD_QRSVD_SLOTS);
636         if (nb_ops > free_slots)
637                 nb_ops = free_slots;
638
639         count = 0;
640         while (likely(count < nb_ops)) {
641
642                 /* Enqueue single op */
643                 req = otx_cpt_enq_single(instance, ops[count], op_type);
644
645                 if (unlikely(req == NULL))
646                         break;
647
648                 pending_queue_push(pqueue, req, count, DEFAULT_CMD_QLEN);
649                 count++;
650         }
651
652         if (likely(count)) {
653                 pending_queue_commit(pqueue, count, DEFAULT_CMD_QLEN);
654                 otx_cpt_ring_dbell(instance, count);
655         }
656         return count;
657 }
658
659 static uint16_t
660 otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
661 {
662         return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
663 }
664
665 static uint16_t
666 otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
667 {
668         return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
669 }
670
671 static __rte_always_inline void
672 submit_request_to_sso(struct ssows *ws, uintptr_t req,
673                       struct rte_event *rsp_info)
674 {
675         uint64_t add_work;
676
677         add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) |
678                    (rsp_info->sub_event_type << 20) |
679                    ((uint64_t)(rsp_info->sched_type) << 32);
680
681         if (!rsp_info->sched_type)
682                 ssows_head_wait(ws);
683
684         rte_atomic_thread_fence(__ATOMIC_RELEASE);
685         ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]);
686 }
687
688 uint16_t __rte_hot
689 otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op)
690 {
691         union rte_event_crypto_metadata *ec_mdata;
692         struct cpt_instance *instance;
693         struct cpt_request_info *req;
694         struct rte_event *rsp_info;
695         uint8_t op_type, cdev_id;
696         uint16_t qp_id;
697
698         ec_mdata = rte_cryptodev_session_event_mdata_get(op);
699         if (unlikely(ec_mdata == NULL)) {
700                 rte_errno = EINVAL;
701                 return 0;
702         }
703
704         cdev_id = ec_mdata->request_info.cdev_id;
705         qp_id = ec_mdata->request_info.queue_pair_id;
706         rsp_info = &ec_mdata->response_info;
707         instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
708
709         if (unlikely(!instance->ca_enabled)) {
710                 rte_errno = EINVAL;
711                 return 0;
712         }
713
714         op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
715                                                              OP_TYPE_ASYM;
716         req = otx_cpt_enq_single(instance, op, op_type);
717         if (unlikely(req == NULL))
718                 return 0;
719
720         otx_cpt_ring_dbell(instance, 1);
721         req->qp = instance;
722         submit_request_to_sso(port, (uintptr_t)req, rsp_info);
723
724         return 1;
725 }
726
727 static inline void
728 otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
729                     struct rte_crypto_rsa_xform *rsa_ctx)
730
731 {
732         struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
733
734         switch (rsa->op_type) {
735         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
736                 rsa->cipher.length = rsa_ctx->n.length;
737                 memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
738                 break;
739         case RTE_CRYPTO_ASYM_OP_DECRYPT:
740                 if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE)
741                         rsa->message.length = rsa_ctx->n.length;
742                 else {
743                         /* Get length of decrypted output */
744                         rsa->message.length = rte_cpu_to_be_16
745                                         (*((uint16_t *)req->rptr));
746
747                         /* Offset data pointer by length fields */
748                         req->rptr += 2;
749                 }
750                 memcpy(rsa->message.data, req->rptr, rsa->message.length);
751                 break;
752         case RTE_CRYPTO_ASYM_OP_SIGN:
753                 rsa->sign.length = rsa_ctx->n.length;
754                 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
755                 break;
756         case RTE_CRYPTO_ASYM_OP_VERIFY:
757                 if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE)
758                         rsa->sign.length = rsa_ctx->n.length;
759                 else {
760                         /* Get length of decrypted output */
761                         rsa->sign.length = rte_cpu_to_be_16
762                                         (*((uint16_t *)req->rptr));
763
764                         /* Offset data pointer by length fields */
765                         req->rptr += 2;
766                 }
767                 memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
768
769                 if (memcmp(rsa->sign.data, rsa->message.data,
770                            rsa->message.length)) {
771                         CPT_LOG_DP_ERR("RSA verification failed");
772                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
773                 }
774                 break;
775         default:
776                 CPT_LOG_DP_DEBUG("Invalid RSA operation type");
777                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
778                 break;
779         }
780 }
781
782 static __rte_always_inline void
783 otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa,
784                             struct cpt_request_info *req,
785                             struct cpt_asym_ec_ctx *ec)
786
787 {
788         int prime_len = ec_grp[ec->curveid].prime.length;
789
790         if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
791                 return;
792
793         /* Separate out sign r and s components */
794         memcpy(ecdsa->r.data, req->rptr, prime_len);
795         memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
796                prime_len);
797         ecdsa->r.length = prime_len;
798         ecdsa->s.length = prime_len;
799 }
800
801 static __rte_always_inline void
802 otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm,
803                              struct cpt_request_info *req,
804                              struct cpt_asym_ec_ctx *ec)
805 {
806         int prime_len = ec_grp[ec->curveid].prime.length;
807
808         memcpy(ecpm->r.x.data, req->rptr, prime_len);
809         memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
810                prime_len);
811         ecpm->r.x.length = prime_len;
812         ecpm->r.y.length = prime_len;
813 }
814
815 static __rte_always_inline void __rte_hot
816 otx_cpt_asym_post_process(struct rte_crypto_op *cop,
817                           struct cpt_request_info *req)
818 {
819         struct rte_crypto_asym_op *op = cop->asym;
820         struct cpt_asym_sess_misc *sess;
821
822         sess = (struct cpt_asym_sess_misc *) op->session->sess_private_data;
823
824         switch (sess->xfrm_type) {
825         case RTE_CRYPTO_ASYM_XFORM_RSA:
826                 otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
827                 break;
828         case RTE_CRYPTO_ASYM_XFORM_MODEX:
829                 op->modex.result.length = sess->mod_ctx.modulus.length;
830                 memcpy(op->modex.result.data, req->rptr,
831                        op->modex.result.length);
832                 break;
833         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
834                 otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx);
835                 break;
836         case RTE_CRYPTO_ASYM_XFORM_ECPM:
837                 otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx);
838                 break;
839         default:
840                 CPT_LOG_DP_DEBUG("Invalid crypto xform type");
841                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
842                 break;
843         }
844 }
845
846 static __rte_always_inline void __rte_hot
847 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
848                              const uint8_t op_type)
849 {
850         /* H/w has returned success */
851         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
852
853         /* Perform further post processing */
854
855         if ((op_type == OP_TYPE_SYM) &&
856             (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
857                 /* Check if auth verify need to be completed */
858                 if (unlikely(rsp[2]))
859                         compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
860                 return;
861         }
862
863         if ((op_type == OP_TYPE_ASYM) &&
864             (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
865                 rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
866                 otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
867         }
868
869         return;
870 }
871
872 static inline void
873 free_sym_session_data(const struct cpt_instance *instance,
874                       struct rte_crypto_op *cop)
875 {
876         void *sess_private_data_t = get_sym_session_private_data(
877                 cop->sym->session, otx_cryptodev_driver_id);
878         memset(sess_private_data_t, 0, cpt_get_session_size());
879         memset(cop->sym->session, 0,
880                rte_cryptodev_sym_get_existing_header_session_size(
881                        cop->sym->session));
882         rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
883         rte_mempool_put(instance->sess_mp, cop->sym->session);
884         cop->sym->session = NULL;
885 }
886
887 static __rte_always_inline struct rte_crypto_op *
888 otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp,
889                          uint8_t cc, const uint8_t op_type)
890 {
891         struct rte_crypto_op *cop;
892         void *metabuf;
893
894         metabuf = (void *)rsp[0];
895         cop = (void *)rsp[1];
896
897         /* Check completion code */
898         if (likely(cc == 0)) {
899                 /* H/w success pkt. Post process */
900                 otx_cpt_dequeue_post_process(cop, rsp, op_type);
901         } else if (cc == ERR_GC_ICV_MISCOMPARE) {
902                 /* auth data mismatch */
903                 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
904         } else {
905                 /* Error */
906                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
907         }
908
909         if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
910                 free_sym_session_data(instance, cop);
911         free_op_meta(metabuf, instance->meta_info.pool);
912
913         return cop;
914 }
915
916 static __rte_always_inline uint16_t __rte_hot
917 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
918                     const uint8_t op_type)
919 {
920         struct cpt_instance *instance = (struct cpt_instance *)qptr;
921         struct cpt_request_info *user_req;
922         struct cpt_vf *cptvf = (struct cpt_vf *)instance;
923         uint8_t cc[nb_ops];
924         int i, count, pcount;
925         uint8_t ret;
926         int nb_completed;
927         struct pending_queue *pqueue = &cptvf->pqueue;
928
929         pcount = pending_queue_level(pqueue, DEFAULT_CMD_QLEN);
930
931         /* Ensure pcount isn't read before data lands */
932         rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
933
934         count = (nb_ops > pcount) ? pcount : nb_ops;
935
936         for (i = 0; i < count; i++) {
937                 pending_queue_peek(pqueue, (void **) &user_req,
938                         DEFAULT_CMD_QLEN, i + 1 < count);
939
940                 ret = check_nb_command_id(user_req, instance);
941
942                 if (unlikely(ret == ERR_REQ_PENDING)) {
943                         /* Stop checking for completions */
944                         break;
945                 }
946
947                 /* Return completion code and op handle */
948                 cc[i] = ret;
949                 ops[i] = user_req->op;
950
951                 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
952                                  user_req, user_req->op, ret);
953
954                 pending_queue_pop(pqueue, DEFAULT_CMD_QLEN);
955         }
956
957         nb_completed = i;
958
959         for (i = 0; i < nb_completed; i++) {
960                 if (likely((i + 1) < nb_completed))
961                         rte_prefetch0(ops[i+1]);
962
963                 ops[i] = otx_cpt_process_response(instance, (void *)ops[i],
964                                                   cc[i], op_type);
965         }
966
967         return nb_completed;
968 }
969
970 static uint16_t
971 otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
972 {
973         return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
974 }
975
976 static uint16_t
977 otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
978 {
979         return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
980 }
981
982 uintptr_t __rte_hot
983 otx_crypto_adapter_dequeue(uintptr_t get_work1)
984 {
985         const struct cpt_instance *instance;
986         struct cpt_request_info *req;
987         struct rte_crypto_op *cop;
988         uint8_t cc, op_type;
989         uintptr_t *rsp;
990
991         req = (struct cpt_request_info *)get_work1;
992         instance = req->qp;
993         rsp = req->op;
994         cop = (void *)rsp[1];
995         op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
996                                                               OP_TYPE_ASYM;
997
998         do {
999                 cc = check_nb_command_id(
1000                         req, (struct cpt_instance *)(uintptr_t)instance);
1001         } while (cc == ERR_REQ_PENDING);
1002
1003         cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type);
1004
1005         return (uintptr_t)(cop);
1006 }
1007
1008 static struct rte_cryptodev_ops cptvf_ops = {
1009         /* Device related operations */
1010         .dev_configure = otx_cpt_dev_config,
1011         .dev_start = otx_cpt_dev_start,
1012         .dev_stop = otx_cpt_dev_stop,
1013         .dev_close = otx_cpt_dev_close,
1014         .dev_infos_get = otx_cpt_dev_info_get,
1015
1016         .stats_get = NULL,
1017         .stats_reset = NULL,
1018         .queue_pair_setup = otx_cpt_que_pair_setup,
1019         .queue_pair_release = otx_cpt_que_pair_release,
1020
1021         /* Crypto related operations */
1022         .sym_session_get_size = otx_cpt_get_session_size,
1023         .sym_session_configure = otx_cpt_session_cfg,
1024         .sym_session_clear = otx_cpt_session_clear,
1025
1026         .asym_session_get_size = otx_cpt_asym_session_size_get,
1027         .asym_session_configure = otx_cpt_asym_session_cfg,
1028         .asym_session_clear = otx_cpt_asym_session_clear,
1029 };
1030
1031 int
1032 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
1033 {
1034         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
1035         struct cpt_vf *cptvf = NULL;
1036         void *reg_base;
1037         char dev_name[32];
1038         int ret;
1039
1040         if (pdev->mem_resource[0].phys_addr == 0ULL)
1041                 return -EIO;
1042
1043         /* for secondary processes, we don't initialise any further as primary
1044          * has already done this work.
1045          */
1046         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1047                 return 0;
1048
1049         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
1050                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
1051                         rte_socket_id());
1052
1053         if (cptvf == NULL) {
1054                 CPT_LOG_ERR("Cannot allocate memory for device private data");
1055                 return -ENOMEM;
1056         }
1057
1058         snprintf(dev_name, 32, "%02x:%02x.%x",
1059                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1060
1061         reg_base = pdev->mem_resource[0].addr;
1062         if (!reg_base) {
1063                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
1064                 ret = -ENODEV;
1065                 goto fail;
1066         }
1067
1068         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
1069         if (ret) {
1070                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
1071                 ret = -EIO;
1072                 goto fail;
1073         }
1074
1075         switch (cptvf->vftype) {
1076         case OTX_CPT_VF_TYPE_AE:
1077                 /* Set asymmetric cpt feature flags */
1078                 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
1079                                 RTE_CRYPTODEV_FF_HW_ACCELERATED |
1080                                 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
1081                 break;
1082         case OTX_CPT_VF_TYPE_SE:
1083                 /* Set symmetric cpt feature flags */
1084                 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1085                                 RTE_CRYPTODEV_FF_HW_ACCELERATED |
1086                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1087                                 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
1088                                 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1089                                 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1090                                 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
1091                                 RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
1092                                 RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
1093                 break;
1094         default:
1095                 /* Feature not supported. Abort */
1096                 CPT_LOG_ERR("VF type not supported by %s", dev_name);
1097                 ret = -EIO;
1098                 goto deinit_dev;
1099         }
1100
1101         /* Start off timer for mailbox interrupts */
1102         otx_cpt_periodic_alarm_start(cptvf);
1103
1104         c_dev->dev_ops = &cptvf_ops;
1105
1106         if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
1107                 c_dev->enqueue_burst = otx_cpt_enqueue_sym;
1108                 c_dev->dequeue_burst = otx_cpt_dequeue_sym;
1109         } else {
1110                 c_dev->enqueue_burst = otx_cpt_enqueue_asym;
1111                 c_dev->dequeue_burst = otx_cpt_dequeue_asym;
1112         }
1113
1114         /* Save dev private data */
1115         c_dev->data->dev_private = cptvf;
1116
1117         return 0;
1118
1119 deinit_dev:
1120         otx_cpt_deinit_device(cptvf);
1121
1122 fail:
1123         if (cptvf) {
1124                 /* Free private data allocated */
1125                 rte_free(cptvf);
1126         }
1127
1128         return ret;
1129 }