4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_cryptodev_pmd.h>
39 #include "rte_zuc_pmd_private.h"
41 static const struct rte_cryptodev_capabilities zuc_pmd_capabilities[] = {
43 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
45 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
47 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
68 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
70 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
72 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
87 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
90 /** Configure device */
92 zuc_pmd_config(__rte_unused struct rte_cryptodev *dev,
93 __rte_unused struct rte_cryptodev_config *config)
100 zuc_pmd_start(__rte_unused struct rte_cryptodev *dev)
107 zuc_pmd_stop(__rte_unused struct rte_cryptodev *dev)
113 zuc_pmd_close(__rte_unused struct rte_cryptodev *dev)
119 /** Get device statistics */
121 zuc_pmd_stats_get(struct rte_cryptodev *dev,
122 struct rte_cryptodev_stats *stats)
126 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
127 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
129 stats->enqueued_count += qp->qp_stats.enqueued_count;
130 stats->dequeued_count += qp->qp_stats.dequeued_count;
132 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
133 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
137 /** Reset device statistics */
139 zuc_pmd_stats_reset(struct rte_cryptodev *dev)
143 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
144 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
146 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
151 /** Get device info */
153 zuc_pmd_info_get(struct rte_cryptodev *dev,
154 struct rte_cryptodev_info *dev_info)
156 struct zuc_private *internals = dev->data->dev_private;
158 if (dev_info != NULL) {
159 dev_info->driver_id = dev->driver_id;
160 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
161 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
162 dev_info->feature_flags = dev->feature_flags;
163 dev_info->capabilities = zuc_pmd_capabilities;
167 /** Release queue pair */
169 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
171 if (dev->data->queue_pairs[qp_id] != NULL) {
172 rte_free(dev->data->queue_pairs[qp_id]);
173 dev->data->queue_pairs[qp_id] = NULL;
178 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
180 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
183 unsigned n = snprintf(qp->name, sizeof(qp->name),
185 dev->data->dev_id, qp->id);
187 if (n >= sizeof(qp->name))
193 /** Create a ring to place processed ops on */
194 static struct rte_ring *
195 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
196 unsigned ring_size, int socket_id)
200 r = rte_ring_lookup(qp->name);
202 if (rte_ring_get_size(r) >= ring_size) {
203 ZUC_LOG_INFO("Reusing existing ring %s"
204 " for processed packets",
209 ZUC_LOG_ERR("Unable to reuse existing ring %s"
210 " for processed packets",
215 return rte_ring_create(qp->name, ring_size, socket_id,
216 RING_F_SP_ENQ | RING_F_SC_DEQ);
219 /** Setup a queue pair */
221 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
222 const struct rte_cryptodev_qp_conf *qp_conf,
223 int socket_id, struct rte_mempool *session_pool)
225 struct zuc_qp *qp = NULL;
227 /* Free memory prior to re-allocation if needed. */
228 if (dev->data->queue_pairs[qp_id] != NULL)
229 zuc_pmd_qp_release(dev, qp_id);
231 /* Allocate the queue pair data structure. */
232 qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
233 RTE_CACHE_LINE_SIZE, socket_id);
238 dev->data->queue_pairs[qp_id] = qp;
240 if (zuc_pmd_qp_set_unique_name(dev, qp))
241 goto qp_setup_cleanup;
243 qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
244 qp_conf->nb_descriptors, socket_id);
245 if (qp->processed_ops == NULL)
246 goto qp_setup_cleanup;
248 qp->sess_mp = session_pool;
250 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
261 /** Start queue pair */
263 zuc_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
264 __rte_unused uint16_t queue_pair_id)
269 /** Stop queue pair */
271 zuc_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
272 __rte_unused uint16_t queue_pair_id)
277 /** Return the number of allocated queue pairs */
279 zuc_pmd_qp_count(struct rte_cryptodev *dev)
281 return dev->data->nb_queue_pairs;
284 /** Returns the size of the ZUC session structure */
286 zuc_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
288 return sizeof(struct zuc_session);
291 /** Configure a ZUC session from a crypto xform chain */
293 zuc_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
294 struct rte_crypto_sym_xform *xform,
295 struct rte_cryptodev_sym_session *sess,
296 struct rte_mempool *mempool)
298 void *sess_private_data;
301 if (unlikely(sess == NULL)) {
302 ZUC_LOG_ERR("invalid session struct");
306 if (rte_mempool_get(mempool, &sess_private_data)) {
308 "Couldn't get object from session mempool");
312 ret = zuc_set_session_parameters(sess_private_data, xform);
314 ZUC_LOG_ERR("failed configure session parameters");
316 /* Return session to mempool */
317 rte_mempool_put(mempool, sess_private_data);
321 set_session_private_data(sess, dev->driver_id,
327 /** Clear the memory of session so it doesn't leave key material behind */
329 zuc_pmd_session_clear(struct rte_cryptodev *dev,
330 struct rte_cryptodev_sym_session *sess)
332 uint8_t index = dev->driver_id;
333 void *sess_priv = get_session_private_data(sess, index);
335 /* Zero out the whole structure */
337 memset(sess_priv, 0, sizeof(struct zuc_session));
338 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
339 set_session_private_data(sess, index, NULL);
340 rte_mempool_put(sess_mp, sess_priv);
344 struct rte_cryptodev_ops zuc_pmd_ops = {
345 .dev_configure = zuc_pmd_config,
346 .dev_start = zuc_pmd_start,
347 .dev_stop = zuc_pmd_stop,
348 .dev_close = zuc_pmd_close,
350 .stats_get = zuc_pmd_stats_get,
351 .stats_reset = zuc_pmd_stats_reset,
353 .dev_infos_get = zuc_pmd_info_get,
355 .queue_pair_setup = zuc_pmd_qp_setup,
356 .queue_pair_release = zuc_pmd_qp_release,
357 .queue_pair_start = zuc_pmd_qp_start,
358 .queue_pair_stop = zuc_pmd_qp_stop,
359 .queue_pair_count = zuc_pmd_qp_count,
361 .session_get_size = zuc_pmd_session_get_size,
362 .session_configure = zuc_pmd_session_configure,
363 .session_clear = zuc_pmd_session_clear
366 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;