xref: /dpdk/drivers/crypto/null/null_crypto_pmd_ops.c (revision b74fd6b842b7e41e4ee6a037dd37735aeedd8095)
15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
25566a3e3SBruce Richardson  * Copyright(c) 2016-2017 Intel Corporation
394b0ad8eSDeclan Doherty  */
494b0ad8eSDeclan Doherty 
594b0ad8eSDeclan Doherty #include <string.h>
694b0ad8eSDeclan Doherty 
794b0ad8eSDeclan Doherty #include <rte_common.h>
894b0ad8eSDeclan Doherty #include <rte_malloc.h>
994b0ad8eSDeclan Doherty #include <rte_cryptodev_pmd.h>
1094b0ad8eSDeclan Doherty 
1194b0ad8eSDeclan Doherty #include "null_crypto_pmd_private.h"
1294b0ad8eSDeclan Doherty 
1326c2e4adSDeclan Doherty static const struct rte_cryptodev_capabilities null_crypto_pmd_capabilities[] = {
1426c2e4adSDeclan Doherty 	{	/* NULL (AUTH) */
1526c2e4adSDeclan Doherty 		.op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
16ff150afdSFiona Trahe 		{.sym = {
1726c2e4adSDeclan Doherty 			.xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
18ff150afdSFiona Trahe 			{.auth = {
1926c2e4adSDeclan Doherty 				.algo = RTE_CRYPTO_AUTH_NULL,
2026c2e4adSDeclan Doherty 				.block_size = 1,
2126c2e4adSDeclan Doherty 				.key_size = {
2226c2e4adSDeclan Doherty 					.min = 0,
2326c2e4adSDeclan Doherty 					.max = 0,
2426c2e4adSDeclan Doherty 					.increment = 0
2526c2e4adSDeclan Doherty 				},
2626c2e4adSDeclan Doherty 				.digest_size = {
2726c2e4adSDeclan Doherty 					.min = 0,
2826c2e4adSDeclan Doherty 					.max = 0,
2926c2e4adSDeclan Doherty 					.increment = 0
3026c2e4adSDeclan Doherty 				},
31acf86169SPablo de Lara 				.iv_size = { 0 }
32ff150afdSFiona Trahe 			}, },
33ff150afdSFiona Trahe 		}, },
3426c2e4adSDeclan Doherty 	},
3526c2e4adSDeclan Doherty 	{	/* NULL (CIPHER) */
3626c2e4adSDeclan Doherty 		.op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
37ff150afdSFiona Trahe 		{.sym = {
3826c2e4adSDeclan Doherty 			.xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
39ff150afdSFiona Trahe 			{.cipher = {
4026c2e4adSDeclan Doherty 				.algo = RTE_CRYPTO_CIPHER_NULL,
4126c2e4adSDeclan Doherty 				.block_size = 1,
4226c2e4adSDeclan Doherty 				.key_size = {
4326c2e4adSDeclan Doherty 					.min = 0,
4426c2e4adSDeclan Doherty 					.max = 0,
456bc43003SDeepak Kumar Jain 					.increment = 0
4626c2e4adSDeclan Doherty 				},
470fbd75a9SPablo de Lara 				.iv_size = { 0 }
48ff150afdSFiona Trahe 			}, },
49ff150afdSFiona Trahe 		}, }
5026c2e4adSDeclan Doherty 	},
5126c2e4adSDeclan Doherty 	RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
5226c2e4adSDeclan Doherty };
5326c2e4adSDeclan Doherty 
5494b0ad8eSDeclan Doherty /** Configure device */
5594b0ad8eSDeclan Doherty static int
5660e686c2SFan Zhang null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
5760e686c2SFan Zhang 		__rte_unused struct rte_cryptodev_config *config)
5894b0ad8eSDeclan Doherty {
5994b0ad8eSDeclan Doherty 	return 0;
6094b0ad8eSDeclan Doherty }
6194b0ad8eSDeclan Doherty 
6294b0ad8eSDeclan Doherty /** Start device */
6394b0ad8eSDeclan Doherty static int
6494b0ad8eSDeclan Doherty null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
6594b0ad8eSDeclan Doherty {
6694b0ad8eSDeclan Doherty 	return 0;
6794b0ad8eSDeclan Doherty }
6894b0ad8eSDeclan Doherty 
6994b0ad8eSDeclan Doherty /** Stop device */
7094b0ad8eSDeclan Doherty static void
7194b0ad8eSDeclan Doherty null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
7294b0ad8eSDeclan Doherty {
7394b0ad8eSDeclan Doherty }
7494b0ad8eSDeclan Doherty 
7594b0ad8eSDeclan Doherty /** Close device */
7694b0ad8eSDeclan Doherty static int
7794b0ad8eSDeclan Doherty null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
7894b0ad8eSDeclan Doherty {
7994b0ad8eSDeclan Doherty 	return 0;
8094b0ad8eSDeclan Doherty }
8194b0ad8eSDeclan Doherty 
8294b0ad8eSDeclan Doherty /** Get device statistics */
8394b0ad8eSDeclan Doherty static void
8494b0ad8eSDeclan Doherty null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
8594b0ad8eSDeclan Doherty 		struct rte_cryptodev_stats *stats)
8694b0ad8eSDeclan Doherty {
8794b0ad8eSDeclan Doherty 	int qp_id;
8894b0ad8eSDeclan Doherty 
8994b0ad8eSDeclan Doherty 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
9094b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
9194b0ad8eSDeclan Doherty 
9294b0ad8eSDeclan Doherty 		stats->enqueued_count += qp->qp_stats.enqueued_count;
9394b0ad8eSDeclan Doherty 		stats->dequeued_count += qp->qp_stats.dequeued_count;
9494b0ad8eSDeclan Doherty 
9594b0ad8eSDeclan Doherty 		stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
9694b0ad8eSDeclan Doherty 		stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
9794b0ad8eSDeclan Doherty 	}
9894b0ad8eSDeclan Doherty }
9994b0ad8eSDeclan Doherty 
10094b0ad8eSDeclan Doherty /** Reset device statistics */
10194b0ad8eSDeclan Doherty static void
10294b0ad8eSDeclan Doherty null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
10394b0ad8eSDeclan Doherty {
10494b0ad8eSDeclan Doherty 	int qp_id;
10594b0ad8eSDeclan Doherty 
10694b0ad8eSDeclan Doherty 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
10794b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
10894b0ad8eSDeclan Doherty 
10994b0ad8eSDeclan Doherty 		memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
11094b0ad8eSDeclan Doherty 	}
11194b0ad8eSDeclan Doherty }
11294b0ad8eSDeclan Doherty 
11394b0ad8eSDeclan Doherty 
11494b0ad8eSDeclan Doherty /** Get device info */
11594b0ad8eSDeclan Doherty static void
11694b0ad8eSDeclan Doherty null_crypto_pmd_info_get(struct rte_cryptodev *dev,
11794b0ad8eSDeclan Doherty 		struct rte_cryptodev_info *dev_info)
11894b0ad8eSDeclan Doherty {
11994b0ad8eSDeclan Doherty 	struct null_crypto_private *internals = dev->data->dev_private;
12094b0ad8eSDeclan Doherty 
12194b0ad8eSDeclan Doherty 	if (dev_info != NULL) {
1227a364faeSSlawomir Mrozowicz 		dev_info->driver_id = dev->driver_id;
12394b0ad8eSDeclan Doherty 		dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
124e1fc5b76SPablo de Lara 		/* No limit of number of sessions */
125e1fc5b76SPablo de Lara 		dev_info->sym.max_nb_sessions = 0;
12626c2e4adSDeclan Doherty 		dev_info->feature_flags = dev->feature_flags;
12726c2e4adSDeclan Doherty 		dev_info->capabilities = null_crypto_pmd_capabilities;
12894b0ad8eSDeclan Doherty 	}
12994b0ad8eSDeclan Doherty }
13094b0ad8eSDeclan Doherty 
13194b0ad8eSDeclan Doherty /** Release queue pair */
13294b0ad8eSDeclan Doherty static int
13394b0ad8eSDeclan Doherty null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
13494b0ad8eSDeclan Doherty {
13594b0ad8eSDeclan Doherty 	if (dev->data->queue_pairs[qp_id] != NULL) {
13694b0ad8eSDeclan Doherty 		rte_free(dev->data->queue_pairs[qp_id]);
13794b0ad8eSDeclan Doherty 		dev->data->queue_pairs[qp_id] = NULL;
13894b0ad8eSDeclan Doherty 	}
13994b0ad8eSDeclan Doherty 	return 0;
14094b0ad8eSDeclan Doherty }
14194b0ad8eSDeclan Doherty 
14294b0ad8eSDeclan Doherty /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
14394b0ad8eSDeclan Doherty static int
14494b0ad8eSDeclan Doherty null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
14594b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp)
14694b0ad8eSDeclan Doherty {
14794b0ad8eSDeclan Doherty 	unsigned n = snprintf(qp->name, sizeof(qp->name),
14894b0ad8eSDeclan Doherty 			"null_crypto_pmd_%u_qp_%u",
14994b0ad8eSDeclan Doherty 			dev->data->dev_id, qp->id);
15094b0ad8eSDeclan Doherty 
1516ab25e63STomasz Duszynski 	if (n >= sizeof(qp->name))
15294b0ad8eSDeclan Doherty 		return -1;
15394b0ad8eSDeclan Doherty 
15494b0ad8eSDeclan Doherty 	return 0;
15594b0ad8eSDeclan Doherty }
15694b0ad8eSDeclan Doherty 
15794b0ad8eSDeclan Doherty /** Create a ring to place process packets on */
15894b0ad8eSDeclan Doherty static struct rte_ring *
15994b0ad8eSDeclan Doherty null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
16094b0ad8eSDeclan Doherty 		unsigned ring_size, int socket_id)
16194b0ad8eSDeclan Doherty {
16294b0ad8eSDeclan Doherty 	struct rte_ring *r;
16394b0ad8eSDeclan Doherty 
16494b0ad8eSDeclan Doherty 	r = rte_ring_lookup(qp->name);
16594b0ad8eSDeclan Doherty 	if (r) {
166b11c78a2SBruce Richardson 		if (rte_ring_get_size(r) >= ring_size) {
167735b783dSPallantla Poornima 			NULL_LOG(INFO,
168735b783dSPallantla Poornima 					"Reusing existing ring %s for "
169735b783dSPallantla Poornima 					" processed packets", qp->name);
17094b0ad8eSDeclan Doherty 			return r;
17194b0ad8eSDeclan Doherty 		}
17294b0ad8eSDeclan Doherty 
173735b783dSPallantla Poornima 		NULL_LOG(INFO,
174735b783dSPallantla Poornima 				"Unable to reuse existing ring %s for "
175735b783dSPallantla Poornima 				" processed packets", qp->name);
17694b0ad8eSDeclan Doherty 		return NULL;
17794b0ad8eSDeclan Doherty 	}
17894b0ad8eSDeclan Doherty 
17994b0ad8eSDeclan Doherty 	return rte_ring_create(qp->name, ring_size, socket_id,
18094b0ad8eSDeclan Doherty 			RING_F_SP_ENQ | RING_F_SC_DEQ);
18194b0ad8eSDeclan Doherty }
18294b0ad8eSDeclan Doherty 
18394b0ad8eSDeclan Doherty /** Setup a queue pair */
18494b0ad8eSDeclan Doherty static int
18594b0ad8eSDeclan Doherty null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
18694b0ad8eSDeclan Doherty 		const struct rte_cryptodev_qp_conf *qp_conf,
187f7db6f82SPablo de Lara 		int socket_id, struct rte_mempool *session_pool)
18894b0ad8eSDeclan Doherty {
18994b0ad8eSDeclan Doherty 	struct null_crypto_private *internals = dev->data->dev_private;
19094b0ad8eSDeclan Doherty 	struct null_crypto_qp *qp;
19194b0ad8eSDeclan Doherty 	int retval;
19294b0ad8eSDeclan Doherty 
19394b0ad8eSDeclan Doherty 	if (qp_id >= internals->max_nb_qpairs) {
194735b783dSPallantla Poornima 		NULL_LOG(ERR, "Invalid qp_id %u, greater than maximum "
19594b0ad8eSDeclan Doherty 				"number of queue pairs supported (%u).",
19694b0ad8eSDeclan Doherty 				qp_id, internals->max_nb_qpairs);
19794b0ad8eSDeclan Doherty 		return (-EINVAL);
19894b0ad8eSDeclan Doherty 	}
19994b0ad8eSDeclan Doherty 
20094b0ad8eSDeclan Doherty 	/* Free memory prior to re-allocation if needed. */
20194b0ad8eSDeclan Doherty 	if (dev->data->queue_pairs[qp_id] != NULL)
20294b0ad8eSDeclan Doherty 		null_crypto_pmd_qp_release(dev, qp_id);
20394b0ad8eSDeclan Doherty 
20494b0ad8eSDeclan Doherty 	/* Allocate the queue pair data structure. */
20594b0ad8eSDeclan Doherty 	qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
20694b0ad8eSDeclan Doherty 					RTE_CACHE_LINE_SIZE, socket_id);
20794b0ad8eSDeclan Doherty 	if (qp == NULL) {
208735b783dSPallantla Poornima 		NULL_LOG(ERR, "Failed to allocate queue pair memory");
20994b0ad8eSDeclan Doherty 		return (-ENOMEM);
21094b0ad8eSDeclan Doherty 	}
21194b0ad8eSDeclan Doherty 
21294b0ad8eSDeclan Doherty 	qp->id = qp_id;
21394b0ad8eSDeclan Doherty 	dev->data->queue_pairs[qp_id] = qp;
21494b0ad8eSDeclan Doherty 
21594b0ad8eSDeclan Doherty 	retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
21694b0ad8eSDeclan Doherty 	if (retval) {
217735b783dSPallantla Poornima 		NULL_LOG(ERR, "Failed to create unique name for null "
21894b0ad8eSDeclan Doherty 				"crypto device");
219735b783dSPallantla Poornima 
22094b0ad8eSDeclan Doherty 		goto qp_setup_cleanup;
22194b0ad8eSDeclan Doherty 	}
22294b0ad8eSDeclan Doherty 
22394b0ad8eSDeclan Doherty 	qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
22494b0ad8eSDeclan Doherty 			qp_conf->nb_descriptors, socket_id);
22594b0ad8eSDeclan Doherty 	if (qp->processed_pkts == NULL) {
226735b783dSPallantla Poornima 		NULL_LOG(ERR, "Failed to create unique name for null "
22794b0ad8eSDeclan Doherty 				"crypto device");
22894b0ad8eSDeclan Doherty 		goto qp_setup_cleanup;
22994b0ad8eSDeclan Doherty 	}
23094b0ad8eSDeclan Doherty 
231f7db6f82SPablo de Lara 	qp->sess_mp = session_pool;
23294b0ad8eSDeclan Doherty 
23394b0ad8eSDeclan Doherty 	memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
23494b0ad8eSDeclan Doherty 
23594b0ad8eSDeclan Doherty 	return 0;
23694b0ad8eSDeclan Doherty 
23794b0ad8eSDeclan Doherty qp_setup_cleanup:
23894b0ad8eSDeclan Doherty 	if (qp)
23994b0ad8eSDeclan Doherty 		rte_free(qp);
24094b0ad8eSDeclan Doherty 
24194b0ad8eSDeclan Doherty 	return -1;
24294b0ad8eSDeclan Doherty }
24394b0ad8eSDeclan Doherty 
24494b0ad8eSDeclan Doherty /** Return the number of allocated queue pairs */
24594b0ad8eSDeclan Doherty static uint32_t
24694b0ad8eSDeclan Doherty null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
24794b0ad8eSDeclan Doherty {
24894b0ad8eSDeclan Doherty 	return dev->data->nb_queue_pairs;
24994b0ad8eSDeclan Doherty }
25094b0ad8eSDeclan Doherty 
25194b0ad8eSDeclan Doherty /** Returns the size of the NULL crypto session structure */
25294b0ad8eSDeclan Doherty static unsigned
253012c5076SPablo de Lara null_crypto_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
25494b0ad8eSDeclan Doherty {
25594b0ad8eSDeclan Doherty 	return sizeof(struct null_crypto_session);
25694b0ad8eSDeclan Doherty }
25794b0ad8eSDeclan Doherty 
25894b0ad8eSDeclan Doherty /** Configure a null crypto session from a crypto xform chain */
259b3bbd9e5SSlawomir Mrozowicz static int
260012c5076SPablo de Lara null_crypto_pmd_sym_session_configure(struct rte_cryptodev *dev __rte_unused,
261b3bbd9e5SSlawomir Mrozowicz 		struct rte_crypto_sym_xform *xform,
262b3bbd9e5SSlawomir Mrozowicz 		struct rte_cryptodev_sym_session *sess,
263b3bbd9e5SSlawomir Mrozowicz 		struct rte_mempool *mp)
26494b0ad8eSDeclan Doherty {
265b3bbd9e5SSlawomir Mrozowicz 	void *sess_private_data;
26627391b53SPablo de Lara 	int ret;
26794b0ad8eSDeclan Doherty 
26894b0ad8eSDeclan Doherty 	if (unlikely(sess == NULL)) {
269735b783dSPallantla Poornima 		NULL_LOG(ERR, "invalid session struct");
27027391b53SPablo de Lara 		return -EINVAL;
27194b0ad8eSDeclan Doherty 	}
27294b0ad8eSDeclan Doherty 
273b3bbd9e5SSlawomir Mrozowicz 	if (rte_mempool_get(mp, &sess_private_data)) {
274735b783dSPallantla Poornima 		NULL_LOG(ERR,
275b3bbd9e5SSlawomir Mrozowicz 				"Couldn't get object from session mempool");
27627391b53SPablo de Lara 		return -ENOMEM;
277b3bbd9e5SSlawomir Mrozowicz 	}
278b3bbd9e5SSlawomir Mrozowicz 
27927391b53SPablo de Lara 	ret = null_crypto_set_session_parameters(sess_private_data, xform);
28027391b53SPablo de Lara 	if (ret != 0) {
281735b783dSPallantla Poornima 		NULL_LOG(ERR, "failed configure session parameters");
282b3bbd9e5SSlawomir Mrozowicz 
283b3bbd9e5SSlawomir Mrozowicz 		/* Return session to mempool */
284b3bbd9e5SSlawomir Mrozowicz 		rte_mempool_put(mp, sess_private_data);
28527391b53SPablo de Lara 		return ret;
286b3bbd9e5SSlawomir Mrozowicz 	}
287b3bbd9e5SSlawomir Mrozowicz 
288012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
289b3bbd9e5SSlawomir Mrozowicz 		sess_private_data);
290b3bbd9e5SSlawomir Mrozowicz 
291b3bbd9e5SSlawomir Mrozowicz 	return 0;
29294b0ad8eSDeclan Doherty }
29394b0ad8eSDeclan Doherty 
29494b0ad8eSDeclan Doherty /** Clear the memory of session so it doesn't leave key material behind */
29594b0ad8eSDeclan Doherty static void
296012c5076SPablo de Lara null_crypto_pmd_sym_session_clear(struct rte_cryptodev *dev,
297b3bbd9e5SSlawomir Mrozowicz 		struct rte_cryptodev_sym_session *sess)
29894b0ad8eSDeclan Doherty {
299b3bbd9e5SSlawomir Mrozowicz 	uint8_t index = dev->driver_id;
300012c5076SPablo de Lara 	void *sess_priv = get_sym_session_private_data(sess, index);
301b3bbd9e5SSlawomir Mrozowicz 
302b3bbd9e5SSlawomir Mrozowicz 	/* Zero out the whole structure */
303b3bbd9e5SSlawomir Mrozowicz 	if (sess_priv) {
304b3bbd9e5SSlawomir Mrozowicz 		memset(sess_priv, 0, sizeof(struct null_crypto_session));
305b3bbd9e5SSlawomir Mrozowicz 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
306012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
307b3bbd9e5SSlawomir Mrozowicz 		rte_mempool_put(sess_mp, sess_priv);
308b3bbd9e5SSlawomir Mrozowicz 	}
30994b0ad8eSDeclan Doherty }
31094b0ad8eSDeclan Doherty 
311*b74fd6b8SFerruh Yigit static struct rte_cryptodev_ops pmd_ops = {
31294b0ad8eSDeclan Doherty 		.dev_configure		= null_crypto_pmd_config,
31394b0ad8eSDeclan Doherty 		.dev_start		= null_crypto_pmd_start,
31494b0ad8eSDeclan Doherty 		.dev_stop		= null_crypto_pmd_stop,
31594b0ad8eSDeclan Doherty 		.dev_close		= null_crypto_pmd_close,
31694b0ad8eSDeclan Doherty 
31794b0ad8eSDeclan Doherty 		.stats_get		= null_crypto_pmd_stats_get,
31894b0ad8eSDeclan Doherty 		.stats_reset		= null_crypto_pmd_stats_reset,
31994b0ad8eSDeclan Doherty 
32094b0ad8eSDeclan Doherty 		.dev_infos_get		= null_crypto_pmd_info_get,
32194b0ad8eSDeclan Doherty 
32294b0ad8eSDeclan Doherty 		.queue_pair_setup	= null_crypto_pmd_qp_setup,
32394b0ad8eSDeclan Doherty 		.queue_pair_release	= null_crypto_pmd_qp_release,
32494b0ad8eSDeclan Doherty 		.queue_pair_count	= null_crypto_pmd_qp_count,
32594b0ad8eSDeclan Doherty 
326012c5076SPablo de Lara 		.sym_session_get_size	= null_crypto_pmd_sym_session_get_size,
327012c5076SPablo de Lara 		.sym_session_configure	= null_crypto_pmd_sym_session_configure,
328012c5076SPablo de Lara 		.sym_session_clear	= null_crypto_pmd_sym_session_clear
32994b0ad8eSDeclan Doherty };
33094b0ad8eSDeclan Doherty 
33194b0ad8eSDeclan Doherty struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;
332