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