1fc047bef3ddc0d7c96aa0d1f7149fdf7efd1eb7
[deb_dpdk.git] / drivers / crypto / aesni_gcm / aesni_gcm_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 "aesni_gcm_pmd_private.h"
40
41 static const struct rte_cryptodev_capabilities aesni_gcm_pmd_capabilities[] = {
42         {       /* AES GMAC (AUTH) */
43                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
44                 {.sym = {
45                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
46                         {.auth = {
47                                 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
48                                 .block_size = 16,
49                                 .key_size = {
50                                         .min = 16,
51                                         .max = 32,
52                                         .increment = 16
53                                 },
54                                 .digest_size = {
55                                         .min = 8,
56                                         .max = 16,
57                                         .increment = 4
58                                 },
59                                 .aad_size = {
60                                         .min = 0,
61                                         .max = 65535,
62                                         .increment = 1
63                                 }
64                         }, }
65                 }, }
66         },
67         {       /* AES GCM (AUTH) */
68                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
69                 {.sym = {
70                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
71                         {.auth = {
72                                 .algo = RTE_CRYPTO_AUTH_AES_GCM,
73                                 .block_size = 16,
74                                 .key_size = {
75                                         .min = 16,
76                                         .max = 32,
77                                         .increment = 16
78                                 },
79                                 .digest_size = {
80                                         .min = 8,
81                                         .max = 16,
82                                         .increment = 4
83                                 },
84                                 .aad_size = {
85                                         .min = 0,
86                                         .max = 65535,
87                                         .increment = 1
88                                 }
89                         }, }
90                 }, }
91         },
92         {       /* AES GCM (CIPHER) */
93                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
94                 {.sym = {
95                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
96                         {.cipher = {
97                                 .algo = RTE_CRYPTO_CIPHER_AES_GCM,
98                                 .block_size = 16,
99                                 .key_size = {
100                                         .min = 16,
101                                         .max = 32,
102                                         .increment = 16
103                                 },
104                                 .iv_size = {
105                                         .min = 12,
106                                         .max = 12,
107                                         .increment = 0
108                                 }
109                         }, }
110                 }, }
111         },
112         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
113 };
114
115 /** Configure device */
116 static int
117 aesni_gcm_pmd_config(__rte_unused struct rte_cryptodev *dev,
118                 __rte_unused struct rte_cryptodev_config *config)
119 {
120         return 0;
121 }
122
123 /** Start device */
124 static int
125 aesni_gcm_pmd_start(__rte_unused struct rte_cryptodev *dev)
126 {
127         return 0;
128 }
129
130 /** Stop device */
131 static void
132 aesni_gcm_pmd_stop(__rte_unused struct rte_cryptodev *dev)
133 {
134 }
135
136 /** Close device */
137 static int
138 aesni_gcm_pmd_close(__rte_unused struct rte_cryptodev *dev)
139 {
140         return 0;
141 }
142
143
144 /** Get device statistics */
145 static void
146 aesni_gcm_pmd_stats_get(struct rte_cryptodev *dev,
147                 struct rte_cryptodev_stats *stats)
148 {
149         int qp_id;
150
151         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
152                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
153
154                 stats->enqueued_count += qp->qp_stats.enqueued_count;
155                 stats->dequeued_count += qp->qp_stats.dequeued_count;
156
157                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
158                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
159         }
160 }
161
162 /** Reset device statistics */
163 static void
164 aesni_gcm_pmd_stats_reset(struct rte_cryptodev *dev)
165 {
166         int qp_id;
167
168         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
169                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
170
171                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
172         }
173 }
174
175
176 /** Get device info */
177 static void
178 aesni_gcm_pmd_info_get(struct rte_cryptodev *dev,
179                 struct rte_cryptodev_info *dev_info)
180 {
181         struct aesni_gcm_private *internals = dev->data->dev_private;
182
183         if (dev_info != NULL) {
184                 dev_info->dev_type = dev->dev_type;
185                 dev_info->feature_flags = dev->feature_flags;
186                 dev_info->capabilities = aesni_gcm_pmd_capabilities;
187
188                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
189                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
190         }
191 }
192
193 /** Release queue pair */
194 static int
195 aesni_gcm_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
196 {
197         if (dev->data->queue_pairs[qp_id] != NULL) {
198                 rte_free(dev->data->queue_pairs[qp_id]);
199                 dev->data->queue_pairs[qp_id] = NULL;
200         }
201         return 0;
202 }
203
204 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
205 static int
206 aesni_gcm_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
207                 struct aesni_gcm_qp *qp)
208 {
209         unsigned n = snprintf(qp->name, sizeof(qp->name),
210                         "aesni_gcm_pmd_%u_qp_%u",
211                         dev->data->dev_id, qp->id);
212
213         if (n > sizeof(qp->name))
214                 return -1;
215
216         return 0;
217 }
218
219 /** Create a ring to place process packets on */
220 static struct rte_ring *
221 aesni_gcm_pmd_qp_create_processed_pkts_ring(struct aesni_gcm_qp *qp,
222                 unsigned ring_size, int socket_id)
223 {
224         struct rte_ring *r;
225
226         r = rte_ring_lookup(qp->name);
227         if (r) {
228                 if (rte_ring_get_size(r) >= ring_size) {
229                         GCM_LOG_INFO("Reusing existing ring %s for processed"
230                                         " packets", qp->name);
231                         return r;
232                 }
233
234                 GCM_LOG_ERR("Unable to reuse existing ring %s for processed"
235                                 " packets", qp->name);
236                 return NULL;
237         }
238
239         return rte_ring_create(qp->name, ring_size, socket_id,
240                         RING_F_SP_ENQ | RING_F_SC_DEQ);
241 }
242
243 /** Setup a queue pair */
244 static int
245 aesni_gcm_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
246                 const struct rte_cryptodev_qp_conf *qp_conf,
247                  int socket_id)
248 {
249         struct aesni_gcm_qp *qp = NULL;
250
251         /* Free memory prior to re-allocation if needed. */
252         if (dev->data->queue_pairs[qp_id] != NULL)
253                 aesni_gcm_pmd_qp_release(dev, qp_id);
254
255         /* Allocate the queue pair data structure. */
256         qp = rte_zmalloc_socket("AES-NI PMD Queue Pair", sizeof(*qp),
257                                         RTE_CACHE_LINE_SIZE, socket_id);
258         if (qp == NULL)
259                 return (-ENOMEM);
260
261         qp->id = qp_id;
262         dev->data->queue_pairs[qp_id] = qp;
263
264         if (aesni_gcm_pmd_qp_set_unique_name(dev, qp))
265                 goto qp_setup_cleanup;
266
267         qp->processed_pkts = aesni_gcm_pmd_qp_create_processed_pkts_ring(qp,
268                         qp_conf->nb_descriptors, socket_id);
269         if (qp->processed_pkts == NULL)
270                 goto qp_setup_cleanup;
271
272         qp->sess_mp = dev->data->session_pool;
273
274         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
275
276         return 0;
277
278 qp_setup_cleanup:
279         if (qp)
280                 rte_free(qp);
281
282         return -1;
283 }
284
285 /** Start queue pair */
286 static int
287 aesni_gcm_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
288                 __rte_unused uint16_t queue_pair_id)
289 {
290         return -ENOTSUP;
291 }
292
293 /** Stop queue pair */
294 static int
295 aesni_gcm_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
296                 __rte_unused uint16_t queue_pair_id)
297 {
298         return -ENOTSUP;
299 }
300
301 /** Return the number of allocated queue pairs */
302 static uint32_t
303 aesni_gcm_pmd_qp_count(struct rte_cryptodev *dev)
304 {
305         return dev->data->nb_queue_pairs;
306 }
307
308 /** Returns the size of the aesni gcm session structure */
309 static unsigned
310 aesni_gcm_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
311 {
312         return sizeof(struct aesni_gcm_session);
313 }
314
315 /** Configure a aesni gcm session from a crypto xform chain */
316 static void *
317 aesni_gcm_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
318                 struct rte_crypto_sym_xform *xform,     void *sess)
319 {
320         if (unlikely(sess == NULL)) {
321                 GCM_LOG_ERR("invalid session struct");
322                 return NULL;
323         }
324
325         if (aesni_gcm_set_session_parameters(sess, xform) != 0) {
326                 GCM_LOG_ERR("failed configure session parameters");
327                 return NULL;
328         }
329
330         return sess;
331 }
332
333 /** Clear the memory of session so it doesn't leave key material behind */
334 static void
335 aesni_gcm_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
336 {
337         if (sess)
338                 memset(sess, 0, sizeof(struct aesni_gcm_session));
339 }
340
341 struct rte_cryptodev_ops aesni_gcm_pmd_ops = {
342                 .dev_configure          = aesni_gcm_pmd_config,
343                 .dev_start              = aesni_gcm_pmd_start,
344                 .dev_stop               = aesni_gcm_pmd_stop,
345                 .dev_close              = aesni_gcm_pmd_close,
346
347                 .stats_get              = aesni_gcm_pmd_stats_get,
348                 .stats_reset            = aesni_gcm_pmd_stats_reset,
349
350                 .dev_infos_get          = aesni_gcm_pmd_info_get,
351
352                 .queue_pair_setup       = aesni_gcm_pmd_qp_setup,
353                 .queue_pair_release     = aesni_gcm_pmd_qp_release,
354                 .queue_pair_start       = aesni_gcm_pmd_qp_start,
355                 .queue_pair_stop        = aesni_gcm_pmd_qp_stop,
356                 .queue_pair_count       = aesni_gcm_pmd_qp_count,
357
358                 .session_get_size       = aesni_gcm_pmd_session_get_size,
359                 .session_configure      = aesni_gcm_pmd_session_configure,
360                 .session_clear          = aesni_gcm_pmd_session_clear
361 };
362
363 struct rte_cryptodev_ops *rte_aesni_gcm_pmd_ops = &aesni_gcm_pmd_ops;