26abd0c52dae2795f7b6aa87f6679cce3dffc63c
[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 status of queue pairs setup on a specific crypto device
723  *
724  * @param       dev_id          Crypto device identifier.
725  * @param       queue_pair_id   The index of the queue pairs to set up. The
726  *                              value must be in the range [0, nb_queue_pair
727  *                              - 1] previously supplied to
728  *                              rte_cryptodev_configure().
729  * @return
730  *   - 0: qp was not configured
731  *       - 1: qp was configured
732  *       - -EINVAL: device was not configured
733  */
734 __rte_experimental
735 int
736 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id);
737
738 /**
739  * Get the number of queue pairs on a specific crypto device
740  *
741  * @param       dev_id          Crypto device identifier.
742  * @return
743  *   - The number of configured queue pairs.
744  */
745 extern uint16_t
746 rte_cryptodev_queue_pair_count(uint8_t dev_id);
747
748
749 /**
750  * Retrieve the general I/O statistics of a device.
751  *
752  * @param       dev_id          The identifier of the device.
753  * @param       stats           A pointer to a structure of type
754  *                              *rte_cryptodev_stats* to be filled with the
755  *                              values of device counters.
756  * @return
757  *   - Zero if successful.
758  *   - Non-zero otherwise.
759  */
760 extern int
761 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
762
763 /**
764  * Reset the general I/O statistics of a device.
765  *
766  * @param       dev_id          The identifier of the device.
767  */
768 extern void
769 rte_cryptodev_stats_reset(uint8_t dev_id);
770
771 /**
772  * Retrieve the contextual information of a device.
773  *
774  * @param       dev_id          The identifier of the device.
775  * @param       dev_info        A pointer to a structure of type
776  *                              *rte_cryptodev_info* to be filled with the
777  *                              contextual information of the device.
778  *
779  * @note The capabilities field of dev_info is set to point to the first
780  * element of an array of struct rte_cryptodev_capabilities. The element after
781  * the last valid element has it's op field set to
782  * RTE_CRYPTO_OP_TYPE_UNDEFINED.
783  */
784 extern void
785 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
786
787
788 /**
789  * Register a callback function for specific device id.
790  *
791  * @param       dev_id          Device id.
792  * @param       event           Event interested.
793  * @param       cb_fn           User supplied callback function to be called.
794  * @param       cb_arg          Pointer to the parameters for the registered
795  *                              callback.
796  *
797  * @return
798  *  - On success, zero.
799  *  - On failure, a negative value.
800  */
801 extern int
802 rte_cryptodev_callback_register(uint8_t dev_id,
803                 enum rte_cryptodev_event_type event,
804                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
805
806 /**
807  * Unregister a callback function for specific device id.
808  *
809  * @param       dev_id          The device identifier.
810  * @param       event           Event interested.
811  * @param       cb_fn           User supplied callback function to be called.
812  * @param       cb_arg          Pointer to the parameters for the registered
813  *                              callback.
814  *
815  * @return
816  *  - On success, zero.
817  *  - On failure, a negative value.
818  */
819 extern int
820 rte_cryptodev_callback_unregister(uint8_t dev_id,
821                 enum rte_cryptodev_event_type event,
822                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
823
824
825 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
826                 struct rte_crypto_op **ops,     uint16_t nb_ops);
827 /**< Dequeue processed packets from queue pair of a device. */
828
829 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
830                 struct rte_crypto_op **ops,     uint16_t nb_ops);
831 /**< Enqueue packets for processing on queue pair of a device. */
832
833
834
835
836 struct rte_cryptodev_callback;
837
838 /** Structure to keep track of registered callbacks */
839 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
840
841 /** The data structure associated with each crypto device. */
842 struct rte_cryptodev {
843         dequeue_pkt_burst_t dequeue_burst;
844         /**< Pointer to PMD receive function. */
845         enqueue_pkt_burst_t enqueue_burst;
846         /**< Pointer to PMD transmit function. */
847
848         struct rte_cryptodev_data *data;
849         /**< Pointer to device data */
850         struct rte_cryptodev_ops *dev_ops;
851         /**< Functions exported by PMD */
852         uint64_t feature_flags;
853         /**< Feature flags exposes HW/SW features for the given device */
854         struct rte_device *device;
855         /**< Backing device */
856
857         uint8_t driver_id;
858         /**< Crypto driver identifier*/
859
860         struct rte_cryptodev_cb_list link_intr_cbs;
861         /**< User application callback for interrupts if present */
862
863         void *security_ctx;
864         /**< Context for security ops */
865
866         __extension__
867         uint8_t attached : 1;
868         /**< Flag indicating the device is attached */
869 } __rte_cache_aligned;
870
871 void *
872 rte_cryptodev_get_sec_ctx(uint8_t dev_id);
873
874 /**
875  *
876  * The data part, with no function pointers, associated with each device.
877  *
878  * This structure is safe to place in shared memory to be common among
879  * different processes in a multi-process configuration.
880  */
881 struct rte_cryptodev_data {
882         uint8_t dev_id;
883         /**< Device ID for this instance */
884         uint8_t socket_id;
885         /**< Socket ID where memory is allocated */
886         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
887         /**< Unique identifier name */
888
889         __extension__
890         uint8_t dev_started : 1;
891         /**< Device state: STARTED(1)/STOPPED(0) */
892
893         struct rte_mempool *session_pool;
894         /**< Session memory pool */
895         void **queue_pairs;
896         /**< Array of pointers to queue pairs. */
897         uint16_t nb_queue_pairs;
898         /**< Number of device queue pairs. */
899
900         void *dev_private;
901         /**< PMD-specific private data */
902 } __rte_cache_aligned;
903
904 extern struct rte_cryptodev *rte_cryptodevs;
905 /**
906  *
907  * Dequeue a burst of processed crypto operations from a queue on the crypto
908  * device. The dequeued operation are stored in *rte_crypto_op* structures
909  * whose pointers are supplied in the *ops* array.
910  *
911  * The rte_cryptodev_dequeue_burst() function returns the number of ops
912  * actually dequeued, which is the number of *rte_crypto_op* data structures
913  * effectively supplied into the *ops* array.
914  *
915  * A return value equal to *nb_ops* indicates that the queue contained
916  * at least *nb_ops* operations, and this is likely to signify that other
917  * processed operations remain in the devices output queue. Applications
918  * implementing a "retrieve as many processed operations as possible" policy
919  * can check this specific case and keep invoking the
920  * rte_cryptodev_dequeue_burst() function until a value less than
921  * *nb_ops* is returned.
922  *
923  * The rte_cryptodev_dequeue_burst() function does not provide any error
924  * notification to avoid the corresponding overhead.
925  *
926  * @param       dev_id          The symmetric crypto device identifier
927  * @param       qp_id           The index of the queue pair from which to
928  *                              retrieve processed packets. The value must be
929  *                              in the range [0, nb_queue_pair - 1] previously
930  *                              supplied to rte_cryptodev_configure().
931  * @param       ops             The address of an array of pointers to
932  *                              *rte_crypto_op* structures that must be
933  *                              large enough to store *nb_ops* pointers in it.
934  * @param       nb_ops          The maximum number of operations to dequeue.
935  *
936  * @return
937  *   - The number of operations actually dequeued, which is the number
938  *   of pointers to *rte_crypto_op* structures effectively supplied to the
939  *   *ops* array.
940  */
941 static inline uint16_t
942 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
943                 struct rte_crypto_op **ops, uint16_t nb_ops)
944 {
945         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
946
947         nb_ops = (*dev->dequeue_burst)
948                         (dev->data->queue_pairs[qp_id], ops, nb_ops);
949
950         rte_cryptodev_trace_dequeue_burst(dev_id, qp_id, (void **)ops, nb_ops);
951         return nb_ops;
952 }
953
954 /**
955  * Enqueue a burst of operations for processing on a crypto device.
956  *
957  * The rte_cryptodev_enqueue_burst() function is invoked to place
958  * crypto operations on the queue *qp_id* of the device designated by
959  * its *dev_id*.
960  *
961  * The *nb_ops* parameter is the number of operations to process which are
962  * supplied in the *ops* array of *rte_crypto_op* structures.
963  *
964  * The rte_cryptodev_enqueue_burst() function returns the number of
965  * operations it actually enqueued for processing. A return value equal to
966  * *nb_ops* means that all packets have been enqueued.
967  *
968  * @param       dev_id          The identifier of the device.
969  * @param       qp_id           The index of the queue pair which packets are
970  *                              to be enqueued for processing. The value
971  *                              must be in the range [0, nb_queue_pairs - 1]
972  *                              previously supplied to
973  *                               *rte_cryptodev_configure*.
974  * @param       ops             The address of an array of *nb_ops* pointers
975  *                              to *rte_crypto_op* structures which contain
976  *                              the crypto operations to be processed.
977  * @param       nb_ops          The number of operations to process.
978  *
979  * @return
980  * The number of operations actually enqueued on the crypto device. The return
981  * value can be less than the value of the *nb_ops* parameter when the
982  * crypto devices queue is full or if invalid parameters are specified in
983  * a *rte_crypto_op*.
984  */
985 static inline uint16_t
986 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
987                 struct rte_crypto_op **ops, uint16_t nb_ops)
988 {
989         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
990
991         rte_cryptodev_trace_enqueue_burst(dev_id, qp_id, (void **)ops, nb_ops);
992         return (*dev->enqueue_burst)(
993                         dev->data->queue_pairs[qp_id], ops, nb_ops);
994 }
995
996
997 /** Cryptodev symmetric crypto session
998  * Each session is derived from a fixed xform chain. Therefore each session
999  * has a fixed algo, key, op-type, digest_len etc.
1000  */
1001 struct rte_cryptodev_sym_session {
1002         uint64_t opaque_data;
1003         /**< Can be used for external metadata */
1004         uint16_t nb_drivers;
1005         /**< number of elements in sess_data array */
1006         uint16_t user_data_sz;
1007         /**< session user data will be placed after sess_data */
1008         __extension__ struct {
1009                 void *data;
1010                 uint16_t refcnt;
1011         } sess_data[0];
1012         /**< Driver specific session material, variable size */
1013 };
1014
1015 /** Cryptodev asymmetric crypto session */
1016 struct rte_cryptodev_asym_session {
1017         __extension__ void *sess_private_data[0];
1018         /**< Private asymmetric session material */
1019 };
1020
1021 /**
1022  * Create a symmetric session mempool.
1023  *
1024  * @param name
1025  *   The unique mempool name.
1026  * @param nb_elts
1027  *   The number of elements in the mempool.
1028  * @param elt_size
1029  *   The size of the element. This value will be ignored if it is smaller than
1030  *   the minimum session header size required for the system. For the user who
1031  *   want to use the same mempool for sym session and session private data it
1032  *   can be the maximum value of all existing devices' private data and session
1033  *   header sizes.
1034  * @param cache_size
1035  *   The number of per-lcore cache elements
1036  * @param priv_size
1037  *   The private data size of each session.
1038  * @param socket_id
1039  *   The *socket_id* argument is the socket identifier in the case of
1040  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
1041  *   constraint for the reserved zone.
1042  *
1043  * @return
1044  *  - On success return size of the session
1045  *  - On failure returns 0
1046  */
1047 __rte_experimental
1048 struct rte_mempool *
1049 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1050         uint32_t elt_size, uint32_t cache_size, uint16_t priv_size,
1051         int socket_id);
1052
1053 /**
1054  * Create symmetric crypto session header (generic with no private data)
1055  *
1056  * @param   mempool    Symmetric session mempool to allocate session
1057  *                     objects from
1058  * @return
1059  *  - On success return pointer to sym-session
1060  *  - On failure returns NULL
1061  */
1062 struct rte_cryptodev_sym_session *
1063 rte_cryptodev_sym_session_create(struct rte_mempool *mempool);
1064
1065 /**
1066  * Create asymmetric crypto session header (generic with no private data)
1067  *
1068  * @param   mempool    mempool to allocate asymmetric session
1069  *                     objects from
1070  * @return
1071  *  - On success return pointer to asym-session
1072  *  - On failure returns NULL
1073  */
1074 __rte_experimental
1075 struct rte_cryptodev_asym_session *
1076 rte_cryptodev_asym_session_create(struct rte_mempool *mempool);
1077
1078 /**
1079  * Frees symmetric crypto session header, after checking that all
1080  * the device private data has been freed, returning it
1081  * to its original mempool.
1082  *
1083  * @param   sess     Session header to be freed.
1084  *
1085  * @return
1086  *  - 0 if successful.
1087  *  - -EINVAL if session is NULL.
1088  *  - -EBUSY if not all device private data has been freed.
1089  */
1090 int
1091 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess);
1092
1093 /**
1094  * Frees asymmetric crypto session header, after checking that all
1095  * the device private data has been freed, returning it
1096  * to its original mempool.
1097  *
1098  * @param   sess     Session header to be freed.
1099  *
1100  * @return
1101  *  - 0 if successful.
1102  *  - -EINVAL if session is NULL.
1103  *  - -EBUSY if not all device private data has been freed.
1104  */
1105 __rte_experimental
1106 int
1107 rte_cryptodev_asym_session_free(struct rte_cryptodev_asym_session *sess);
1108
1109 /**
1110  * Fill out private data for the device id, based on its device type.
1111  *
1112  * @param   dev_id   ID of device that we want the session to be used on
1113  * @param   sess     Session where the private data will be attached to
1114  * @param   xforms   Symmetric crypto transform operations to apply on flow
1115  *                   processed with this session
1116  * @param   mempool  Mempool where the private data is allocated.
1117  *
1118  * @return
1119  *  - On success, zero.
1120  *  - -EINVAL if input parameters are invalid.
1121  *  - -ENOTSUP if crypto device does not support the crypto transform or
1122  *    does not support symmetric operations.
1123  *  - -ENOMEM if the private session could not be allocated.
1124  */
1125 int
1126 rte_cryptodev_sym_session_init(uint8_t dev_id,
1127                         struct rte_cryptodev_sym_session *sess,
1128                         struct rte_crypto_sym_xform *xforms,
1129                         struct rte_mempool *mempool);
1130
1131 /**
1132  * Initialize asymmetric session on a device with specific asymmetric xform
1133  *
1134  * @param   dev_id   ID of device that we want the session to be used on
1135  * @param   sess     Session to be set up on a device
1136  * @param   xforms   Asymmetric crypto transform operations to apply on flow
1137  *                   processed with this session
1138  * @param   mempool  Mempool to be used for internal allocation.
1139  *
1140  * @return
1141  *  - On success, zero.
1142  *  - -EINVAL if input parameters are invalid.
1143  *  - -ENOTSUP if crypto device does not support the crypto transform.
1144  *  - -ENOMEM if the private session could not be allocated.
1145  */
1146 __rte_experimental
1147 int
1148 rte_cryptodev_asym_session_init(uint8_t dev_id,
1149                         struct rte_cryptodev_asym_session *sess,
1150                         struct rte_crypto_asym_xform *xforms,
1151                         struct rte_mempool *mempool);
1152
1153 /**
1154  * Frees private data for the device id, based on its device type,
1155  * returning it to its mempool. It is the application's responsibility
1156  * to ensure that private session data is not cleared while there are
1157  * still in-flight operations using it.
1158  *
1159  * @param   dev_id   ID of device that uses the session.
1160  * @param   sess     Session containing the reference to the private data
1161  *
1162  * @return
1163  *  - 0 if successful.
1164  *  - -EINVAL if device is invalid or session is NULL.
1165  *  - -ENOTSUP if crypto device does not support symmetric operations.
1166  */
1167 int
1168 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1169                         struct rte_cryptodev_sym_session *sess);
1170
1171 /**
1172  * Frees resources held by asymmetric session during rte_cryptodev_session_init
1173  *
1174  * @param   dev_id   ID of device that uses the asymmetric session.
1175  * @param   sess     Asymmetric session setup on device using
1176  *                                       rte_cryptodev_session_init
1177  * @return
1178  *  - 0 if successful.
1179  *  - -EINVAL if device is invalid or session is NULL.
1180  */
1181 __rte_experimental
1182 int
1183 rte_cryptodev_asym_session_clear(uint8_t dev_id,
1184                         struct rte_cryptodev_asym_session *sess);
1185
1186 /**
1187  * Get the size of the header session, for all registered drivers excluding
1188  * the user data size.
1189  *
1190  * @return
1191  *   Size of the symmetric header session.
1192  */
1193 unsigned int
1194 rte_cryptodev_sym_get_header_session_size(void);
1195
1196 /**
1197  * Get the size of the header session from created session.
1198  *
1199  * @param sess
1200  *   The sym cryptodev session pointer
1201  *
1202  * @return
1203  *   - If sess is not NULL, return the size of the header session including
1204  *   the private data size defined within sess.
1205  *   - If sess is NULL, return 0.
1206  */
1207 __rte_experimental
1208 unsigned int
1209 rte_cryptodev_sym_get_existing_header_session_size(
1210                 struct rte_cryptodev_sym_session *sess);
1211
1212 /**
1213  * Get the size of the asymmetric session header, for all registered drivers.
1214  *
1215  * @return
1216  *   Size of the asymmetric header session.
1217  */
1218 __rte_experimental
1219 unsigned int
1220 rte_cryptodev_asym_get_header_session_size(void);
1221
1222 /**
1223  * Get the size of the private symmetric session data
1224  * for a device.
1225  *
1226  * @param       dev_id          The device identifier.
1227  *
1228  * @return
1229  *   - Size of the private data, if successful
1230  *   - 0 if device is invalid or does not have private
1231  *   symmetric session
1232  */
1233 unsigned int
1234 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id);
1235
1236 /**
1237  * Get the size of the private data for asymmetric session
1238  * on device
1239  *
1240  * @param       dev_id          The device identifier.
1241  *
1242  * @return
1243  *   - Size of the asymmetric private data, if successful
1244  *   - 0 if device is invalid or does not have private session
1245  */
1246 __rte_experimental
1247 unsigned int
1248 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id);
1249
1250 /**
1251  * Provide driver identifier.
1252  *
1253  * @param name
1254  *   The pointer to a driver name.
1255  * @return
1256  *  The driver type identifier or -1 if no driver found
1257  */
1258 int rte_cryptodev_driver_id_get(const char *name);
1259
1260 /**
1261  * Provide driver name.
1262  *
1263  * @param driver_id
1264  *   The driver identifier.
1265  * @return
1266  *  The driver name or null if no driver found
1267  */
1268 const char *rte_cryptodev_driver_name_get(uint8_t driver_id);
1269
1270 /**
1271  * Store user data in a session.
1272  *
1273  * @param       sess            Session pointer allocated by
1274  *                              *rte_cryptodev_sym_session_create*.
1275  * @param       data            Pointer to the user data.
1276  * @param       size            Size of the user data.
1277  *
1278  * @return
1279  *  - On success, zero.
1280  *  - On failure, a negative value.
1281  */
1282 __rte_experimental
1283 int
1284 rte_cryptodev_sym_session_set_user_data(
1285                                         struct rte_cryptodev_sym_session *sess,
1286                                         void *data,
1287                                         uint16_t size);
1288
1289 /**
1290  * Get user data stored in a session.
1291  *
1292  * @param       sess            Session pointer allocated by
1293  *                              *rte_cryptodev_sym_session_create*.
1294  *
1295  * @return
1296  *  - On success return pointer to user data.
1297  *  - On failure returns NULL.
1298  */
1299 __rte_experimental
1300 void *
1301 rte_cryptodev_sym_session_get_user_data(
1302                                         struct rte_cryptodev_sym_session *sess);
1303
1304 /**
1305  * Perform actual crypto processing (encrypt/digest or auth/decrypt)
1306  * on user provided data.
1307  *
1308  * @param       dev_id  The device identifier.
1309  * @param       sess    Cryptodev session structure
1310  * @param       ofs     Start and stop offsets for auth and cipher operations
1311  * @param       vec     Vectorized operation descriptor
1312  *
1313  * @return
1314  *  - Returns number of successfully processed packets.
1315  */
1316 __rte_experimental
1317 uint32_t
1318 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
1319         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
1320         struct rte_crypto_sym_vec *vec);
1321
1322 #ifdef __cplusplus
1323 }
1324 #endif
1325
1326 #endif /* _RTE_CRYPTODEV_H_ */