12c946c98910f2bd7be77a59bedf7ef5bb1cc72d
[deb_dpdk.git] / drivers / crypto / null / null_crypto_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 "null_crypto_pmd_private.h"
40
41 static const struct rte_cryptodev_capabilities null_crypto_pmd_capabilities[] = {
42         {       /* NULL (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_NULL,
48                                 .block_size = 1,
49                                 .key_size = {
50                                         .min = 0,
51                                         .max = 0,
52                                         .increment = 0
53                                 },
54                                 .digest_size = {
55                                         .min = 0,
56                                         .max = 0,
57                                         .increment = 0
58                                 },
59                                 .aad_size = { 0 }
60                         }, },
61                 }, },
62         },
63         {       /* NULL (CIPHER) */
64                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
65                 {.sym = {
66                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
67                         {.cipher = {
68                                 .algo = RTE_CRYPTO_CIPHER_NULL,
69                                 .block_size = 1,
70                                 .key_size = {
71                                         .min = 0,
72                                         .max = 0,
73                                         .increment = 0
74                                 },
75                                 .iv_size = {
76                                         .min = 0,
77                                         .max = 0,
78                                         .increment = 0
79                                 }
80                         }, },
81                 }, }
82         },
83         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
84 };
85
86 /** Configure device */
87 static int
88 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
89                 __rte_unused struct rte_cryptodev_config *config)
90 {
91         return 0;
92 }
93
94 /** Start device */
95 static int
96 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
97 {
98         return 0;
99 }
100
101 /** Stop device */
102 static void
103 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
104 {
105 }
106
107 /** Close device */
108 static int
109 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
110 {
111         return 0;
112 }
113
114 /** Get device statistics */
115 static void
116 null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
117                 struct rte_cryptodev_stats *stats)
118 {
119         int qp_id;
120
121         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
122                 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
123
124                 stats->enqueued_count += qp->qp_stats.enqueued_count;
125                 stats->dequeued_count += qp->qp_stats.dequeued_count;
126
127                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
128                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
129         }
130 }
131
132 /** Reset device statistics */
133 static void
134 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
135 {
136         int qp_id;
137
138         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
139                 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
140
141                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
142         }
143 }
144
145
146 /** Get device info */
147 static void
148 null_crypto_pmd_info_get(struct rte_cryptodev *dev,
149                 struct rte_cryptodev_info *dev_info)
150 {
151         struct null_crypto_private *internals = dev->data->dev_private;
152
153         if (dev_info != NULL) {
154                 dev_info->dev_type = dev->dev_type;
155                 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
156                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
157                 dev_info->feature_flags = dev->feature_flags;
158                 dev_info->capabilities = null_crypto_pmd_capabilities;
159         }
160 }
161
162 /** Release queue pair */
163 static int
164 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
165 {
166         if (dev->data->queue_pairs[qp_id] != NULL) {
167                 rte_free(dev->data->queue_pairs[qp_id]);
168                 dev->data->queue_pairs[qp_id] = NULL;
169         }
170         return 0;
171 }
172
173 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
174 static int
175 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
176                 struct null_crypto_qp *qp)
177 {
178         unsigned n = snprintf(qp->name, sizeof(qp->name),
179                         "null_crypto_pmd_%u_qp_%u",
180                         dev->data->dev_id, qp->id);
181
182         if (n > sizeof(qp->name))
183                 return -1;
184
185         return 0;
186 }
187
188 /** Create a ring to place process packets on */
189 static struct rte_ring *
190 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
191                 unsigned ring_size, int socket_id)
192 {
193         struct rte_ring *r;
194
195         r = rte_ring_lookup(qp->name);
196         if (r) {
197                 if (rte_ring_get_size(r) >= ring_size) {
198                         NULL_CRYPTO_LOG_INFO(
199                                 "Reusing existing ring %s for processed packets",
200                                 qp->name);
201                         return r;
202                 }
203
204                 NULL_CRYPTO_LOG_INFO(
205                         "Unable to reuse existing ring %s for processed packets",
206                          qp->name);
207                 return NULL;
208         }
209
210         return rte_ring_create(qp->name, ring_size, socket_id,
211                         RING_F_SP_ENQ | RING_F_SC_DEQ);
212 }
213
214 /** Setup a queue pair */
215 static int
216 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
217                 const struct rte_cryptodev_qp_conf *qp_conf,
218                  int socket_id)
219 {
220         struct null_crypto_private *internals = dev->data->dev_private;
221         struct null_crypto_qp *qp;
222         int retval;
223
224         if (qp_id >= internals->max_nb_qpairs) {
225                 NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum "
226                                 "number of queue pairs supported (%u).",
227                                 qp_id, internals->max_nb_qpairs);
228                 return (-EINVAL);
229         }
230
231         /* Free memory prior to re-allocation if needed. */
232         if (dev->data->queue_pairs[qp_id] != NULL)
233                 null_crypto_pmd_qp_release(dev, qp_id);
234
235         /* Allocate the queue pair data structure. */
236         qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
237                                         RTE_CACHE_LINE_SIZE, socket_id);
238         if (qp == NULL) {
239                 NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory");
240                 return (-ENOMEM);
241         }
242
243         qp->id = qp_id;
244         dev->data->queue_pairs[qp_id] = qp;
245
246         retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
247         if (retval) {
248                 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
249                                 "crypto device");
250                 goto qp_setup_cleanup;
251         }
252
253         qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
254                         qp_conf->nb_descriptors, socket_id);
255         if (qp->processed_pkts == NULL) {
256                 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
257                                 "crypto device");
258                 goto qp_setup_cleanup;
259         }
260
261         qp->sess_mp = dev->data->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 null_crypto_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 null_crypto_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 null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
293 {
294         return dev->data->nb_queue_pairs;
295 }
296
297 /** Returns the size of the NULL crypto session structure */
298 static unsigned
299 null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
300 {
301         return sizeof(struct null_crypto_session);
302 }
303
304 /** Configure a null crypto session from a crypto xform chain */
305 static void *
306 null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
307                 struct rte_crypto_sym_xform *xform, void *sess)
308 {
309         int retval;
310
311         if (unlikely(sess == NULL)) {
312                 NULL_CRYPTO_LOG_ERR("invalid session struct");
313                 return NULL;
314         }
315         retval = null_crypto_set_session_parameters(
316                         (struct null_crypto_session *)sess, xform);
317         if (retval != 0) {
318                 NULL_CRYPTO_LOG_ERR("failed configure session parameters");
319                 return NULL;
320         }
321
322         return sess;
323 }
324
325 /** Clear the memory of session so it doesn't leave key material behind */
326 static void
327 null_crypto_pmd_session_clear(struct rte_cryptodev *dev __rte_unused,
328                 void *sess)
329 {
330         if (sess)
331                 memset(sess, 0, sizeof(struct null_crypto_session));
332 }
333
334 struct rte_cryptodev_ops pmd_ops = {
335                 .dev_configure          = null_crypto_pmd_config,
336                 .dev_start              = null_crypto_pmd_start,
337                 .dev_stop               = null_crypto_pmd_stop,
338                 .dev_close              = null_crypto_pmd_close,
339
340                 .stats_get              = null_crypto_pmd_stats_get,
341                 .stats_reset            = null_crypto_pmd_stats_reset,
342
343                 .dev_infos_get          = null_crypto_pmd_info_get,
344
345                 .queue_pair_setup       = null_crypto_pmd_qp_setup,
346                 .queue_pair_release     = null_crypto_pmd_qp_release,
347                 .queue_pair_start       = null_crypto_pmd_qp_start,
348                 .queue_pair_stop        = null_crypto_pmd_qp_stop,
349                 .queue_pair_count       = null_crypto_pmd_qp_count,
350
351                 .session_get_size       = null_crypto_pmd_session_get_size,
352                 .session_configure      = null_crypto_pmd_session_configure,
353                 .session_clear          = null_crypto_pmd_session_clear
354 };
355
356 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;