cryptodev: add mempool pointer in queue pair setup
[dpdk.git] / drivers / crypto / aesni_gcm / aesni_gcm_pmd_ops.c
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
41 static const struct rte_cryptodev_capabilities aesni_gcm_pmd_capabilities[] = {
42         {       /* AES GMAC (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_GMAC,
48                                 .block_size = 16,
49                                 .key_size = {
50                                         .min = 16,
51                                         .max = 32,
52                                         .increment = 8
53                                 },
54                                 .digest_size = {
55                                         .min = 8,
56                                         .max = 16,
57                                         .increment = 4
58                                 },
59                                 .aad_size = { 0 },
60                                 .iv_size = {
61                                         .min = 12,
62                                         .max = 12,
63                                         .increment = 0
64                                 }
65                         }, }
66                 }, }
67         },
68         {       /* AES GCM */
69                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
70                 {.sym = {
71                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
72                         {.aead = {
73                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
74                                 .block_size = 16,
75                                 .key_size = {
76                                         .min = 16,
77                                         .max = 32,
78                                         .increment = 8
79                                 },
80                                 .digest_size = {
81                                         .min = 8,
82                                         .max = 16,
83                                         .increment = 4
84                                 },
85                                 .aad_size = {
86                                         .min = 0,
87                                         .max = 65535,
88                                         .increment = 1
89                                 },
90                                 .iv_size = {
91                                         .min = 12,
92                                         .max = 12,
93                                         .increment = 0
94                                 }
95                         }, }
96                 }, }
97         },
98         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
99 };
100
101 /** Configure device */
102 static int
103 aesni_gcm_pmd_config(__rte_unused struct rte_cryptodev *dev,
104                 __rte_unused struct rte_cryptodev_config *config)
105 {
106         return 0;
107 }
108
109 /** Start device */
110 static int
111 aesni_gcm_pmd_start(__rte_unused struct rte_cryptodev *dev)
112 {
113         return 0;
114 }
115
116 /** Stop device */
117 static void
118 aesni_gcm_pmd_stop(__rte_unused struct rte_cryptodev *dev)
119 {
120 }
121
122 /** Close device */
123 static int
124 aesni_gcm_pmd_close(__rte_unused struct rte_cryptodev *dev)
125 {
126         return 0;
127 }
128
129
130 /** Get device statistics */
131 static void
132 aesni_gcm_pmd_stats_get(struct rte_cryptodev *dev,
133                 struct rte_cryptodev_stats *stats)
134 {
135         int qp_id;
136
137         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
138                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
139
140                 stats->enqueued_count += qp->qp_stats.enqueued_count;
141                 stats->dequeued_count += qp->qp_stats.dequeued_count;
142
143                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
144                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
145         }
146 }
147
148 /** Reset device statistics */
149 static void
150 aesni_gcm_pmd_stats_reset(struct rte_cryptodev *dev)
151 {
152         int qp_id;
153
154         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
155                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
156
157                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
158         }
159 }
160
161
162 /** Get device info */
163 static void
164 aesni_gcm_pmd_info_get(struct rte_cryptodev *dev,
165                 struct rte_cryptodev_info *dev_info)
166 {
167         struct aesni_gcm_private *internals = dev->data->dev_private;
168
169         if (dev_info != NULL) {
170                 dev_info->driver_id = dev->driver_id;
171                 dev_info->feature_flags = dev->feature_flags;
172                 dev_info->capabilities = aesni_gcm_pmd_capabilities;
173
174                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
175                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
176         }
177 }
178
179 /** Release queue pair */
180 static int
181 aesni_gcm_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
182 {
183         if (dev->data->queue_pairs[qp_id] != NULL) {
184                 rte_free(dev->data->queue_pairs[qp_id]);
185                 dev->data->queue_pairs[qp_id] = NULL;
186         }
187         return 0;
188 }
189
190 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
191 static int
192 aesni_gcm_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
193                 struct aesni_gcm_qp *qp)
194 {
195         unsigned n = snprintf(qp->name, sizeof(qp->name),
196                         "aesni_gcm_pmd_%u_qp_%u",
197                         dev->data->dev_id, qp->id);
198
199         if (n > sizeof(qp->name))
200                 return -1;
201
202         return 0;
203 }
204
205 /** Create a ring to place process packets on */
206 static struct rte_ring *
207 aesni_gcm_pmd_qp_create_processed_pkts_ring(struct aesni_gcm_qp *qp,
208                 unsigned ring_size, int socket_id)
209 {
210         struct rte_ring *r;
211
212         r = rte_ring_lookup(qp->name);
213         if (r) {
214                 if (rte_ring_get_size(r) >= ring_size) {
215                         GCM_LOG_INFO("Reusing existing ring %s for processed"
216                                         " packets", qp->name);
217                         return r;
218                 }
219
220                 GCM_LOG_ERR("Unable to reuse existing ring %s for processed"
221                                 " packets", qp->name);
222                 return NULL;
223         }
224
225         return rte_ring_create(qp->name, ring_size, socket_id,
226                         RING_F_SP_ENQ | RING_F_SC_DEQ);
227 }
228
229 /** Setup a queue pair */
230 static int
231 aesni_gcm_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
232                 const struct rte_cryptodev_qp_conf *qp_conf,
233                 int socket_id, struct rte_mempool *session_pool)
234 {
235         struct aesni_gcm_qp *qp = NULL;
236         struct aesni_gcm_private *internals = dev->data->dev_private;
237
238         /* Free memory prior to re-allocation if needed. */
239         if (dev->data->queue_pairs[qp_id] != NULL)
240                 aesni_gcm_pmd_qp_release(dev, qp_id);
241
242         /* Allocate the queue pair data structure. */
243         qp = rte_zmalloc_socket("AES-NI PMD Queue Pair", sizeof(*qp),
244                                         RTE_CACHE_LINE_SIZE, socket_id);
245         if (qp == NULL)
246                 return (-ENOMEM);
247
248         qp->id = qp_id;
249         dev->data->queue_pairs[qp_id] = qp;
250
251         if (aesni_gcm_pmd_qp_set_unique_name(dev, qp))
252                 goto qp_setup_cleanup;
253
254         qp->ops = (const struct aesni_gcm_ops *)gcm_ops[internals->vector_mode];
255
256         qp->processed_pkts = aesni_gcm_pmd_qp_create_processed_pkts_ring(qp,
257                         qp_conf->nb_descriptors, socket_id);
258         if (qp->processed_pkts == NULL)
259                 goto qp_setup_cleanup;
260
261         qp->sess_mp = session_pool;
262
263         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
264
265         return 0;
266
267 qp_setup_cleanup:
268         if (qp)
269                 rte_free(qp);
270
271         return -1;
272 }
273
274 /** Start queue pair */
275 static int
276 aesni_gcm_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
277                 __rte_unused uint16_t queue_pair_id)
278 {
279         return -ENOTSUP;
280 }
281
282 /** Stop queue pair */
283 static int
284 aesni_gcm_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
285                 __rte_unused uint16_t queue_pair_id)
286 {
287         return -ENOTSUP;
288 }
289
290 /** Return the number of allocated queue pairs */
291 static uint32_t
292 aesni_gcm_pmd_qp_count(struct rte_cryptodev *dev)
293 {
294         return dev->data->nb_queue_pairs;
295 }
296
297 /** Returns the size of the aesni gcm session structure */
298 static unsigned
299 aesni_gcm_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
300 {
301         return sizeof(struct aesni_gcm_session);
302 }
303
304 /** Configure a aesni gcm session from a crypto xform chain */
305 static int
306 aesni_gcm_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
307                 struct rte_crypto_sym_xform *xform,
308                 struct rte_cryptodev_sym_session *sess,
309                 struct rte_mempool *mempool)
310 {
311         void *sess_private_data;
312         struct aesni_gcm_private *internals = dev->data->dev_private;
313
314         if (unlikely(sess == NULL)) {
315                 GCM_LOG_ERR("invalid session struct");
316                 return -1;
317         }
318
319         if (rte_mempool_get(mempool, &sess_private_data)) {
320                 CDEV_LOG_ERR(
321                         "Couldn't get object from session mempool");
322                 return -1;
323         }
324         if (aesni_gcm_set_session_parameters(gcm_ops[internals->vector_mode],
325                                 sess_private_data, xform) != 0) {
326                 GCM_LOG_ERR("failed configure session parameters");
327
328                 /* Return session to mempool */
329                 rte_mempool_put(mempool, sess_private_data);
330                 return -1;
331         }
332
333         set_session_private_data(sess, dev->driver_id,
334                         sess_private_data);
335
336         return 0;
337 }
338
339 /** Clear the memory of session so it doesn't leave key material behind */
340 static void
341 aesni_gcm_pmd_session_clear(struct rte_cryptodev *dev,
342                 struct rte_cryptodev_sym_session *sess)
343 {
344         uint8_t index = dev->driver_id;
345         void *sess_priv = get_session_private_data(sess, index);
346
347         /* Zero out the whole structure */
348         if (sess_priv) {
349                 memset(sess_priv, 0, sizeof(struct aesni_gcm_session));
350                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
351                 set_session_private_data(sess, index, NULL);
352                 rte_mempool_put(sess_mp, sess_priv);
353         }
354 }
355
356 struct rte_cryptodev_ops aesni_gcm_pmd_ops = {
357                 .dev_configure          = aesni_gcm_pmd_config,
358                 .dev_start              = aesni_gcm_pmd_start,
359                 .dev_stop               = aesni_gcm_pmd_stop,
360                 .dev_close              = aesni_gcm_pmd_close,
361
362                 .stats_get              = aesni_gcm_pmd_stats_get,
363                 .stats_reset            = aesni_gcm_pmd_stats_reset,
364
365                 .dev_infos_get          = aesni_gcm_pmd_info_get,
366
367                 .queue_pair_setup       = aesni_gcm_pmd_qp_setup,
368                 .queue_pair_release     = aesni_gcm_pmd_qp_release,
369                 .queue_pair_start       = aesni_gcm_pmd_qp_start,
370                 .queue_pair_stop        = aesni_gcm_pmd_qp_stop,
371                 .queue_pair_count       = aesni_gcm_pmd_qp_count,
372
373                 .session_get_size       = aesni_gcm_pmd_session_get_size,
374                 .session_configure      = aesni_gcm_pmd_session_configure,
375                 .session_clear          = aesni_gcm_pmd_session_clear
376 };
377
378 struct rte_cryptodev_ops *rte_aesni_gcm_pmd_ops = &aesni_gcm_pmd_ops;