examples/fips_validation: remove illegal usage of API
[dpdk.git] / lib / cryptodev / rte_cryptodev_pmd.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation.
3  */
4
5 #ifndef _RTE_CRYPTODEV_PMD_H_
6 #define _RTE_CRYPTODEV_PMD_H_
7
8 /** @file
9  * RTE Crypto PMD APIs
10  *
11  * @note
12  * These API are from crypto PMD only and user applications should not call
13  * them directly.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <string.h>
21
22 #include <rte_config.h>
23 #include <rte_dev.h>
24 #include <rte_malloc.h>
25 #include <rte_mbuf.h>
26 #include <rte_mempool.h>
27 #include <rte_log.h>
28 #include <rte_common.h>
29
30 #include "rte_crypto.h"
31 #include "rte_cryptodev.h"
32
33
34 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS    8
35
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")
39
40
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,
45         NULL
46 };
47
48 /**
49  * @internal
50  * Initialisation parameters for crypto devices
51  */
52 struct rte_cryptodev_pmd_init_params {
53         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
54         size_t private_data_size;
55         int socket_id;
56         unsigned int max_nb_queue_pairs;
57 };
58
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 */
65 };
66
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;
71         uint8_t id;
72 };
73
74 /**
75  * Get the rte_cryptodev structure device pointer for the device. Assumes a
76  * valid device index.
77  *
78  * @param       dev_id  Device ID value to select the device structure.
79  *
80  * @return
81  *   - The rte_cryptodev structure pointer for the given device ID.
82  */
83 struct rte_cryptodev *
84 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
85
86 /**
87  * Get the rte_cryptodev structure device pointer for the named device.
88  *
89  * @param       name    device name to select the device structure.
90  *
91  * @return
92  *   - The rte_cryptodev structure pointer for the given device ID.
93  */
94 struct rte_cryptodev *
95 rte_cryptodev_pmd_get_named_dev(const char *name);
96
97 /**
98  * The pool of rte_cryptodev structures.
99  */
100 extern struct rte_cryptodev *rte_cryptodevs;
101
102
103 /**
104  * Definitions of all functions exported by a driver through the
105  * the generic structure of type *crypto_dev_ops* supplied in the
106  * *rte_cryptodev* structure associated with a device.
107  */
108
109 /**
110  *      Function used to configure device.
111  *
112  * @param       dev     Crypto device pointer
113  * @param       config  Crypto device configurations
114  *
115  * @return      Returns 0 on success
116  */
117 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
118                 struct rte_cryptodev_config *config);
119
120 /**
121  * Function used to start a configured device.
122  *
123  * @param       dev     Crypto device pointer
124  *
125  * @return      Returns 0 on success
126  */
127 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
128
129 /**
130  * Function used to stop a configured device.
131  *
132  * @param       dev     Crypto device pointer
133  */
134 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
135
136 /**
137  * Function used to close a configured device.
138  *
139  * @param       dev     Crypto device pointer
140  * @return
141  * - 0 on success.
142  * - EAGAIN if can't close as device is busy
143  */
144 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
145
146
147 /**
148  * Function used to get statistics of a device.
149  *
150  * @param       dev     Crypto device pointer
151  * @param       stats   Pointer to crypto device stats structure to populate
152  */
153 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
154                                 struct rte_cryptodev_stats *stats);
155
156
157 /**
158  * Function used to reset statistics of a device.
159  *
160  * @param       dev     Crypto device pointer
161  */
162 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
163
164
165 /**
166  * Function used to get specific information of a device.
167  *
168  * @param       dev             Crypto device pointer
169  * @param       dev_info        Pointer to infos structure to populate
170  */
171 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
172                                 struct rte_cryptodev_info *dev_info);
173
174 /**
175  * Setup a queue pair for a device.
176  *
177  * @param       dev             Crypto device pointer
178  * @param       qp_id           Queue Pair Index
179  * @param       qp_conf         Queue configuration structure
180  * @param       socket_id       Socket Index
181  *
182  * @return      Returns 0 on success.
183  */
184 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
185                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
186                 int socket_id);
187
188 /**
189  * Release memory resources allocated by given queue pair.
190  *
191  * @param       dev     Crypto device pointer
192  * @param       qp_id   Queue Pair Index
193  *
194  * @return
195  * - 0 on success.
196  * - EAGAIN if can't close as device is busy
197  */
198 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
199                 uint16_t qp_id);
200
201 /**
202  * Create a session mempool to allocate sessions from
203  *
204  * @param       dev             Crypto device pointer
205  * @param       nb_objs         number of sessions objects in mempool
206  * @param       obj_cache_size  l-core object cache size, see *rte_ring_create*
207  * @param       socket_id       Socket Id to allocate  mempool on.
208  *
209  * @return
210  * - On success returns a pointer to a rte_mempool
211  * - On failure returns a NULL pointer
212  */
213 typedef int (*cryptodev_sym_create_session_pool_t)(
214                 struct rte_cryptodev *dev, unsigned nb_objs,
215                 unsigned obj_cache_size, int socket_id);
216
217
218 /**
219  * Get the size of a cryptodev session
220  *
221  * @param       dev             Crypto device pointer
222  *
223  * @return
224  *  - On success returns the size of the session structure for device
225  *  - On failure returns 0
226  */
227 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
228                 struct rte_cryptodev *dev);
229 /**
230  * Get the size of a asymmetric cryptodev session
231  *
232  * @param       dev             Crypto device pointer
233  *
234  * @return
235  *  - On success returns the size of the session structure for device
236  *  - On failure returns 0
237  */
238 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
239                 struct rte_cryptodev *dev);
240
241 /**
242  * Configure a Crypto session on a device.
243  *
244  * @param       dev             Crypto device pointer
245  * @param       xform           Single or chain of crypto xforms
246  * @param       session         Pointer to cryptodev's private session structure
247  * @param       mp              Mempool where the private session is allocated
248  *
249  * @return
250  *  - Returns 0 if private session structure have been created successfully.
251  *  - Returns -EINVAL if input parameters are invalid.
252  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
253  *  - Returns -ENOMEM if the private session could not be allocated.
254  */
255 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
256                 struct rte_crypto_sym_xform *xform,
257                 struct rte_cryptodev_sym_session *session,
258                 struct rte_mempool *mp);
259 /**
260  * Configure a Crypto asymmetric session on a device.
261  *
262  * @param       dev             Crypto device pointer
263  * @param       xform           Single or chain of crypto xforms
264  * @param       session         Pointer to cryptodev's private session structure
265  * @param       mp              Mempool where the private session is allocated
266  *
267  * @return
268  *  - Returns 0 if private session structure have been created successfully.
269  *  - Returns -EINVAL if input parameters are invalid.
270  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
271  *  - Returns -ENOMEM if the private session could not be allocated.
272  */
273 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
274                 struct rte_crypto_asym_xform *xform,
275                 struct rte_cryptodev_asym_session *session,
276                 struct rte_mempool *mp);
277 /**
278  * Free driver private session data.
279  *
280  * @param       dev             Crypto device pointer
281  * @param       sess            Cryptodev session structure
282  */
283 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
284                 struct rte_cryptodev_sym_session *sess);
285 /**
286  * Free asymmetric session private data.
287  *
288  * @param       dev             Crypto device pointer
289  * @param       sess            Cryptodev session structure
290  */
291 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
292                 struct rte_cryptodev_asym_session *sess);
293 /**
294  * Perform actual crypto processing (encrypt/digest or auth/decrypt)
295  * on user provided data.
296  *
297  * @param       dev     Crypto device pointer
298  * @param       sess    Cryptodev session structure
299  * @param       ofs     Start and stop offsets for auth and cipher operations
300  * @param       vec     Vectorized operation descriptor
301  *
302  * @return
303  *  - Returns number of successfully processed packets.
304  *
305  */
306 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
307         (struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
308         union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
309
310 /**
311  * Typedef that the driver provided to get service context private date size.
312  *
313  * @param       dev     Crypto device pointer.
314  *
315  * @return
316  *   - On success return the size of the device's service context private data.
317  *   - On failure return negative integer.
318  */
319 typedef int (*cryptodev_sym_get_raw_dp_ctx_size_t)(struct rte_cryptodev *dev);
320
321 /**
322  * Typedef that the driver provided to configure raw data-path context.
323  *
324  * @param       dev             Crypto device pointer.
325  * @param       qp_id           Crypto device queue pair index.
326  * @param       ctx             The raw data-path context data.
327  * @param       sess_type       session type.
328  * @param       session_ctx     Session context data. If NULL the driver
329  *                              shall only configure the drv_ctx_data in
330  *                              ctx buffer. Otherwise the driver shall only
331  *                              parse the session_ctx to set appropriate
332  *                              function pointers in ctx.
333  * @param       is_update       Set 0 if it is to initialize the ctx.
334  *                              Set 1 if ctx is initialized and only to update
335  *                              session context data.
336  * @return
337  *   - On success return 0.
338  *   - On failure return negative integer.
339  */
340 typedef int (*cryptodev_sym_configure_raw_dp_ctx_t)(
341         struct rte_cryptodev *dev, uint16_t qp_id,
342         struct rte_crypto_raw_dp_ctx *ctx,
343         enum rte_crypto_op_sess_type sess_type,
344         union rte_cryptodev_session_ctx session_ctx, uint8_t is_update);
345
346 /** Crypto device operations function pointer table */
347 struct rte_cryptodev_ops {
348         cryptodev_configure_t dev_configure;    /**< Configure device. */
349         cryptodev_start_t dev_start;            /**< Start device. */
350         cryptodev_stop_t dev_stop;              /**< Stop device. */
351         cryptodev_close_t dev_close;            /**< Close device. */
352
353         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
354
355         cryptodev_stats_get_t stats_get;
356         /**< Get device statistics. */
357         cryptodev_stats_reset_t stats_reset;
358         /**< Reset device statistics. */
359
360         cryptodev_queue_pair_setup_t queue_pair_setup;
361         /**< Set up a device queue pair. */
362         cryptodev_queue_pair_release_t queue_pair_release;
363         /**< Release a queue pair. */
364
365         cryptodev_sym_get_session_private_size_t sym_session_get_size;
366         /**< Return private session. */
367         cryptodev_asym_get_session_private_size_t asym_session_get_size;
368         /**< Return asym session private size. */
369         cryptodev_sym_configure_session_t sym_session_configure;
370         /**< Configure a Crypto session. */
371         cryptodev_asym_configure_session_t asym_session_configure;
372         /**< Configure asymmetric Crypto session. */
373         cryptodev_sym_free_session_t sym_session_clear;
374         /**< Clear a Crypto sessions private data. */
375         cryptodev_asym_free_session_t asym_session_clear;
376         /**< Clear a Crypto sessions private data. */
377         union {
378                 cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
379                 /**< process input data synchronously (cpu-crypto). */
380                 __extension__
381                 struct {
382                         cryptodev_sym_get_raw_dp_ctx_size_t
383                                 sym_get_raw_dp_ctx_size;
384                         /**< Get raw data path service context data size. */
385                         cryptodev_sym_configure_raw_dp_ctx_t
386                                 sym_configure_raw_dp_ctx;
387                         /**< Initialize raw data path context data. */
388                 };
389         };
390 };
391
392
393 /**
394  * Function for internal use by dummy drivers primarily, e.g. ring-based
395  * driver.
396  * Allocates a new cryptodev slot for an crypto device and returns the pointer
397  * to that slot for the driver to use.
398  *
399  * @param       name            Unique identifier name for each device
400  * @param       socket_id       Socket to allocate resources on.
401  * @return
402  *   - Slot in the rte_dev_devices array for a new device;
403  */
404 struct rte_cryptodev *
405 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
406
407 /**
408  * Function for internal use by dummy drivers primarily, e.g. ring-based
409  * driver.
410  * Release the specified cryptodev device.
411  *
412  * @param cryptodev
413  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
414  * @return
415  *   - 0 on success, negative on error
416  */
417 extern int
418 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
419
420
421 /**
422  * @internal
423  *
424  * PMD assist function to parse initialisation arguments for crypto driver
425  * when creating a new crypto PMD device instance.
426  *
427  * PMD driver should set default values for that PMD before calling function,
428  * these default values will be over-written with successfully parsed values
429  * from args string.
430  *
431  * @param       params  parsed PMD initialisation parameters
432  * @param       args    input argument string to parse
433  *
434  * @return
435  *  - 0 on success
436  *  - errno on failure
437  */
438 int
439 rte_cryptodev_pmd_parse_input_args(
440                 struct rte_cryptodev_pmd_init_params *params,
441                 const char *args);
442
443 /**
444  * @internal
445  *
446  * PMD assist function to provide boiler plate code for crypto driver to create
447  * and allocate resources for a new crypto PMD device instance.
448  *
449  * @param       name    crypto device name.
450  * @param       device  base device instance
451  * @param       params  PMD initialisation parameters
452  *
453  * @return
454  *  - crypto device instance on success
455  *  - NULL on creation failure
456  */
457 struct rte_cryptodev *
458 rte_cryptodev_pmd_create(const char *name,
459                 struct rte_device *device,
460                 struct rte_cryptodev_pmd_init_params *params);
461
462 /**
463  * @internal
464  *
465  * PMD assist function to provide boiler plate code for crypto driver to
466  * destroy and free resources associated with a crypto PMD device instance.
467  *
468  * @param       cryptodev       crypto device handle.
469  *
470  * @return
471  *  - 0 on success
472  *  - errno on failure
473  */
474 int
475 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
476
477 /**
478  * Executes all the user application registered callbacks for the specific
479  * device.
480  *  *
481  * @param       dev     Pointer to cryptodev struct
482  * @param       event   Crypto device interrupt event type.
483  *
484  * @return
485  *  void
486  */
487 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
488                                 enum rte_cryptodev_event_type event);
489
490 /**
491  * @internal
492  * Create unique device name
493  */
494 int
495 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
496
497 /**
498  * @internal
499  * Allocate Cryptodev driver.
500  *
501  * @param crypto_drv
502  *   Pointer to cryptodev_driver.
503  * @param drv
504  *   Pointer to rte_driver.
505  *
506  * @return
507  *  The driver type identifier
508  */
509 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
510                 const struct rte_driver *drv);
511
512
513 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
514 RTE_INIT(init_ ##driver_id)\
515 {\
516         driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
517 }
518
519 static inline void *
520 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
521                 uint8_t driver_id) {
522         if (unlikely(sess->nb_drivers <= driver_id))
523                 return NULL;
524
525         return sess->sess_data[driver_id].data;
526 }
527
528 static inline void
529 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
530                 uint8_t driver_id, void *private_data)
531 {
532         if (unlikely(sess->nb_drivers <= driver_id)) {
533                 CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
534                                 driver_id);
535                 return;
536         }
537
538         sess->sess_data[driver_id].data = private_data;
539 }
540
541 static inline void *
542 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
543                 uint8_t driver_id) {
544         return sess->sess_private_data[driver_id];
545 }
546
547 static inline void
548 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
549                 uint8_t driver_id, void *private_data)
550 {
551         sess->sess_private_data[driver_id] = private_data;
552 }
553
554 #ifdef __cplusplus
555 }
556 #endif
557
558 #endif /* _RTE_CRYPTODEV_PMD_H_ */