52c6aed8496baf08c5d3b7600eff3aa3adc4b58d
[deb_dpdk.git] / drivers / crypto / zuc / rte_zuc_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_zuc_pmd_private.h"
40
41 static const struct rte_cryptodev_capabilities zuc_pmd_capabilities[] = {
42         {       /* ZUC (EIA3) */
43                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
44                 {.sym = {
45                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
46                         {.auth = {
47                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
48                                 .block_size = 16,
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 = 16,
61                                         .max = 16,
62                                         .increment = 0
63                                 }
64                         }, }
65                 }, }
66         },
67         {       /* ZUC (EEA3) */
68                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
69                 {.sym = {
70                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
71                         {.cipher = {
72                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
73                                 .block_size = 16,
74                                 .key_size = {
75                                         .min = 16,
76                                         .max = 16,
77                                         .increment = 0
78                                 },
79                                 .iv_size = {
80                                         .min = 16,
81                                         .max = 16,
82                                         .increment = 0
83                                 },
84                         }, }
85                 }, }
86         },
87         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
88 };
89
90 /** Configure device */
91 static int
92 zuc_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 zuc_pmd_start(__rte_unused struct rte_cryptodev *dev)
101 {
102         return 0;
103 }
104
105 /** Stop device */
106 static void
107 zuc_pmd_stop(__rte_unused struct rte_cryptodev *dev)
108 {
109 }
110
111 /** Close device */
112 static int
113 zuc_pmd_close(__rte_unused struct rte_cryptodev *dev)
114 {
115         return 0;
116 }
117
118
119 /** Get device statistics */
120 static void
121 zuc_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 zuc_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 zuc_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 zuc_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 zuc_pmd_info_get(struct rte_cryptodev *dev,
154                 struct rte_cryptodev_info *dev_info)
155 {
156         struct zuc_private *internals = dev->data->dev_private;
157
158         if (dev_info != NULL) {
159                 dev_info->driver_id = dev->driver_id;
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 = zuc_pmd_capabilities;
164         }
165 }
166
167 /** Release queue pair */
168 static int
169 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
170 {
171         if (dev->data->queue_pairs[qp_id] != NULL) {
172                 rte_free(dev->data->queue_pairs[qp_id]);
173                 dev->data->queue_pairs[qp_id] = NULL;
174         }
175         return 0;
176 }
177
178 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
179 static int
180 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
181                 struct zuc_qp *qp)
182 {
183         unsigned n = snprintf(qp->name, sizeof(qp->name),
184                         "zuc_pmd_%u_qp_%u",
185                         dev->data->dev_id, qp->id);
186
187         if (n > sizeof(qp->name))
188                 return -1;
189
190         return 0;
191 }
192
193 /** Create a ring to place processed ops on */
194 static struct rte_ring *
195 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
196                 unsigned ring_size, int socket_id)
197 {
198         struct rte_ring *r;
199
200         r = rte_ring_lookup(qp->name);
201         if (r) {
202                 if (rte_ring_get_size(r) >= ring_size) {
203                         ZUC_LOG_INFO("Reusing existing ring %s"
204                                         " for processed packets",
205                                          qp->name);
206                         return r;
207                 }
208
209                 ZUC_LOG_ERR("Unable to reuse existing ring %s"
210                                 " for processed packets",
211                                  qp->name);
212                 return NULL;
213         }
214
215         return rte_ring_create(qp->name, ring_size, socket_id,
216                         RING_F_SP_ENQ | RING_F_SC_DEQ);
217 }
218
219 /** Setup a queue pair */
220 static int
221 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
222                 const struct rte_cryptodev_qp_conf *qp_conf,
223                 int socket_id, struct rte_mempool *session_pool)
224 {
225         struct zuc_qp *qp = NULL;
226
227         /* Free memory prior to re-allocation if needed. */
228         if (dev->data->queue_pairs[qp_id] != NULL)
229                 zuc_pmd_qp_release(dev, qp_id);
230
231         /* Allocate the queue pair data structure. */
232         qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
233                                         RTE_CACHE_LINE_SIZE, socket_id);
234         if (qp == NULL)
235                 return (-ENOMEM);
236
237         qp->id = qp_id;
238         dev->data->queue_pairs[qp_id] = qp;
239
240         if (zuc_pmd_qp_set_unique_name(dev, qp))
241                 goto qp_setup_cleanup;
242
243         qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
244                         qp_conf->nb_descriptors, socket_id);
245         if (qp->processed_ops == NULL)
246                 goto qp_setup_cleanup;
247
248         qp->sess_mp = session_pool;
249
250         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
251
252         return 0;
253
254 qp_setup_cleanup:
255         if (qp)
256                 rte_free(qp);
257
258         return -1;
259 }
260
261 /** Start queue pair */
262 static int
263 zuc_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
264                 __rte_unused uint16_t queue_pair_id)
265 {
266         return -ENOTSUP;
267 }
268
269 /** Stop queue pair */
270 static int
271 zuc_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
272                 __rte_unused uint16_t queue_pair_id)
273 {
274         return -ENOTSUP;
275 }
276
277 /** Return the number of allocated queue pairs */
278 static uint32_t
279 zuc_pmd_qp_count(struct rte_cryptodev *dev)
280 {
281         return dev->data->nb_queue_pairs;
282 }
283
284 /** Returns the size of the ZUC session structure */
285 static unsigned
286 zuc_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
287 {
288         return sizeof(struct zuc_session);
289 }
290
291 /** Configure a ZUC session from a crypto xform chain */
292 static int
293 zuc_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
294                 struct rte_crypto_sym_xform *xform,
295                 struct rte_cryptodev_sym_session *sess,
296                 struct rte_mempool *mempool)
297 {
298         void *sess_private_data;
299         int ret;
300
301         if (unlikely(sess == NULL)) {
302                 ZUC_LOG_ERR("invalid session struct");
303                 return -EINVAL;
304         }
305
306         if (rte_mempool_get(mempool, &sess_private_data)) {
307                 CDEV_LOG_ERR(
308                         "Couldn't get object from session mempool");
309                 return -ENOMEM;
310         }
311
312         ret = zuc_set_session_parameters(sess_private_data, xform);
313         if (ret != 0) {
314                 ZUC_LOG_ERR("failed configure session parameters");
315
316                 /* Return session to mempool */
317                 rte_mempool_put(mempool, sess_private_data);
318                 return ret;
319         }
320
321         set_session_private_data(sess, dev->driver_id,
322                 sess_private_data);
323
324         return 0;
325 }
326
327 /** Clear the memory of session so it doesn't leave key material behind */
328 static void
329 zuc_pmd_session_clear(struct rte_cryptodev *dev,
330                 struct rte_cryptodev_sym_session *sess)
331 {
332         uint8_t index = dev->driver_id;
333         void *sess_priv = get_session_private_data(sess, index);
334
335         /* Zero out the whole structure */
336         if (sess_priv) {
337                 memset(sess_priv, 0, sizeof(struct zuc_session));
338                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
339                 set_session_private_data(sess, index, NULL);
340                 rte_mempool_put(sess_mp, sess_priv);
341         }
342 }
343
344 struct rte_cryptodev_ops zuc_pmd_ops = {
345                 .dev_configure      = zuc_pmd_config,
346                 .dev_start          = zuc_pmd_start,
347                 .dev_stop           = zuc_pmd_stop,
348                 .dev_close          = zuc_pmd_close,
349
350                 .stats_get          = zuc_pmd_stats_get,
351                 .stats_reset        = zuc_pmd_stats_reset,
352
353                 .dev_infos_get      = zuc_pmd_info_get,
354
355                 .queue_pair_setup   = zuc_pmd_qp_setup,
356                 .queue_pair_release = zuc_pmd_qp_release,
357                 .queue_pair_start   = zuc_pmd_qp_start,
358                 .queue_pair_stop    = zuc_pmd_qp_stop,
359                 .queue_pair_count   = zuc_pmd_qp_count,
360
361                 .session_get_size   = zuc_pmd_session_get_size,
362                 .session_configure  = zuc_pmd_session_configure,
363                 .session_clear      = zuc_pmd_session_clear
364 };
365
366 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;