1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Intel Corporation.
5 #ifndef _CRYPTODEV_PMD_H_
6 #define _CRYPTODEV_PMD_H_
12 * These API are from crypto PMD only and user applications should not call
18 #include <rte_config.h>
20 #include <rte_malloc.h>
22 #include <rte_mempool.h>
24 #include <rte_common.h>
26 #include "rte_crypto.h"
27 #include "rte_cryptodev.h"
30 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS 8
32 #define RTE_CRYPTODEV_PMD_NAME_ARG ("name")
33 #define RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG ("max_nb_queue_pairs")
34 #define RTE_CRYPTODEV_PMD_SOCKET_ID_ARG ("socket_id")
37 static const char * const cryptodev_pmd_valid_params[] = {
38 RTE_CRYPTODEV_PMD_NAME_ARG,
39 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
40 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
46 * Initialisation parameters for crypto devices
48 struct rte_cryptodev_pmd_init_params {
49 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
50 size_t private_data_size;
52 unsigned int max_nb_queue_pairs;
57 * The data part, with no function pointers, associated with each device.
59 * This structure is safe to place in shared memory to be common among
60 * different processes in a multi-process configuration.
62 struct rte_cryptodev_data {
63 /** Device ID for this instance */
65 /** Socket ID where memory is allocated */
67 /** Unique identifier name */
68 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
71 /** Device state: STARTED(1)/STOPPED(0) */
72 uint8_t dev_started : 1;
74 /** Session memory pool */
75 struct rte_mempool *session_pool;
76 /** Array of pointers to queue pairs. */
78 /** Number of device queue pairs. */
79 uint16_t nb_queue_pairs;
81 /** PMD-specific private data */
83 } __rte_cache_aligned;
85 /** @internal The data structure associated with each crypto device. */
86 struct rte_cryptodev {
87 /** Pointer to PMD dequeue function. */
88 dequeue_pkt_burst_t dequeue_burst;
89 /** Pointer to PMD enqueue function. */
90 enqueue_pkt_burst_t enqueue_burst;
92 /** Pointer to device data */
93 struct rte_cryptodev_data *data;
94 /** Functions exported by PMD */
95 struct rte_cryptodev_ops *dev_ops;
96 /** Feature flags exposes HW/SW features for the given device */
97 uint64_t feature_flags;
99 struct rte_device *device;
101 /** Crypto driver identifier*/
104 /** User application callback for interrupts if present */
105 struct rte_cryptodev_cb_list link_intr_cbs;
107 /** Context for security ops */
111 /** Flag indicating the device is attached */
112 uint8_t attached : 1;
114 /** User application callback for pre enqueue processing */
115 struct rte_cryptodev_cb_rcu *enq_cbs;
116 /** User application callback for post dequeue processing */
117 struct rte_cryptodev_cb_rcu *deq_cbs;
118 } __rte_cache_aligned;
120 /** Global structure used for maintaining state of allocated crypto devices */
121 struct rte_cryptodev_global {
122 struct rte_cryptodev *devs; /**< Device information array */
123 struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
124 /**< Device private data */
125 uint8_t nb_devs; /**< Number of devices found */
128 /* Cryptodev driver, containing the driver ID */
129 struct cryptodev_driver {
130 RTE_TAILQ_ENTRY(cryptodev_driver) next; /**< Next in list. */
131 const struct rte_driver *driver;
136 * Get the rte_cryptodev structure device pointer for the device. Assumes a
137 * valid device index.
139 * @param dev_id Device ID value to select the device structure.
142 * - The rte_cryptodev structure pointer for the given device ID.
145 struct rte_cryptodev *
146 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
149 * Get the rte_cryptodev structure device pointer for the named device.
151 * @param name device name to select the device structure.
154 * - The rte_cryptodev structure pointer for the given device ID.
157 struct rte_cryptodev *
158 rte_cryptodev_pmd_get_named_dev(const char *name);
161 * Definitions of all functions exported by a driver through the
162 * the generic structure of type *crypto_dev_ops* supplied in the
163 * *rte_cryptodev* structure associated with a device.
167 * Function used to configure device.
169 * @param dev Crypto device pointer
170 * @param config Crypto device configurations
172 * @return Returns 0 on success
174 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
175 struct rte_cryptodev_config *config);
178 * Function used to start a configured device.
180 * @param dev Crypto device pointer
182 * @return Returns 0 on success
184 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
187 * Function used to stop a configured device.
189 * @param dev Crypto device pointer
191 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
194 * Function used to close a configured device.
196 * @param dev Crypto device pointer
199 * - EAGAIN if can't close as device is busy
201 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
205 * Function used to get statistics of a device.
207 * @param dev Crypto device pointer
208 * @param stats Pointer to crypto device stats structure to populate
210 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
211 struct rte_cryptodev_stats *stats);
215 * Function used to reset statistics of a device.
217 * @param dev Crypto device pointer
219 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
223 * Function used to get specific information of a device.
225 * @param dev Crypto device pointer
226 * @param dev_info Pointer to infos structure to populate
228 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
229 struct rte_cryptodev_info *dev_info);
232 * Setup a queue pair for a device.
234 * @param dev Crypto device pointer
235 * @param qp_id Queue Pair Index
236 * @param qp_conf Queue configuration structure
237 * @param socket_id Socket Index
239 * @return Returns 0 on success.
241 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
242 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
246 * Release memory resources allocated by given queue pair.
248 * @param dev Crypto device pointer
249 * @param qp_id Queue Pair Index
253 * - EAGAIN if can't close as device is busy
255 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
259 * Create a session mempool to allocate sessions from
261 * @param dev Crypto device pointer
262 * @param nb_objs number of sessions objects in mempool
263 * @param obj_cache_size l-core object cache size, see *rte_ring_create*
264 * @param socket_id Socket Id to allocate mempool on.
267 * - On success returns a pointer to a rte_mempool
268 * - On failure returns a NULL pointer
270 typedef int (*cryptodev_sym_create_session_pool_t)(
271 struct rte_cryptodev *dev, unsigned nb_objs,
272 unsigned obj_cache_size, int socket_id);
276 * Get the size of a cryptodev session
278 * @param dev Crypto device pointer
281 * - On success returns the size of the session structure for device
282 * - On failure returns 0
284 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
285 struct rte_cryptodev *dev);
287 * Get the size of a asymmetric cryptodev session
289 * @param dev Crypto device pointer
292 * - On success returns the size of the session structure for device
293 * - On failure returns 0
295 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
296 struct rte_cryptodev *dev);
299 * Configure a Crypto session on a device.
301 * @param dev Crypto device pointer
302 * @param xform Single or chain of crypto xforms
303 * @param session Pointer to cryptodev's private session structure
304 * @param mp Mempool where the private session is allocated
307 * - Returns 0 if private session structure have been created successfully.
308 * - Returns -EINVAL if input parameters are invalid.
309 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
310 * - Returns -ENOMEM if the private session could not be allocated.
312 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
313 struct rte_crypto_sym_xform *xform,
314 struct rte_cryptodev_sym_session *session,
315 struct rte_mempool *mp);
317 * Configure a Crypto asymmetric session on a device.
319 * @param dev Crypto device pointer
320 * @param xform Single or chain of crypto xforms
321 * @param session Pointer to cryptodev's private session structure
322 * @param mp Mempool where the private session is allocated
325 * - Returns 0 if private session structure have been created successfully.
326 * - Returns -EINVAL if input parameters are invalid.
327 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
328 * - Returns -ENOMEM if the private session could not be allocated.
330 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
331 struct rte_crypto_asym_xform *xform,
332 struct rte_cryptodev_asym_session *session,
333 struct rte_mempool *mp);
335 * Free driver private session data.
337 * @param dev Crypto device pointer
338 * @param sess Cryptodev session structure
340 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
341 struct rte_cryptodev_sym_session *sess);
343 * Free asymmetric session private data.
345 * @param dev Crypto device pointer
346 * @param sess Cryptodev session structure
348 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
349 struct rte_cryptodev_asym_session *sess);
351 * Perform actual crypto processing (encrypt/digest or auth/decrypt)
352 * on user provided data.
354 * @param dev Crypto device pointer
355 * @param sess Cryptodev session structure
356 * @param ofs Start and stop offsets for auth and cipher operations
357 * @param vec Vectorized operation descriptor
360 * - Returns number of successfully processed packets.
363 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
364 (struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
365 union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
368 * Typedef that the driver provided to get service context private date size.
370 * @param dev Crypto device pointer.
373 * - On success return the size of the device's service context private data.
374 * - On failure return negative integer.
376 typedef int (*cryptodev_sym_get_raw_dp_ctx_size_t)(struct rte_cryptodev *dev);
379 * Typedef that the driver provided to configure raw data-path context.
381 * @param dev Crypto device pointer.
382 * @param qp_id Crypto device queue pair index.
383 * @param ctx The raw data-path context data.
384 * @param sess_type session type.
385 * @param session_ctx Session context data. If NULL the driver
386 * shall only configure the drv_ctx_data in
387 * ctx buffer. Otherwise the driver shall only
388 * parse the session_ctx to set appropriate
389 * function pointers in ctx.
390 * @param is_update Set 0 if it is to initialize the ctx.
391 * Set 1 if ctx is initialized and only to update
392 * session context data.
394 * - On success return 0.
395 * - On failure return negative integer.
397 typedef int (*cryptodev_sym_configure_raw_dp_ctx_t)(
398 struct rte_cryptodev *dev, uint16_t qp_id,
399 struct rte_crypto_raw_dp_ctx *ctx,
400 enum rte_crypto_op_sess_type sess_type,
401 union rte_cryptodev_session_ctx session_ctx, uint8_t is_update);
403 /** Crypto device operations function pointer table */
404 struct rte_cryptodev_ops {
405 cryptodev_configure_t dev_configure; /**< Configure device. */
406 cryptodev_start_t dev_start; /**< Start device. */
407 cryptodev_stop_t dev_stop; /**< Stop device. */
408 cryptodev_close_t dev_close; /**< Close device. */
410 cryptodev_info_get_t dev_infos_get; /**< Get device info. */
412 cryptodev_stats_get_t stats_get;
413 /**< Get device statistics. */
414 cryptodev_stats_reset_t stats_reset;
415 /**< Reset device statistics. */
417 cryptodev_queue_pair_setup_t queue_pair_setup;
418 /**< Set up a device queue pair. */
419 cryptodev_queue_pair_release_t queue_pair_release;
420 /**< Release a queue pair. */
422 cryptodev_sym_get_session_private_size_t sym_session_get_size;
423 /**< Return private session. */
424 cryptodev_asym_get_session_private_size_t asym_session_get_size;
425 /**< Return asym session private size. */
426 cryptodev_sym_configure_session_t sym_session_configure;
427 /**< Configure a Crypto session. */
428 cryptodev_asym_configure_session_t asym_session_configure;
429 /**< Configure asymmetric Crypto session. */
430 cryptodev_sym_free_session_t sym_session_clear;
431 /**< Clear a Crypto sessions private data. */
432 cryptodev_asym_free_session_t asym_session_clear;
433 /**< Clear a Crypto sessions private data. */
435 cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
436 /**< process input data synchronously (cpu-crypto). */
439 cryptodev_sym_get_raw_dp_ctx_size_t
440 sym_get_raw_dp_ctx_size;
441 /**< Get raw data path service context data size. */
442 cryptodev_sym_configure_raw_dp_ctx_t
443 sym_configure_raw_dp_ctx;
444 /**< Initialize raw data path context data. */
451 * Function for internal use by dummy drivers primarily, e.g. ring-based
453 * Allocates a new cryptodev slot for an crypto device and returns the pointer
454 * to that slot for the driver to use.
456 * @param name Unique identifier name for each device
457 * @param socket_id Socket to allocate resources on.
459 * - Slot in the rte_dev_devices array for a new device;
462 struct rte_cryptodev *
463 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
466 * Function for internal use by dummy drivers primarily, e.g. ring-based
468 * Release the specified cryptodev device.
471 * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
473 * - 0 on success, negative on error
477 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
483 * PMD assist function to parse initialisation arguments for crypto driver
484 * when creating a new crypto PMD device instance.
486 * PMD driver should set default values for that PMD before calling function,
487 * these default values will be over-written with successfully parsed values
490 * @param params parsed PMD initialisation parameters
491 * @param args input argument string to parse
499 rte_cryptodev_pmd_parse_input_args(
500 struct rte_cryptodev_pmd_init_params *params,
506 * PMD assist function to provide boiler plate code for crypto driver to create
507 * and allocate resources for a new crypto PMD device instance.
509 * @param name crypto device name.
510 * @param device base device instance
511 * @param params PMD initialisation parameters
514 * - crypto device instance on success
515 * - NULL on creation failure
518 struct rte_cryptodev *
519 rte_cryptodev_pmd_create(const char *name,
520 struct rte_device *device,
521 struct rte_cryptodev_pmd_init_params *params);
526 * PMD assist function to provide boiler plate code for crypto driver to
527 * destroy and free resources associated with a crypto PMD device instance.
529 * @param cryptodev crypto device handle.
537 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
540 * Executes all the user application registered callbacks for the specific
543 * @param dev Pointer to cryptodev struct
544 * @param event Crypto device interrupt event type.
550 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
551 enum rte_cryptodev_event_type event);
555 * Create unique device name
559 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
563 * Allocate Cryptodev driver.
566 * Pointer to cryptodev_driver.
568 * Pointer to rte_driver.
571 * The driver type identifier
574 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
575 const struct rte_driver *drv);
579 * This is the last step of device probing. It must be called after a
580 * cryptodev is allocated and initialized successfully.
582 * @param dev Pointer to cryptodev struct
589 rte_cryptodev_pmd_probing_finish(struct rte_cryptodev *dev);
591 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
592 RTE_INIT(init_ ##driver_id)\
594 driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
597 /* Reset crypto device fastpath APIs to dummy values. */
600 cryptodev_fp_ops_reset(struct rte_crypto_fp_ops *fp_ops);
602 /* Setup crypto device fastpath APIs. */
605 cryptodev_fp_ops_set(struct rte_crypto_fp_ops *fp_ops,
606 const struct rte_cryptodev *dev);
609 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
611 if (unlikely(sess->nb_drivers <= driver_id))
614 return sess->sess_data[driver_id].data;
618 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
619 uint8_t driver_id, void *private_data)
621 if (unlikely(sess->nb_drivers <= driver_id)) {
622 CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
627 sess->sess_data[driver_id].data = private_data;
631 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
633 return sess->sess_private_data[driver_id];
637 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
638 uint8_t driver_id, void *private_data)
640 sess->sess_private_data[driver_id] = private_data;
643 #endif /* _CRYPTODEV_PMD_H_ */