New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / crypto / null / null_crypto_pmd_ops.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 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                                 .iv_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 = { 0 }
76                         }, },
77                 }, }
78         },
79         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
80 };
81
82 /** Configure device */
83 static int
84 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
85                 __rte_unused struct rte_cryptodev_config *config)
86 {
87         return 0;
88 }
89
90 /** Start device */
91 static int
92 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
93 {
94         return 0;
95 }
96
97 /** Stop device */
98 static void
99 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
100 {
101 }
102
103 /** Close device */
104 static int
105 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
106 {
107         return 0;
108 }
109
110 /** Get device statistics */
111 static void
112 null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
113                 struct rte_cryptodev_stats *stats)
114 {
115         int qp_id;
116
117         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
118                 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
119
120                 stats->enqueued_count += qp->qp_stats.enqueued_count;
121                 stats->dequeued_count += qp->qp_stats.dequeued_count;
122
123                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
124                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
125         }
126 }
127
128 /** Reset device statistics */
129 static void
130 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
131 {
132         int qp_id;
133
134         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
135                 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
136
137                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
138         }
139 }
140
141
142 /** Get device info */
143 static void
144 null_crypto_pmd_info_get(struct rte_cryptodev *dev,
145                 struct rte_cryptodev_info *dev_info)
146 {
147         struct null_crypto_private *internals = dev->data->dev_private;
148
149         if (dev_info != NULL) {
150                 dev_info->driver_id = dev->driver_id;
151                 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
152                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
153                 dev_info->feature_flags = dev->feature_flags;
154                 dev_info->capabilities = null_crypto_pmd_capabilities;
155         }
156 }
157
158 /** Release queue pair */
159 static int
160 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
161 {
162         if (dev->data->queue_pairs[qp_id] != NULL) {
163                 rte_free(dev->data->queue_pairs[qp_id]);
164                 dev->data->queue_pairs[qp_id] = NULL;
165         }
166         return 0;
167 }
168
169 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
170 static int
171 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
172                 struct null_crypto_qp *qp)
173 {
174         unsigned n = snprintf(qp->name, sizeof(qp->name),
175                         "null_crypto_pmd_%u_qp_%u",
176                         dev->data->dev_id, qp->id);
177
178         if (n >= sizeof(qp->name))
179                 return -1;
180
181         return 0;
182 }
183
184 /** Create a ring to place process packets on */
185 static struct rte_ring *
186 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
187                 unsigned ring_size, int socket_id)
188 {
189         struct rte_ring *r;
190
191         r = rte_ring_lookup(qp->name);
192         if (r) {
193                 if (rte_ring_get_size(r) >= ring_size) {
194                         NULL_CRYPTO_LOG_INFO(
195                                 "Reusing existing ring %s for processed packets",
196                                 qp->name);
197                         return r;
198                 }
199
200                 NULL_CRYPTO_LOG_INFO(
201                         "Unable to reuse existing ring %s for processed packets",
202                          qp->name);
203                 return NULL;
204         }
205
206         return rte_ring_create(qp->name, ring_size, socket_id,
207                         RING_F_SP_ENQ | RING_F_SC_DEQ);
208 }
209
210 /** Setup a queue pair */
211 static int
212 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
213                 const struct rte_cryptodev_qp_conf *qp_conf,
214                 int socket_id, struct rte_mempool *session_pool)
215 {
216         struct null_crypto_private *internals = dev->data->dev_private;
217         struct null_crypto_qp *qp;
218         int retval;
219
220         if (qp_id >= internals->max_nb_qpairs) {
221                 NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum "
222                                 "number of queue pairs supported (%u).",
223                                 qp_id, internals->max_nb_qpairs);
224                 return (-EINVAL);
225         }
226
227         /* Free memory prior to re-allocation if needed. */
228         if (dev->data->queue_pairs[qp_id] != NULL)
229                 null_crypto_pmd_qp_release(dev, qp_id);
230
231         /* Allocate the queue pair data structure. */
232         qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
233                                         RTE_CACHE_LINE_SIZE, socket_id);
234         if (qp == NULL) {
235                 NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory");
236                 return (-ENOMEM);
237         }
238
239         qp->id = qp_id;
240         dev->data->queue_pairs[qp_id] = qp;
241
242         retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
243         if (retval) {
244                 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
245                                 "crypto device");
246                 goto qp_setup_cleanup;
247         }
248
249         qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
250                         qp_conf->nb_descriptors, socket_id);
251         if (qp->processed_pkts == NULL) {
252                 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
253                                 "crypto device");
254                 goto qp_setup_cleanup;
255         }
256
257         qp->sess_mp = session_pool;
258
259         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
260
261         return 0;
262
263 qp_setup_cleanup:
264         if (qp)
265                 rte_free(qp);
266
267         return -1;
268 }
269
270 /** Start queue pair */
271 static int
272 null_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
273                 __rte_unused uint16_t queue_pair_id)
274 {
275         return -ENOTSUP;
276 }
277
278 /** Stop queue pair */
279 static int
280 null_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
281                 __rte_unused uint16_t queue_pair_id)
282 {
283         return -ENOTSUP;
284 }
285
286 /** Return the number of allocated queue pairs */
287 static uint32_t
288 null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
289 {
290         return dev->data->nb_queue_pairs;
291 }
292
293 /** Returns the size of the NULL crypto session structure */
294 static unsigned
295 null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
296 {
297         return sizeof(struct null_crypto_session);
298 }
299
300 /** Configure a null crypto session from a crypto xform chain */
301 static int
302 null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
303                 struct rte_crypto_sym_xform *xform,
304                 struct rte_cryptodev_sym_session *sess,
305                 struct rte_mempool *mp)
306 {
307         void *sess_private_data;
308         int ret;
309
310         if (unlikely(sess == NULL)) {
311                 NULL_CRYPTO_LOG_ERR("invalid session struct");
312                 return -EINVAL;
313         }
314
315         if (rte_mempool_get(mp, &sess_private_data)) {
316                 CDEV_LOG_ERR(
317                         "Couldn't get object from session mempool");
318                 return -ENOMEM;
319         }
320
321         ret = null_crypto_set_session_parameters(sess_private_data, xform);
322         if (ret != 0) {
323                 NULL_CRYPTO_LOG_ERR("failed configure session parameters");
324
325                 /* Return session to mempool */
326                 rte_mempool_put(mp, sess_private_data);
327                 return ret;
328         }
329
330         set_session_private_data(sess, dev->driver_id,
331                 sess_private_data);
332
333         return 0;
334 }
335
336 /** Clear the memory of session so it doesn't leave key material behind */
337 static void
338 null_crypto_pmd_session_clear(struct rte_cryptodev *dev,
339                 struct rte_cryptodev_sym_session *sess)
340 {
341         uint8_t index = dev->driver_id;
342         void *sess_priv = get_session_private_data(sess, index);
343
344         /* Zero out the whole structure */
345         if (sess_priv) {
346                 memset(sess_priv, 0, sizeof(struct null_crypto_session));
347                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
348                 set_session_private_data(sess, index, NULL);
349                 rte_mempool_put(sess_mp, sess_priv);
350         }
351 }
352
353 struct rte_cryptodev_ops pmd_ops = {
354                 .dev_configure          = null_crypto_pmd_config,
355                 .dev_start              = null_crypto_pmd_start,
356                 .dev_stop               = null_crypto_pmd_stop,
357                 .dev_close              = null_crypto_pmd_close,
358
359                 .stats_get              = null_crypto_pmd_stats_get,
360                 .stats_reset            = null_crypto_pmd_stats_reset,
361
362                 .dev_infos_get          = null_crypto_pmd_info_get,
363
364                 .queue_pair_setup       = null_crypto_pmd_qp_setup,
365                 .queue_pair_release     = null_crypto_pmd_qp_release,
366                 .queue_pair_start       = null_crypto_pmd_qp_start,
367                 .queue_pair_stop        = null_crypto_pmd_qp_stop,
368                 .queue_pair_count       = null_crypto_pmd_qp_count,
369
370                 .session_get_size       = null_crypto_pmd_session_get_size,
371                 .session_configure      = null_crypto_pmd_session_configure,
372                 .session_clear          = null_crypto_pmd_session_clear
373 };
374
375 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;