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