cryptodev: add tracepoints
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation.
3  */
4
5 #ifndef _RTE_CRYPTODEV_H_
6 #define _RTE_CRYPTODEV_H_
7
8 /**
9  * @file rte_cryptodev.h
10  *
11  * RTE Cryptographic Device APIs
12  *
13  * Defines RTE Crypto Device APIs for the provisioning of cipher and
14  * authentication operations.
15  */
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #include "rte_kvargs.h"
22 #include "rte_crypto.h"
23 #include "rte_dev.h"
24 #include <rte_common.h>
25 #include <rte_config.h>
26
27 #include "rte_cryptodev_trace_fp.h"
28
29 extern const char **rte_cyptodev_names;
30
31 /* Logging Macros */
32
33 #define CDEV_LOG_ERR(...) \
34         RTE_LOG(ERR, CRYPTODEV, \
35                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
36                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
37
38 #define CDEV_LOG_INFO(...) \
39         RTE_LOG(INFO, CRYPTODEV, \
40                 RTE_FMT(RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
41                         RTE_FMT_TAIL(__VA_ARGS__,)))
42
43 #define CDEV_LOG_DEBUG(...) \
44         RTE_LOG(DEBUG, CRYPTODEV, \
45                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
46                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
47
48 #define CDEV_PMD_TRACE(...) \
49         RTE_LOG(DEBUG, CRYPTODEV, \
50                 RTE_FMT("[%s] %s: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
51                         dev, __func__, RTE_FMT_TAIL(__VA_ARGS__,)))
52
53 /**
54  * A macro that points to an offset from the start
55  * of the crypto operation structure (rte_crypto_op)
56  *
57  * The returned pointer is cast to type t.
58  *
59  * @param c
60  *   The crypto operation.
61  * @param o
62  *   The offset from the start of the crypto operation.
63  * @param t
64  *   The type to cast the result into.
65  */
66 #define rte_crypto_op_ctod_offset(c, t, o)      \
67         ((t)((char *)(c) + (o)))
68
69 /**
70  * A macro that returns the physical address that points
71  * to an offset from the start of the crypto operation
72  * (rte_crypto_op)
73  *
74  * @param c
75  *   The crypto operation.
76  * @param o
77  *   The offset from the start of the crypto operation
78  *   to calculate address from.
79  */
80 #define rte_crypto_op_ctophys_offset(c, o)      \
81         (rte_iova_t)((c)->phys_addr + (o))
82
83 /**
84  * Crypto parameters range description
85  */
86 struct rte_crypto_param_range {
87         uint16_t min;   /**< minimum size */
88         uint16_t max;   /**< maximum size */
89         uint16_t increment;
90         /**< if a range of sizes are supported,
91          * this parameter is used to indicate
92          * increments in byte size that are supported
93          * between the minimum and maximum
94          */
95 };
96
97 /**
98  * Symmetric Crypto Capability
99  */
100 struct rte_cryptodev_symmetric_capability {
101         enum rte_crypto_sym_xform_type xform_type;
102         /**< Transform type : Authentication / Cipher / AEAD */
103         RTE_STD_C11
104         union {
105                 struct {
106                         enum rte_crypto_auth_algorithm algo;
107                         /**< authentication algorithm */
108                         uint16_t block_size;
109                         /**< algorithm block size */
110                         struct rte_crypto_param_range key_size;
111                         /**< auth key size range */
112                         struct rte_crypto_param_range digest_size;
113                         /**< digest size range */
114                         struct rte_crypto_param_range aad_size;
115                         /**< Additional authentication data size range */
116                         struct rte_crypto_param_range iv_size;
117                         /**< Initialisation vector data size range */
118                 } auth;
119                 /**< Symmetric Authentication transform capabilities */
120                 struct {
121                         enum rte_crypto_cipher_algorithm algo;
122                         /**< cipher algorithm */
123                         uint16_t block_size;
124                         /**< algorithm block size */
125                         struct rte_crypto_param_range key_size;
126                         /**< cipher key size range */
127                         struct rte_crypto_param_range iv_size;
128                         /**< Initialisation vector data size range */
129                 } cipher;
130                 /**< Symmetric Cipher transform capabilities */
131                 struct {
132                         enum rte_crypto_aead_algorithm algo;
133                         /**< AEAD algorithm */
134                         uint16_t block_size;
135                         /**< algorithm block size */
136                         struct rte_crypto_param_range key_size;
137                         /**< AEAD key size range */
138                         struct rte_crypto_param_range digest_size;
139                         /**< digest size range */
140                         struct rte_crypto_param_range aad_size;
141                         /**< Additional authentication data size range */
142                         struct rte_crypto_param_range iv_size;
143                         /**< Initialisation vector data size range */
144                 } aead;
145         };
146 };
147
148 /**
149  * Asymmetric Xform Crypto Capability
150  *
151  */
152 struct rte_cryptodev_asymmetric_xform_capability {
153         enum rte_crypto_asym_xform_type xform_type;
154         /**< Transform type: RSA/MODEXP/DH/DSA/MODINV */
155
156         uint32_t op_types;
157         /**< bitmask for supported rte_crypto_asym_op_type */
158
159         __extension__
160         union {
161                 struct rte_crypto_param_range modlen;
162                 /**< Range of modulus length supported by modulus based xform.
163                  * Value 0 mean implementation default
164                  */
165         };
166 };
167
168 /**
169  * Asymmetric Crypto Capability
170  *
171  */
172 struct rte_cryptodev_asymmetric_capability {
173         struct rte_cryptodev_asymmetric_xform_capability xform_capa;
174 };
175
176
177 /** Structure used to capture a capability of a crypto device */
178 struct rte_cryptodev_capabilities {
179         enum rte_crypto_op_type op;
180         /**< Operation type */
181
182         RTE_STD_C11
183         union {
184                 struct rte_cryptodev_symmetric_capability sym;
185                 /**< Symmetric operation capability parameters */
186                 struct rte_cryptodev_asymmetric_capability asym;
187                 /**< Asymmetric operation capability parameters */
188         };
189 };
190
191 /** Structure used to describe crypto algorithms */
192 struct rte_cryptodev_sym_capability_idx {
193         enum rte_crypto_sym_xform_type type;
194         union {
195                 enum rte_crypto_cipher_algorithm cipher;
196                 enum rte_crypto_auth_algorithm auth;
197                 enum rte_crypto_aead_algorithm aead;
198         } algo;
199 };
200
201 /**
202  * Structure used to describe asymmetric crypto xforms
203  * Each xform maps to one asym algorithm.
204  *
205  */
206 struct rte_cryptodev_asym_capability_idx {
207         enum rte_crypto_asym_xform_type type;
208         /**< Asymmetric xform (algo) type */
209 };
210
211 /**
212  * Provide capabilities available for defined device and algorithm
213  *
214  * @param       dev_id          The identifier of the device.
215  * @param       idx             Description of crypto algorithms.
216  *
217  * @return
218  *   - Return description of the symmetric crypto capability if exist.
219  *   - Return NULL if the capability not exist.
220  */
221 const struct rte_cryptodev_symmetric_capability *
222 rte_cryptodev_sym_capability_get(uint8_t dev_id,
223                 const struct rte_cryptodev_sym_capability_idx *idx);
224
225 /**
226  *  Provide capabilities available for defined device and xform
227  *
228  * @param       dev_id          The identifier of the device.
229  * @param       idx             Description of asym crypto xform.
230  *
231  * @return
232  *   - Return description of the asymmetric crypto capability if exist.
233  *   - Return NULL if the capability not exist.
234  */
235 __rte_experimental
236 const struct rte_cryptodev_asymmetric_xform_capability *
237 rte_cryptodev_asym_capability_get(uint8_t dev_id,
238                 const struct rte_cryptodev_asym_capability_idx *idx);
239
240 /**
241  * Check if key size and initial vector are supported
242  * in crypto cipher capability
243  *
244  * @param       capability      Description of the symmetric crypto capability.
245  * @param       key_size        Cipher key size.
246  * @param       iv_size         Cipher initial vector size.
247  *
248  * @return
249  *   - Return 0 if the parameters are in range of the capability.
250  *   - Return -1 if the parameters are out of range of the capability.
251  */
252 int
253 rte_cryptodev_sym_capability_check_cipher(
254                 const struct rte_cryptodev_symmetric_capability *capability,
255                 uint16_t key_size, uint16_t iv_size);
256
257 /**
258  * Check if key size and initial vector are supported
259  * in crypto auth capability
260  *
261  * @param       capability      Description of the symmetric crypto capability.
262  * @param       key_size        Auth key size.
263  * @param       digest_size     Auth digest size.
264  * @param       iv_size         Auth initial vector size.
265  *
266  * @return
267  *   - Return 0 if the parameters are in range of the capability.
268  *   - Return -1 if the parameters are out of range of the capability.
269  */
270 int
271 rte_cryptodev_sym_capability_check_auth(
272                 const struct rte_cryptodev_symmetric_capability *capability,
273                 uint16_t key_size, uint16_t digest_size, uint16_t iv_size);
274
275 /**
276  * Check if key, digest, AAD and initial vector sizes are supported
277  * in crypto AEAD capability
278  *
279  * @param       capability      Description of the symmetric crypto capability.
280  * @param       key_size        AEAD key size.
281  * @param       digest_size     AEAD digest size.
282  * @param       aad_size        AEAD AAD size.
283  * @param       iv_size         AEAD IV size.
284  *
285  * @return
286  *   - Return 0 if the parameters are in range of the capability.
287  *   - Return -1 if the parameters are out of range of the capability.
288  */
289 int
290 rte_cryptodev_sym_capability_check_aead(
291                 const struct rte_cryptodev_symmetric_capability *capability,
292                 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
293                 uint16_t iv_size);
294
295 /**
296  * Check if op type is supported
297  *
298  * @param       capability      Description of the asymmetric crypto capability.
299  * @param       op_type         op type
300  *
301  * @return
302  *   - Return 1 if the op type is supported
303  *   - Return 0 if unsupported
304  */
305 __rte_experimental
306 int
307 rte_cryptodev_asym_xform_capability_check_optype(
308         const struct rte_cryptodev_asymmetric_xform_capability *capability,
309                 enum rte_crypto_asym_op_type op_type);
310
311 /**
312  * Check if modulus length is in supported range
313  *
314  * @param       capability      Description of the asymmetric crypto capability.
315  * @param       modlen          modulus length.
316  *
317  * @return
318  *   - Return 0 if the parameters are in range of the capability.
319  *   - Return -1 if the parameters are out of range of the capability.
320  */
321 __rte_experimental
322 int
323 rte_cryptodev_asym_xform_capability_check_modlen(
324         const struct rte_cryptodev_asymmetric_xform_capability *capability,
325                 uint16_t modlen);
326
327 /**
328  * Provide the cipher algorithm enum, given an algorithm string
329  *
330  * @param       algo_enum       A pointer to the cipher algorithm
331  *                              enum to be filled
332  * @param       algo_string     Authentication algo string
333  *
334  * @return
335  * - Return -1 if string is not valid
336  * - Return 0 is the string is valid
337  */
338 int
339 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
340                 const char *algo_string);
341
342 /**
343  * Provide the authentication algorithm enum, given an algorithm string
344  *
345  * @param       algo_enum       A pointer to the authentication algorithm
346  *                              enum to be filled
347  * @param       algo_string     Authentication algo string
348  *
349  * @return
350  * - Return -1 if string is not valid
351  * - Return 0 is the string is valid
352  */
353 int
354 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
355                 const char *algo_string);
356
357 /**
358  * Provide the AEAD algorithm enum, given an algorithm string
359  *
360  * @param       algo_enum       A pointer to the AEAD algorithm
361  *                              enum to be filled
362  * @param       algo_string     AEAD algorithm string
363  *
364  * @return
365  * - Return -1 if string is not valid
366  * - Return 0 is the string is valid
367  */
368 int
369 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
370                 const char *algo_string);
371
372 /**
373  * Provide the Asymmetric xform enum, given an xform string
374  *
375  * @param       xform_enum      A pointer to the xform type
376  *                              enum to be filled
377  * @param       xform_string    xform string
378  *
379  * @return
380  * - Return -1 if string is not valid
381  * - Return 0 if the string is valid
382  */
383 __rte_experimental
384 int
385 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
386                 const char *xform_string);
387
388
389 /** Macro used at end of crypto PMD list */
390 #define RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() \
391         { RTE_CRYPTO_OP_TYPE_UNDEFINED }
392
393
394 /**
395  * Crypto device supported feature flags
396  *
397  * Note:
398  * New features flags should be added to the end of the list
399  *
400  * Keep these flags synchronised with rte_cryptodev_get_feature_name()
401  */
402 #define RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO               (1ULL << 0)
403 /**< Symmetric crypto operations are supported */
404 #define RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO              (1ULL << 1)
405 /**< Asymmetric crypto operations are supported */
406 #define RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING         (1ULL << 2)
407 /**< Chaining symmetric crypto operations are supported */
408 #define RTE_CRYPTODEV_FF_CPU_SSE                        (1ULL << 3)
409 /**< Utilises CPU SIMD SSE instructions */
410 #define RTE_CRYPTODEV_FF_CPU_AVX                        (1ULL << 4)
411 /**< Utilises CPU SIMD AVX instructions */
412 #define RTE_CRYPTODEV_FF_CPU_AVX2                       (1ULL << 5)
413 /**< Utilises CPU SIMD AVX2 instructions */
414 #define RTE_CRYPTODEV_FF_CPU_AESNI                      (1ULL << 6)
415 /**< Utilises CPU AES-NI instructions */
416 #define RTE_CRYPTODEV_FF_HW_ACCELERATED                 (1ULL << 7)
417 /**< Operations are off-loaded to an
418  * external hardware accelerator
419  */
420 #define RTE_CRYPTODEV_FF_CPU_AVX512                     (1ULL << 8)
421 /**< Utilises CPU SIMD AVX512 instructions */
422 #define RTE_CRYPTODEV_FF_IN_PLACE_SGL                   (1ULL << 9)
423 /**< In-place Scatter-gather (SGL) buffers, with multiple segments,
424  * are supported
425  */
426 #define RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT             (1ULL << 10)
427 /**< Out-of-place Scatter-gather (SGL) buffers are
428  * supported in input and output
429  */
430 #define RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT              (1ULL << 11)
431 /**< Out-of-place Scatter-gather (SGL) buffers are supported
432  * in input, combined with linear buffers (LB), with a
433  * single segment in output
434  */
435 #define RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT              (1ULL << 12)
436 /**< Out-of-place Scatter-gather (SGL) buffers are supported
437  * in output, combined with linear buffers (LB) in input
438  */
439 #define RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT               (1ULL << 13)
440 /**< Out-of-place linear buffers (LB) are supported in input and output */
441 #define RTE_CRYPTODEV_FF_CPU_NEON                       (1ULL << 14)
442 /**< Utilises CPU NEON instructions */
443 #define RTE_CRYPTODEV_FF_CPU_ARM_CE                     (1ULL << 15)
444 /**< Utilises ARM CPU Cryptographic Extensions */
445 #define RTE_CRYPTODEV_FF_SECURITY                       (1ULL << 16)
446 /**< Support Security Protocol Processing */
447 #define RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP            (1ULL << 17)
448 /**< Support RSA Private Key OP with exponent */
449 #define RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT             (1ULL << 18)
450 /**< Support RSA Private Key OP with CRT (quintuple) Keys */
451 #define RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED               (1ULL << 19)
452 /**< Support encrypted-digest operations where digest is appended to data */
453 #define RTE_CRYPTODEV_FF_ASYM_SESSIONLESS               (1ULL << 20)
454 /**< Support asymmetric session-less operations */
455 #define RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO                 (1ULL << 21)
456 /**< Support symmetric cpu-crypto processing */
457 #define RTE_CRYPTODEV_FF_SYM_SESSIONLESS                (1ULL << 22)
458 /**< Support symmetric session-less operations */
459
460
461 /**
462  * Get the name of a crypto device feature flag
463  *
464  * @param       flag    The mask describing the flag.
465  *
466  * @return
467  *   The name of this flag, or NULL if it's not a valid feature flag.
468  */
469
470 extern const char *
471 rte_cryptodev_get_feature_name(uint64_t flag);
472
473 /**  Crypto device information */
474 struct rte_cryptodev_info {
475         const char *driver_name;        /**< Driver name. */
476         uint8_t driver_id;              /**< Driver identifier */
477         struct rte_device *device;      /**< Generic device information. */
478
479         uint64_t feature_flags;
480         /**< Feature flags exposes HW/SW features for the given device */
481
482         const struct rte_cryptodev_capabilities *capabilities;
483         /**< Array of devices supported capabilities */
484
485         unsigned max_nb_queue_pairs;
486         /**< Maximum number of queues pairs supported by device. */
487
488         uint16_t min_mbuf_headroom_req;
489         /**< Minimum mbuf headroom required by device */
490
491         uint16_t min_mbuf_tailroom_req;
492         /**< Minimum mbuf tailroom required by device */
493
494         struct {
495                 unsigned max_nb_sessions;
496                 /**< Maximum number of sessions supported by device.
497                  * If 0, the device does not have any limitation in
498                  * number of sessions that can be used.
499                  */
500         } sym;
501 };
502
503 #define RTE_CRYPTODEV_DETACHED  (0)
504 #define RTE_CRYPTODEV_ATTACHED  (1)
505
506 /** Definitions of Crypto device event types */
507 enum rte_cryptodev_event_type {
508         RTE_CRYPTODEV_EVENT_UNKNOWN,    /**< unknown event type */
509         RTE_CRYPTODEV_EVENT_ERROR,      /**< error interrupt event */
510         RTE_CRYPTODEV_EVENT_MAX         /**< max value of this enum */
511 };
512
513 /** Crypto device queue pair configuration structure. */
514 struct rte_cryptodev_qp_conf {
515         uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
516         struct rte_mempool *mp_session;
517         /**< The mempool for creating session in sessionless mode */
518         struct rte_mempool *mp_session_private;
519         /**< The mempool for creating sess private data in sessionless mode */
520 };
521
522 /**
523  * Typedef for application callback function to be registered by application
524  * software for notification of device events
525  *
526  * @param       dev_id  Crypto device identifier
527  * @param       event   Crypto device event to register for notification of.
528  * @param       cb_arg  User specified parameter to be passed as to passed to
529  *                      users callback function.
530  */
531 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
532                 enum rte_cryptodev_event_type event, void *cb_arg);
533
534
535 /** Crypto Device statistics */
536 struct rte_cryptodev_stats {
537         uint64_t enqueued_count;
538         /**< Count of all operations enqueued */
539         uint64_t dequeued_count;
540         /**< Count of all operations dequeued */
541
542         uint64_t enqueue_err_count;
543         /**< Total error count on operations enqueued */
544         uint64_t dequeue_err_count;
545         /**< Total error count on operations dequeued */
546 };
547
548 #define RTE_CRYPTODEV_NAME_MAX_LEN      (64)
549 /**< Max length of name of crypto PMD */
550
551 /**
552  * Get the device identifier for the named crypto device.
553  *
554  * @param       name    device name to select the device structure.
555  *
556  * @return
557  *   - Returns crypto device identifier on success.
558  *   - Return -1 on failure to find named crypto device.
559  */
560 extern int
561 rte_cryptodev_get_dev_id(const char *name);
562
563 /**
564  * Get the crypto device name given a device identifier.
565  *
566  * @param dev_id
567  *   The identifier of the device
568  *
569  * @return
570  *   - Returns crypto device name.
571  *   - Returns NULL if crypto device is not present.
572  */
573 extern const char *
574 rte_cryptodev_name_get(uint8_t dev_id);
575
576 /**
577  * Get the total number of crypto devices that have been successfully
578  * initialised.
579  *
580  * @return
581  *   - The total number of usable crypto devices.
582  */
583 extern uint8_t
584 rte_cryptodev_count(void);
585
586 /**
587  * Get number of crypto device defined type.
588  *
589  * @param       driver_id       driver identifier.
590  *
591  * @return
592  *   Returns number of crypto device.
593  */
594 extern uint8_t
595 rte_cryptodev_device_count_by_driver(uint8_t driver_id);
596
597 /**
598  * Get number and identifiers of attached crypto devices that
599  * use the same crypto driver.
600  *
601  * @param       driver_name     driver name.
602  * @param       devices         output devices identifiers.
603  * @param       nb_devices      maximal number of devices.
604  *
605  * @return
606  *   Returns number of attached crypto device.
607  */
608 uint8_t
609 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
610                 uint8_t nb_devices);
611 /*
612  * Return the NUMA socket to which a device is connected
613  *
614  * @param dev_id
615  *   The identifier of the device
616  * @return
617  *   The NUMA socket id to which the device is connected or
618  *   a default of zero if the socket could not be determined.
619  *   -1 if returned is the dev_id value is out of range.
620  */
621 extern int
622 rte_cryptodev_socket_id(uint8_t dev_id);
623
624 /** Crypto device configuration structure */
625 struct rte_cryptodev_config {
626         int socket_id;                  /**< Socket to allocate resources on */
627         uint16_t nb_queue_pairs;
628         /**< Number of queue pairs to configure on device */
629         uint64_t ff_disable;
630         /**< Feature flags to be disabled. Only the following features are
631          * allowed to be disabled,
632          *  - RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO
633          *  - RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO
634          *  - RTE_CRYTPODEV_FF_SECURITY
635          */
636 };
637
638 /**
639  * Configure a device.
640  *
641  * This function must be invoked first before any other function in the
642  * API. This function can also be re-invoked when a device is in the
643  * stopped state.
644  *
645  * @param       dev_id          The identifier of the device to configure.
646  * @param       config          The crypto device configuration structure.
647  *
648  * @return
649  *   - 0: Success, device configured.
650  *   - <0: Error code returned by the driver configuration function.
651  */
652 extern int
653 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
654
655 /**
656  * Start an device.
657  *
658  * The device start step is the last one and consists of setting the configured
659  * offload features and in starting the transmit and the receive units of the
660  * device.
661  * On success, all basic functions exported by the API (link status,
662  * receive/transmit, and so on) can be invoked.
663  *
664  * @param dev_id
665  *   The identifier of the device.
666  * @return
667  *   - 0: Success, device started.
668  *   - <0: Error code of the driver device start function.
669  */
670 extern int
671 rte_cryptodev_start(uint8_t dev_id);
672
673 /**
674  * Stop an device. The device can be restarted with a call to
675  * rte_cryptodev_start()
676  *
677  * @param       dev_id          The identifier of the device.
678  */
679 extern void
680 rte_cryptodev_stop(uint8_t dev_id);
681
682 /**
683  * Close an device. The device cannot be restarted!
684  *
685  * @param       dev_id          The identifier of the device.
686  *
687  * @return
688  *  - 0 on successfully closing device
689  *  - <0 on failure to close device
690  */
691 extern int
692 rte_cryptodev_close(uint8_t dev_id);
693
694 /**
695  * Allocate and set up a receive queue pair for a device.
696  *
697  *
698  * @param       dev_id          The identifier of the device.
699  * @param       queue_pair_id   The index of the queue pairs to set up. The
700  *                              value must be in the range [0, nb_queue_pair
701  *                              - 1] previously supplied to
702  *                              rte_cryptodev_configure().
703  * @param       qp_conf         The pointer to the configuration data to be
704  *                              used for the queue pair.
705  * @param       socket_id       The *socket_id* argument is the socket
706  *                              identifier in case of NUMA. The value can be
707  *                              *SOCKET_ID_ANY* if there is no NUMA constraint
708  *                              for the DMA memory allocated for the receive
709  *                              queue pair.
710  *
711  * @return
712  *   - 0: Success, queue pair correctly set up.
713  *   - <0: Queue pair configuration failed
714  */
715 extern int
716 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
717                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id);
718
719 /**
720  * Get the number of queue pairs on a specific crypto device
721  *
722  * @param       dev_id          Crypto device identifier.
723  * @return
724  *   - The number of configured queue pairs.
725  */
726 extern uint16_t
727 rte_cryptodev_queue_pair_count(uint8_t dev_id);
728
729
730 /**
731  * Retrieve the general I/O statistics of a device.
732  *
733  * @param       dev_id          The identifier of the device.
734  * @param       stats           A pointer to a structure of type
735  *                              *rte_cryptodev_stats* to be filled with the
736  *                              values of device counters.
737  * @return
738  *   - Zero if successful.
739  *   - Non-zero otherwise.
740  */
741 extern int
742 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
743
744 /**
745  * Reset the general I/O statistics of a device.
746  *
747  * @param       dev_id          The identifier of the device.
748  */
749 extern void
750 rte_cryptodev_stats_reset(uint8_t dev_id);
751
752 /**
753  * Retrieve the contextual information of a device.
754  *
755  * @param       dev_id          The identifier of the device.
756  * @param       dev_info        A pointer to a structure of type
757  *                              *rte_cryptodev_info* to be filled with the
758  *                              contextual information of the device.
759  *
760  * @note The capabilities field of dev_info is set to point to the first
761  * element of an array of struct rte_cryptodev_capabilities. The element after
762  * the last valid element has it's op field set to
763  * RTE_CRYPTO_OP_TYPE_UNDEFINED.
764  */
765 extern void
766 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
767
768
769 /**
770  * Register a callback function for specific device id.
771  *
772  * @param       dev_id          Device id.
773  * @param       event           Event interested.
774  * @param       cb_fn           User supplied callback function to be called.
775  * @param       cb_arg          Pointer to the parameters for the registered
776  *                              callback.
777  *
778  * @return
779  *  - On success, zero.
780  *  - On failure, a negative value.
781  */
782 extern int
783 rte_cryptodev_callback_register(uint8_t dev_id,
784                 enum rte_cryptodev_event_type event,
785                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
786
787 /**
788  * Unregister a callback function for specific device id.
789  *
790  * @param       dev_id          The device identifier.
791  * @param       event           Event interested.
792  * @param       cb_fn           User supplied callback function to be called.
793  * @param       cb_arg          Pointer to the parameters for the registered
794  *                              callback.
795  *
796  * @return
797  *  - On success, zero.
798  *  - On failure, a negative value.
799  */
800 extern int
801 rte_cryptodev_callback_unregister(uint8_t dev_id,
802                 enum rte_cryptodev_event_type event,
803                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
804
805
806 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
807                 struct rte_crypto_op **ops,     uint16_t nb_ops);
808 /**< Dequeue processed packets from queue pair of a device. */
809
810 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
811                 struct rte_crypto_op **ops,     uint16_t nb_ops);
812 /**< Enqueue packets for processing on queue pair of a device. */
813
814
815
816
817 struct rte_cryptodev_callback;
818
819 /** Structure to keep track of registered callbacks */
820 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
821
822 /** The data structure associated with each crypto device. */
823 struct rte_cryptodev {
824         dequeue_pkt_burst_t dequeue_burst;
825         /**< Pointer to PMD receive function. */
826         enqueue_pkt_burst_t enqueue_burst;
827         /**< Pointer to PMD transmit function. */
828
829         struct rte_cryptodev_data *data;
830         /**< Pointer to device data */
831         struct rte_cryptodev_ops *dev_ops;
832         /**< Functions exported by PMD */
833         uint64_t feature_flags;
834         /**< Feature flags exposes HW/SW features for the given device */
835         struct rte_device *device;
836         /**< Backing device */
837
838         uint8_t driver_id;
839         /**< Crypto driver identifier*/
840
841         struct rte_cryptodev_cb_list link_intr_cbs;
842         /**< User application callback for interrupts if present */
843
844         void *security_ctx;
845         /**< Context for security ops */
846
847         __extension__
848         uint8_t attached : 1;
849         /**< Flag indicating the device is attached */
850 } __rte_cache_aligned;
851
852 void *
853 rte_cryptodev_get_sec_ctx(uint8_t dev_id);
854
855 /**
856  *
857  * The data part, with no function pointers, associated with each device.
858  *
859  * This structure is safe to place in shared memory to be common among
860  * different processes in a multi-process configuration.
861  */
862 struct rte_cryptodev_data {
863         uint8_t dev_id;
864         /**< Device ID for this instance */
865         uint8_t socket_id;
866         /**< Socket ID where memory is allocated */
867         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
868         /**< Unique identifier name */
869
870         __extension__
871         uint8_t dev_started : 1;
872         /**< Device state: STARTED(1)/STOPPED(0) */
873
874         struct rte_mempool *session_pool;
875         /**< Session memory pool */
876         void **queue_pairs;
877         /**< Array of pointers to queue pairs. */
878         uint16_t nb_queue_pairs;
879         /**< Number of device queue pairs. */
880
881         void *dev_private;
882         /**< PMD-specific private data */
883 } __rte_cache_aligned;
884
885 extern struct rte_cryptodev *rte_cryptodevs;
886 /**
887  *
888  * Dequeue a burst of processed crypto operations from a queue on the crypto
889  * device. The dequeued operation are stored in *rte_crypto_op* structures
890  * whose pointers are supplied in the *ops* array.
891  *
892  * The rte_cryptodev_dequeue_burst() function returns the number of ops
893  * actually dequeued, which is the number of *rte_crypto_op* data structures
894  * effectively supplied into the *ops* array.
895  *
896  * A return value equal to *nb_ops* indicates that the queue contained
897  * at least *nb_ops* operations, and this is likely to signify that other
898  * processed operations remain in the devices output queue. Applications
899  * implementing a "retrieve as many processed operations as possible" policy
900  * can check this specific case and keep invoking the
901  * rte_cryptodev_dequeue_burst() function until a value less than
902  * *nb_ops* is returned.
903  *
904  * The rte_cryptodev_dequeue_burst() function does not provide any error
905  * notification to avoid the corresponding overhead.
906  *
907  * @param       dev_id          The symmetric crypto device identifier
908  * @param       qp_id           The index of the queue pair from which to
909  *                              retrieve processed packets. The value must be
910  *                              in the range [0, nb_queue_pair - 1] previously
911  *                              supplied to rte_cryptodev_configure().
912  * @param       ops             The address of an array of pointers to
913  *                              *rte_crypto_op* structures that must be
914  *                              large enough to store *nb_ops* pointers in it.
915  * @param       nb_ops          The maximum number of operations to dequeue.
916  *
917  * @return
918  *   - The number of operations actually dequeued, which is the number
919  *   of pointers to *rte_crypto_op* structures effectively supplied to the
920  *   *ops* array.
921  */
922 static inline uint16_t
923 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
924                 struct rte_crypto_op **ops, uint16_t nb_ops)
925 {
926         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
927
928         nb_ops = (*dev->dequeue_burst)
929                         (dev->data->queue_pairs[qp_id], ops, nb_ops);
930
931         rte_cryptodev_trace_dequeue_burst(dev_id, qp_id, (void **)ops, nb_ops);
932         return nb_ops;
933 }
934
935 /**
936  * Enqueue a burst of operations for processing on a crypto device.
937  *
938  * The rte_cryptodev_enqueue_burst() function is invoked to place
939  * crypto operations on the queue *qp_id* of the device designated by
940  * its *dev_id*.
941  *
942  * The *nb_ops* parameter is the number of operations to process which are
943  * supplied in the *ops* array of *rte_crypto_op* structures.
944  *
945  * The rte_cryptodev_enqueue_burst() function returns the number of
946  * operations it actually enqueued for processing. A return value equal to
947  * *nb_ops* means that all packets have been enqueued.
948  *
949  * @param       dev_id          The identifier of the device.
950  * @param       qp_id           The index of the queue pair which packets are
951  *                              to be enqueued for processing. The value
952  *                              must be in the range [0, nb_queue_pairs - 1]
953  *                              previously supplied to
954  *                               *rte_cryptodev_configure*.
955  * @param       ops             The address of an array of *nb_ops* pointers
956  *                              to *rte_crypto_op* structures which contain
957  *                              the crypto operations to be processed.
958  * @param       nb_ops          The number of operations to process.
959  *
960  * @return
961  * The number of operations actually enqueued on the crypto device. The return
962  * value can be less than the value of the *nb_ops* parameter when the
963  * crypto devices queue is full or if invalid parameters are specified in
964  * a *rte_crypto_op*.
965  */
966 static inline uint16_t
967 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
968                 struct rte_crypto_op **ops, uint16_t nb_ops)
969 {
970         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
971
972         rte_cryptodev_trace_enqueue_burst(dev_id, qp_id, (void **)ops, nb_ops);
973         return (*dev->enqueue_burst)(
974                         dev->data->queue_pairs[qp_id], ops, nb_ops);
975 }
976
977
978 /** Cryptodev symmetric crypto session
979  * Each session is derived from a fixed xform chain. Therefore each session
980  * has a fixed algo, key, op-type, digest_len etc.
981  */
982 struct rte_cryptodev_sym_session {
983         uint64_t opaque_data;
984         /**< Can be used for external metadata */
985         uint16_t nb_drivers;
986         /**< number of elements in sess_data array */
987         uint16_t user_data_sz;
988         /**< session user data will be placed after sess_data */
989         __extension__ struct {
990                 void *data;
991                 uint16_t refcnt;
992         } sess_data[0];
993         /**< Driver specific session material, variable size */
994 };
995
996 /** Cryptodev asymmetric crypto session */
997 struct rte_cryptodev_asym_session {
998         __extension__ void *sess_private_data[0];
999         /**< Private asymmetric session material */
1000 };
1001
1002 /**
1003  * Create a symmetric session mempool.
1004  *
1005  * @param name
1006  *   The unique mempool name.
1007  * @param nb_elts
1008  *   The number of elements in the mempool.
1009  * @param elt_size
1010  *   The size of the element. This value will be ignored if it is smaller than
1011  *   the minimum session header size required for the system. For the user who
1012  *   want to use the same mempool for sym session and session private data it
1013  *   can be the maximum value of all existing devices' private data and session
1014  *   header sizes.
1015  * @param cache_size
1016  *   The number of per-lcore cache elements
1017  * @param priv_size
1018  *   The private data size of each session.
1019  * @param socket_id
1020  *   The *socket_id* argument is the socket identifier in the case of
1021  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
1022  *   constraint for the reserved zone.
1023  *
1024  * @return
1025  *  - On success return size of the session
1026  *  - On failure returns 0
1027  */
1028 __rte_experimental
1029 struct rte_mempool *
1030 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1031         uint32_t elt_size, uint32_t cache_size, uint16_t priv_size,
1032         int socket_id);
1033
1034 /**
1035  * Create symmetric crypto session header (generic with no private data)
1036  *
1037  * @param   mempool    Symmetric session mempool to allocate session
1038  *                     objects from
1039  * @return
1040  *  - On success return pointer to sym-session
1041  *  - On failure returns NULL
1042  */
1043 struct rte_cryptodev_sym_session *
1044 rte_cryptodev_sym_session_create(struct rte_mempool *mempool);
1045
1046 /**
1047  * Create asymmetric crypto session header (generic with no private data)
1048  *
1049  * @param   mempool    mempool to allocate asymmetric session
1050  *                     objects from
1051  * @return
1052  *  - On success return pointer to asym-session
1053  *  - On failure returns NULL
1054  */
1055 __rte_experimental
1056 struct rte_cryptodev_asym_session *
1057 rte_cryptodev_asym_session_create(struct rte_mempool *mempool);
1058
1059 /**
1060  * Frees symmetric crypto session header, after checking that all
1061  * the device private data has been freed, returning it
1062  * to its original mempool.
1063  *
1064  * @param   sess     Session header to be freed.
1065  *
1066  * @return
1067  *  - 0 if successful.
1068  *  - -EINVAL if session is NULL.
1069  *  - -EBUSY if not all device private data has been freed.
1070  */
1071 int
1072 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess);
1073
1074 /**
1075  * Frees asymmetric crypto session header, after checking that all
1076  * the device private data has been freed, returning it
1077  * to its original mempool.
1078  *
1079  * @param   sess     Session header to be freed.
1080  *
1081  * @return
1082  *  - 0 if successful.
1083  *  - -EINVAL if session is NULL.
1084  *  - -EBUSY if not all device private data has been freed.
1085  */
1086 __rte_experimental
1087 int
1088 rte_cryptodev_asym_session_free(struct rte_cryptodev_asym_session *sess);
1089
1090 /**
1091  * Fill out private data for the device id, based on its device type.
1092  *
1093  * @param   dev_id   ID of device that we want the session to be used on
1094  * @param   sess     Session where the private data will be attached to
1095  * @param   xforms   Symmetric crypto transform operations to apply on flow
1096  *                   processed with this session
1097  * @param   mempool  Mempool where the private data is allocated.
1098  *
1099  * @return
1100  *  - On success, zero.
1101  *  - -EINVAL if input parameters are invalid.
1102  *  - -ENOTSUP if crypto device does not support the crypto transform or
1103  *    does not support symmetric operations.
1104  *  - -ENOMEM if the private session could not be allocated.
1105  */
1106 int
1107 rte_cryptodev_sym_session_init(uint8_t dev_id,
1108                         struct rte_cryptodev_sym_session *sess,
1109                         struct rte_crypto_sym_xform *xforms,
1110                         struct rte_mempool *mempool);
1111
1112 /**
1113  * Initialize asymmetric session on a device with specific asymmetric xform
1114  *
1115  * @param   dev_id   ID of device that we want the session to be used on
1116  * @param   sess     Session to be set up on a device
1117  * @param   xforms   Asymmetric crypto transform operations to apply on flow
1118  *                   processed with this session
1119  * @param   mempool  Mempool to be used for internal allocation.
1120  *
1121  * @return
1122  *  - On success, zero.
1123  *  - -EINVAL if input parameters are invalid.
1124  *  - -ENOTSUP if crypto device does not support the crypto transform.
1125  *  - -ENOMEM if the private session could not be allocated.
1126  */
1127 __rte_experimental
1128 int
1129 rte_cryptodev_asym_session_init(uint8_t dev_id,
1130                         struct rte_cryptodev_asym_session *sess,
1131                         struct rte_crypto_asym_xform *xforms,
1132                         struct rte_mempool *mempool);
1133
1134 /**
1135  * Frees private data for the device id, based on its device type,
1136  * returning it to its mempool. It is the application's responsibility
1137  * to ensure that private session data is not cleared while there are
1138  * still in-flight operations using it.
1139  *
1140  * @param   dev_id   ID of device that uses the session.
1141  * @param   sess     Session containing the reference to the private data
1142  *
1143  * @return
1144  *  - 0 if successful.
1145  *  - -EINVAL if device is invalid or session is NULL.
1146  *  - -ENOTSUP if crypto device does not support symmetric operations.
1147  */
1148 int
1149 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1150                         struct rte_cryptodev_sym_session *sess);
1151
1152 /**
1153  * Frees resources held by asymmetric session during rte_cryptodev_session_init
1154  *
1155  * @param   dev_id   ID of device that uses the asymmetric session.
1156  * @param   sess     Asymmetric session setup on device using
1157  *                                       rte_cryptodev_session_init
1158  * @return
1159  *  - 0 if successful.
1160  *  - -EINVAL if device is invalid or session is NULL.
1161  */
1162 __rte_experimental
1163 int
1164 rte_cryptodev_asym_session_clear(uint8_t dev_id,
1165                         struct rte_cryptodev_asym_session *sess);
1166
1167 /**
1168  * Get the size of the header session, for all registered drivers excluding
1169  * the user data size.
1170  *
1171  * @return
1172  *   Size of the symmetric header session.
1173  */
1174 unsigned int
1175 rte_cryptodev_sym_get_header_session_size(void);
1176
1177 /**
1178  * Get the size of the header session from created session.
1179  *
1180  * @param sess
1181  *   The sym cryptodev session pointer
1182  *
1183  * @return
1184  *   - If sess is not NULL, return the size of the header session including
1185  *   the private data size defined within sess.
1186  *   - If sess is NULL, return 0.
1187  */
1188 __rte_experimental
1189 unsigned int
1190 rte_cryptodev_sym_get_existing_header_session_size(
1191                 struct rte_cryptodev_sym_session *sess);
1192
1193 /**
1194  * Get the size of the asymmetric session header, for all registered drivers.
1195  *
1196  * @return
1197  *   Size of the asymmetric header session.
1198  */
1199 __rte_experimental
1200 unsigned int
1201 rte_cryptodev_asym_get_header_session_size(void);
1202
1203 /**
1204  * Get the size of the private symmetric session data
1205  * for a device.
1206  *
1207  * @param       dev_id          The device identifier.
1208  *
1209  * @return
1210  *   - Size of the private data, if successful
1211  *   - 0 if device is invalid or does not have private
1212  *   symmetric session
1213  */
1214 unsigned int
1215 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id);
1216
1217 /**
1218  * Get the size of the private data for asymmetric session
1219  * on device
1220  *
1221  * @param       dev_id          The device identifier.
1222  *
1223  * @return
1224  *   - Size of the asymmetric private data, if successful
1225  *   - 0 if device is invalid or does not have private session
1226  */
1227 __rte_experimental
1228 unsigned int
1229 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id);
1230
1231 /**
1232  * Provide driver identifier.
1233  *
1234  * @param name
1235  *   The pointer to a driver name.
1236  * @return
1237  *  The driver type identifier or -1 if no driver found
1238  */
1239 int rte_cryptodev_driver_id_get(const char *name);
1240
1241 /**
1242  * Provide driver name.
1243  *
1244  * @param driver_id
1245  *   The driver identifier.
1246  * @return
1247  *  The driver name or null if no driver found
1248  */
1249 const char *rte_cryptodev_driver_name_get(uint8_t driver_id);
1250
1251 /**
1252  * Store user data in a session.
1253  *
1254  * @param       sess            Session pointer allocated by
1255  *                              *rte_cryptodev_sym_session_create*.
1256  * @param       data            Pointer to the user data.
1257  * @param       size            Size of the user data.
1258  *
1259  * @return
1260  *  - On success, zero.
1261  *  - On failure, a negative value.
1262  */
1263 __rte_experimental
1264 int
1265 rte_cryptodev_sym_session_set_user_data(
1266                                         struct rte_cryptodev_sym_session *sess,
1267                                         void *data,
1268                                         uint16_t size);
1269
1270 /**
1271  * Get user data stored in a session.
1272  *
1273  * @param       sess            Session pointer allocated by
1274  *                              *rte_cryptodev_sym_session_create*.
1275  *
1276  * @return
1277  *  - On success return pointer to user data.
1278  *  - On failure returns NULL.
1279  */
1280 __rte_experimental
1281 void *
1282 rte_cryptodev_sym_session_get_user_data(
1283                                         struct rte_cryptodev_sym_session *sess);
1284
1285 /**
1286  * Perform actual crypto processing (encrypt/digest or auth/decrypt)
1287  * on user provided data.
1288  *
1289  * @param       dev_id  The device identifier.
1290  * @param       sess    Cryptodev session structure
1291  * @param       ofs     Start and stop offsets for auth and cipher operations
1292  * @param       vec     Vectorized operation descriptor
1293  *
1294  * @return
1295  *  - Returns number of successfully processed packets.
1296  */
1297 __rte_experimental
1298 uint32_t
1299 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
1300         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
1301         struct rte_crypto_sym_vec *vec);
1302
1303 #ifdef __cplusplus
1304 }
1305 #endif
1306
1307 #endif /* _RTE_CRYPTODEV_H_ */