62ebdbd2d5464dc37922c5c7ced77c04f77e8b1a
[deb_dpdk.git] / drivers / crypto / kasumi / rte_kasumi_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 "rte_kasumi_pmd_private.h"
40
41 static const struct rte_cryptodev_capabilities kasumi_pmd_capabilities[] = {
42         {       /* KASUMI (F9) */
43                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
44                 {.sym = {
45                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
46                         {.auth = {
47                                 .algo = RTE_CRYPTO_AUTH_KASUMI_F9,
48                                 .block_size = 8,
49                                 .key_size = {
50                                         .min = 16,
51                                         .max = 16,
52                                         .increment = 0
53                                 },
54                                 .digest_size = {
55                                         .min = 4,
56                                         .max = 4,
57                                         .increment = 0
58                                 },
59                                 .aad_size = {
60                                         .min = 8,
61                                         .max = 8,
62                                         .increment = 0
63                                 }
64                         }, }
65                 }, }
66         },
67         {       /* KASUMI (F8) */
68                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
69                 {.sym = {
70                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
71                         {.cipher = {
72                                 .algo = RTE_CRYPTO_CIPHER_KASUMI_F8,
73                                 .block_size = 8,
74                                 .key_size = {
75                                         .min = 16,
76                                         .max = 16,
77                                         .increment = 0
78                                 },
79                                 .iv_size = {
80                                         .min = 8,
81                                         .max = 8,
82                                         .increment = 0
83                                 }
84                         }, }
85                 }, }
86         },
87         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
88 };
89
90 /** Configure device */
91 static int
92 kasumi_pmd_config(__rte_unused struct rte_cryptodev *dev,
93                 __rte_unused struct rte_cryptodev_config *config)
94 {
95         return 0;
96 }
97
98 /** Start device */
99 static int
100 kasumi_pmd_start(__rte_unused struct rte_cryptodev *dev)
101 {
102         return 0;
103 }
104
105 /** Stop device */
106 static void
107 kasumi_pmd_stop(__rte_unused struct rte_cryptodev *dev)
108 {
109 }
110
111 /** Close device */
112 static int
113 kasumi_pmd_close(__rte_unused struct rte_cryptodev *dev)
114 {
115         return 0;
116 }
117
118
119 /** Get device statistics */
120 static void
121 kasumi_pmd_stats_get(struct rte_cryptodev *dev,
122                 struct rte_cryptodev_stats *stats)
123 {
124         int qp_id;
125
126         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
127                 struct kasumi_qp *qp = dev->data->queue_pairs[qp_id];
128
129                 stats->enqueued_count += qp->qp_stats.enqueued_count;
130                 stats->dequeued_count += qp->qp_stats.dequeued_count;
131
132                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
133                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
134         }
135 }
136
137 /** Reset device statistics */
138 static void
139 kasumi_pmd_stats_reset(struct rte_cryptodev *dev)
140 {
141         int qp_id;
142
143         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
144                 struct kasumi_qp *qp = dev->data->queue_pairs[qp_id];
145
146                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
147         }
148 }
149
150
151 /** Get device info */
152 static void
153 kasumi_pmd_info_get(struct rte_cryptodev *dev,
154                 struct rte_cryptodev_info *dev_info)
155 {
156         struct kasumi_private *internals = dev->data->dev_private;
157
158         if (dev_info != NULL) {
159                 dev_info->dev_type = dev->dev_type;
160                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
161                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
162                 dev_info->feature_flags = dev->feature_flags;
163                 dev_info->capabilities = kasumi_pmd_capabilities;
164         }
165 }
166
167 /** Release queue pair */
168 static int
169 kasumi_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
170 {
171         struct kasumi_qp *qp = dev->data->queue_pairs[qp_id];
172
173         if (qp != NULL) {
174                 rte_ring_free(qp->processed_ops);
175                 rte_free(qp);
176                 dev->data->queue_pairs[qp_id] = NULL;
177         }
178         return 0;
179 }
180
181 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
182 static int
183 kasumi_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
184                 struct kasumi_qp *qp)
185 {
186         unsigned n = snprintf(qp->name, sizeof(qp->name),
187                         "kasumi_pmd_%u_qp_%u",
188                         dev->data->dev_id, qp->id);
189
190         if (n > sizeof(qp->name))
191                 return -1;
192
193         return 0;
194 }
195
196 /** Create a ring to place processed ops on */
197 static struct rte_ring *
198 kasumi_pmd_qp_create_processed_ops_ring(struct kasumi_qp *qp,
199                 unsigned ring_size, int socket_id)
200 {
201         struct rte_ring *r;
202
203         r = rte_ring_lookup(qp->name);
204         if (r) {
205                 if (rte_ring_get_size(r) == ring_size) {
206                         KASUMI_LOG_INFO("Reusing existing ring %s"
207                                         " for processed packets",
208                                          qp->name);
209                         return r;
210                 }
211
212                 KASUMI_LOG_ERR("Unable to reuse existing ring %s"
213                                 " for processed packets",
214                                  qp->name);
215                 return NULL;
216         }
217
218         return rte_ring_create(qp->name, ring_size, socket_id,
219                         RING_F_SP_ENQ | RING_F_SC_DEQ);
220 }
221
222 /** Setup a queue pair */
223 static int
224 kasumi_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
225                 const struct rte_cryptodev_qp_conf *qp_conf,
226                  int socket_id)
227 {
228         struct kasumi_qp *qp = NULL;
229
230         /* Free memory prior to re-allocation if needed. */
231         if (dev->data->queue_pairs[qp_id] != NULL)
232                 kasumi_pmd_qp_release(dev, qp_id);
233
234         /* Allocate the queue pair data structure. */
235         qp = rte_zmalloc_socket("KASUMI PMD Queue Pair", sizeof(*qp),
236                                         RTE_CACHE_LINE_SIZE, socket_id);
237         if (qp == NULL)
238                 return (-ENOMEM);
239
240         qp->id = qp_id;
241         dev->data->queue_pairs[qp_id] = qp;
242
243         if (kasumi_pmd_qp_set_unique_name(dev, qp))
244                 goto qp_setup_cleanup;
245
246         qp->processed_ops = kasumi_pmd_qp_create_processed_ops_ring(qp,
247                         qp_conf->nb_descriptors, socket_id);
248         if (qp->processed_ops == NULL)
249                 goto qp_setup_cleanup;
250
251         qp->sess_mp = dev->data->session_pool;
252
253         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
254
255         return 0;
256
257 qp_setup_cleanup:
258         rte_free(qp);
259
260         return -1;
261 }
262
263 /** Start queue pair */
264 static int
265 kasumi_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
266                 __rte_unused uint16_t queue_pair_id)
267 {
268         return -ENOTSUP;
269 }
270
271 /** Stop queue pair */
272 static int
273 kasumi_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
274                 __rte_unused uint16_t queue_pair_id)
275 {
276         return -ENOTSUP;
277 }
278
279 /** Return the number of allocated queue pairs */
280 static uint32_t
281 kasumi_pmd_qp_count(struct rte_cryptodev *dev)
282 {
283         return dev->data->nb_queue_pairs;
284 }
285
286 /** Returns the size of the KASUMI session structure */
287 static unsigned
288 kasumi_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
289 {
290         return sizeof(struct kasumi_session);
291 }
292
293 /** Configure a KASUMI session from a crypto xform chain */
294 static void *
295 kasumi_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
296                 struct rte_crypto_sym_xform *xform,     void *sess)
297 {
298         if (unlikely(sess == NULL)) {
299                 KASUMI_LOG_ERR("invalid session struct");
300                 return NULL;
301         }
302
303         if (kasumi_set_session_parameters(sess, xform) != 0) {
304                 KASUMI_LOG_ERR("failed configure session parameters");
305                 return NULL;
306         }
307
308         return sess;
309 }
310
311 /** Clear the memory of session so it doesn't leave key material behind */
312 static void
313 kasumi_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
314 {
315         /*
316          * Current just resetting the whole data structure, need to investigate
317          * whether a more selective reset of key would be more performant
318          */
319         if (sess)
320                 memset(sess, 0, sizeof(struct kasumi_session));
321 }
322
323 struct rte_cryptodev_ops kasumi_pmd_ops = {
324                 .dev_configure      = kasumi_pmd_config,
325                 .dev_start          = kasumi_pmd_start,
326                 .dev_stop           = kasumi_pmd_stop,
327                 .dev_close          = kasumi_pmd_close,
328
329                 .stats_get          = kasumi_pmd_stats_get,
330                 .stats_reset        = kasumi_pmd_stats_reset,
331
332                 .dev_infos_get      = kasumi_pmd_info_get,
333
334                 .queue_pair_setup   = kasumi_pmd_qp_setup,
335                 .queue_pair_release = kasumi_pmd_qp_release,
336                 .queue_pair_start   = kasumi_pmd_qp_start,
337                 .queue_pair_stop    = kasumi_pmd_qp_stop,
338                 .queue_pair_count   = kasumi_pmd_qp_count,
339
340                 .session_get_size   = kasumi_pmd_session_get_size,
341                 .session_configure  = kasumi_pmd_session_configure,
342                 .session_clear      = kasumi_pmd_session_clear
343 };
344
345 struct rte_cryptodev_ops *rte_kasumi_pmd_ops = &kasumi_pmd_ops;