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