1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Intel Corporation.
5 #ifndef _RTE_CRYPTODEV_PMD_H_
6 #define _RTE_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.
83 struct rte_cryptodev *
84 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
87 * Get the rte_cryptodev structure device pointer for the named device.
89 * @param name device name to select the device structure.
92 * - The rte_cryptodev structure pointer for the given device ID.
94 struct rte_cryptodev *
95 rte_cryptodev_pmd_get_named_dev(const char *name);
98 * Validate if the crypto device index is valid attached crypto device.
100 * @param dev_id Crypto device index.
103 * - If the device index is valid (1) or not (0).
106 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id);
109 * The pool of rte_cryptodev structures.
111 extern struct rte_cryptodev *rte_cryptodevs;
115 * Definitions of all functions exported by a driver through the
116 * the generic structure of type *crypto_dev_ops* supplied in the
117 * *rte_cryptodev* structure associated with a device.
121 * Function used to configure device.
123 * @param dev Crypto device pointer
124 * config Crypto device configurations
126 * @return Returns 0 on success
128 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
129 struct rte_cryptodev_config *config);
132 * Function used to start a configured device.
134 * @param dev Crypto device pointer
136 * @return Returns 0 on success
138 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
141 * Function used to stop a configured device.
143 * @param dev Crypto device pointer
145 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
148 * Function used to close a configured device.
150 * @param dev Crypto device pointer
153 * - EAGAIN if can't close as device is busy
155 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
159 * Function used to get statistics of a device.
161 * @param dev Crypto device pointer
162 * @param stats Pointer to crypto device stats structure to populate
164 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
165 struct rte_cryptodev_stats *stats);
169 * Function used to reset statistics of a device.
171 * @param dev Crypto device pointer
173 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
177 * Function used to get specific information of a device.
179 * @param dev Crypto device pointer
181 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
182 struct rte_cryptodev_info *dev_info);
185 * Setup a queue pair for a device.
187 * @param dev Crypto device pointer
188 * @param qp_id Queue Pair Index
189 * @param qp_conf Queue configuration structure
190 * @param socket_id Socket Index
192 * @return Returns 0 on success.
194 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
195 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
199 * Release memory resources allocated by given queue pair.
201 * @param dev Crypto device pointer
202 * @param qp_id Queue Pair Index
206 * - EAGAIN if can't close as device is busy
208 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
212 * Create a session mempool to allocate sessions from
214 * @param dev Crypto device pointer
215 * @param nb_objs number of sessions objects in mempool
216 * @param obj_cache l-core object cache size, see *rte_ring_create*
217 * @param socket_id Socket Id to allocate mempool on.
220 * - On success returns a pointer to a rte_mempool
221 * - On failure returns a NULL pointer
223 typedef int (*cryptodev_sym_create_session_pool_t)(
224 struct rte_cryptodev *dev, unsigned nb_objs,
225 unsigned obj_cache_size, int socket_id);
229 * Get the size of a cryptodev session
231 * @param dev Crypto device pointer
234 * - On success returns the size of the session structure for device
235 * - On failure returns 0
237 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
238 struct rte_cryptodev *dev);
240 * Get the size of a asymmetric cryptodev session
242 * @param dev Crypto device pointer
245 * - On success returns the size of the session structure for device
246 * - On failure returns 0
248 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
249 struct rte_cryptodev *dev);
252 * Configure a Crypto session on a device.
254 * @param dev Crypto device pointer
255 * @param xform Single or chain of crypto xforms
256 * @param priv_sess Pointer to cryptodev's private session structure
257 * @param mp Mempool where the private session is allocated
260 * - Returns 0 if private session structure have been created successfully.
261 * - Returns -EINVAL if input parameters are invalid.
262 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
263 * - Returns -ENOMEM if the private session could not be allocated.
265 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
266 struct rte_crypto_sym_xform *xform,
267 struct rte_cryptodev_sym_session *session,
268 struct rte_mempool *mp);
270 * Configure a Crypto asymmetric session on a device.
272 * @param dev Crypto device pointer
273 * @param xform Single or chain of crypto xforms
274 * @param priv_sess Pointer to cryptodev's private session structure
275 * @param mp Mempool where the private session is allocated
278 * - Returns 0 if private session structure have been created successfully.
279 * - Returns -EINVAL if input parameters are invalid.
280 * - Returns -ENOTSUP if crypto device does not support the crypto transform.
281 * - Returns -ENOMEM if the private session could not be allocated.
283 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
284 struct rte_crypto_asym_xform *xform,
285 struct rte_cryptodev_asym_session *session,
286 struct rte_mempool *mp);
288 * Free driver private session data.
290 * @param dev Crypto device pointer
291 * @param sess Cryptodev session structure
293 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
294 struct rte_cryptodev_sym_session *sess);
296 * Free asymmetric session private data.
298 * @param dev Crypto device pointer
299 * @param sess Cryptodev session structure
301 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
302 struct rte_cryptodev_asym_session *sess);
304 * Perform actual crypto processing (encrypt/digest or auth/decrypt)
305 * on user provided data.
307 * @param dev Crypto device pointer
308 * @param sess Cryptodev session structure
309 * @param ofs Start and stop offsets for auth and cipher operations
310 * @param vec Vectorized operation descriptor
313 * - Returns number of successfully processed packets.
316 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
317 (struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
318 union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
321 /** Crypto device operations function pointer table */
322 struct rte_cryptodev_ops {
323 cryptodev_configure_t dev_configure; /**< Configure device. */
324 cryptodev_start_t dev_start; /**< Start device. */
325 cryptodev_stop_t dev_stop; /**< Stop device. */
326 cryptodev_close_t dev_close; /**< Close device. */
328 cryptodev_info_get_t dev_infos_get; /**< Get device info. */
330 cryptodev_stats_get_t stats_get;
331 /**< Get device statistics. */
332 cryptodev_stats_reset_t stats_reset;
333 /**< Reset device statistics. */
335 cryptodev_queue_pair_setup_t queue_pair_setup;
336 /**< Set up a device queue pair. */
337 cryptodev_queue_pair_release_t queue_pair_release;
338 /**< Release a queue pair. */
340 cryptodev_sym_get_session_private_size_t sym_session_get_size;
341 /**< Return private session. */
342 cryptodev_asym_get_session_private_size_t asym_session_get_size;
343 /**< Return asym session private size. */
344 cryptodev_sym_configure_session_t sym_session_configure;
345 /**< Configure a Crypto session. */
346 cryptodev_asym_configure_session_t asym_session_configure;
347 /**< Configure asymmetric Crypto session. */
348 cryptodev_sym_free_session_t sym_session_clear;
349 /**< Clear a Crypto sessions private data. */
350 cryptodev_asym_free_session_t asym_session_clear;
351 /**< Clear a Crypto sessions private data. */
352 cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
353 /**< process input data synchronously (cpu-crypto). */
358 * Function for internal use by dummy drivers primarily, e.g. ring-based
360 * Allocates a new cryptodev slot for an crypto device and returns the pointer
361 * to that slot for the driver to use.
363 * @param name Unique identifier name for each device
364 * @param socket_id Socket to allocate resources on.
366 * - Slot in the rte_dev_devices array for a new device;
368 struct rte_cryptodev *
369 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
372 * Function for internal use by dummy drivers primarily, e.g. ring-based
374 * Release the specified cryptodev device.
377 * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
379 * - 0 on success, negative on error
382 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
388 * PMD assist function to parse initialisation arguments for crypto driver
389 * when creating a new crypto PMD device instance.
391 * PMD driver should set default values for that PMD before calling function,
392 * these default values will be over-written with successfully parsed values
395 * @param params parsed PMD initialisation parameters
396 * @param args input argument string to parse
403 rte_cryptodev_pmd_parse_input_args(
404 struct rte_cryptodev_pmd_init_params *params,
410 * PMD assist function to provide boiler plate code for crypto driver to create
411 * and allocate resources for a new crypto PMD device instance.
413 * @param name crypto device name.
414 * @param device base device instance
415 * @param params PMD initialisation parameters
418 * - crypto device instance on success
419 * - NULL on creation failure
421 struct rte_cryptodev *
422 rte_cryptodev_pmd_create(const char *name,
423 struct rte_device *device,
424 struct rte_cryptodev_pmd_init_params *params);
429 * PMD assist function to provide boiler plate code for crypto driver to
430 * destroy and free resources associated with a crypto PMD device instance.
432 * @param cryptodev crypto device handle.
439 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
442 * Executes all the user application registered callbacks for the specific
445 * @param dev Pointer to cryptodev struct
446 * @param event Crypto device interrupt event type.
451 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
452 enum rte_cryptodev_event_type event);
456 * Create unique device name
459 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
463 * Allocate Cryptodev driver.
466 * Pointer to cryptodev_driver.
468 * Pointer to rte_driver.
471 * The driver type identifier
473 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
474 const struct rte_driver *drv);
477 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
478 RTE_INIT(init_ ##driver_id)\
480 driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
484 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
486 if (unlikely(sess->nb_drivers <= driver_id))
489 return sess->sess_data[driver_id].data;
493 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
494 uint8_t driver_id, void *private_data)
496 if (unlikely(sess->nb_drivers <= driver_id)) {
497 CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
502 sess->sess_data[driver_id].data = private_data;
506 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
508 return sess->sess_private_data[driver_id];
512 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
513 uint8_t driver_id, void *private_data)
515 sess->sess_private_data[driver_id] = private_data;
522 #endif /* _RTE_CRYPTODEV_PMD_H_ */