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