4 * Copyright(c) 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.
34 #include <rte_common.h>
35 #include <rte_malloc.h>
37 #include <rte_cryptodev.h>
38 #include <rte_cryptodev_pmd.h>
39 #include <rte_reorder.h>
41 #include "scheduler_pmd_private.h"
43 /** attaching the slaves predefined by scheduler's EAL options */
45 scheduler_attach_init_slave(struct rte_cryptodev *dev)
47 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
48 uint8_t scheduler_id = dev->data->dev_id;
51 for (i = sched_ctx->nb_init_slaves - 1; i >= 0; i--) {
52 const char *dev_name = sched_ctx->init_slave_names[i];
53 struct rte_cryptodev *slave_dev =
54 rte_cryptodev_pmd_get_named_dev(dev_name);
58 CS_LOG_ERR("Failed to locate slave dev %s",
63 status = rte_cryptodev_scheduler_slave_attach(
64 scheduler_id, slave_dev->data->dev_id);
67 CS_LOG_ERR("Failed to attach slave cryptodev %u",
68 slave_dev->data->dev_id);
72 CS_LOG_INFO("Scheduler %s attached slave %s\n",
74 sched_ctx->init_slave_names[i]);
76 rte_free(sched_ctx->init_slave_names[i]);
78 sched_ctx->nb_init_slaves -= 1;
83 /** Configure device */
85 scheduler_pmd_config(struct rte_cryptodev *dev,
86 struct rte_cryptodev_config *config)
88 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
92 /* although scheduler_attach_init_slave presents multiple times,
93 * there will be only 1 meaningful execution.
95 ret = scheduler_attach_init_slave(dev);
99 for (i = 0; i < sched_ctx->nb_slaves; i++) {
100 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
102 ret = rte_cryptodev_configure(slave_dev_id, config);
111 update_order_ring(struct rte_cryptodev *dev, uint16_t qp_id)
113 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
114 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
116 if (sched_ctx->reordering_enabled) {
117 char order_ring_name[RTE_CRYPTODEV_NAME_MAX_LEN];
118 uint32_t buff_size = rte_align32pow2(
119 sched_ctx->nb_slaves * PER_SLAVE_BUFF_SIZE);
121 if (qp_ctx->order_ring) {
122 rte_ring_free(qp_ctx->order_ring);
123 qp_ctx->order_ring = NULL;
129 if (snprintf(order_ring_name, RTE_CRYPTODEV_NAME_MAX_LEN,
130 "%s_rb_%u_%u", RTE_STR(CRYPTODEV_NAME_SCHEDULER_PMD),
131 dev->data->dev_id, qp_id) < 0) {
132 CS_LOG_ERR("failed to create unique reorder buffer "
137 qp_ctx->order_ring = rte_ring_create(order_ring_name,
138 buff_size, rte_socket_id(),
139 RING_F_SP_ENQ | RING_F_SC_DEQ);
140 if (!qp_ctx->order_ring) {
141 CS_LOG_ERR("failed to create order ring");
145 if (qp_ctx->order_ring) {
146 rte_ring_free(qp_ctx->order_ring);
147 qp_ctx->order_ring = NULL;
156 scheduler_pmd_start(struct rte_cryptodev *dev)
158 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
162 if (dev->data->dev_started)
165 /* although scheduler_attach_init_slave presents multiple times,
166 * there will be only 1 meaningful execution.
168 ret = scheduler_attach_init_slave(dev);
172 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
173 ret = update_order_ring(dev, i);
175 CS_LOG_ERR("Failed to update reorder buffer");
180 if (sched_ctx->mode == CDEV_SCHED_MODE_NOT_SET) {
181 CS_LOG_ERR("Scheduler mode is not set");
185 if (!sched_ctx->nb_slaves) {
186 CS_LOG_ERR("No slave in the scheduler");
190 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.slave_attach, -ENOTSUP);
192 for (i = 0; i < sched_ctx->nb_slaves; i++) {
193 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
195 if ((*sched_ctx->ops.slave_attach)(dev, slave_dev_id) < 0) {
196 CS_LOG_ERR("Failed to attach slave");
201 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.scheduler_start, -ENOTSUP);
203 if ((*sched_ctx->ops.scheduler_start)(dev) < 0) {
204 CS_LOG_ERR("Scheduler start failed");
208 /* start all slaves */
209 for (i = 0; i < sched_ctx->nb_slaves; i++) {
210 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
211 struct rte_cryptodev *slave_dev =
212 rte_cryptodev_pmd_get_dev(slave_dev_id);
214 ret = (*slave_dev->dev_ops->dev_start)(slave_dev);
216 CS_LOG_ERR("Failed to start slave dev %u",
227 scheduler_pmd_stop(struct rte_cryptodev *dev)
229 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
232 if (!dev->data->dev_started)
235 /* stop all slaves first */
236 for (i = 0; i < sched_ctx->nb_slaves; i++) {
237 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
238 struct rte_cryptodev *slave_dev =
239 rte_cryptodev_pmd_get_dev(slave_dev_id);
241 (*slave_dev->dev_ops->dev_stop)(slave_dev);
244 if (*sched_ctx->ops.scheduler_stop)
245 (*sched_ctx->ops.scheduler_stop)(dev);
247 for (i = 0; i < sched_ctx->nb_slaves; i++) {
248 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
250 if (*sched_ctx->ops.slave_detach)
251 (*sched_ctx->ops.slave_detach)(dev, slave_dev_id);
257 scheduler_pmd_close(struct rte_cryptodev *dev)
259 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
263 /* the dev should be stopped before being closed */
264 if (dev->data->dev_started)
267 /* close all slaves first */
268 for (i = 0; i < sched_ctx->nb_slaves; i++) {
269 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
270 struct rte_cryptodev *slave_dev =
271 rte_cryptodev_pmd_get_dev(slave_dev_id);
273 ret = (*slave_dev->dev_ops->dev_close)(slave_dev);
278 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
279 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
281 if (qp_ctx->order_ring) {
282 rte_ring_free(qp_ctx->order_ring);
283 qp_ctx->order_ring = NULL;
286 if (qp_ctx->private_qp_ctx) {
287 rte_free(qp_ctx->private_qp_ctx);
288 qp_ctx->private_qp_ctx = NULL;
292 if (sched_ctx->private_ctx)
293 rte_free(sched_ctx->private_ctx);
295 if (sched_ctx->capabilities)
296 rte_free(sched_ctx->capabilities);
301 /** Get device statistics */
303 scheduler_pmd_stats_get(struct rte_cryptodev *dev,
304 struct rte_cryptodev_stats *stats)
306 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
309 for (i = 0; i < sched_ctx->nb_slaves; i++) {
310 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
311 struct rte_cryptodev *slave_dev =
312 rte_cryptodev_pmd_get_dev(slave_dev_id);
313 struct rte_cryptodev_stats slave_stats = {0};
315 (*slave_dev->dev_ops->stats_get)(slave_dev, &slave_stats);
317 stats->enqueued_count += slave_stats.enqueued_count;
318 stats->dequeued_count += slave_stats.dequeued_count;
320 stats->enqueue_err_count += slave_stats.enqueue_err_count;
321 stats->dequeue_err_count += slave_stats.dequeue_err_count;
325 /** Reset device statistics */
327 scheduler_pmd_stats_reset(struct rte_cryptodev *dev)
329 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
332 for (i = 0; i < sched_ctx->nb_slaves; i++) {
333 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
334 struct rte_cryptodev *slave_dev =
335 rte_cryptodev_pmd_get_dev(slave_dev_id);
337 (*slave_dev->dev_ops->stats_reset)(slave_dev);
341 /** Get device info */
343 scheduler_pmd_info_get(struct rte_cryptodev *dev,
344 struct rte_cryptodev_info *dev_info)
346 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
347 uint32_t max_nb_sessions = sched_ctx->nb_slaves ?
348 UINT32_MAX : RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS;
354 /* although scheduler_attach_init_slave presents multiple times,
355 * there will be only 1 meaningful execution.
357 scheduler_attach_init_slave(dev);
359 for (i = 0; i < sched_ctx->nb_slaves; i++) {
360 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
361 struct rte_cryptodev_info slave_info;
363 rte_cryptodev_info_get(slave_dev_id, &slave_info);
364 max_nb_sessions = slave_info.sym.max_nb_sessions <
366 slave_info.sym.max_nb_sessions :
370 dev_info->driver_id = dev->driver_id;
371 dev_info->feature_flags = dev->feature_flags;
372 dev_info->capabilities = sched_ctx->capabilities;
373 dev_info->max_nb_queue_pairs = sched_ctx->max_nb_queue_pairs;
374 dev_info->sym.max_nb_sessions = max_nb_sessions;
377 /** Release queue pair */
379 scheduler_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
381 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
386 if (qp_ctx->order_ring)
387 rte_ring_free(qp_ctx->order_ring);
388 if (qp_ctx->private_qp_ctx)
389 rte_free(qp_ctx->private_qp_ctx);
392 dev->data->queue_pairs[qp_id] = NULL;
397 /** Setup a queue pair */
399 scheduler_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
400 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id,
401 struct rte_mempool *session_pool)
403 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
404 struct scheduler_qp_ctx *qp_ctx;
405 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
409 if (snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
410 "CRYTO_SCHE PMD %u QP %u",
411 dev->data->dev_id, qp_id) < 0) {
412 CS_LOG_ERR("Failed to create unique queue pair name");
416 /* Free memory prior to re-allocation if needed. */
417 if (dev->data->queue_pairs[qp_id] != NULL)
418 scheduler_pmd_qp_release(dev, qp_id);
420 for (i = 0; i < sched_ctx->nb_slaves; i++) {
421 uint8_t slave_id = sched_ctx->slaves[i].dev_id;
424 * All slaves will share the same session mempool
425 * for session-less operations, so the objects
426 * must be big enough for all the drivers used.
428 ret = rte_cryptodev_queue_pair_setup(slave_id, qp_id,
429 qp_conf, socket_id, session_pool);
434 /* Allocate the queue pair data structure. */
435 qp_ctx = rte_zmalloc_socket(name, sizeof(*qp_ctx), RTE_CACHE_LINE_SIZE,
440 /* The actual available object number = nb_descriptors - 1 */
441 qp_ctx->max_nb_objs = qp_conf->nb_descriptors - 1;
443 dev->data->queue_pairs[qp_id] = qp_ctx;
445 /* although scheduler_attach_init_slave presents multiple times,
446 * there will be only 1 meaningful execution.
448 ret = scheduler_attach_init_slave(dev);
450 CS_LOG_ERR("Failed to attach slave");
451 scheduler_pmd_qp_release(dev, qp_id);
455 if (*sched_ctx->ops.config_queue_pair) {
456 if ((*sched_ctx->ops.config_queue_pair)(dev, qp_id) < 0) {
457 CS_LOG_ERR("Unable to configure queue pair");
465 /** Start queue pair */
467 scheduler_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
468 __rte_unused uint16_t queue_pair_id)
473 /** Stop queue pair */
475 scheduler_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
476 __rte_unused uint16_t queue_pair_id)
481 /** Return the number of allocated queue pairs */
483 scheduler_pmd_qp_count(struct rte_cryptodev *dev)
485 return dev->data->nb_queue_pairs;
489 scheduler_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
491 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
493 uint32_t max_priv_sess_size = 0;
495 /* Check what is the maximum private session size for all slaves */
496 for (i = 0; i < sched_ctx->nb_slaves; i++) {
497 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
498 struct rte_cryptodev *dev = &rte_cryptodevs[slave_dev_id];
499 uint32_t priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
501 if (max_priv_sess_size < priv_sess_size)
502 max_priv_sess_size = priv_sess_size;
505 return max_priv_sess_size;
509 scheduler_pmd_session_configure(struct rte_cryptodev *dev,
510 struct rte_crypto_sym_xform *xform,
511 struct rte_cryptodev_sym_session *sess,
512 struct rte_mempool *mempool)
514 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
518 for (i = 0; i < sched_ctx->nb_slaves; i++) {
519 struct scheduler_slave *slave = &sched_ctx->slaves[i];
521 ret = rte_cryptodev_sym_session_init(slave->dev_id, sess,
524 CS_LOG_ERR("unabled to config sym session");
532 /** Clear the memory of session so it doesn't leave key material behind */
534 scheduler_pmd_session_clear(struct rte_cryptodev *dev,
535 struct rte_cryptodev_sym_session *sess)
537 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
540 /* Clear private data of slaves */
541 for (i = 0; i < sched_ctx->nb_slaves; i++) {
542 struct scheduler_slave *slave = &sched_ctx->slaves[i];
544 rte_cryptodev_sym_session_clear(slave->dev_id, sess);
548 struct rte_cryptodev_ops scheduler_pmd_ops = {
549 .dev_configure = scheduler_pmd_config,
550 .dev_start = scheduler_pmd_start,
551 .dev_stop = scheduler_pmd_stop,
552 .dev_close = scheduler_pmd_close,
554 .stats_get = scheduler_pmd_stats_get,
555 .stats_reset = scheduler_pmd_stats_reset,
557 .dev_infos_get = scheduler_pmd_info_get,
559 .queue_pair_setup = scheduler_pmd_qp_setup,
560 .queue_pair_release = scheduler_pmd_qp_release,
561 .queue_pair_start = scheduler_pmd_qp_start,
562 .queue_pair_stop = scheduler_pmd_qp_stop,
563 .queue_pair_count = scheduler_pmd_qp_count,
565 .session_get_size = scheduler_pmd_session_get_size,
566 .session_configure = scheduler_pmd_session_configure,
567 .session_clear = scheduler_pmd_session_clear,
570 struct rte_cryptodev_ops *rte_crypto_scheduler_pmd_ops = &scheduler_pmd_ops;