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