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_config.h>
35 #include <rte_common.h>
36 #include <rte_malloc.h>
38 #include <rte_cryptodev.h>
39 #include <rte_cryptodev_pmd.h>
40 #include <rte_cryptodev_vdev.h>
41 #include <rte_reorder.h>
43 #include "scheduler_pmd_private.h"
45 /** attaching the slaves predefined by scheduler's EAL options */
47 scheduler_attach_init_slave(struct rte_cryptodev *dev)
49 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
50 uint8_t scheduler_id = dev->data->dev_id;
53 for (i = sched_ctx->nb_init_slaves - 1; i >= 0; i--) {
54 const char *dev_name = sched_ctx->init_slave_names[i];
55 struct rte_cryptodev *slave_dev =
56 rte_cryptodev_pmd_get_named_dev(dev_name);
60 CS_LOG_ERR("Failed to locate slave dev %s",
65 status = rte_cryptodev_scheduler_slave_attach(
66 scheduler_id, slave_dev->data->dev_id);
69 CS_LOG_ERR("Failed to attach slave cryptodev %u",
70 slave_dev->data->dev_id);
74 CS_LOG_INFO("Scheduler %s attached slave %s\n",
76 sched_ctx->init_slave_names[i]);
78 rte_free(sched_ctx->init_slave_names[i]);
80 sched_ctx->nb_init_slaves -= 1;
85 /** Configure device */
87 scheduler_pmd_config(struct rte_cryptodev *dev,
88 struct rte_cryptodev_config *config)
90 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
94 /* although scheduler_attach_init_slave presents multiple times,
95 * there will be only 1 meaningful execution.
97 ret = scheduler_attach_init_slave(dev);
101 for (i = 0; i < sched_ctx->nb_slaves; i++) {
102 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
104 ret = rte_cryptodev_configure(slave_dev_id, config);
113 update_order_ring(struct rte_cryptodev *dev, uint16_t qp_id)
115 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
116 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
118 if (sched_ctx->reordering_enabled) {
119 char order_ring_name[RTE_CRYPTODEV_NAME_MAX_LEN];
120 uint32_t buff_size = rte_align32pow2(
121 sched_ctx->nb_slaves * PER_SLAVE_BUFF_SIZE);
123 if (qp_ctx->order_ring) {
124 rte_ring_free(qp_ctx->order_ring);
125 qp_ctx->order_ring = NULL;
131 if (snprintf(order_ring_name, RTE_CRYPTODEV_NAME_MAX_LEN,
132 "%s_rb_%u_%u", RTE_STR(CRYPTODEV_NAME_SCHEDULER_PMD),
133 dev->data->dev_id, qp_id) < 0) {
134 CS_LOG_ERR("failed to create unique reorder buffer "
139 qp_ctx->order_ring = rte_ring_create(order_ring_name,
140 buff_size, rte_socket_id(),
141 RING_F_SP_ENQ | RING_F_SC_DEQ);
142 if (!qp_ctx->order_ring) {
143 CS_LOG_ERR("failed to create order ring");
147 if (qp_ctx->order_ring) {
148 rte_ring_free(qp_ctx->order_ring);
149 qp_ctx->order_ring = NULL;
158 scheduler_pmd_start(struct rte_cryptodev *dev)
160 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
164 if (dev->data->dev_started)
167 /* although scheduler_attach_init_slave presents multiple times,
168 * there will be only 1 meaningful execution.
170 ret = scheduler_attach_init_slave(dev);
174 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
175 ret = update_order_ring(dev, i);
177 CS_LOG_ERR("Failed to update reorder buffer");
182 if (sched_ctx->mode == CDEV_SCHED_MODE_NOT_SET) {
183 CS_LOG_ERR("Scheduler mode is not set");
187 if (!sched_ctx->nb_slaves) {
188 CS_LOG_ERR("No slave in the scheduler");
192 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.slave_attach, -ENOTSUP);
194 for (i = 0; i < sched_ctx->nb_slaves; i++) {
195 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
197 if ((*sched_ctx->ops.slave_attach)(dev, slave_dev_id) < 0) {
198 CS_LOG_ERR("Failed to attach slave");
203 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.scheduler_start, -ENOTSUP);
205 if ((*sched_ctx->ops.scheduler_start)(dev) < 0) {
206 CS_LOG_ERR("Scheduler start failed");
210 /* start all slaves */
211 for (i = 0; i < sched_ctx->nb_slaves; i++) {
212 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
213 struct rte_cryptodev *slave_dev =
214 rte_cryptodev_pmd_get_dev(slave_dev_id);
216 ret = (*slave_dev->dev_ops->dev_start)(slave_dev);
218 CS_LOG_ERR("Failed to start slave dev %u",
229 scheduler_pmd_stop(struct rte_cryptodev *dev)
231 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
234 if (!dev->data->dev_started)
237 /* stop all slaves first */
238 for (i = 0; i < sched_ctx->nb_slaves; i++) {
239 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
240 struct rte_cryptodev *slave_dev =
241 rte_cryptodev_pmd_get_dev(slave_dev_id);
243 (*slave_dev->dev_ops->dev_stop)(slave_dev);
246 if (*sched_ctx->ops.scheduler_stop)
247 (*sched_ctx->ops.scheduler_stop)(dev);
249 for (i = 0; i < sched_ctx->nb_slaves; i++) {
250 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
252 if (*sched_ctx->ops.slave_detach)
253 (*sched_ctx->ops.slave_detach)(dev, slave_dev_id);
259 scheduler_pmd_close(struct rte_cryptodev *dev)
261 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
265 /* the dev should be stopped before being closed */
266 if (dev->data->dev_started)
269 /* close all slaves first */
270 for (i = 0; i < sched_ctx->nb_slaves; i++) {
271 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
272 struct rte_cryptodev *slave_dev =
273 rte_cryptodev_pmd_get_dev(slave_dev_id);
275 ret = (*slave_dev->dev_ops->dev_close)(slave_dev);
280 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
281 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
283 if (qp_ctx->order_ring) {
284 rte_ring_free(qp_ctx->order_ring);
285 qp_ctx->order_ring = NULL;
288 if (qp_ctx->private_qp_ctx) {
289 rte_free(qp_ctx->private_qp_ctx);
290 qp_ctx->private_qp_ctx = NULL;
294 if (sched_ctx->private_ctx)
295 rte_free(sched_ctx->private_ctx);
297 if (sched_ctx->capabilities)
298 rte_free(sched_ctx->capabilities);
303 /** Get device statistics */
305 scheduler_pmd_stats_get(struct rte_cryptodev *dev,
306 struct rte_cryptodev_stats *stats)
308 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
311 for (i = 0; i < sched_ctx->nb_slaves; i++) {
312 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
313 struct rte_cryptodev *slave_dev =
314 rte_cryptodev_pmd_get_dev(slave_dev_id);
315 struct rte_cryptodev_stats slave_stats = {0};
317 (*slave_dev->dev_ops->stats_get)(slave_dev, &slave_stats);
319 stats->enqueued_count += slave_stats.enqueued_count;
320 stats->dequeued_count += slave_stats.dequeued_count;
322 stats->enqueue_err_count += slave_stats.enqueue_err_count;
323 stats->dequeue_err_count += slave_stats.dequeue_err_count;
327 /** Reset device statistics */
329 scheduler_pmd_stats_reset(struct rte_cryptodev *dev)
331 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
334 for (i = 0; i < sched_ctx->nb_slaves; i++) {
335 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
336 struct rte_cryptodev *slave_dev =
337 rte_cryptodev_pmd_get_dev(slave_dev_id);
339 (*slave_dev->dev_ops->stats_reset)(slave_dev);
343 /** Get device info */
345 scheduler_pmd_info_get(struct rte_cryptodev *dev,
346 struct rte_cryptodev_info *dev_info)
348 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
349 uint32_t max_nb_sessions = sched_ctx->nb_slaves ?
350 UINT32_MAX : RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS;
356 /* although scheduler_attach_init_slave presents multiple times,
357 * there will be only 1 meaningful execution.
359 scheduler_attach_init_slave(dev);
361 for (i = 0; i < sched_ctx->nb_slaves; i++) {
362 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
363 struct rte_cryptodev_info slave_info;
365 rte_cryptodev_info_get(slave_dev_id, &slave_info);
366 max_nb_sessions = slave_info.sym.max_nb_sessions <
368 slave_info.sym.max_nb_sessions :
372 dev_info->driver_id = dev->driver_id;
373 dev_info->feature_flags = dev->feature_flags;
374 dev_info->capabilities = sched_ctx->capabilities;
375 dev_info->max_nb_queue_pairs = sched_ctx->max_nb_queue_pairs;
376 dev_info->sym.max_nb_sessions = max_nb_sessions;
379 /** Release queue pair */
381 scheduler_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
383 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
388 if (qp_ctx->order_ring)
389 rte_ring_free(qp_ctx->order_ring);
390 if (qp_ctx->private_qp_ctx)
391 rte_free(qp_ctx->private_qp_ctx);
394 dev->data->queue_pairs[qp_id] = NULL;
399 /** Setup a queue pair */
401 scheduler_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
402 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id,
403 struct rte_mempool *session_pool)
405 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
406 struct scheduler_qp_ctx *qp_ctx;
407 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
411 if (snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
412 "CRYTO_SCHE PMD %u QP %u",
413 dev->data->dev_id, qp_id) < 0) {
414 CS_LOG_ERR("Failed to create unique queue pair name");
418 /* Free memory prior to re-allocation if needed. */
419 if (dev->data->queue_pairs[qp_id] != NULL)
420 scheduler_pmd_qp_release(dev, qp_id);
422 for (i = 0; i < sched_ctx->nb_slaves; i++) {
423 uint8_t slave_id = sched_ctx->slaves[i].dev_id;
426 * All slaves will share the same session mempool
427 * for session-less operations, so the objects
428 * must be big enough for all the drivers used.
430 ret = rte_cryptodev_queue_pair_setup(slave_id, qp_id,
431 qp_conf, socket_id, session_pool);
436 /* Allocate the queue pair data structure. */
437 qp_ctx = rte_zmalloc_socket(name, sizeof(*qp_ctx), RTE_CACHE_LINE_SIZE,
442 /* The actual available object number = nb_descriptors - 1 */
443 qp_ctx->max_nb_objs = qp_conf->nb_descriptors - 1;
445 dev->data->queue_pairs[qp_id] = qp_ctx;
447 /* although scheduler_attach_init_slave presents multiple times,
448 * there will be only 1 meaningful execution.
450 ret = scheduler_attach_init_slave(dev);
452 CS_LOG_ERR("Failed to attach slave");
453 scheduler_pmd_qp_release(dev, qp_id);
457 if (*sched_ctx->ops.config_queue_pair) {
458 if ((*sched_ctx->ops.config_queue_pair)(dev, qp_id) < 0) {
459 CS_LOG_ERR("Unable to configure queue pair");
467 /** Start queue pair */
469 scheduler_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
470 __rte_unused uint16_t queue_pair_id)
475 /** Stop queue pair */
477 scheduler_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
478 __rte_unused uint16_t queue_pair_id)
483 /** Return the number of allocated queue pairs */
485 scheduler_pmd_qp_count(struct rte_cryptodev *dev)
487 return dev->data->nb_queue_pairs;
491 scheduler_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
493 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
495 uint32_t max_priv_sess_size = 0;
497 /* Check what is the maximum private session size for all slaves */
498 for (i = 0; i < sched_ctx->nb_slaves; i++) {
499 uint8_t slave_dev_id = sched_ctx->slaves[i].dev_id;
500 struct rte_cryptodev *dev = &rte_cryptodevs[slave_dev_id];
501 uint32_t priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
503 if (max_priv_sess_size < priv_sess_size)
504 max_priv_sess_size = priv_sess_size;
507 return max_priv_sess_size;
511 scheduler_pmd_session_configure(struct rte_cryptodev *dev,
512 struct rte_crypto_sym_xform *xform,
513 struct rte_cryptodev_sym_session *sess,
514 struct rte_mempool *mempool)
516 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
519 for (i = 0; i < sched_ctx->nb_slaves; i++) {
520 struct scheduler_slave *slave = &sched_ctx->slaves[i];
522 if (rte_cryptodev_sym_session_init(slave->dev_id, sess,
523 xform, mempool) < 0) {
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;