xref: /dpdk/drivers/crypto/null/null_crypto_pmd_ops.c (revision 94b0ad8e0aa556230183f4c4d06b68bfd145dce3)
1*94b0ad8eSDeclan Doherty /*-
2*94b0ad8eSDeclan Doherty  *   BSD LICENSE
3*94b0ad8eSDeclan Doherty  *
4*94b0ad8eSDeclan Doherty  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5*94b0ad8eSDeclan Doherty  *
6*94b0ad8eSDeclan Doherty  *   Redistribution and use in source and binary forms, with or without
7*94b0ad8eSDeclan Doherty  *   modification, are permitted provided that the following conditions
8*94b0ad8eSDeclan Doherty  *   are met:
9*94b0ad8eSDeclan Doherty  *
10*94b0ad8eSDeclan Doherty  *     * Redistributions of source code must retain the above copyright
11*94b0ad8eSDeclan Doherty  *       notice, this list of conditions and the following disclaimer.
12*94b0ad8eSDeclan Doherty  *     * Redistributions in binary form must reproduce the above copyright
13*94b0ad8eSDeclan Doherty  *       notice, this list of conditions and the following disclaimer in
14*94b0ad8eSDeclan Doherty  *       the documentation and/or other materials provided with the
15*94b0ad8eSDeclan Doherty  *       distribution.
16*94b0ad8eSDeclan Doherty  *     * Neither the name of Intel Corporation nor the names of its
17*94b0ad8eSDeclan Doherty  *       contributors may be used to endorse or promote products derived
18*94b0ad8eSDeclan Doherty  *       from this software without specific prior written permission.
19*94b0ad8eSDeclan Doherty  *
20*94b0ad8eSDeclan Doherty  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*94b0ad8eSDeclan Doherty  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*94b0ad8eSDeclan Doherty  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*94b0ad8eSDeclan Doherty  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*94b0ad8eSDeclan Doherty  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*94b0ad8eSDeclan Doherty  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*94b0ad8eSDeclan Doherty  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*94b0ad8eSDeclan Doherty  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*94b0ad8eSDeclan Doherty  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*94b0ad8eSDeclan Doherty  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*94b0ad8eSDeclan Doherty  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*94b0ad8eSDeclan Doherty  */
32*94b0ad8eSDeclan Doherty 
33*94b0ad8eSDeclan Doherty #include <string.h>
34*94b0ad8eSDeclan Doherty 
35*94b0ad8eSDeclan Doherty #include <rte_common.h>
36*94b0ad8eSDeclan Doherty #include <rte_malloc.h>
37*94b0ad8eSDeclan Doherty #include <rte_cryptodev_pmd.h>
38*94b0ad8eSDeclan Doherty 
39*94b0ad8eSDeclan Doherty #include "null_crypto_pmd_private.h"
40*94b0ad8eSDeclan Doherty 
41*94b0ad8eSDeclan Doherty /** Configure device */
42*94b0ad8eSDeclan Doherty static int
43*94b0ad8eSDeclan Doherty null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev)
44*94b0ad8eSDeclan Doherty {
45*94b0ad8eSDeclan Doherty 	return 0;
46*94b0ad8eSDeclan Doherty }
47*94b0ad8eSDeclan Doherty 
48*94b0ad8eSDeclan Doherty /** Start device */
49*94b0ad8eSDeclan Doherty static int
50*94b0ad8eSDeclan Doherty null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
51*94b0ad8eSDeclan Doherty {
52*94b0ad8eSDeclan Doherty 	return 0;
53*94b0ad8eSDeclan Doherty }
54*94b0ad8eSDeclan Doherty 
55*94b0ad8eSDeclan Doherty /** Stop device */
56*94b0ad8eSDeclan Doherty static void
57*94b0ad8eSDeclan Doherty null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
58*94b0ad8eSDeclan Doherty {
59*94b0ad8eSDeclan Doherty }
60*94b0ad8eSDeclan Doherty 
61*94b0ad8eSDeclan Doherty /** Close device */
62*94b0ad8eSDeclan Doherty static int
63*94b0ad8eSDeclan Doherty null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
64*94b0ad8eSDeclan Doherty {
65*94b0ad8eSDeclan Doherty 	return 0;
66*94b0ad8eSDeclan Doherty }
67*94b0ad8eSDeclan Doherty 
68*94b0ad8eSDeclan Doherty /** Get device statistics */
69*94b0ad8eSDeclan Doherty static void
70*94b0ad8eSDeclan Doherty null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
71*94b0ad8eSDeclan Doherty 		struct rte_cryptodev_stats *stats)
72*94b0ad8eSDeclan Doherty {
73*94b0ad8eSDeclan Doherty 	int qp_id;
74*94b0ad8eSDeclan Doherty 
75*94b0ad8eSDeclan Doherty 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
76*94b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
77*94b0ad8eSDeclan Doherty 
78*94b0ad8eSDeclan Doherty 		stats->enqueued_count += qp->qp_stats.enqueued_count;
79*94b0ad8eSDeclan Doherty 		stats->dequeued_count += qp->qp_stats.dequeued_count;
80*94b0ad8eSDeclan Doherty 
81*94b0ad8eSDeclan Doherty 		stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
82*94b0ad8eSDeclan Doherty 		stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
83*94b0ad8eSDeclan Doherty 	}
84*94b0ad8eSDeclan Doherty }
85*94b0ad8eSDeclan Doherty 
86*94b0ad8eSDeclan Doherty /** Reset device statistics */
87*94b0ad8eSDeclan Doherty static void
88*94b0ad8eSDeclan Doherty null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
89*94b0ad8eSDeclan Doherty {
90*94b0ad8eSDeclan Doherty 	int qp_id;
91*94b0ad8eSDeclan Doherty 
92*94b0ad8eSDeclan Doherty 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
93*94b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
94*94b0ad8eSDeclan Doherty 
95*94b0ad8eSDeclan Doherty 		memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
96*94b0ad8eSDeclan Doherty 	}
97*94b0ad8eSDeclan Doherty }
98*94b0ad8eSDeclan Doherty 
99*94b0ad8eSDeclan Doherty 
100*94b0ad8eSDeclan Doherty /** Get device info */
101*94b0ad8eSDeclan Doherty static void
102*94b0ad8eSDeclan Doherty null_crypto_pmd_info_get(struct rte_cryptodev *dev,
103*94b0ad8eSDeclan Doherty 		struct rte_cryptodev_info *dev_info)
104*94b0ad8eSDeclan Doherty {
105*94b0ad8eSDeclan Doherty 	struct null_crypto_private *internals = dev->data->dev_private;
106*94b0ad8eSDeclan Doherty 
107*94b0ad8eSDeclan Doherty 	if (dev_info != NULL) {
108*94b0ad8eSDeclan Doherty 		dev_info->dev_type = dev->dev_type;
109*94b0ad8eSDeclan Doherty 		dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
110*94b0ad8eSDeclan Doherty 		dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
111*94b0ad8eSDeclan Doherty 	}
112*94b0ad8eSDeclan Doherty }
113*94b0ad8eSDeclan Doherty 
114*94b0ad8eSDeclan Doherty /** Release queue pair */
115*94b0ad8eSDeclan Doherty static int
116*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
117*94b0ad8eSDeclan Doherty {
118*94b0ad8eSDeclan Doherty 	if (dev->data->queue_pairs[qp_id] != NULL) {
119*94b0ad8eSDeclan Doherty 		rte_free(dev->data->queue_pairs[qp_id]);
120*94b0ad8eSDeclan Doherty 		dev->data->queue_pairs[qp_id] = NULL;
121*94b0ad8eSDeclan Doherty 	}
122*94b0ad8eSDeclan Doherty 	return 0;
123*94b0ad8eSDeclan Doherty }
124*94b0ad8eSDeclan Doherty 
125*94b0ad8eSDeclan Doherty /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
126*94b0ad8eSDeclan Doherty static int
127*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
128*94b0ad8eSDeclan Doherty 		struct null_crypto_qp *qp)
129*94b0ad8eSDeclan Doherty {
130*94b0ad8eSDeclan Doherty 	unsigned n = snprintf(qp->name, sizeof(qp->name),
131*94b0ad8eSDeclan Doherty 			"null_crypto_pmd_%u_qp_%u",
132*94b0ad8eSDeclan Doherty 			dev->data->dev_id, qp->id);
133*94b0ad8eSDeclan Doherty 
134*94b0ad8eSDeclan Doherty 	if (n > sizeof(qp->name))
135*94b0ad8eSDeclan Doherty 		return -1;
136*94b0ad8eSDeclan Doherty 
137*94b0ad8eSDeclan Doherty 	return 0;
138*94b0ad8eSDeclan Doherty }
139*94b0ad8eSDeclan Doherty 
140*94b0ad8eSDeclan Doherty /** Create a ring to place process packets on */
141*94b0ad8eSDeclan Doherty static struct rte_ring *
142*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
143*94b0ad8eSDeclan Doherty 		unsigned ring_size, int socket_id)
144*94b0ad8eSDeclan Doherty {
145*94b0ad8eSDeclan Doherty 	struct rte_ring *r;
146*94b0ad8eSDeclan Doherty 
147*94b0ad8eSDeclan Doherty 	r = rte_ring_lookup(qp->name);
148*94b0ad8eSDeclan Doherty 	if (r) {
149*94b0ad8eSDeclan Doherty 		if (r->prod.size >= ring_size) {
150*94b0ad8eSDeclan Doherty 			NULL_CRYPTO_LOG_INFO(
151*94b0ad8eSDeclan Doherty 				"Reusing existing ring %s for processed packets",
152*94b0ad8eSDeclan Doherty 				qp->name);
153*94b0ad8eSDeclan Doherty 			return r;
154*94b0ad8eSDeclan Doherty 		}
155*94b0ad8eSDeclan Doherty 
156*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_INFO(
157*94b0ad8eSDeclan Doherty 			"Unable to reuse existing ring %s for processed packets",
158*94b0ad8eSDeclan Doherty 			 qp->name);
159*94b0ad8eSDeclan Doherty 		return NULL;
160*94b0ad8eSDeclan Doherty 	}
161*94b0ad8eSDeclan Doherty 
162*94b0ad8eSDeclan Doherty 	return rte_ring_create(qp->name, ring_size, socket_id,
163*94b0ad8eSDeclan Doherty 			RING_F_SP_ENQ | RING_F_SC_DEQ);
164*94b0ad8eSDeclan Doherty }
165*94b0ad8eSDeclan Doherty 
166*94b0ad8eSDeclan Doherty /** Setup a queue pair */
167*94b0ad8eSDeclan Doherty static int
168*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
169*94b0ad8eSDeclan Doherty 		const struct rte_cryptodev_qp_conf *qp_conf,
170*94b0ad8eSDeclan Doherty 		 int socket_id)
171*94b0ad8eSDeclan Doherty {
172*94b0ad8eSDeclan Doherty 	struct null_crypto_private *internals = dev->data->dev_private;
173*94b0ad8eSDeclan Doherty 	struct null_crypto_qp *qp;
174*94b0ad8eSDeclan Doherty 	int retval;
175*94b0ad8eSDeclan Doherty 
176*94b0ad8eSDeclan Doherty 	if (qp_id >= internals->max_nb_qpairs) {
177*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum "
178*94b0ad8eSDeclan Doherty 				"number of queue pairs supported (%u).",
179*94b0ad8eSDeclan Doherty 				qp_id, internals->max_nb_qpairs);
180*94b0ad8eSDeclan Doherty 		return (-EINVAL);
181*94b0ad8eSDeclan Doherty 	}
182*94b0ad8eSDeclan Doherty 
183*94b0ad8eSDeclan Doherty 	/* Free memory prior to re-allocation if needed. */
184*94b0ad8eSDeclan Doherty 	if (dev->data->queue_pairs[qp_id] != NULL)
185*94b0ad8eSDeclan Doherty 		null_crypto_pmd_qp_release(dev, qp_id);
186*94b0ad8eSDeclan Doherty 
187*94b0ad8eSDeclan Doherty 	/* Allocate the queue pair data structure. */
188*94b0ad8eSDeclan Doherty 	qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
189*94b0ad8eSDeclan Doherty 					RTE_CACHE_LINE_SIZE, socket_id);
190*94b0ad8eSDeclan Doherty 	if (qp == NULL) {
191*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory");
192*94b0ad8eSDeclan Doherty 		return (-ENOMEM);
193*94b0ad8eSDeclan Doherty 	}
194*94b0ad8eSDeclan Doherty 
195*94b0ad8eSDeclan Doherty 	qp->id = qp_id;
196*94b0ad8eSDeclan Doherty 	dev->data->queue_pairs[qp_id] = qp;
197*94b0ad8eSDeclan Doherty 
198*94b0ad8eSDeclan Doherty 	retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
199*94b0ad8eSDeclan Doherty 	if (retval) {
200*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
201*94b0ad8eSDeclan Doherty 				"crypto device");
202*94b0ad8eSDeclan Doherty 		goto qp_setup_cleanup;
203*94b0ad8eSDeclan Doherty 	}
204*94b0ad8eSDeclan Doherty 
205*94b0ad8eSDeclan Doherty 	qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
206*94b0ad8eSDeclan Doherty 			qp_conf->nb_descriptors, socket_id);
207*94b0ad8eSDeclan Doherty 	if (qp->processed_pkts == NULL) {
208*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
209*94b0ad8eSDeclan Doherty 				"crypto device");
210*94b0ad8eSDeclan Doherty 		goto qp_setup_cleanup;
211*94b0ad8eSDeclan Doherty 	}
212*94b0ad8eSDeclan Doherty 
213*94b0ad8eSDeclan Doherty 	qp->sess_mp = dev->data->session_pool;
214*94b0ad8eSDeclan Doherty 
215*94b0ad8eSDeclan Doherty 	memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
216*94b0ad8eSDeclan Doherty 
217*94b0ad8eSDeclan Doherty 	return 0;
218*94b0ad8eSDeclan Doherty 
219*94b0ad8eSDeclan Doherty qp_setup_cleanup:
220*94b0ad8eSDeclan Doherty 	if (qp)
221*94b0ad8eSDeclan Doherty 		rte_free(qp);
222*94b0ad8eSDeclan Doherty 
223*94b0ad8eSDeclan Doherty 	return -1;
224*94b0ad8eSDeclan Doherty }
225*94b0ad8eSDeclan Doherty 
226*94b0ad8eSDeclan Doherty /** Start queue pair */
227*94b0ad8eSDeclan Doherty static int
228*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
229*94b0ad8eSDeclan Doherty 		__rte_unused uint16_t queue_pair_id)
230*94b0ad8eSDeclan Doherty {
231*94b0ad8eSDeclan Doherty 	return -ENOTSUP;
232*94b0ad8eSDeclan Doherty }
233*94b0ad8eSDeclan Doherty 
234*94b0ad8eSDeclan Doherty /** Stop queue pair */
235*94b0ad8eSDeclan Doherty static int
236*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
237*94b0ad8eSDeclan Doherty 		__rte_unused uint16_t queue_pair_id)
238*94b0ad8eSDeclan Doherty {
239*94b0ad8eSDeclan Doherty 	return -ENOTSUP;
240*94b0ad8eSDeclan Doherty }
241*94b0ad8eSDeclan Doherty 
242*94b0ad8eSDeclan Doherty /** Return the number of allocated queue pairs */
243*94b0ad8eSDeclan Doherty static uint32_t
244*94b0ad8eSDeclan Doherty null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
245*94b0ad8eSDeclan Doherty {
246*94b0ad8eSDeclan Doherty 	return dev->data->nb_queue_pairs;
247*94b0ad8eSDeclan Doherty }
248*94b0ad8eSDeclan Doherty 
249*94b0ad8eSDeclan Doherty /** Returns the size of the NULL crypto session structure */
250*94b0ad8eSDeclan Doherty static unsigned
251*94b0ad8eSDeclan Doherty null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
252*94b0ad8eSDeclan Doherty {
253*94b0ad8eSDeclan Doherty 	return sizeof(struct null_crypto_session);
254*94b0ad8eSDeclan Doherty }
255*94b0ad8eSDeclan Doherty 
256*94b0ad8eSDeclan Doherty /** Configure a null crypto session from a crypto xform chain */
257*94b0ad8eSDeclan Doherty static void *
258*94b0ad8eSDeclan Doherty null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
259*94b0ad8eSDeclan Doherty 		struct rte_crypto_sym_xform *xform, void *sess)
260*94b0ad8eSDeclan Doherty {
261*94b0ad8eSDeclan Doherty 	int retval;
262*94b0ad8eSDeclan Doherty 
263*94b0ad8eSDeclan Doherty 	if (unlikely(sess == NULL)) {
264*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("invalid session struct");
265*94b0ad8eSDeclan Doherty 		return NULL;
266*94b0ad8eSDeclan Doherty 	}
267*94b0ad8eSDeclan Doherty 	retval = null_crypto_set_session_parameters(
268*94b0ad8eSDeclan Doherty 			(struct null_crypto_session *)sess, xform);
269*94b0ad8eSDeclan Doherty 	if (retval != 0) {
270*94b0ad8eSDeclan Doherty 		NULL_CRYPTO_LOG_ERR("failed configure session parameters");
271*94b0ad8eSDeclan Doherty 		return NULL;
272*94b0ad8eSDeclan Doherty 	}
273*94b0ad8eSDeclan Doherty 
274*94b0ad8eSDeclan Doherty 	return sess;
275*94b0ad8eSDeclan Doherty }
276*94b0ad8eSDeclan Doherty 
277*94b0ad8eSDeclan Doherty /** Clear the memory of session so it doesn't leave key material behind */
278*94b0ad8eSDeclan Doherty static void
279*94b0ad8eSDeclan Doherty null_crypto_pmd_session_clear(struct rte_cryptodev *dev __rte_unused,
280*94b0ad8eSDeclan Doherty 		void *sess)
281*94b0ad8eSDeclan Doherty {
282*94b0ad8eSDeclan Doherty 	if (sess)
283*94b0ad8eSDeclan Doherty 		memset(sess, 0, sizeof(struct null_crypto_session));
284*94b0ad8eSDeclan Doherty }
285*94b0ad8eSDeclan Doherty 
286*94b0ad8eSDeclan Doherty struct rte_cryptodev_ops pmd_ops = {
287*94b0ad8eSDeclan Doherty 		.dev_configure		= null_crypto_pmd_config,
288*94b0ad8eSDeclan Doherty 		.dev_start		= null_crypto_pmd_start,
289*94b0ad8eSDeclan Doherty 		.dev_stop		= null_crypto_pmd_stop,
290*94b0ad8eSDeclan Doherty 		.dev_close		= null_crypto_pmd_close,
291*94b0ad8eSDeclan Doherty 
292*94b0ad8eSDeclan Doherty 		.stats_get		= null_crypto_pmd_stats_get,
293*94b0ad8eSDeclan Doherty 		.stats_reset		= null_crypto_pmd_stats_reset,
294*94b0ad8eSDeclan Doherty 
295*94b0ad8eSDeclan Doherty 		.dev_infos_get		= null_crypto_pmd_info_get,
296*94b0ad8eSDeclan Doherty 
297*94b0ad8eSDeclan Doherty 		.queue_pair_setup	= null_crypto_pmd_qp_setup,
298*94b0ad8eSDeclan Doherty 		.queue_pair_release	= null_crypto_pmd_qp_release,
299*94b0ad8eSDeclan Doherty 		.queue_pair_start	= null_crypto_pmd_qp_start,
300*94b0ad8eSDeclan Doherty 		.queue_pair_stop	= null_crypto_pmd_qp_stop,
301*94b0ad8eSDeclan Doherty 		.queue_pair_count	= null_crypto_pmd_qp_count,
302*94b0ad8eSDeclan Doherty 
303*94b0ad8eSDeclan Doherty 		.session_get_size	= null_crypto_pmd_session_get_size,
304*94b0ad8eSDeclan Doherty 		.session_configure	= null_crypto_pmd_session_configure,
305*94b0ad8eSDeclan Doherty 		.session_clear		= null_crypto_pmd_session_clear
306*94b0ad8eSDeclan Doherty };
307*94b0ad8eSDeclan Doherty 
308*94b0ad8eSDeclan Doherty struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;
309