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