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
22 #include <rte_config.h>
24 #include <rte_malloc.h>
26 #include <rte_mempool.h>
28 #include <rte_common.h>
30 #include "rte_crypto.h"
31 #include "rte_cryptodev.h"
34 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS 8
36 #define RTE_CRYPTODEV_PMD_NAME_ARG ("name")
37 #define RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG ("max_nb_queue_pairs")
38 #define RTE_CRYPTODEV_PMD_SOCKET_ID_ARG ("socket_id")
41 static const char * const cryptodev_pmd_valid_params[] = {
42 RTE_CRYPTODEV_PMD_NAME_ARG,
43 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
44 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
50 * Initialisation parameters for crypto devices
52 struct rte_cryptodev_pmd_init_params {
53 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
54 size_t private_data_size;
56 unsigned int max_nb_queue_pairs;
59 /** Global structure used for maintaining state of allocated crypto devices */
60 struct rte_cryptodev_global {
61 struct rte_cryptodev *devs; /**< Device information array */
62 struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
63 /**< Device private data */
64 uint8_t nb_devs; /**< Number of devices found */
67 /* Cryptodev driver, containing the driver ID */
68 struct cryptodev_driver {
69 TAILQ_ENTRY(cryptodev_driver) next; /**< Next in list. */
70 const struct rte_driver *driver;
75 * Get the rte_cryptodev structure device pointer for the device. Assumes a
78 * @param dev_id Device ID value to select the device structure.
81 * - The rte_cryptodev structure pointer for the given device ID.
84 struct rte_cryptodev *
85 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
88 * Get the rte_cryptodev structure device pointer for the named device.
90 * @param name device name to select the device structure.
93 * - The rte_cryptodev structure pointer for the given device ID.
96 struct rte_cryptodev *
97 rte_cryptodev_pmd_get_named_dev(const char *name);
100 * The pool of rte_cryptodev structures.
102 extern struct rte_cryptodev *rte_cryptodevs;
106 * Definitions of all functions exported by a driver through the
107 * the generic structure of type *crypto_dev_ops* supplied in the
108 * *rte_cryptodev* structure associated with a device.
112 * Function used to configure device.
114 * @param dev Crypto device pointer
115 * @param config Crypto device configurations
117 * @return Returns 0 on success
119 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
120 struct rte_cryptodev_config *config);
123 * Function used to start a configured device.
125 * @param dev Crypto device pointer
127 * @return Returns 0 on success
129 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
132 * Function used to stop a configured device.
134 * @param dev Crypto device pointer
136 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
139 * Function used to close a configured device.
141 * @param dev Crypto device pointer
144 * - EAGAIN if can't close as device is busy
146 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
150 * Function used to get statistics of a device.
152 * @param dev Crypto device pointer
153 * @param stats Pointer to crypto device stats structure to populate
155 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
156 struct rte_cryptodev_stats *stats);
160 * Function used to reset statistics of a device.
162 * @param dev Crypto device pointer
164 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
168 * Function used to get specific information of a device.
170 * @param dev Crypto device pointer
171 * @param dev_info Pointer to infos structure to populate
173 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
174 struct rte_cryptodev_info *dev_info);
177 * Setup a queue pair for a device.
179 * @param dev Crypto device pointer
180 * @param qp_id Queue Pair Index
181 * @param qp_conf Queue configuration structure
182 * @param socket_id Socket Index
184 * @return Returns 0 on success.
186 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
187 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
191 * Release memory resources allocated by given queue pair.
193 * @param dev Crypto device pointer
194 * @param qp_id Queue Pair Index
198 * - EAGAIN if can't close as device is busy
200 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
204 * Create a session mempool to allocate sessions from
206 * @param dev Crypto device pointer
207 * @param nb_objs number of sessions objects in mempool
208 * @param obj_cache_size l-core object cache size, see *rte_ring_create*
209 * @param socket_id Socket Id to allocate mempool on.
212 * - On success returns a pointer to a rte_mempool
213 * - On failure returns a NULL pointer
215 typedef int (*cryptodev_sym_create_session_pool_t)(
216 struct rte_cryptodev *dev, unsigned nb_objs,
217 unsigned obj_cache_size, int socket_id);
221 * Get the size of a cryptodev session
223 * @param dev Crypto device pointer
226 * - On success returns the size of the session structure for device
227 * - On failure returns 0
229 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
230 struct rte_cryptodev *dev);
232 * Get the size of a asymmetric cryptodev session
234 * @param dev Crypto device pointer
237 * - On success returns the size of the session structure for device
238 * - On failure returns 0
240 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
241 struct rte_cryptodev *dev);
244 * Configure a Crypto session on a device.
246 * @param dev Crypto device pointer
247 * @param xform Single or chain of crypto xforms
248 * @param session Pointer to cryptodev's private session structure
249 * @param mp Mempool where the private session is allocated
252 * - Returns 0 if private session structure have been created successfully.
253 * - Returns -EINVAL if input parameters are invalid.
254 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
255 * - Returns -ENOMEM if the private session could not be allocated.
257 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
258 struct rte_crypto_sym_xform *xform,
259 struct rte_cryptodev_sym_session *session,
260 struct rte_mempool *mp);
262 * Configure a Crypto asymmetric session on a device.
264 * @param dev Crypto device pointer
265 * @param xform Single or chain of crypto xforms
266 * @param session Pointer to cryptodev's private session structure
267 * @param mp Mempool where the private session is allocated
270 * - Returns 0 if private session structure have been created successfully.
271 * - Returns -EINVAL if input parameters are invalid.
272 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
273 * - Returns -ENOMEM if the private session could not be allocated.
275 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
276 struct rte_crypto_asym_xform *xform,
277 struct rte_cryptodev_asym_session *session,
278 struct rte_mempool *mp);
280 * Free driver private session data.
282 * @param dev Crypto device pointer
283 * @param sess Cryptodev session structure
285 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
286 struct rte_cryptodev_sym_session *sess);
288 * Free asymmetric session private data.
290 * @param dev Crypto device pointer
291 * @param sess Cryptodev session structure
293 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
294 struct rte_cryptodev_asym_session *sess);
296 * Perform actual crypto processing (encrypt/digest or auth/decrypt)
297 * on user provided data.
299 * @param dev Crypto device pointer
300 * @param sess Cryptodev session structure
301 * @param ofs Start and stop offsets for auth and cipher operations
302 * @param vec Vectorized operation descriptor
305 * - Returns number of successfully processed packets.
308 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
309 (struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
310 union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
313 * Typedef that the driver provided to get service context private date size.
315 * @param dev Crypto device pointer.
318 * - On success return the size of the device's service context private data.
319 * - On failure return negative integer.
321 typedef int (*cryptodev_sym_get_raw_dp_ctx_size_t)(struct rte_cryptodev *dev);
324 * Typedef that the driver provided to configure raw data-path context.
326 * @param dev Crypto device pointer.
327 * @param qp_id Crypto device queue pair index.
328 * @param ctx The raw data-path context data.
329 * @param sess_type session type.
330 * @param session_ctx Session context data. If NULL the driver
331 * shall only configure the drv_ctx_data in
332 * ctx buffer. Otherwise the driver shall only
333 * parse the session_ctx to set appropriate
334 * function pointers in ctx.
335 * @param is_update Set 0 if it is to initialize the ctx.
336 * Set 1 if ctx is initialized and only to update
337 * session context data.
339 * - On success return 0.
340 * - On failure return negative integer.
342 typedef int (*cryptodev_sym_configure_raw_dp_ctx_t)(
343 struct rte_cryptodev *dev, uint16_t qp_id,
344 struct rte_crypto_raw_dp_ctx *ctx,
345 enum rte_crypto_op_sess_type sess_type,
346 union rte_cryptodev_session_ctx session_ctx, uint8_t is_update);
348 /** Crypto device operations function pointer table */
349 struct rte_cryptodev_ops {
350 cryptodev_configure_t dev_configure; /**< Configure device. */
351 cryptodev_start_t dev_start; /**< Start device. */
352 cryptodev_stop_t dev_stop; /**< Stop device. */
353 cryptodev_close_t dev_close; /**< Close device. */
355 cryptodev_info_get_t dev_infos_get; /**< Get device info. */
357 cryptodev_stats_get_t stats_get;
358 /**< Get device statistics. */
359 cryptodev_stats_reset_t stats_reset;
360 /**< Reset device statistics. */
362 cryptodev_queue_pair_setup_t queue_pair_setup;
363 /**< Set up a device queue pair. */
364 cryptodev_queue_pair_release_t queue_pair_release;
365 /**< Release a queue pair. */
367 cryptodev_sym_get_session_private_size_t sym_session_get_size;
368 /**< Return private session. */
369 cryptodev_asym_get_session_private_size_t asym_session_get_size;
370 /**< Return asym session private size. */
371 cryptodev_sym_configure_session_t sym_session_configure;
372 /**< Configure a Crypto session. */
373 cryptodev_asym_configure_session_t asym_session_configure;
374 /**< Configure asymmetric Crypto session. */
375 cryptodev_sym_free_session_t sym_session_clear;
376 /**< Clear a Crypto sessions private data. */
377 cryptodev_asym_free_session_t asym_session_clear;
378 /**< Clear a Crypto sessions private data. */
380 cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
381 /**< process input data synchronously (cpu-crypto). */
384 cryptodev_sym_get_raw_dp_ctx_size_t
385 sym_get_raw_dp_ctx_size;
386 /**< Get raw data path service context data size. */
387 cryptodev_sym_configure_raw_dp_ctx_t
388 sym_configure_raw_dp_ctx;
389 /**< Initialize raw data path context data. */
396 * Function for internal use by dummy drivers primarily, e.g. ring-based
398 * Allocates a new cryptodev slot for an crypto device and returns the pointer
399 * to that slot for the driver to use.
401 * @param name Unique identifier name for each device
402 * @param socket_id Socket to allocate resources on.
404 * - Slot in the rte_dev_devices array for a new device;
407 struct rte_cryptodev *
408 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
411 * Function for internal use by dummy drivers primarily, e.g. ring-based
413 * Release the specified cryptodev device.
416 * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
418 * - 0 on success, negative on error
422 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
428 * PMD assist function to parse initialisation arguments for crypto driver
429 * when creating a new crypto PMD device instance.
431 * PMD driver should set default values for that PMD before calling function,
432 * these default values will be over-written with successfully parsed values
435 * @param params parsed PMD initialisation parameters
436 * @param args input argument string to parse
444 rte_cryptodev_pmd_parse_input_args(
445 struct rte_cryptodev_pmd_init_params *params,
451 * PMD assist function to provide boiler plate code for crypto driver to create
452 * and allocate resources for a new crypto PMD device instance.
454 * @param name crypto device name.
455 * @param device base device instance
456 * @param params PMD initialisation parameters
459 * - crypto device instance on success
460 * - NULL on creation failure
463 struct rte_cryptodev *
464 rte_cryptodev_pmd_create(const char *name,
465 struct rte_device *device,
466 struct rte_cryptodev_pmd_init_params *params);
471 * PMD assist function to provide boiler plate code for crypto driver to
472 * destroy and free resources associated with a crypto PMD device instance.
474 * @param cryptodev crypto device handle.
482 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
485 * Executes all the user application registered callbacks for the specific
488 * @param dev Pointer to cryptodev struct
489 * @param event Crypto device interrupt event type.
495 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
496 enum rte_cryptodev_event_type event);
500 * Create unique device name
504 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
508 * Allocate Cryptodev driver.
511 * Pointer to cryptodev_driver.
513 * Pointer to rte_driver.
516 * The driver type identifier
519 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
520 const struct rte_driver *drv);
523 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
524 RTE_INIT(init_ ##driver_id)\
526 driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
530 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
532 if (unlikely(sess->nb_drivers <= driver_id))
535 return sess->sess_data[driver_id].data;
539 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
540 uint8_t driver_id, void *private_data)
542 if (unlikely(sess->nb_drivers <= driver_id)) {
543 CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
548 sess->sess_data[driver_id].data = private_data;
552 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
554 return sess->sess_private_data[driver_id];
558 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
559 uint8_t driver_id, void *private_data)
561 sess->sess_private_data[driver_id] = private_data;
568 #endif /* _CRYPTODEV_PMD_H_ */