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