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