1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium, Inc
6 #include <rte_bus_pci.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
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"
19 #include "cpt_pmd_logs.h"
20 #include "cpt_ucode.h"
22 /* Forward declarations */
25 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
30 otx_cpt_alarm_cb(void *arg)
32 struct cpt_vf *cptvf = arg;
33 otx_cpt_poll_misc(cptvf);
34 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
35 otx_cpt_alarm_cb, cptvf);
39 otx_cpt_periodic_alarm_start(void *arg)
41 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
42 otx_cpt_alarm_cb, arg);
46 otx_cpt_periodic_alarm_stop(void *arg)
48 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
54 otx_cpt_dev_config(struct rte_cryptodev *dev __rte_unused,
55 struct rte_cryptodev_config *config __rte_unused)
57 CPT_PMD_INIT_FUNC_TRACE();
62 otx_cpt_dev_start(struct rte_cryptodev *c_dev)
64 void *cptvf = c_dev->data->dev_private;
66 CPT_PMD_INIT_FUNC_TRACE();
68 return otx_cpt_start_device(cptvf);
72 otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
74 void *cptvf = c_dev->data->dev_private;
76 CPT_PMD_INIT_FUNC_TRACE();
78 otx_cpt_stop_device(cptvf);
82 otx_cpt_dev_close(struct rte_cryptodev *c_dev)
84 void *cptvf = c_dev->data->dev_private;
87 CPT_PMD_INIT_FUNC_TRACE();
89 for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
90 ret = otx_cpt_que_pair_release(c_dev, i);
95 otx_cpt_periodic_alarm_stop(cptvf);
96 otx_cpt_deinit_device(cptvf);
102 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
104 CPT_PMD_INIT_FUNC_TRACE();
106 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
107 info->feature_flags = dev->feature_flags;
108 info->capabilities = otx_get_capabilities();
109 info->sym.max_nb_sessions = 0;
110 info->driver_id = otx_cryptodev_driver_id;
111 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
112 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
117 otx_cpt_stats_get(struct rte_cryptodev *dev __rte_unused,
118 struct rte_cryptodev_stats *stats __rte_unused)
120 CPT_PMD_INIT_FUNC_TRACE();
124 otx_cpt_stats_reset(struct rte_cryptodev *dev __rte_unused)
126 CPT_PMD_INIT_FUNC_TRACE();
130 otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
131 uint16_t que_pair_id,
132 const struct rte_cryptodev_qp_conf *qp_conf,
133 int socket_id __rte_unused)
135 struct cpt_instance *instance = NULL;
136 struct rte_pci_device *pci_dev;
139 CPT_PMD_INIT_FUNC_TRACE();
141 if (dev->data->queue_pairs[que_pair_id] != NULL) {
142 ret = otx_cpt_que_pair_release(dev, que_pair_id);
147 if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
148 CPT_LOG_INFO("Number of descriptors too big %d, using default "
149 "queue length of %d", qp_conf->nb_descriptors,
153 pci_dev = RTE_DEV_TO_PCI(dev->device);
155 if (pci_dev->mem_resource[0].addr == NULL) {
156 CPT_LOG_ERR("PCI mem address null");
160 ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
161 if (ret != 0 || instance == NULL) {
162 CPT_LOG_ERR("Error getting instance handle from device %s : "
163 "ret = %d", dev->data->name, ret);
167 instance->queue_id = que_pair_id;
168 instance->sess_mp = qp_conf->mp_session;
169 instance->sess_mp_priv = qp_conf->mp_session_private;
170 dev->data->queue_pairs[que_pair_id] = instance;
176 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
178 struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
181 CPT_PMD_INIT_FUNC_TRACE();
183 ret = otx_cpt_put_resource(instance);
185 CPT_LOG_ERR("Error putting instance handle of device %s : "
186 "ret = %d", dev->data->name, ret);
190 dev->data->queue_pairs[que_pair_id] = NULL;
196 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
198 return cpt_get_session_size();
202 otx_cpt_session_init(void *sym_sess, uint8_t driver_id)
204 struct rte_cryptodev_sym_session *sess = sym_sess;
205 struct cpt_sess_misc *cpt_sess =
206 (struct cpt_sess_misc *) get_sym_session_private_data(sess, driver_id);
208 CPT_PMD_INIT_FUNC_TRACE();
209 cpt_sess->ctx_dma_addr = rte_mempool_virt2iova(cpt_sess) +
210 sizeof(struct cpt_sess_misc);
214 otx_cpt_session_cfg(struct rte_cryptodev *dev,
215 struct rte_crypto_sym_xform *xform,
216 struct rte_cryptodev_sym_session *sess,
217 struct rte_mempool *mempool)
219 struct rte_crypto_sym_xform *chain;
220 void *sess_private_data = NULL;
222 CPT_PMD_INIT_FUNC_TRACE();
224 if (cpt_is_algo_supported(xform))
227 if (unlikely(sess == NULL)) {
228 CPT_LOG_ERR("invalid session struct");
232 if (rte_mempool_get(mempool, &sess_private_data)) {
233 CPT_LOG_ERR("Could not allocate sess_private_data");
239 switch (chain->type) {
240 case RTE_CRYPTO_SYM_XFORM_AEAD:
241 if (fill_sess_aead(chain, sess_private_data))
244 case RTE_CRYPTO_SYM_XFORM_CIPHER:
245 if (fill_sess_cipher(chain, sess_private_data))
248 case RTE_CRYPTO_SYM_XFORM_AUTH:
249 if (chain->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
250 if (fill_sess_gmac(chain, sess_private_data))
253 if (fill_sess_auth(chain, sess_private_data))
258 CPT_LOG_ERR("Invalid crypto xform type");
263 set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
264 otx_cpt_session_init(sess, dev->driver_id);
268 if (sess_private_data)
269 rte_mempool_put(mempool, sess_private_data);
274 otx_cpt_session_clear(struct rte_cryptodev *dev,
275 struct rte_cryptodev_sym_session *sess)
277 void *sess_priv = get_sym_session_private_data(sess, dev->driver_id);
279 CPT_PMD_INIT_FUNC_TRACE();
281 memset(sess_priv, 0, otx_cpt_get_session_size(dev));
282 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
283 set_sym_session_private_data(sess, dev->driver_id, NULL);
284 rte_mempool_put(sess_mp, sess_priv);
288 static __rte_always_inline int32_t __hot
289 otx_cpt_request_enqueue(struct cpt_instance *instance,
290 struct pending_queue *pqueue,
293 struct cpt_request_info *user_req = (struct cpt_request_info *)req;
295 if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN))
298 fill_cpt_inst(instance, req);
300 CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
302 /* Fill time_out cycles */
303 user_req->time_out = rte_get_timer_cycles() +
304 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
305 user_req->extra_time = 0;
307 /* Default mode of software queue */
308 mark_cpt_inst(instance);
310 pqueue->rid_queue[pqueue->enq_tail].rid = (uintptr_t)user_req;
312 /* We will use soft queue length here to limit requests */
313 MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
314 pqueue->pending_count += 1;
316 CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
317 "op: %p", user_req, user_req->op);
321 static __rte_always_inline int __hot
322 otx_cpt_enq_single_sym(struct cpt_instance *instance,
323 struct rte_crypto_op *op,
324 struct pending_queue *pqueue)
326 struct cpt_sess_misc *sess;
327 struct rte_crypto_sym_op *sym_op = op->sym;
328 void *prep_req, *mdata = NULL;
332 sess = (struct cpt_sess_misc *)
333 get_sym_session_private_data(sym_op->session,
334 otx_cryptodev_driver_id);
336 cpt_op = sess->cpt_op;
338 if (likely(cpt_op & CPT_OP_CIPHER_MASK))
339 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
342 ret = fill_digest_params(op, sess, &instance->meta_info,
346 CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
347 "ret 0x%x", op, (unsigned int)cpt_op, ret);
351 /* Enqueue prepared instruction to h/w */
352 ret = otx_cpt_request_enqueue(instance, pqueue, prep_req);
355 /* Buffer allocated for request preparation need to be freed */
356 free_op_meta(mdata, instance->meta_info.pool);
363 static __rte_always_inline int __hot
364 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
365 struct rte_crypto_op *op,
366 struct pending_queue *pqueue)
368 struct cpt_sess_misc *sess;
369 struct rte_crypto_sym_op *sym_op = op->sym;
372 void *sess_private_data_t = NULL;
374 /* Create tmp session */
376 if (rte_mempool_get(instance->sess_mp, (void **)&sess_t)) {
381 if (rte_mempool_get(instance->sess_mp_priv,
382 (void **)&sess_private_data_t)) {
387 sess = (struct cpt_sess_misc *)sess_private_data_t;
389 sess->ctx_dma_addr = rte_mempool_virt2iova(sess) +
390 sizeof(struct cpt_sess_misc);
392 ret = instance_session_cfg(sym_op->xform, (void *)sess);
398 /* Save tmp session in op */
400 sym_op->session = (struct rte_cryptodev_sym_session *)sess_t;
401 set_sym_session_private_data(sym_op->session, otx_cryptodev_driver_id,
402 sess_private_data_t);
404 /* Enqueue op with the tmp session set */
405 ret = otx_cpt_enq_single_sym(instance, op, pqueue);
413 rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
415 rte_mempool_put(instance->sess_mp, sess_t);
420 static __rte_always_inline int __hot
421 otx_cpt_enq_single(struct cpt_instance *inst,
422 struct rte_crypto_op *op,
423 struct pending_queue *pqueue)
425 /* Check for the type */
427 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
428 return otx_cpt_enq_single_sym(inst, op, pqueue);
429 else if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
430 return otx_cpt_enq_single_sym_sessless(inst, op, pqueue);
432 /* Should not reach here */
437 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
439 struct cpt_instance *instance = (struct cpt_instance *)qptr;
442 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
443 struct pending_queue *pqueue = &cptvf->pqueue;
445 count = DEFAULT_CMD_QLEN - pqueue->pending_count;
450 while (likely(count < nb_ops)) {
452 /* Enqueue single op */
453 ret = otx_cpt_enq_single(instance, ops[count], pqueue);
459 otx_cpt_ring_dbell(instance, count);
463 static __rte_always_inline void
464 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp)
466 /* H/w has returned success */
467 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
469 /* Perform further post processing */
471 if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
472 /* Check if auth verify need to be completed */
473 if (unlikely(rsp[2]))
474 compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
480 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
482 struct cpt_instance *instance = (struct cpt_instance *)qptr;
483 struct cpt_request_info *user_req;
484 struct cpt_vf *cptvf = (struct cpt_vf *)instance;
487 int i, count, pcount;
490 struct pending_queue *pqueue = &cptvf->pqueue;
491 struct rte_crypto_op *cop;
495 pcount = pqueue->pending_count;
496 count = (nb_ops > pcount) ? pcount : nb_ops;
498 for (i = 0; i < count; i++) {
499 rid_e = &pqueue->rid_queue[pqueue->deq_head];
500 user_req = (struct cpt_request_info *)(rid_e->rid);
502 if (likely((i+1) < count))
503 rte_prefetch_non_temporal((void *)rid_e[1].rid);
505 ret = check_nb_command_id(user_req, instance);
507 if (unlikely(ret == ERR_REQ_PENDING)) {
508 /* Stop checking for completions */
512 /* Return completion code and op handle */
514 ops[i] = user_req->op;
516 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
517 user_req, user_req->op, ret);
519 MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
520 pqueue->pending_count -= 1;
525 for (i = 0; i < nb_completed; i++) {
527 rsp = (void *)ops[i];
529 if (likely((i + 1) < nb_completed))
530 rte_prefetch0(ops[i+1]);
532 metabuf = (void *)rsp[0];
533 cop = (void *)rsp[1];
537 /* Check completion code */
539 if (likely(cc[i] == 0)) {
540 /* H/w success pkt. Post process */
541 otx_cpt_dequeue_post_process(cop, rsp);
542 } else if (cc[i] == ERR_GC_ICV_MISCOMPARE) {
543 /* auth data mismatch */
544 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
547 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
550 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
551 void *sess_private_data_t =
552 get_sym_session_private_data(cop->sym->session,
553 otx_cryptodev_driver_id);
554 memset(sess_private_data_t, 0,
555 cpt_get_session_size());
556 memset(cop->sym->session, 0,
557 rte_cryptodev_sym_get_existing_header_session_size(
559 rte_mempool_put(instance->sess_mp_priv,
560 sess_private_data_t);
561 rte_mempool_put(instance->sess_mp, cop->sym->session);
562 cop->sym->session = NULL;
564 free_op_meta(metabuf, instance->meta_info.pool);
570 static struct rte_cryptodev_ops cptvf_ops = {
571 /* Device related operations */
572 .dev_configure = otx_cpt_dev_config,
573 .dev_start = otx_cpt_dev_start,
574 .dev_stop = otx_cpt_dev_stop,
575 .dev_close = otx_cpt_dev_close,
576 .dev_infos_get = otx_cpt_dev_info_get,
578 .stats_get = otx_cpt_stats_get,
579 .stats_reset = otx_cpt_stats_reset,
580 .queue_pair_setup = otx_cpt_que_pair_setup,
581 .queue_pair_release = otx_cpt_que_pair_release,
582 .queue_pair_count = NULL,
584 /* Crypto related operations */
585 .sym_session_get_size = otx_cpt_get_session_size,
586 .sym_session_configure = otx_cpt_session_cfg,
587 .sym_session_clear = otx_cpt_session_clear
591 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
593 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
594 struct cpt_vf *cptvf = NULL;
599 if (pdev->mem_resource[0].phys_addr == 0ULL)
602 /* for secondary processes, we don't initialise any further as primary
603 * has already done this work.
605 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
608 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
609 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
613 CPT_LOG_ERR("Cannot allocate memory for device private data");
617 snprintf(dev_name, 32, "%02x:%02x.%x",
618 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
620 reg_base = pdev->mem_resource[0].addr;
622 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
627 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
629 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
634 switch (cptvf->vftype) {
635 case OTX_CPT_VF_TYPE_AE:
636 /* Set asymmetric cpt feature flags */
637 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
638 RTE_CRYPTODEV_FF_HW_ACCELERATED;
640 case OTX_CPT_VF_TYPE_SE:
641 /* Set symmetric cpt feature flags */
642 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
643 RTE_CRYPTODEV_FF_HW_ACCELERATED |
644 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
645 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
646 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
647 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
650 /* Feature not supported. Abort */
651 CPT_LOG_ERR("VF type not supported by %s", dev_name);
656 /* Start off timer for mailbox interrupts */
657 otx_cpt_periodic_alarm_start(cptvf);
659 c_dev->dev_ops = &cptvf_ops;
661 c_dev->enqueue_burst = otx_cpt_pkt_enqueue;
662 c_dev->dequeue_burst = otx_cpt_pkt_dequeue;
664 /* Save dev private data */
665 c_dev->data->dev_private = cptvf;
670 otx_cpt_deinit_device(cptvf);
674 /* Free private data allocated */