remove extra parentheses in return statement
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd_ops.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 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_aesni_mb_pmd_private.h"
40
41 /** Configure device */
42 static int
43 aesni_mb_pmd_config(__rte_unused struct rte_cryptodev *dev)
44 {
45         return 0;
46 }
47
48 /** Start device */
49 static int
50 aesni_mb_pmd_start(__rte_unused struct rte_cryptodev *dev)
51 {
52         return 0;
53 }
54
55 /** Stop device */
56 static void
57 aesni_mb_pmd_stop(__rte_unused struct rte_cryptodev *dev)
58 {
59 }
60
61 /** Close device */
62 static int
63 aesni_mb_pmd_close(__rte_unused struct rte_cryptodev *dev)
64 {
65         return 0;
66 }
67
68
69 /** Get device statistics */
70 static void
71 aesni_mb_pmd_stats_get(struct rte_cryptodev *dev,
72                 struct rte_cryptodev_stats *stats)
73 {
74         int qp_id;
75
76         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
77                 struct aesni_mb_qp *qp = dev->data->queue_pairs[qp_id];
78
79                 stats->enqueued_count += qp->qp_stats.enqueued_count;
80                 stats->dequeued_count += qp->qp_stats.dequeued_count;
81
82                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
83                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
84         }
85 }
86
87 /** Reset device statistics */
88 static void
89 aesni_mb_pmd_stats_reset(struct rte_cryptodev *dev)
90 {
91         int qp_id;
92
93         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
94                 struct aesni_mb_qp *qp = dev->data->queue_pairs[qp_id];
95
96                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
97         }
98 }
99
100
101 /** Get device info */
102 static void
103 aesni_mb_pmd_info_get(struct rte_cryptodev *dev,
104                 struct rte_cryptodev_info *dev_info)
105 {
106         struct aesni_mb_private *internals = dev->data->dev_private;
107
108         if (dev_info != NULL) {
109                 dev_info->dev_type = dev->dev_type;
110                 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
111                 dev_info->max_nb_sessions = internals->max_nb_sessions;
112         }
113 }
114
115 /** Release queue pair */
116 static int
117 aesni_mb_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
118 {
119         if (dev->data->queue_pairs[qp_id] != NULL) {
120                 rte_free(dev->data->queue_pairs[qp_id]);
121                 dev->data->queue_pairs[qp_id] = NULL;
122         }
123         return 0;
124 }
125
126 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
127 static int
128 aesni_mb_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
129                 struct aesni_mb_qp *qp)
130 {
131         unsigned n = snprintf(qp->name, sizeof(qp->name),
132                         "aesni_mb_pmd_%u_qp_%u",
133                         dev->data->dev_id, qp->id);
134
135         if (n > sizeof(qp->name))
136                 return -1;
137
138         return 0;
139 }
140
141 /** Create a ring to place process packets on */
142 static struct rte_ring *
143 aesni_mb_pmd_qp_create_processed_pkts_ring(struct aesni_mb_qp *qp,
144                 unsigned ring_size, int socket_id)
145 {
146         struct rte_ring *r;
147
148         r = rte_ring_lookup(qp->name);
149         if (r) {
150                 if (r->prod.size >= ring_size) {
151                         MB_LOG_INFO("Reusing existing ring %s for processed packets",
152                                          qp->name);
153                         return r;
154                 }
155
156                 MB_LOG_ERR("Unable to reuse existing ring %s for processed packets",
157                                  qp->name);
158                 return NULL;
159         }
160
161         return rte_ring_create(qp->name, ring_size, socket_id,
162                         RING_F_SP_ENQ | RING_F_SC_DEQ);
163 }
164
165 /** Setup a queue pair */
166 static int
167 aesni_mb_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
168                 const struct rte_cryptodev_qp_conf *qp_conf,
169                  int socket_id)
170 {
171         struct aesni_mb_qp *qp = NULL;
172         struct aesni_mb_private *internals = dev->data->dev_private;
173
174         /* Free memory prior to re-allocation if needed. */
175         if (dev->data->queue_pairs[qp_id] != NULL)
176                 aesni_mb_pmd_qp_release(dev, qp_id);
177
178         /* Allocate the queue pair data structure. */
179         qp = rte_zmalloc_socket("AES-NI PMD Queue Pair", sizeof(*qp),
180                                         RTE_CACHE_LINE_SIZE, socket_id);
181         if (qp == NULL)
182                 return -ENOMEM;
183
184         qp->id = qp_id;
185         dev->data->queue_pairs[qp_id] = qp;
186
187         if (aesni_mb_pmd_qp_set_unique_name(dev, qp))
188                 goto qp_setup_cleanup;
189
190         qp->ops = &job_ops[internals->vector_mode];
191
192         qp->processed_pkts = aesni_mb_pmd_qp_create_processed_pkts_ring(qp,
193                         qp_conf->nb_descriptors, socket_id);
194         if (qp->processed_pkts == NULL)
195                 goto qp_setup_cleanup;
196
197         qp->sess_mp = dev->data->session_pool;
198
199         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
200
201         /* Initialise multi-buffer manager */
202         (*qp->ops->job.init_mgr)(&qp->mb_mgr);
203
204         return 0;
205
206 qp_setup_cleanup:
207         if (qp)
208                 rte_free(qp);
209
210         return -1;
211 }
212
213 /** Start queue pair */
214 static int
215 aesni_mb_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
216                 __rte_unused uint16_t queue_pair_id)
217 {
218         return -ENOTSUP;
219 }
220
221 /** Stop queue pair */
222 static int
223 aesni_mb_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
224                 __rte_unused uint16_t queue_pair_id)
225 {
226         return -ENOTSUP;
227 }
228
229 /** Return the number of allocated queue pairs */
230 static uint32_t
231 aesni_mb_pmd_qp_count(struct rte_cryptodev *dev)
232 {
233         return dev->data->nb_queue_pairs;
234 }
235
236 /** Returns the size of the aesni multi-buffer session structure */
237 static unsigned
238 aesni_mb_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
239 {
240         return sizeof(struct aesni_mb_session);
241 }
242
243 /** Configure a aesni multi-buffer session from a crypto xform chain */
244 static void *
245 aesni_mb_pmd_session_configure(struct rte_cryptodev *dev,
246                 struct rte_crypto_xform *xform, void *sess)
247 {
248         struct aesni_mb_private *internals = dev->data->dev_private;
249
250         if (unlikely(sess == NULL)) {
251                 MB_LOG_ERR("invalid session struct");
252                 return NULL;
253         }
254
255         if (aesni_mb_set_session_parameters(&job_ops[internals->vector_mode],
256                         sess, xform) != 0) {
257                 MB_LOG_ERR("failed configure session parameters");
258                 return NULL;
259         }
260
261         return sess;
262 }
263
264 /** Clear the memory of session so it doesn't leave key material behind */
265 static void
266 aesni_mb_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
267 {
268         /*
269          * Current just resetting the whole data structure, need to investigate
270          * whether a more selective reset of key would be more performant
271          */
272         if (sess)
273                 memset(sess, 0, sizeof(struct aesni_mb_session));
274 }
275
276 struct rte_cryptodev_ops aesni_mb_pmd_ops = {
277                 .dev_configure          = aesni_mb_pmd_config,
278                 .dev_start              = aesni_mb_pmd_start,
279                 .dev_stop               = aesni_mb_pmd_stop,
280                 .dev_close              = aesni_mb_pmd_close,
281
282                 .stats_get              = aesni_mb_pmd_stats_get,
283                 .stats_reset            = aesni_mb_pmd_stats_reset,
284
285                 .dev_infos_get          = aesni_mb_pmd_info_get,
286
287                 .queue_pair_setup       = aesni_mb_pmd_qp_setup,
288                 .queue_pair_release     = aesni_mb_pmd_qp_release,
289                 .queue_pair_start       = aesni_mb_pmd_qp_start,
290                 .queue_pair_stop        = aesni_mb_pmd_qp_stop,
291                 .queue_pair_count       = aesni_mb_pmd_qp_count,
292
293                 .session_get_size       = aesni_mb_pmd_session_get_size,
294                 .session_configure      = aesni_mb_pmd_session_configure,
295                 .session_clear          = aesni_mb_pmd_session_clear
296 };
297
298 struct rte_cryptodev_ops *rte_aesni_mb_pmd_ops = &aesni_mb_pmd_ops;