crypto/aesni_gcm: migrate from MB library to ISA-L
[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 {
119         return 0;
120 }
121
122 /** Start device */
123 static int
124 aesni_gcm_pmd_start(__rte_unused struct rte_cryptodev *dev)
125 {
126         return 0;
127 }
128
129 /** Stop device */
130 static void
131 aesni_gcm_pmd_stop(__rte_unused struct rte_cryptodev *dev)
132 {
133 }
134
135 /** Close device */
136 static int
137 aesni_gcm_pmd_close(__rte_unused struct rte_cryptodev *dev)
138 {
139         return 0;
140 }
141
142
143 /** Get device statistics */
144 static void
145 aesni_gcm_pmd_stats_get(struct rte_cryptodev *dev,
146                 struct rte_cryptodev_stats *stats)
147 {
148         int qp_id;
149
150         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
151                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
152
153                 stats->enqueued_count += qp->qp_stats.enqueued_count;
154                 stats->dequeued_count += qp->qp_stats.dequeued_count;
155
156                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
157                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
158         }
159 }
160
161 /** Reset device statistics */
162 static void
163 aesni_gcm_pmd_stats_reset(struct rte_cryptodev *dev)
164 {
165         int qp_id;
166
167         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
168                 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
169
170                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
171         }
172 }
173
174
175 /** Get device info */
176 static void
177 aesni_gcm_pmd_info_get(struct rte_cryptodev *dev,
178                 struct rte_cryptodev_info *dev_info)
179 {
180         struct aesni_gcm_private *internals = dev->data->dev_private;
181
182         if (dev_info != NULL) {
183                 dev_info->dev_type = dev->dev_type;
184                 dev_info->feature_flags = dev->feature_flags;
185                 dev_info->capabilities = aesni_gcm_pmd_capabilities;
186
187                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
188                 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
189         }
190 }
191
192 /** Release queue pair */
193 static int
194 aesni_gcm_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
195 {
196         if (dev->data->queue_pairs[qp_id] != NULL) {
197                 rte_free(dev->data->queue_pairs[qp_id]);
198                 dev->data->queue_pairs[qp_id] = NULL;
199         }
200         return 0;
201 }
202
203 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
204 static int
205 aesni_gcm_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
206                 struct aesni_gcm_qp *qp)
207 {
208         unsigned n = snprintf(qp->name, sizeof(qp->name),
209                         "aesni_gcm_pmd_%u_qp_%u",
210                         dev->data->dev_id, qp->id);
211
212         if (n > sizeof(qp->name))
213                 return -1;
214
215         return 0;
216 }
217
218 /** Create a ring to place process packets on */
219 static struct rte_ring *
220 aesni_gcm_pmd_qp_create_processed_pkts_ring(struct aesni_gcm_qp *qp,
221                 unsigned ring_size, int socket_id)
222 {
223         struct rte_ring *r;
224
225         r = rte_ring_lookup(qp->name);
226         if (r) {
227                 if (r->prod.size >= ring_size) {
228                         GCM_LOG_INFO("Reusing existing ring %s for processed"
229                                         " packets", qp->name);
230                         return r;
231                 }
232
233                 GCM_LOG_ERR("Unable to reuse existing ring %s for processed"
234                                 " packets", qp->name);
235                 return NULL;
236         }
237
238         return rte_ring_create(qp->name, ring_size, socket_id,
239                         RING_F_SP_ENQ | RING_F_SC_DEQ);
240 }
241
242 /** Setup a queue pair */
243 static int
244 aesni_gcm_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
245                 const struct rte_cryptodev_qp_conf *qp_conf,
246                  int socket_id)
247 {
248         struct aesni_gcm_qp *qp = NULL;
249
250         /* Free memory prior to re-allocation if needed. */
251         if (dev->data->queue_pairs[qp_id] != NULL)
252                 aesni_gcm_pmd_qp_release(dev, qp_id);
253
254         /* Allocate the queue pair data structure. */
255         qp = rte_zmalloc_socket("AES-NI PMD Queue Pair", sizeof(*qp),
256                                         RTE_CACHE_LINE_SIZE, socket_id);
257         if (qp == NULL)
258                 return (-ENOMEM);
259
260         qp->id = qp_id;
261         dev->data->queue_pairs[qp_id] = qp;
262
263         if (aesni_gcm_pmd_qp_set_unique_name(dev, qp))
264                 goto qp_setup_cleanup;
265
266         qp->processed_pkts = aesni_gcm_pmd_qp_create_processed_pkts_ring(qp,
267                         qp_conf->nb_descriptors, socket_id);
268         if (qp->processed_pkts == NULL)
269                 goto qp_setup_cleanup;
270
271         qp->sess_mp = dev->data->session_pool;
272
273         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
274
275         return 0;
276
277 qp_setup_cleanup:
278         if (qp)
279                 rte_free(qp);
280
281         return -1;
282 }
283
284 /** Start queue pair */
285 static int
286 aesni_gcm_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
287                 __rte_unused uint16_t queue_pair_id)
288 {
289         return -ENOTSUP;
290 }
291
292 /** Stop queue pair */
293 static int
294 aesni_gcm_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
295                 __rte_unused uint16_t queue_pair_id)
296 {
297         return -ENOTSUP;
298 }
299
300 /** Return the number of allocated queue pairs */
301 static uint32_t
302 aesni_gcm_pmd_qp_count(struct rte_cryptodev *dev)
303 {
304         return dev->data->nb_queue_pairs;
305 }
306
307 /** Returns the size of the aesni gcm session structure */
308 static unsigned
309 aesni_gcm_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
310 {
311         return sizeof(struct aesni_gcm_session);
312 }
313
314 /** Configure a aesni gcm session from a crypto xform chain */
315 static void *
316 aesni_gcm_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
317                 struct rte_crypto_sym_xform *xform,     void *sess)
318 {
319         if (unlikely(sess == NULL)) {
320                 GCM_LOG_ERR("invalid session struct");
321                 return NULL;
322         }
323
324         if (aesni_gcm_set_session_parameters(sess, xform) != 0) {
325                 GCM_LOG_ERR("failed configure session parameters");
326                 return NULL;
327         }
328
329         return sess;
330 }
331
332 /** Clear the memory of session so it doesn't leave key material behind */
333 static void
334 aesni_gcm_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
335 {
336         if (sess)
337                 memset(sess, 0, sizeof(struct aesni_gcm_session));
338 }
339
340 struct rte_cryptodev_ops aesni_gcm_pmd_ops = {
341                 .dev_configure          = aesni_gcm_pmd_config,
342                 .dev_start              = aesni_gcm_pmd_start,
343                 .dev_stop               = aesni_gcm_pmd_stop,
344                 .dev_close              = aesni_gcm_pmd_close,
345
346                 .stats_get              = aesni_gcm_pmd_stats_get,
347                 .stats_reset            = aesni_gcm_pmd_stats_reset,
348
349                 .dev_infos_get          = aesni_gcm_pmd_info_get,
350
351                 .queue_pair_setup       = aesni_gcm_pmd_qp_setup,
352                 .queue_pair_release     = aesni_gcm_pmd_qp_release,
353                 .queue_pair_start       = aesni_gcm_pmd_qp_start,
354                 .queue_pair_stop        = aesni_gcm_pmd_qp_stop,
355                 .queue_pair_count       = aesni_gcm_pmd_qp_count,
356
357                 .session_get_size       = aesni_gcm_pmd_session_get_size,
358                 .session_configure      = aesni_gcm_pmd_session_configure,
359                 .session_clear          = aesni_gcm_pmd_session_clear
360 };
361
362 struct rte_cryptodev_ops *rte_aesni_gcm_pmd_ops = &aesni_gcm_pmd_ops;