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