net/cxgbe: support updating RSS hash configuration and key
[dpdk.git] / drivers / crypto / zuc / rte_zuc_pmd_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 #include <string.h>
6
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
10
11 #include "rte_zuc_pmd_private.h"
12
13 static const struct rte_cryptodev_capabilities zuc_pmd_capabilities[] = {
14         {       /* ZUC (EIA3) */
15                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
16                 {.sym = {
17                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
18                         {.auth = {
19                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
20                                 .block_size = 16,
21                                 .key_size = {
22                                         .min = 16,
23                                         .max = 16,
24                                         .increment = 0
25                                 },
26                                 .digest_size = {
27                                         .min = 4,
28                                         .max = 4,
29                                         .increment = 0
30                                 },
31                                 .iv_size = {
32                                         .min = 16,
33                                         .max = 16,
34                                         .increment = 0
35                                 }
36                         }, }
37                 }, }
38         },
39         {       /* ZUC (EEA3) */
40                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
41                 {.sym = {
42                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
43                         {.cipher = {
44                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
45                                 .block_size = 16,
46                                 .key_size = {
47                                         .min = 16,
48                                         .max = 16,
49                                         .increment = 0
50                                 },
51                                 .iv_size = {
52                                         .min = 16,
53                                         .max = 16,
54                                         .increment = 0
55                                 },
56                         }, }
57                 }, }
58         },
59         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
60 };
61
62 /** Configure device */
63 static int
64 zuc_pmd_config(__rte_unused struct rte_cryptodev *dev,
65                 __rte_unused struct rte_cryptodev_config *config)
66 {
67         return 0;
68 }
69
70 /** Start device */
71 static int
72 zuc_pmd_start(__rte_unused struct rte_cryptodev *dev)
73 {
74         return 0;
75 }
76
77 /** Stop device */
78 static void
79 zuc_pmd_stop(__rte_unused struct rte_cryptodev *dev)
80 {
81 }
82
83 /** Close device */
84 static int
85 zuc_pmd_close(__rte_unused struct rte_cryptodev *dev)
86 {
87         return 0;
88 }
89
90
91 /** Get device statistics */
92 static void
93 zuc_pmd_stats_get(struct rte_cryptodev *dev,
94                 struct rte_cryptodev_stats *stats)
95 {
96         int qp_id;
97
98         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
99                 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
100
101                 stats->enqueued_count += qp->qp_stats.enqueued_count;
102                 stats->dequeued_count += qp->qp_stats.dequeued_count;
103
104                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
105                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
106         }
107 }
108
109 /** Reset device statistics */
110 static void
111 zuc_pmd_stats_reset(struct rte_cryptodev *dev)
112 {
113         int qp_id;
114
115         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
116                 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
117
118                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
119         }
120 }
121
122
123 /** Get device info */
124 static void
125 zuc_pmd_info_get(struct rte_cryptodev *dev,
126                 struct rte_cryptodev_info *dev_info)
127 {
128         struct zuc_private *internals = dev->data->dev_private;
129
130         if (dev_info != NULL) {
131                 dev_info->driver_id = dev->driver_id;
132                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
133                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
134                 dev_info->feature_flags = dev->feature_flags;
135                 dev_info->capabilities = zuc_pmd_capabilities;
136         }
137 }
138
139 /** Release queue pair */
140 static int
141 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
142 {
143         if (dev->data->queue_pairs[qp_id] != NULL) {
144                 rte_free(dev->data->queue_pairs[qp_id]);
145                 dev->data->queue_pairs[qp_id] = NULL;
146         }
147         return 0;
148 }
149
150 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
151 static int
152 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
153                 struct zuc_qp *qp)
154 {
155         unsigned n = snprintf(qp->name, sizeof(qp->name),
156                         "zuc_pmd_%u_qp_%u",
157                         dev->data->dev_id, qp->id);
158
159         if (n >= sizeof(qp->name))
160                 return -1;
161
162         return 0;
163 }
164
165 /** Create a ring to place processed ops on */
166 static struct rte_ring *
167 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
168                 unsigned ring_size, int socket_id)
169 {
170         struct rte_ring *r;
171
172         r = rte_ring_lookup(qp->name);
173         if (r) {
174                 if (rte_ring_get_size(r) >= ring_size) {
175                         ZUC_LOG_INFO("Reusing existing ring %s"
176                                         " for processed packets",
177                                          qp->name);
178                         return r;
179                 }
180
181                 ZUC_LOG_ERR("Unable to reuse existing ring %s"
182                                 " for processed packets",
183                                  qp->name);
184                 return NULL;
185         }
186
187         return rte_ring_create(qp->name, ring_size, socket_id,
188                         RING_F_SP_ENQ | RING_F_SC_DEQ);
189 }
190
191 /** Setup a queue pair */
192 static int
193 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
194                 const struct rte_cryptodev_qp_conf *qp_conf,
195                 int socket_id, struct rte_mempool *session_pool)
196 {
197         struct zuc_qp *qp = NULL;
198
199         /* Free memory prior to re-allocation if needed. */
200         if (dev->data->queue_pairs[qp_id] != NULL)
201                 zuc_pmd_qp_release(dev, qp_id);
202
203         /* Allocate the queue pair data structure. */
204         qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
205                                         RTE_CACHE_LINE_SIZE, socket_id);
206         if (qp == NULL)
207                 return (-ENOMEM);
208
209         qp->id = qp_id;
210         dev->data->queue_pairs[qp_id] = qp;
211
212         if (zuc_pmd_qp_set_unique_name(dev, qp))
213                 goto qp_setup_cleanup;
214
215         qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
216                         qp_conf->nb_descriptors, socket_id);
217         if (qp->processed_ops == NULL)
218                 goto qp_setup_cleanup;
219
220         qp->sess_mp = session_pool;
221
222         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
223
224         return 0;
225
226 qp_setup_cleanup:
227         if (qp)
228                 rte_free(qp);
229
230         return -1;
231 }
232
233 /** Start queue pair */
234 static int
235 zuc_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
236                 __rte_unused uint16_t queue_pair_id)
237 {
238         return -ENOTSUP;
239 }
240
241 /** Stop queue pair */
242 static int
243 zuc_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
244                 __rte_unused uint16_t queue_pair_id)
245 {
246         return -ENOTSUP;
247 }
248
249 /** Return the number of allocated queue pairs */
250 static uint32_t
251 zuc_pmd_qp_count(struct rte_cryptodev *dev)
252 {
253         return dev->data->nb_queue_pairs;
254 }
255
256 /** Returns the size of the ZUC session structure */
257 static unsigned
258 zuc_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
259 {
260         return sizeof(struct zuc_session);
261 }
262
263 /** Configure a ZUC session from a crypto xform chain */
264 static int
265 zuc_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
266                 struct rte_crypto_sym_xform *xform,
267                 struct rte_cryptodev_sym_session *sess,
268                 struct rte_mempool *mempool)
269 {
270         void *sess_private_data;
271         int ret;
272
273         if (unlikely(sess == NULL)) {
274                 ZUC_LOG_ERR("invalid session struct");
275                 return -EINVAL;
276         }
277
278         if (rte_mempool_get(mempool, &sess_private_data)) {
279                 CDEV_LOG_ERR(
280                         "Couldn't get object from session mempool");
281                 return -ENOMEM;
282         }
283
284         ret = zuc_set_session_parameters(sess_private_data, xform);
285         if (ret != 0) {
286                 ZUC_LOG_ERR("failed configure session parameters");
287
288                 /* Return session to mempool */
289                 rte_mempool_put(mempool, sess_private_data);
290                 return ret;
291         }
292
293         set_session_private_data(sess, dev->driver_id,
294                 sess_private_data);
295
296         return 0;
297 }
298
299 /** Clear the memory of session so it doesn't leave key material behind */
300 static void
301 zuc_pmd_session_clear(struct rte_cryptodev *dev,
302                 struct rte_cryptodev_sym_session *sess)
303 {
304         uint8_t index = dev->driver_id;
305         void *sess_priv = get_session_private_data(sess, index);
306
307         /* Zero out the whole structure */
308         if (sess_priv) {
309                 memset(sess_priv, 0, sizeof(struct zuc_session));
310                 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
311                 set_session_private_data(sess, index, NULL);
312                 rte_mempool_put(sess_mp, sess_priv);
313         }
314 }
315
316 struct rte_cryptodev_ops zuc_pmd_ops = {
317                 .dev_configure      = zuc_pmd_config,
318                 .dev_start          = zuc_pmd_start,
319                 .dev_stop           = zuc_pmd_stop,
320                 .dev_close          = zuc_pmd_close,
321
322                 .stats_get          = zuc_pmd_stats_get,
323                 .stats_reset        = zuc_pmd_stats_reset,
324
325                 .dev_infos_get      = zuc_pmd_info_get,
326
327                 .queue_pair_setup   = zuc_pmd_qp_setup,
328                 .queue_pair_release = zuc_pmd_qp_release,
329                 .queue_pair_start   = zuc_pmd_qp_start,
330                 .queue_pair_stop    = zuc_pmd_qp_stop,
331                 .queue_pair_count   = zuc_pmd_qp_count,
332
333                 .session_get_size   = zuc_pmd_session_get_size,
334                 .session_configure  = zuc_pmd_session_configure,
335                 .session_clear      = zuc_pmd_session_clear
336 };
337
338 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;